为什么此着色器在作为字符串提供给 Material 构造函数时会产生解析错误?
Why is this Shader producing a parse error when given as string to the Material constructor?
我写了以下Shader
:
Shader "Custom/SimpleShader" {
Properties {
_MainTex("Base (RGB)", 2D) = "white" { }
}
SubShader {
Pass {
ZTest Always
Cull Off
ZWrite Off
/* LINE 16*/CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uvb : TEXCOORD1;
};
uniform sampler2D _MainTex;
uniform sampler2D _Texture0;
uniform fixed _Alpha0;
v2f vert(appdata_img v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord);
o.uvb = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col.rgb += (tex2D(_Texture0, i.uvb).rgb * _Alpha0);
return col;
}
ENDCG
}
}
Fallback off
}
Unity 告诉我第 16 行有一个 "Syntax Error"
(见标记)。当我将此文本放入 string
并将此 string
提供给 Material(string contents)
构造函数时,就会发生这种情况。但是,当我将文本放入 .shader
文件并以正常方式导入时,着色器被接受并且不会产生错误。
This documentation 关于 Material(string contents)
构造函数(或者说它的 JavaScript 构造函数)说:
[...] Creating materials this way supports only simple shaders (fixed function ones). If you need a surface shader, or vertex/pixel shaders, you'll need to create shader asset in the editor and use that.
因此,答案是 fragment-
and/or vertex-shaders
不支持这种方式。
我写了以下Shader
:
Shader "Custom/SimpleShader" {
Properties {
_MainTex("Base (RGB)", 2D) = "white" { }
}
SubShader {
Pass {
ZTest Always
Cull Off
ZWrite Off
/* LINE 16*/CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uvb : TEXCOORD1;
};
uniform sampler2D _MainTex;
uniform sampler2D _Texture0;
uniform fixed _Alpha0;
v2f vert(appdata_img v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord);
o.uvb = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col.rgb += (tex2D(_Texture0, i.uvb).rgb * _Alpha0);
return col;
}
ENDCG
}
}
Fallback off
}
Unity 告诉我第 16 行有一个 "Syntax Error"
(见标记)。当我将此文本放入 string
并将此 string
提供给 Material(string contents)
构造函数时,就会发生这种情况。但是,当我将文本放入 .shader
文件并以正常方式导入时,着色器被接受并且不会产生错误。
This documentation 关于 Material(string contents)
构造函数(或者说它的 JavaScript 构造函数)说:
[...] Creating materials this way supports only simple shaders (fixed function ones). If you need a surface shader, or vertex/pixel shaders, you'll need to create shader asset in the editor and use that.
因此,答案是 fragment-
and/or vertex-shaders
不支持这种方式。