- Home /
_MainTex_ST Redefinition error
I need some help I'm trying to put together a particle shader using the surface shader mixed with the soft particle shader and It says the "Redefinition of _MainTex_ST at line 126 (d3d11)" but the code is only 117 lines long. could I have some help with this. I doubt I'm even doing this correctly and need help doing this correctly. thank you.
Shader "Particles/SurfaceParticle(Soft)" {
Properties {
_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_MainTex ("Particle Texture", 2D) = "white" {}
_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
}
Category {
//Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
AlphaTest Greater .01
ColorMask RGB
Cull Off Lighting On ZWrite Off Fog { Color (0,0,0,0) }
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}
SubShader {
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
CGPROGRAM
//#pragma surface surf SimpleLambert
#pragma surface surf Lambert alpha
half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
half NdotL = dot (s.Normal, lightDir);
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten);
c.a = s.Alpha;
return c;
}
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
fixed4 _TintColor;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a;
}
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
#ifdef SOFTPARTICLES_ON
float4 projPos : TEXCOORD1;
#endif
};
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#ifdef SOFTPARTICLES_ON
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
#endif
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
sampler2D _CameraDepthTexture;
float _InvFade;
fixed4 frag (v2f i) : COLOR
{
#ifdef SOFTPARTICLES_ON
float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
float partZ = i.projPos.z;
float fade = saturate (_InvFade * (sceneZ-partZ));
i.color.a *= fade;
#endif
return 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
}
ENDCG
}
// ---- Fragment program cards
// ---- Dual texture cards
SubShader {
Pass {
SetTexture [_MainTex] {
constantColor [_TintColor]
combine constant * primary
}
SetTexture [_MainTex] {
combine texture * previous DOUBLE
}
}
}
// ---- Single texture cards (does not do color tint)
SubShader {
Pass {
SetTexture [_MainTex] {
combine texture * primary
}
}
}
}
}
I'm sorry for bumping this but I would really like some help with this.
I suspect that there's more wrong with this shader than just the redefinition. Specifically, there're a vertex, fragment, and surface shader all wedged together without clear definition for the vertex and fragment shaders. This would usually come in the form of:
#pragma vertex vert
#pragma fragment frag
The problem with the redefinition at the moment, however, is exactly that. Line 59:
float4 _$$anonymous$$ainTex_ST;
is redefining the variable created during the definition of attributes for the surface shader on lines 32-34.
Well I can't use #pragma vertex vert #pragma fragment frag Because unity says #pragma surface does not allow specifying other programs. I just have no idea how to write shaders.
In that case, I would recommend familiarizing yourself with them.
As a general rule, surface shaders are a simplified presentation for vertex/fragment shaders -- Behind the scenes, Unity turns the surface shader into those.
That said, Unity offers some good examples of all of those. You can find surface shader examples to get started here and you can find vertex/fragment shader examples here.
Slow down a bit and take a look at those to learn the ropes and to get an idea of what will go into each type of shader (in each genre), then you'll be better prepared to work on more elaborate ones.
Yeah, you can't exactly just add a surface shader to a pixel shader, without using separate passes. The specific problem you are talking about is the fact that surface shaders auto generate UVs with texture transformations, and therefore declare the _ST values themselves. But yes, you would need to separate these 2 shaders and then combine them using a consistent method. I suppose the easiest way of doing this would be to calculate the lighting in the fragment shader, which you can read up on in the Unity $$anonymous$$anual.
http://docs.unity3d.com/$$anonymous$$anual/SL-VertexFragmentShaderExamples.html
Answer by dogtorque · Jun 19, 2016 at 04:10 AM
okay I almost got the shader to work the way I want too. but now I need to have the alpha in the surface shader part to be using the alpha from the fragment shader part. could I also have some help figuring that part out please?
Shader "Particles/LitParticles" {
Properties {
_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_MainTex ("Particle Texture", 2D) = "white" {}
_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
}
Category {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
AlphaTest Greater .01
ColorMask RGB
Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}
SubShader {
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a*1;
}
ENDCG
// ---- Fragment program cards
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_particles
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 _TintColor;
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
#ifdef SOFTPARTICLES_ON
float4 projPos : TEXCOORD1;
#endif
};
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#ifdef SOFTPARTICLES_ON
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
#endif
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
sampler2D _CameraDepthTexture;
float _InvFade;
fixed4 frag (v2f i) : COLOR
{
#ifdef SOFTPARTICLES_ON
float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
float partZ = i.projPos.z;
float fade = saturate (_InvFade * (sceneZ-partZ));
i.color.a *= fade;
#endif
return 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
}
ENDCG
}
}
// ---- Dual texture cards
SubShader {
Pass {
SetTexture [_MainTex] {
constantColor [_TintColor]
combine constant * primary
}
SetTexture [_MainTex] {
combine texture * previous DOUBLE
}
}
}
// ---- Single texture cards (does not do color tint)
SubShader {
Pass {
SetTexture [_MainTex] {
combine texture * primary
}
}
}
}
}
You posted a seperate question as an answer. Please just edit the original question, not make an answer.
Your answer
Follow this Question
Related Questions
Invalid Subscript Metallic 0 Answers
Mask particles inside an object with a transparent shader 0 Answers
VertexLit Hilight Shader - Flicker on iOS Problem 1 Answer
Is it possible to make my additive-shaded particles only add to each other? 2 Answers
Does liberal use of #pragma shader_feature have an undocumented load time/parsing cost? 2 Answers