- Home /
The question is answered, right answer was accepted
(FIXED) Shader Error due to dynamic conditional 'if' blocks
FIX: Disabled D3D9 support.
I searched online and could find some similar problems, but none of them fixed my problem. It is an error that ONLY occurs when building the game. When playing from the editor no errors are seen and the shader works as intended.
The error:
Shader error in 'Custom/Outline_2DSprite': texld/texldb/texldp/dsx/dsy instructions with r# as source cannot be used inside dynamic conditional 'if' blocks, dynamic conditional subroutine calls, or loop/rep with break*. at line 35 (on d3d9)
The important bit of the shader:
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input
{
float2 uv_MainTex;
fixed4 color : COLOR;
};
sampler2D _MainTex;
float _OutLineSpreadX;
float _OutLineSpreadY;
float4 _Color;
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 tex1 = tex2D(_MainTex, IN.uv_MainTex+float2(_OutLineSpreadX,0.0));
fixed4 tex2 = tex2D(_MainTex, IN.uv_MainTex-float2(_OutLineSpreadX,0.0));
fixed4 tex3 = tex2D(_MainTex, IN.uv_MainTex+float2(0.0,_OutLineSpreadY));
fixed4 tex4 = tex2D(_MainTex, IN.uv_MainTex-float2(0.0,_OutLineSpreadY));
fixed4 TempColor = tex1 + tex2;
TempColor = TempColor + tex3 + tex4;
if(TempColor.a > 0.1){
TempColor.a = 1;
}
fixed4 AlphaColor = fixed4(TempColor.a,TempColor.a,TempColor.a,TempColor.a);
fixed4 mainColor = AlphaColor * _Color.rgba;
fixed4 addcolor = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
if(addcolor.a > 0.95){
mainColor = addcolor;
}
o.Albedo = mainColor.rgb;
o.Alpha = mainColor.a;
}
ENDCG
It has to do with the fact that I can't use tex2D inside an if statement, but can't seem to fix it.
Post the whole shader. It might be the important bit for the effect you want but as far as bugs are concerned importance is a different matter :)
Since editing the code part is a bit wonky? I put the whole shader down here:
Shader "Custom/Outline_2DSprite"
{
Properties
{
_$$anonymous$$ainTex ("Base (RGB)", 2D) = "white" {}
_OutLineSpreadX ("Outline Spread", Range(0,0.03)) = 0.007
_OutLineSpreadY ("Outline Spread", Range(0,0.03)) = 0.007
_Color("Outline Color", Color) = (1.0,1.0,1.0,1.0)
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Blend SrcAlpha One$$anonymous$$inusSrcAlpha Cull Off
Lighting Off
LOD 110
CGPROGRA$$anonymous$$
#pragma surface surf Lambert alpha
struct Input
{
float2 uv_$$anonymous$$ainTex;
fixed4 color : COLOR;
};
sampler2D _$$anonymous$$ainTex;
float _OutLineSpreadX;
float _OutLineSpreadY;
float4 _Color;
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 tex1 = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex+float2(_OutLineSpreadX,0.0));
fixed4 tex2 = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex-float2(_OutLineSpreadX,0.0));
fixed4 tex3 = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex+float2(0.0,_OutLineSpreadY));
fixed4 tex4 = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex-float2(0.0,_OutLineSpreadY));
fixed4 TempColor = tex1 + tex2;
TempColor = TempColor + tex3 + tex4;
if(TempColor.a > 0.1){
TempColor.a = 1;
}
fixed4 AlphaColor = fixed4(TempColor.a,TempColor.a,TempColor.a,TempColor.a);
fixed4 mainColor = AlphaColor * _Color.rgba;
fixed4 addcolor = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex) * IN.color;
if(addcolor.a > 0.95){
mainColor = addcolor;
}
o.Albedo = mainColor.rgb;
o.Alpha = mainColor.a;
}
ENDCG
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Blend One One$$anonymous$$inusSrcAlpha Cull Off Fog { $$anonymous$$ode Off }
LOD 100
Pass {
Tags {"Light$$anonymous$$ode" = "Vertex"}
Color$$anonymous$$aterial AmbientAndDiffuse
Lighting off
SetTexture [_$$anonymous$$ainTex]
{
Combine texture * primary double, texture * primary
}
}
}
Fallback "Diffuse", 1
}
Where does the error appear? I've neither got the thing to work or err.
Answer by tanoshimi · Apr 04, 2017 at 08:57 AM
Do you need D3D9 support? Have you tried excluding that target?
Yes, this worked! I don't need D3D9 support for now, so at least I can keep on testing. Thank you!
@tanoshimi Do you know what it is that might be causing the error?
No, I don't know. I was just making a hypothesis based on the error message, which stated it was a problem with D3D9 compilation.
How can I excluding D3D9 support? I don't know shader program.
Follow this Question
Related Questions
jungle terrain problem 0 Answers
worldPos in shader giving errors when game is paused 0 Answers
[Solved] False "variable not assigned" error 3 Answers
Shader Error GLSL error 0 Answers
Need help with shader not working! 2 Answers