• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by dogtorque · Jun 17, 2016 at 10:45 PM · shadersparticleserror messageshaderlab

_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
              }
          }
      }
  }
  }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dogtorque · Jun 19, 2016 at 12:50 AM 0
Share

I'm sorry for bumping this but I would really like some help with this.

avatar image Eno-Khaon · Jun 19, 2016 at 01:04 AM 0
Share

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.

avatar image dogtorque Eno-Khaon · Jun 19, 2016 at 01:21 AM 0
Share

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.

avatar image Eno-Khaon dogtorque · Jun 19, 2016 at 01:31 AM 0
Share

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.

avatar image Namey5 · Jun 19, 2016 at 02:12 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
              }
          }
      }
  }
  }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Willard720 · Oct 12, 2018 at 11:45 PM 0
Share

You posted a seperate question as an answer. Please just edit the original question, not make an answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

48 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Invalid Subscript Metallic 0 Answers

Unity Advanced Shader Help 0 Answers

Unity inbuilt distorsion shader? 1 Answer

How to convert Legacy/Particles/Alpha Blended to Standard?And How to configure the standard option? 0 Answers

Fragment shader alpha 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges