Search Unity

IBL Shader for Skyshop

Discussion in 'Shaders' started by brzozowsky, Aug 7, 2014.

  1. brzozowsky

    brzozowsky

    Joined:
    Sep 12, 2011
    Posts:
    39
    Hi guys, i need help with one shader for terrain.
    How to edit this shader for Marmoset Skyshop?

    Code (JavaScript):
    1.  
    2. Properties
    3.     {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Diffuse1(RGB)", 2D) = "gray" {}
    6.         _BumpMap ("Bump1(CA)", 2D) = "bump" {}
    7.         _MainTex2 ("Diffuse2(RGB)", 2D) = "gray" {}
    8.         _BumpMap2 ("Bump2(CA)", 2D) = "bump" {}
    9.         _MainTex3 ("Diffuse3(RGB)", 2D) = "gray" {}
    10.         _BumpMap3 ("Bump3(CA)", 2D) = "bump" {}
    11.         _MainTex4 ("Diffuse4(RGB)", 2D) = "gray" {}
    12.         _BumpMap4 ("Bump4(CA)", 2D) = "bump" {}
    13.         _Ambient ("Additional Ambient", Color) = (1,1,1,1)
    14.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    15.         _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    16.         _BakeAmbient ("Shininess", Range (0, 20)) = 0
    17.         _Tile ("Tile", Float) = 0.25
    18.     }
    19.     SubShader
    20.     {
    21.         CGPROGRAM
    22.      
    23.         #pragma surface surf BlinnPhong vertex:vert
    24.         #pragma target 3.0
    25.         struct Input
    26.         {
    27.             float2 uv_MainTex;      
    28.             float3 worldPos;
    29.             float4 color : Color;
    30.             float3 plane;
    31.         };
    32.      
    33.         sampler2D _MainTex;
    34.         sampler2D _BumpMap;
    35.         sampler2D _MainTex2;
    36.         sampler2D _BumpMap2;
    37.         sampler2D _MainTex3;
    38.         sampler2D _BumpMap3;
    39.         sampler2D _MainTex4;
    40.         sampler2D _BumpMap4;
    41.         half3 _Ambient;
    42.         fixed4 _Color;
    43.         half _Shininess;
    44.         half _BakeAmbient;
    45.         half _Tile;
    46.      
    47.         void vert (inout appdata_full v, out Input o)
    48.         {
    49.             UNITY_INITIALIZE_OUTPUT(Input,o);
    50.             o.plane = (0,0,0);
    51.          
    52.             float absNormX = abs(v.normal.x);
    53.             float absNormY = abs(v.normal.y);
    54.             float absNormZ = abs(v.normal.z);
    55.          
    56.             if (v.normal.y > 0.65) { o.plane.y = 1; v.tangent = float4(0,0,1,-1); }
    57.             else if (v.normal.y < -0.75) { o.plane.y = 1; v.tangent = float4(0,0,-1,1); }
    58.          
    59.             else if (v.normal.x > 0.65) { o.plane.x = 1; v.tangent = float4(0,1,0,-1); }
    60.             else if (v.normal.x < -0.65) { o.plane.x = 1; v.tangent = float4(0,-1,0,1); }
    61.          
    62.             else if (v.normal.z > 0) { o.plane.z = 1; v.tangent = float4(0,-1,0,1); }
    63.             else { o.plane.z = 1; v.tangent = float4(0,1,0,-1); }
    64.             v.tangent.xyz = cross(v.normal, v.tangent.xyz);
    65.         }
    66.      
    67.         fixed4 GetTriplanarColorOld (sampler2D tex, float3 worldPos, float3 plane)
    68.         {
    69.             float2 uv = float2(0,0);
    70.          
    71.             uv.x = worldPos.x*0.2;
    72.             uv.y = worldPos.z*0.2;  
    73.             fixed4 texy = tex2D(tex, uv);
    74.             uv.x = worldPos.z*0.2;
    75.             uv.y = worldPos.y*0.2;  
    76.             fixed4 texx = tex2D(tex, uv);
    77.             uv.x = worldPos.x*0.2;
    78.             uv.y = worldPos.y*0.2;  
    79.             fixed4 texz = tex2D(tex, uv);
    80.          
    81.             return texy*plane.y + texx*plane.x + texz*plane.z;
    82.         }
    83.      
    84.         fixed4 GetColor (sampler2D tex1, sampler2D tex2, sampler2D tex3, sampler2D tex4, float2 uv, half4 type)
    85.         {
    86.             if (type.r > 0.99) return tex2D(tex1, uv)*type.r;
    87.             else if (type.g > 0.99) return tex2D(tex1, uv)*type.g;
    88.             else if (type.b > 0.99) return tex2D(tex1, uv)*type.b;
    89.             else if (type.a > 0.99) return tex2D(tex1, uv)*type.a;
    90.          
    91.             else return tex2D(tex1, uv)*type.r
    92.                 + tex2D(tex2, uv)*type.g
    93.                 + tex2D(tex3, uv)*type.b
    94.                 + tex2D(tex4, uv)*type.a;
    95.         }
    96.      
    97.         fixed4 GetTriplanarColor (sampler2D tex1, sampler2D tex2, sampler2D tex3, sampler2D tex4,     float3 worldPos, half4 type, float3 plane)
    98.         {
    99.             float2 uv = float2(0,0);
    100.             uv.x = worldPos.x*0.2;
    101.             uv.y = worldPos.z*0.2;  
    102.             fixed4 texy = fixed4(0,0,0,0);
    103.             //if (plane.y > 0.99)
    104.             texy = tex2D(tex1, uv)*type.r
    105.                 + tex2D(tex2, uv)*type.g
    106.                 + tex2D(tex3, uv)*type.b
    107.                 + tex2D(tex4, uv)*type.a;
    108.             uv.x = worldPos.z*0.2;
    109.             uv.y = worldPos.y*0.2;  
    110.             fixed4 texx = fixed4(0,0,0,0);
    111.             //if (plane.x > 0.99)
    112.             texx = tex2D(tex1, uv)*type.r
    113.                 + tex2D(tex2, uv)*type.g
    114.                 + tex2D(tex3, uv)*type.b
    115.                 + tex2D(tex4, uv)*type.a;
    116.             uv.x = worldPos.x*0.2;
    117.             uv.y = worldPos.y*0.2;  
    118.             fixed4 texz = fixed4(0,0,0,0);
    119.             //if (plane.z > 0.99)
    120.             texz = tex2D(tex1, uv)*type.r
    121.                 + tex2D(tex2, uv)*type.g
    122.                 + tex2D(tex3, uv)*type.b
    123.                 + tex2D(tex4, uv)*type.a;
    124.          
    125.             return texy*plane.y + texx*plane.x + texz*plane.z;
    126.             //return fixed4(0,0,0,0);
    127.         }
    128.      
    129.         void surf (Input IN, inout SurfaceOutput o)
    130.         {
    131.             half4 type = half4(IN.color.r, IN.color.g, IN.color.b, 1-IN.color.r-IN.color.b-IN.color.g);
    132.          
    133.             fixed4 tex = GetTriplanarColor (_MainTex, _MainTex2, _MainTex3, _MainTex4,     IN.worldPos*_Tile, type, IN.plane);
    134.             fixed4 norm = GetTriplanarColor (_BumpMap, _BumpMap2, _BumpMap3, _BumpMap4, IN.worldPos*_Tile, type, IN.plane);
    135.          
    136.             //fixed4 tex = GetColor (_MainTex, _MainTex2, _MainTex3, _MainTex4,      IN.uv_MainTex, type);
    137.             //fixed4 norm = GetColor (_BumpMap, _BumpMap2, _BumpMap3, _BumpMap4,      IN.uv_MainTex, type);
    138.          
    139.             o.Normal = UnpackNormal(norm);
    140.             o.Albedo = tex.rgb * _Color.rgb;
    141.             o.Gloss = tex.a;
    142.             o.Alpha = tex.a * _Color.a;
    143.             o.Specular = _Shininess;
    144.          
    145.             o.Emission = o.Albedo * IN.color.a * _Ambient;
    146.             o.Albedo += o.Emission*_BakeAmbient;
    147.         }
    148.    
    149.         ENDCG
    150.     }
    151.  
    152.     Fallback "VertexLit"
    153. }