Shader error : unexpected $end, expecting TOK_SETTEXTURE or '}' at line 18
I was writing a simple shader which could render a cuboid wireframe of the mesh. However, after compiled the shader, Unity gave out the following error:
Shader error in 'CuboidWireframeShader': Parse error: syntax error, unexpected $end, expecting TOK_SETTEXTURE or '}' at line 18
I am confused because at this line, Because there is only a CGPROGRAMn token. The full shader code is shown below:
Shader "Custom/CuboidWireframeShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Main Texture", 2D) = "white" {}
_Thickness ("Thickness", Float) = 1
}
SubShader
{
Pass
{
Tags { "RenderType"="Opaque" "Queue"="Geometry" }
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
CGPROGRAM
#pragma target 4.0
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
#include "UnityCG.cginc"
float _Thickness;
float4 _Color;
float4 _MainTex_ST;
sampler2D _MainTex;
struct v2g {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
}
struct g2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float3 distance : TEXCOORD1;
}
v2g vert(appdata_base v) {
v2g o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
[maxvertexcount(3)]
void geom(triangle v2g p[3], inout TriangleStream<g2f> triStream) {
v2g np[3];
bool find = false;
for (int i = 0; i < 3 && !find; i++) {
for (int j = i + 1; j < 3 && !find; j++) {
if (abs(p[i].uv.x - p[j].uv.x) > 0.01f && abs(p[i].uv.y - p[j].uv.y) > 0.01f) {
np[0] = p[3 - i - j];
np[1] = p[i];
np[2] = p[j];
find = true;
}
}
}
float2 p0 = _ScreenParams.xy * np[0].pos.xy / np[0].pos.w;
float2 p1 = _ScreenParams.xy * np[1].pos/xy / np[1].pos.w;
float2 p2 = _ScreenParams.xy * np[2].pos/xy / np[2].pos.w;
float2 v0 = p2 - p1;
float2 v1 = p2 - p0;
float2 v2 = p1 - p0;
float area = abs(v1.x * v2.y - v1.y * v2.x);
float dist0 = area / length(v0);
float dist1 = area / length(v1);
float dist2 = area / length(v2);
g2f pIn;
pIn.pos = np[0].pos;
pIn.uv = np[0].uv;
pIn.dist = float3(dist0, 0, 0);
triStream.Append(pIn);
pIn.pos = np[1].pos;
pIn.uv = np[1].uv;
pIn.dist = float3(0, dist1, 0);
triStream.Append(pIn);
pIn.pos = np[2].pos;
pIn.uv = np[2].uv;
pIn.dist = float3(0, 0, dist2);
triStream.Append(pIn);
}
float4 frag(g2f input) : COLOR {
float4 color = _Color * tex2D(_MainTex, input.uv);
float4 transColor = float4(0, 0, 0, 0);
color = (_Thickness > input.dist.y || _Thickness > input.dist.z) ? targetColor : transColor;
if(color.a < 0.5f) discard;
else color.a = 1.0f;
return color;
}
ENDCG
}
}
}
What have I done wrong with this shader? Thanks for helping!
Answer by conjugategames · Apr 07 at 08:29 PM
Take "Cull Off" up one level, out of the Pass {} scope
On further examination, I realize you might need to just remove the Pass entirely, and put everything in SubShader
Cull Off can be placed inside each pass. I don’t see a reason why it can’t.
Anyway it was already 4 years ago, but the answer to this question is due to a shader parser bug, which will result in error if you mix up spaces and tabs in your codes