UnityShaderlab代码编辑器
⽂章著作权归作者所有。转载请联系作者,并在⽂中注明出处,给出原⽂链接。
本系列原更新于作者的github博客,这⾥给出。
需要做的准备
⾸先肯定是需要安装Unity,我这⾥选择的版本是Unity version :2018.3.7f1。学习任何事物都离不开趁⼿的⼯具,Shader也不例外。我们可以选择Sublime Text,JB Rider Free For Student,甚⾄VS作为我们的编辑器,但试⽤之后还是觉得VSCode是⽐较优的选择。下⾯我们将基于VSCode搭建⼀个Shaderlab编辑器。(编辑器不带调试功能,我们仍需要借助Unity进⾏调试。)
⾸先我们直接从VSCode官⽹下载VSCode Free ver。打开VSCode,选择Extensions--Search “Shaderlab”--Install “ShaderlabVSCode”。
接着我们配置Shaderlab的json⽂件。上⽅菜单:File--Preferences--User Snippets--Search “shaderlab”--选择shader.json进⼊。
// Place your snippets for shaderlab here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
//  "prefix": "log",
//  "body": [
//  "console.log('$1');",
//  "$2"
//  ],
//  "description": "Log output to console"
// }
我们可以看到如上的⽂档说明,⼤意是:
在这⾥你可以定制你的shaderlab语法模板。⽤ {} 包围整个代码块,每⼀个模板⽤ , 分隔开。
对于每⼀个模板,内部每⼀个属性也⽤ , 分隔开。⾸先为模板起⼀个名字,对应 "Print to console" ,接着我们声明它的 prefix 属性,这是⽤于匹配你的模板的检索标志,对应 “log” ,即我们输⼊log时,匹配栏会出现这个模板。
接着编辑 “body” 部分,这是模板的内容,格式为["string",],在[]内部,每⼀个字符串⾃成⼀⾏,可以⽤转义字符实现缩进换⾏,若想实现⾼级定制,则可插⼊{number:hint}** 对于每⼀个 **,会按照 number 从⼩到⼤的顺序,给出 hint 提⽰补全模板,使⽤ Tab 切换⾄下⼀个可编辑的单元,最终光标会停留在 $0 处。基于这些,我们便可以灵活的配置⾃⼰所需的模板。下⾯也给出⼀份我⾃⼰常⽤的模板配置,这份模板也会不定期更新。
{
"Shaderlab": {
"prefix": "shaderlab",
"body": [
"Shader \"${1:Shader Name}\" {\n",
"\tProperties {",
"\t\t_MainTex (\"Base (RGB)\", 2D) = \"white\" {}",
"\t\t_Color (\"Color\", Color) = (1, 1, 1, 1)",
"\t}\n",
"\tSubShader {",
"\t\tCGINCLUDE",
"\t\t#include \"inc\"\n",
"\t\tsampler2D _MainTex;",
"\t\tfloat4 _MainTex_ST;\n",
"\t\tstruct a2v {",
"\t\t\tfloat4 vertex : POSITION;",
"\t\t\tfloat2 texcoord : TEXCOORD0;",
"\t\t};\n",
"\t\tstruct v2f {",
"\t\t\tfloat4 pos : SV_POSITION;",
"\t\t\tfloat2 uv : TEXCOORD0;",
"\t\t};\n",
"\t\tv2f vert(a2v v) {",
"\t\t\tv2f o;",
"\t\t\to.pos = UnityObjectToClipPos(v.vertex);",
"\t\t\to.uv = TRANSFORM_rd, _MainTex);",
"\t\t\treturn o;",
代码编辑器怎么下载
"\t\t}\n",
"\t\tfixed4 frag(v2f i) : SV_Target {",
"\t\t\tfixed4 color;",
"\t\t\tcolor = tex2D(_MainTex, i.uv);",
"\t\t\treturn color;",
"\t\t}",
"\t\tENDCG\n",
"\t\tPass {\n",
"\t\t\tCGPROGRAM",
"\t\t\t#pragma vertex vert",
"\t\t\t#pragma fragment frag",
"\t\t\tENDCG",
"\t\t}",
"\t}\n",
Processing math: 0%
"\tFallBack \"Diffuse\"",
"}"
],
"description": "Shaderlab Template"
},
"Range()": {
"prefix": "range",
"body": [
"Range(${1:left}, ${2:right})"
],
"description": "Range for properties"
},
"Pass": {
"prefix": "pass",
"body": [
"Pass {",
"\t$0",
"}"
],
"description": "Pass for Shaderlab Subshader" },
"Tags": {
"prefix": "tags",
"body": [
"Tags {",
"\t$0",
"}"
]
,
"description": "Tags in Pass"
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。