Search Unity

Transparent World Aligned shader order of drawing objects on same z different depending on xPos?

Discussion in 'Shaders' started by patrick-noten, Aug 30, 2016.

  1. patrick-noten

    patrick-noten

    Joined:
    Sep 7, 2013
    Posts:
    12
    So what I am trying to do and what seems to work fine as is, is a shader that gives objects with this material a colour based on a texture "gradient" and their position compared to the camera. This way an object at the bottom of the camera will always have the colour at the bottom of the texture and the one near the top, a colour at the top of the texture.

    This can be seen working here:
    (if image isn't loading check here : https://gyazo.com/768d58e242f2683fa965b5ef55e80fcc)

    Sometimes, when some of the objects are too far inwards in the game however, the platforms above/below start to draw infront/behind the object which gives a very strange result.

    This can be seen here:
    (if image isn't loading check here : https://gyazo.com/d1a5085095991f98c8289065445140b5)

    Here very clearly on the right is seen that above on of the longer platforms tehre is a smaller platform drawing in front of it. I tried figuring out what caused this issue, but couldn't figure out what it was.

    Appreciate your help with this, because without the issue it works perfectly as I want it to!

    Below I added the shader code to produce this effect:

    Code (CSharp):
    1.      Shader "custom_shaders/Transparent_Diffuse_World_Aligned" {
    2.  
    3. Properties {
    4.     _TopColor("Top Color", Color) = (1,1,1)
    5.     _BottomColor ("Bottom Color", Color) = (1,1,1)
    6.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    7.     _Scale ("Texture Scale Multiplier", Float) = 0.1
    8. }
    9.  
    10. SubShader {
    11.         Tags {"Queue"="Transparent-101" "IgnoreProjector"="True" "RenderType"="Transparent"}
    12.         LOD 200 Cull Back Fog {Mode Off}
    13. CGPROGRAM
    14.      #pragma surface surf Lambert alpha
    15.    
    16.      sampler2D _MainTex;
    17.      float4 _TopColor;
    18.      float4 _BottomColor;
    19.      float _Scale;
    20.    
    21.      struct Input {
    22.          float2 uv_MainTex; // unused
    23.          float3 worldNormal;
    24.          float3 worldPos;
    25.      };
    26.    
    27.      void surf (Input IN, inout SurfaceOutput o) {
    28.        // Guess correct planar map from normal. 0.5 is an arbitrary cutoff
    29.        float2 UV;
    30.        // NOTE: assuming no bottom-facing, otherwise use abs()
    31.        if(IN.worldNormal.y > 0.5) UV = IN.worldPos.xz; // top
    32.        else if(abs(IN.worldNormal.x) > 0.5) UV = IN.worldPos.yz; // side
    33.        else {
    34.            float2 fl = (0, -31.3);
    35.            UV = -_WorldSpaceCameraPos.xy + IN.worldPos.xy - fl;
    36.        }
    37.    
    38.        // 0.1 is an arbitrary x10 texture size scale
    39.        half4 c = tex2D (_MainTex, UV * _Scale); // * _Color
    40.        o.Albedo = c.rgb;
    41.        o.Alpha = c.a;
    42.      }
    43.      ENDCG
    44. }
    45.  
    46. Fallback "Transparent/VertexLit" }
     
  2. patrick-noten

    patrick-noten

    Joined:
    Sep 7, 2013
    Posts:
    12
    Any help with this please??