Search Unity

Flipping coordinates on approaching.

Discussion in 'Shaders' started by mouurusai, May 26, 2013.

  1. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    When we look from the top, everything is fine, but if we omit the camera to the level where is visually start of the top layer, coordinates flipped.
    Webplayer demo (Rotate the camera with the mouse)
    Code (csharp):
    1.  
    2.  
    3. Shader "Custom/grass"
    4. {
    5.         Properties
    6.         {
    7.                 _MainTex ("Base (RGB)", 2D) = "white" {}
    8.                 _Height ("Height", float) = 0.1
    9.         }
    10.         SubShader
    11.         {
    12.                 Tags { "RenderType"="Opaque" }
    13.                 LOD 200
    14.                
    15.                 CGPROGRAM
    16.                 #pragma only_renderers d3d9 opengl
    17.                 #pragma target 3.0
    18.                 #pragma surface surf Lambert vertex:vert
    19.  
    20.                 sampler2D _MainTex;
    21.                 half _Height;
    22.  
    23.                 struct Input
    24.                 {
    25.                         float2 uv_MainTex;
    26.                         float3 worldNormal;
    27.                         float3 viewDir;
    28.  
    29.  
    30.                         float3 tangentW;
    31.                         float3 binormalW;
    32.                 };
    33.  
    34.  
    35.                 void vert(inout appdata_full i, out Input o)
    36.                 {
    37.                         float3 normalW = mul((float3x3)_Object2World, i.normal);
    38.                         o.tangentW = mul((float3x3)_Object2World, i.tangent.xyz);
    39.                         o.binormalW = cross(normalW, o.tangentW)*i.tangent.w;
    40.                 }
    41.  
    42.  
    43.                 void surf (Input IN, inout SurfaceOutput o)
    44.                 {
    45.                        
    46.                         float3x3 local2WorldTranspose = float3x3(normalize(IN.tangentW), normalize(IN.binormalW), normalize(IN.worldNormal));
    47.                        
    48.                         half3 viewDirTanSpace = mul(local2WorldTranspose , normalize(IN.viewDir));
    49.                        
    50.                         float2 uv = IN.uv_MainTex;
    51.  
    52.                         half4 c = tex2D (_MainTex, uv);
    53.  
    54.  
    55.                         for(int i=1; i<8; i++)
    56.                         {                              
    57.                                 half2 disp = viewDirTanSpace.xy/viewDirTanSpace.z;
    58.                                 disp = (disp * _Height * i);
    59.  
    60.                                 uv = IN.uv_MainTex + disp;
    61.                                
    62.                                 half4 c1 = tex2D (_MainTex, uv);
    63.  
    64.                                 c = lerp(c, c1*0.125*i, c1.a);
    65.                         }
    66.  
    67.  
    68.                         o.Albedo = c.rgb;
    69.                 }
    70.                 ENDCG
    71.         }
    72.         //FallBack "Diffuse"
    73. }
    74.  
    What am I doing wrong?
    Thanks! (Sorry for my english :mrgreen:)
     
    Last edited: May 26, 2013
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    up
    Can anyone suggest how to determine what the camera is below a layer?
     
    Last edited: May 31, 2013
  3. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    It looks like it's caused by the way you scale the texture. I think it'll be easier for you to understand what is going on, when you manually input the disp value.