Search Unity

Adding second texture to Vertex Fragment Shader

Discussion in 'Shaders' started by eco_bach, Dec 21, 2015.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    I have the following shader which supports a single texture plus lighting. I've been trying to get it working with a second texture (2 sided quad)without luck. In previous tests I was able to determine which side was facing the camera by using the VFACE Semantic in the pixel shader constructor.

    ie
    Code (csharp):
    1. float4 frag(vertexOutput i, float facing : VFACE) : COLOR
    and then simply checking if facing was greater than or < zero. But doesn't seem to work for me here. Any shader gurus that can possibly help?

    Code (csharp):
    1.  
    2. Shader "custom/one sided plus lighting"{
    3.    Properties {
    4.      _Color ("Color Tint", Color) = (1.0,1.0,1.0,1.0)
    5.      _MainTex ("Diffuse Texture", 2D) = "white" {}
    6.      _SpecColor ("Specular Color", Color) = (1.0,1.0,1.0,1.0)
    7.      _Shininess ("Shininess", Float) = 10
    8.  
    9.    }
    10.    SubShader {
    11.      Pass {
    12.        Tags {"LightMode" = "ForwardBase"}
    13.        CGPROGRAM
    14.        #pragma vertex vert
    15.        #pragma fragment frag
    16.      
    17.        //user defined variables
    18.        uniform sampler2D _MainTex;
    19.        uniform float4 _MainTex_ST;
    20.        uniform float4 _Color;
    21.        uniform float4 _SpecColor;
    22.        uniform float _Shininess;
    23.      
    24.        //unity defined variables
    25.        uniform float4 _LightColor0;
    26.      
    27.        //base input structs
    28.        struct vertexInput{
    29.          float4 vertex : POSITION;
    30.          float3 normal : NORMAL;
    31.          float4 texcoord : TEXCOORD0;
    32.        };
    33.        struct vertexOutput{
    34.          float4 pos : SV_POSITION;
    35.          float4 tex : TEXCOORD0;
    36.          float4 posWorld : TEXCOORD1;
    37.          float3 normalDir : TEXCOORD2;
    38.        };
    39.      
    40.        //vertex Function
    41.      
    42.        vertexOutput vert(vertexInput v){
    43.          vertexOutput o;
    44.        
    45.          o.posWorld = mul(_Object2World, v.vertex);
    46.          o.normalDir = normalize( mul( float4( v.normal, 0.0 ), _World2Object ).xyz );
    47.          o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    48.          o.tex = v.texcoord;
    49.        
    50.          return o;
    51.        }
    52.      
    53.        //fragment function
    54.      
    55.        float4 frag(vertexOutput i) : COLOR
    56.        {
    57.          float3 normalDirection = i.normalDir;
    58.          float3 viewDirection = normalize( _WorldSpaceCameraPos.xyz - i.posWorld.xyz );
    59.          float3 lightDirection;
    60.          float atten;
    61.        
    62.          if(_WorldSpaceLightPos0.w == 0.0){ //directional light
    63.            atten = 1.0;
    64.            lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    65.          }
    66.          else{
    67.            float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - i.posWorld.xyz;
    68.            float distance = length(fragmentToLightSource);
    69.            atten = 1.0/distance;
    70.            lightDirection = normalize(fragmentToLightSource);
    71.          }
    72.        
    73.          //Lighting
    74.          float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDirection, lightDirection));
    75.          float3 specularReflection = diffuseReflection * _SpecColor.xyz * pow(saturate(dot(reflect(-lightDirection, normalDirection), viewDirection)) , _Shininess);
    76.        
    77.    
    78.        
    79.          float3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection + specularReflection;// + rimLighting;
    80.        
    81.          //Texture Maps
    82.          float4 tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
    83.        
    84.          return float4(tex.xyz * lightFinal * _Color.xyz, 1.0);
    85.        }
    86.      
    87.        ENDCG
    88.      
    89.      }
    90.    
    91.    
    92.    }
    93.    //Fallback "Specular"
    94. }
    95.  
     
    Last edited: Dec 21, 2015
  2. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    I think you should just do it in 2 pass, one pass cull back with the first texture and another pass cull front with the other texture.
     
  3. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Thanks, what would the tags be in the second pass?
     
  4. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    I think you can do something like:

    Code (CSharp):
    1.  
    2. Tags {"LightMode" = "ForwardBase"}
    3. Cull Back
    4. Pass {
    5. ...
    6. }
    7.  
    8. Cull Front
    9. Pass{
    10. ...
    11. }
     
  5. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601