Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shader not working plzzz help

Discussion in 'Shaders' started by AR_Rizvi, Aug 23, 2013.

  1. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    i combined two shaders from a toutrial video to make a shader that cutout a alpha map but it is not working can plz anyone tel me what i did wrong
    i am realy a noob at shader writing plz help
    here's the code

    Shader "Test/transparentCutout" {
    Properties {

    _Color("color",color)=(1.0,1.0,1.0,1.0)
    _MainTex ("Texture Image", 2D) = "white" {}
    _cutout ("cutout size",Range(0,1))=.5
    }
    Subshader{
    Tags{"Queue"=" Transparent"}
    Pass{
    CGProgram
    #pragma vertex vert
    #pragma fragment frag

    uniform float4 _Color;
    uniform sampler2D _MainTex;
    uniform float4 _MainTex_ST;
    uniform float4 _cutout;

    struct vertexInput {
    float4 vertex:position;
    float4 texcoord : TEXCOORD0;

    };

    struct vertexOutput {
    float4 pos : SV_POSITION;
    float4 vertpos : TEXCOORD0;
    float4 tex : TEXCOORD1;
    };

    vertexOutput vert(vertexInput v){
    vertexOutput o;

    o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
    o.vertpos=v.vertex;
    o.tex = v.texcoord
    return o;
    }

    float4 frag(vertexOutput i): COLOR{
    float4 tex = tex2D (_MainTex,_MainTex_ST.xy*i.tex.xy + _MainTex_ST.zw)
    float4 alpha= tex.a*_Color.a;
    if(i.vertpos.y > _cutout){
    discard;
    }
    return float4(_Color.xyz,alpha)
    }
    ENDCG
    }

    }

    thanks in advance :)
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Each line needs a semi-colon after it.

    float4 tex = tex2D (_MainTex,_MainTex_ST.xy*i.tex.xy + _MainTex_ST.zw)
    to
    float4 tex = tex2D (_MainTex,_MainTex_ST.xy*i.tex.xy + _MainTex_ST.zw);

    return float4(_Color.xyz,alpha)
    to
    return float4(_Color.xyz,alpha);
     
  3. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    so silly of me to miss out semicolns but it is still nt workng thou
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    You're trying to set a float value from a float 4.

    float4 alpha= tex.a*_Color.a;
    if(i.vertpos.y > _cutout){
    discard;
    }
    return float4(_Color.xyz,alpha)

    to

    float alpha= tex.a*_Color.a;
    if(i.vertpos.y > _cutout){
    discard;
    }
    return float4(_Color.xyz,alpha)
     
  5. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    i corrected it to but still it dsnt work
    may b i m missing some functionalities
     
  6. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Check the console, it will tell you what the errors are.

    And

    #include "UnityCG.cginc"
     
  7. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    no luck :( cudnt get it to work
    thanks a lot for helping
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Basically there are a ton of issues with your shader.

    Bad variable types, bad syntax, missing syntax, missing includes...

    I've added a comment to everything I've changed.

    Code (csharp):
    1. Shader "Test/transparentCutout" {
    2.     Properties {
    3.         _Color("color",color)=(1.0,1.0,1.0,1.0)
    4.         _MainTex ("Texture Image", 2D) = "white" {}
    5.         _cutout ("cutout size",Range(0,1))=0.5
    6.     }
    7.  
    8.     Subshader{
    9.         Tags{"Queue" = "Transparent"}       // Removed space from before Transparent.
    10.  
    11.         Pass{
    12.             CGPROGRAM                       // Capitalised this.
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             #include "UnityCG.cginc"        // Included this file.
    16.  
    17.             uniform float4 _Color;
    18.             uniform sampler2D _MainTex;
    19.             uniform float4 _MainTex_ST;
    20.             uniform float _cutout;          // Changed this to float rather than float4.
    21.  
    22.             struct vertexInput {
    23.                 float4 vertex   : POSITION; // float4 vertexposition; means nothing, I think you want this instead.
    24.                 float4 texcoord : TEXCOORD0;
    25.             };
    26.  
    27.             struct vertexOutput {
    28.                 float4 pos : SV_POSITION;
    29.                 float3 vertpos : TEXCOORD0; // Changed this to float3 rather than float4.
    30.                 float2 tex : TEXCOORD1;     // Changed this to float2 rather than float4.
    31.             };
    32.  
    33.             vertexOutput vert(vertexInput v){
    34.                 vertexOutput o;
    35.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    36.                 o.vertpos = v.vertex.xyz;           // Added .xyz.
    37.                 o.tex = v.texcoord.xy;                  // Added semicolon. Added .xy.
    38.                 return o;  
    39.             }
    40.  
    41.             float4 frag(vertexOutput i): COLOR{
    42.                 float4 tex = tex2D(_MainTex, _MainTex_ST.xy * i.tex + _MainTex_ST.zw);  // Added semicolon. Removed .xy from i.tex as it's now a float2.
    43.                 float alpha = tex.a * _Color.a;     // Made this a float rather than float4.
    44.                 clip(i.vertpos.y - _cutout);        // Changed if/discard to clip.
    45.                 return float4(_Color.xyz, alpha);   // Added semicolon.
    46.             }
    47.             ENDCG
    48.         } // Added end Pass bracket.
    49.     }
    50. }
     
    Last edited: Aug 23, 2013
  9. AR_Rizvi

    AR_Rizvi

    Joined:
    Aug 12, 2013
    Posts:
    40
    thank you so much fr your time
    but what can i say the shader is still n0t working properly it is cutting out but it dsnt displaying any materiel

    once again thanks fr making changes into the shader
     
  10. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    You haven't told it to use the texture for color output, only the _Color.rgb and the alpha value.