title: unity-shader-ShaderGraph可视化shader
categories: Unity3d
tags: [unity, shader, ShaderGraph, 可视化]
date: 2019-05-03 18:04:23
comments: false
unity-shader-ShaderGraph可视化shader, 和 shader forge 插件, ue4 材质编辑器 类似的东西, 都是通过可视化链接节点来编写逻辑代码, 然后会自动 编译 就能看到效果.
前篇
- 官方资料
- 【Shader Graph教程】轻松学习Unity2018内置shader可视化编辑器Shader Graph (B站不错的教程) - https://www.bilibili.com/video/av32162824/
- 不错的示例仓库: https://github.com/keijiro/ShaderGraphExamples
安装 shader graph
当前版本 unity2018.3.11f1. 安装了 shader graph 后, 之前写的 xxx.shader 将会无效 (变成紫色).
所以最好还是另起一个lwrp的模板工程.
参考: Unity-Shader Graph 安装 - https://blog.csdn.net/liquanyi007/article/details/80771441
-
默认的 3d 工程没有 shader graph, 需要通过 package manager 安装 lightweight rp, shadergraph 这两个包, 才能使用 shader graph.
如果是 2019 之前的是预览版, 需要在 advance 勾选上 show preview packages 才能显示出来
-
创建一个 lwrp asset. create -> rendering -> lightweight pipeline asset
然后指定项目使用这个 asset, 才肯使用 shader graph. Edit -> Project Setting -> Graphics
-
然后 create -> shader 才会出现有 graph 的选项
自定义节点 custom node
相关参考:
- Shader Graph Custom Node API: Using the Code Function Node - https://blogs.unity3d.com/2018/03/27/shader-graph-custom-node-api-using-the-code-function-node/
- Custom Nodes - https://docs.unity3d.com/Packages/[email protected]/manual/Custom-Nodes-With-CodeFunctionNode.html
-
增加一个自定义节点脚本
using System.Reflection; using UnityEditor.ShaderGraph; using UnityEngine; [Title("Custom", "My Custom Node1")] // 创建节点时 节点所在的组, 可有n多个层级 public class MyCustomNode : CodeFunctionNode { public MyCustomNode() { name = "My Custom Node2"; } protected override MethodInfo GetFunctionToConvert() { return GetType().GetMethod("MyCustomFunction", BindingFlags.Static | BindingFlags.NonPublic); } // 计算公式 static string MyCustomFunction( [Slot(0, Binding.None)] DynamicDimensionVector A, [Slot(1, Binding.None)] DynamicDimensionVector B, [Slot(2, Binding.None)] out DynamicDimensionVector Out) { return @" { Out = A + B; } "; } } -
然后就可以在 graph 中使用
- 节点可以是 vector1,2,3,4, 只要 A 或 B 其中一个换成其他参数类型, 其他两个节点也会跟着变化