Search Unity

Lighting, Texture Blending & Fog

Discussion in 'Shaders' started by Sessional, May 27, 2015.

  1. Sessional

    Sessional

    Joined:
    Apr 21, 2013
    Posts:
    45
    I'm trying to set up a shader that will do all three of these, but I'm new to shaders and haven't found very many resources that explain why they are doing what they are doing.

    I would LOVE pointers to improve every part of this, and help getting the shadows working, I haven't been able to figure out how to get it working with all sorts of variety of methods.

    I have the Texture Blending and Fog working (to an extent, texture blending isn't working how I hoped it would) but I am struggling to put the lighting in to the shader (I think? It's not getting shadows at least)

    So what I am trying to do:
    I was going for was blending based on steepness with additional weight given to heights. So for example, at low altitude it will be a dirt texture, at mid altitudes it will be a grass texture, and at high altitudes it will be stone, however, in the dirt section I want to replace steep areas with stone and in the grass area replace steep areas with stone or dirt depending on steepness. I also want to have fog to help fade the terrain into the distance (helps hide deformations and junk) and lastly I want it to have shadows casted (if possible) but at least lighting since I don't think any applies at all at the moment. I have exactly 1 directional light in my scene.



    First: here is what my terrain looks like - the texture blending works a bit, used tangents and heights to do some really ugly math.
    terrain.PNG

    Second: here's what is happening/not happening. At the base of the tree in the bottom right you will see a small shadow, it works fine on the speed tree objects but not on the terrain as you can see.
    shadow.PNG

    Third: here's that ugly shader of mine. :)
    Code (CSharp):
    1. Shader "Lonely Vale/Terrain Shader" {
    2.     Properties {
    3.         _RedTex ("Texture@Red Coords", 2D) = "white" {}
    4.         _GreenTex ("Texture@Green Coords", 2D) = "white" {}
    5.         _BlueTex ("Texture@Blue Coords", 2D) = "white" {}
    6.  
    7.         _MaxHeight ("Max Height", Float) = 50
    8.         _MinHeight ("Min Height", Float) = 0
    9.         _MidHeight ("Mid Height", Float) = 25
    10.     }
    11.  
    12.     SubShader {
    13.         Tags { "LightMode" = "ForwardBase" }
    14.         Pass
    15.         {
    16.  
    17.             CGPROGRAM
    18.  
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #pragma target 4.0
    22.  
    23.             #include "UnityCG.cginc"
    24.            
    25.             #pragma multi_compile_fwdbase
    26.  
    27.             #include "AutoLight.cginc"
    28.  
    29.             uniform sampler2D _RedTex;
    30.             uniform sampler2D _GreenTex;
    31.             uniform sampler2D _BlueTex;
    32.  
    33.             uniform float _MaxHeight;
    34.             uniform float _MinHeight;
    35.             uniform float _MidHeight;
    36.  
    37.             uniform half unity_FogDensity;
    38.             //uniform half unity_FogColor;
    39.  
    40.             struct inputVert
    41.             {
    42.                 float4 pos : POSITION;
    43.                 float4 col : COLOR;
    44.                 float4 tangent : TANGENT;
    45.                 float3 normal : NORMAL;
    46.                 float2 coord : TEXCOORD0;
    47.  
    48.             };
    49.  
    50.             struct outputVert
    51.             {
    52.                 float4 pos : SV_POSITION;
    53.                 float4 col :COLOR;
    54.                 float4 tan : TANGET;
    55.                 float4 unPos : NORMAL;
    56.                 float2 uv : TEXCOORD0;
    57.                 half2 fogDepth : TEXCOORD1;
    58.                 LIGHTING_COORDS(2, 3)
    59.             };
    60.  
    61.             outputVert vert(inputVert v)
    62.             {
    63.                 outputVert o;
    64.                 o.pos = mul(UNITY_MATRIX_MVP, v.pos);
    65.                 o.uv = v.coord;
    66.                 o.col = v.col;
    67.                 o.tan = v.tangent;
    68.                 o.unPos = v.pos;
    69.                 o.fogDepth.x = length(mul (UNITY_MATRIX_MV, v.pos).xyz);
    70.                 o.fogDepth.y = o.fogDepth.x * unity_FogDensity;
    71.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    72.                 return o;
    73.             }
    74.  
    75.             float4 frag(outputVert v) : COLOR
    76.             {
    77.                 float4 redCol = tex2D(_RedTex, v.uv);
    78.                 float4 greenCol = tex2D(_GreenTex, v.uv);
    79.                 float4 blueCol = tex2D(_BlueTex, v.uv);
    80.                
    81.                 if (v.tan.x < 0)
    82.                 {
    83.                     v.tan.x = -v.tan.x;
    84.                 }
    85.  
    86.                 if (v.tan.y < 0)
    87.                 {
    88.                     v.tan.y = -v.tan.y;
    89.                 }
    90.  
    91.                 if (v.tan.z < 0)
    92.                 {
    93.                     v.tan.z = -v.tan.z;
    94.                 }
    95.  
    96.                 float lerpMagic = (v.unPos.y - (_MidHeight - (0.3 * (_MaxHeight - _MinHeight))));
    97.                 if (lerpMagic < 0)
    98.                 {
    99.                     lerpMagic = 0;
    100.                 }
    101.                 lerpMagic = lerpMagic / (_MaxHeight - _MidHeight);
    102.  
    103.                 float magicalSteepColorR = lerp(greenCol.r, blueCol.r, lerpMagic);
    104.                 float magicalSteepColorG = lerp(greenCol.g, blueCol.g, lerpMagic);
    105.                 float magicalSteepColorB = lerp(greenCol.b, blueCol.b, lerpMagic);
    106.  
    107.                 float tangentRed = v.tan.x * redCol.r + v.tan.y * magicalSteepColorR + v.tan.z * blueCol.r;
    108.                 float tangentGreen = v.tan.x * redCol.g + v.tan.y * magicalSteepColorG + v.tan.z * blueCol.g;
    109.                 float tangentBlue = v.tan.x * redCol.b + v.tan.y * magicalSteepColorB + v.tan.z * blueCol.b;
    110.  
    111.                 float fogAmt = v.fogDepth.y * v.fogDepth.y;
    112.                 fogAmt = exp(-fogAmt);
    113.                 float attenuation = LIGHT_ATTENUATION(i);
    114.                 return lerp(float4(tangentRed, tangentGreen, tangentBlue, 0.0), unity_FogColor, 1-fogAmt) * attenuation;
    115.             }
    116.             ENDCG
    117.         }
    118.     }
    119.     FallBack "Diffuse"
    120. }
     
  2. Sessional

    Sessional

    Joined:
    Apr 21, 2013
    Posts:
    45
    Bump. Still looking for input. :)