Search Unity

Revisiting ShadowGun Shader for 4.6.x

Discussion in 'Shaders' started by ippdev, Oct 20, 2014.

  1. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    I am experimenting with shaders for mobile and this used to give me good performance. I pulled it out today and dropped it in a new project and got this error..

    "Shader error in 'Mobile/BackgroundQueueAdditive2': Did not find shader function 'vert' to compile"

    Code (CSharp):
    1. Shader "Mobile/BackgroundQueueAdditive2"
    2. {
    3. Properties {
    4.         _Color ("Main Color", Color) = (0.5, 0.5, 0.5, 0)
    5.         _EnvTex ("EnvMap", 2D) = "black" {}                                                
    6.     }
    7.     SubShader {
    8. //      Tags { "Queue"="Background" "IgnoreProjector" = "True" "RenderType"="Background"}
    9.         Tags { "Queue"="Transparent" "IgnoreProjector" = "True" "RenderType"="Transparent"}
    10.      
    11.         Pass{
    12. //          Alphatest Greater 0
    13. //          Cull Off
    14.             ZWrite Off
    15. //          Blend SrcAlpha OneMinusSrcAlpha     // Alpha blending
    16.             Blend One One                       // Additive
    17. //          Blend One OneMinusDstColor          // Soft Additive
    18. //          Blend DstColor Zero                 // Multiplicative
    19. //          Blend DstColor SrcColor             // 2x Multiplicative
    20.          
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #include "UnityCG.cginc"
    25.          
    26.             //Declare Properties
    27.             sampler2D _EnvTex;
    28.             fixed4 _Color;
    29.             //------------------
    30.          
    31.             //Declare structures
    32.             struct v2f{
    33.                 float4 pos : SV_POSITION;
    34.                 //half4 color : COLOR0;
    35.                 //half2 uv : TEXCOORD1;
    36.                 half2 uvSphere : TEXCOORD0;
    37.             };
    38.             struct appdata_custom {
    39.                 float4 vertex : POSITION;
    40.                 //float4 tangent : TANGENT;
    41.                 half3 normal : NORMAL;
    42.                 //half4 texcoord : TEXCOORD0;
    43.                 //half4 color : COLOR;
    44.             };
    45.             //--------------------
    46.          
    47.             half4 _EnvTex_ST;;
    48.          
    49.             //vertex shader
    50.             v2f vert (appdata_custom v)
    51.             {
    52.                 v2f o;
    53.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    54.              
    55.                 //Sphere mapping ported from http://www.ozone3d.net/tutorials/glsl_texturing_p04.php
    56.                 half3 u = normalize( mul(UNITY_MATRIX_MV, v.vertex));
    57.                 half3 n = mul(UNITY_MATRIX_IT_MV, float4(v.normal.xyz,0));
    58.                 half3 r = reflect( u, n);
    59.              
    60.                 half m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) );
    61.                 o.uvSphere = half2(r.x/m+0.5, r.y/m+0.5);
    62.                 //---------------
    63.                              
    64.                 //o.uv = TRANSFORM_TEX (v.texcoord, _EnvTex);;
    65.                 //o.color = v.color;
    66.                 return o;
    67.             }
    68.            //fragment shader
    69.                 fixed4 frag (v2f i) : COLOR {
    70.                 fixed4 tex = tex2D(_EnvTex, i.uvSphere) * _Color;
    71.                 tex.rgb *= 2; //double the color values, but leave alpha untouched
    72.                 return tex;
    73.             }
    74.             ENDCG
    75.          
    76.         }
    77.     }
    78. }
    What would I change the vert function to in the latest and greatest Unity?


    TIA
     
  2. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    half4 _EnvTex_ST;;

    remove one semicolon
     
  3. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    Ummm..duh..thank you. Not sure when that snuck in there..
     
  4. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    OK.. In trying to use this shader which now compiles without errors, I get the hot pink color and the texture slot will not accept textures. What am I doing wrong here? I am trying to use it as a car shader.
     
  5. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    It plays fine at Unity 5 Beta, perhaps restart computer ?
     
  6. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Here is a simple implementation of using the above shader brains for cheap speculars.

    Code (HLSL):
    1. Shader "Mobile/SpecAdd"
    2. {
    3.  
    4. Properties
    5. {
    6.         _EnvTex ("EnvMap", 2D) = "black" {}
    7.         _Texture("_Texture", 2D) = "white" {}                                            
    8. }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 250
    13.         ZWrite On
    14.         Cull Back
    15.         Lighting Off
    16.         Fog { Mode Off }
    17.  
    18.         Pass
    19.         {
    20.      
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #include "UnityCG.cginc"
    25.  
    26.             sampler2D _EnvTex;
    27.             sampler2D _Texture;
    28.             half4 _Texture_ST;
    29.  
    30.             struct v2f
    31.             {
    32.                 float4 pos : SV_POSITION;
    33.                 half2 uv : TEXCOORD0;
    34.                 half2 uvSphere : TEXCOORD1;
    35.             };
    36.             struct appdata_custom
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 half3 normal : NORMAL;
    40.                 half4 texcoord : TEXCOORD0;
    41.  
    42.             };
    43.  
    44.             v2f vert (appdata_custom v)
    45.             {
    46.                 v2f o;
    47.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    48.                 half3 u = normalize( mul(UNITY_MATRIX_MV, v.vertex));
    49.                 half3 n = mul(UNITY_MATRIX_IT_MV, float4(v.normal.xyz,0));
    50.                 half3 r = reflect( u, n);
    51.                 half m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) );
    52.  
    53.                 o.uvSphere = half2(r.x/m+0.5, r.y/m+0.5);
    54.                 o.uv = TRANSFORM_TEX (v.texcoord, _Texture);;
    55.                 return o;
    56.             }
    57.  
    58.             fixed4 frag (v2f i) : COLOR
    59.             {
    60.                 fixed4 tex = tex2D(_EnvTex, i.uvSphere);
    61.                 tex.rgb *= 2;
    62.                 fixed4 tex2 = tex2D(_Texture, i.uv);
    63.                 return tex + tex2;
    64.             }
    65.             ENDCG
    66.         }
    67.     }
    68. }
    Add the particles blob at EnvTex slot and your diffuse texture at Texture
     
    Last edited: Oct 23, 2014