Search Unity

WetLayer shader

Discussion in 'Shaders' started by Deleted User, Jul 10, 2010.

  1. Deleted User

    Deleted User

    Guest

    Hi

    I've been doing some shader porting from Crysis to Unity's shaderlab today. I tried to port WetLayer just now, and even though it "works", it doesn't really work as it should, giving some interesting and colorful results!
    I am pretty sure that it's because I don't know how to return the pixelshader properly. I don't know what to return there lol.

    Here's the shaderlab shader:
    (I've commented out 2-3 lines because I just didn't know how to convert them to shaderlab)
    Code (csharp):
    1. Shader "Edvinas/WetLayer" {
    2.     Properties {
    3.        
    4. WetMultiplier ("WetMultiplier", Float) = 1.0
    5. DarkeningAmount ("DarkeningAmount", Float) = 0.125
    6. DetailBumpTile ("DetailBumpTile", Float) = 0.2
    7. BumpScale ("BumpScale", Float) = 0.3
    8. AnimScale ("AnimScale", Float) = 1.0
    9. NoiseOffset ("NoiseOffset", Float) = 0.0
    10. VariationTileU ("VariationTileU", Float) = 4.0
    11. VariationTileV ("VariationTileV", Float) = 4.0
    12. VariationTileW ("VariationTileW", Float) = 4.0
    13.  
    14. detailBumpSampler ("detailBumpSampler", 2D) = "bump" {}
    15. bumpMapSampler ("bumpMapSampler", 2D) = "bump" {}
    16. envMapSampler ("envMapSampler", 2D) = "white" {}
    17. fresnelShlickMapSampler ("fresnelShlickMapSampler", 2D) = "white" {}
    18. noiseMapSampler ("noiseMapSampler", CUBE) = "" {}
    19.  
    20.    
    21.     }
    22.    
    23.     SubShader {
    24. Pass {
    25.  
    26. Cull Off
    27. Blend One OneMinusSrcAlpha
    28. ZWrite Off
    29.  
    30. CGPROGRAM
    31.  
    32.  
    33. #pragma target 3.0
    34. #pragma vertex vert
    35. #pragma fragment frag
    36. #include "UnityCG.cginc"
    37.  
    38. //samplers
    39. samplerCUBE noiseMapSampler;
    40. sampler2D detailBumpSampler;
    41. sampler2D envMapSampler;
    42. sampler2D fresnelShlickMapSampler;
    43. sampler2D bumpMapSampler;
    44.  
    45. //floats
    46. float WetMultiplier;
    47. float DarkeningAmount;
    48. float DetailBumpTile;
    49. float BumpScale;
    50. float AnimScale;
    51. float NoiseOffset;
    52. float VariationTileU;
    53. float VariationTileV;
    54. float VariationTileW;
    55.  
    56. struct v2f
    57. {
    58.   float4 Position  : POSITION;
    59.   float4 tcBase : TEXCOORD0;
    60.  
    61.   float4 tcDetail0 : TEXCOORD1;
    62.   float4 tcDetail1 : TEXCOORD2;
    63.  
    64.   float4 vReflectionWS : TEXCOORD3;
    65.   float4 vEye : TEXCOORD4;
    66.  
    67.   float4 shadowTC : TEXCOORD5;  
    68. };
    69.  
    70. // Common functions
    71. half4 EXPAND( half4 a )
    72. {
    73.   return a * 2 - 1;
    74. }
    75. half3 EXPAND( half3 a )
    76. {
    77.   return a * 2 - 1;
    78. }
    79. half EXPAND( half a )
    80. {
    81.   return a * 2 - 1;
    82. }
    83. half2 EXPAND( half2 a )
    84. {
    85.   return a * 2 - 1;
    86. }
    87.  
    88. half4 GetFresnelTex( float NdotI, float bias)
    89. {
    90.  
    91.   return tex2D( fresnelShlickMapSampler, float2(NdotI, bias) );
    92.  
    93. }
    94. half GetFresnel(half NdotI, half bias, half power)
    95. {
    96.   half facing = (1.0 - NdotI);
    97.   return bias + (1-bias)*pow(facing, power);
    98. }
    99.  
    100. half4 GetTexture2D(sampler2D MapSampler, float2 texTC)
    101. {
    102.   half4 texColor = tex2D(MapSampler, texTC.xy);
    103.  
    104.   return texColor;
    105. }
    106.  
    107. half3 GetNormalMap(sampler2D bumpMap, float2 bumpTC)
    108. {
    109.   half3 bumpNormal;
    110.   bumpNormal.xy = EXPAND(tex2D(bumpMap, bumpTC.xy).xy);    
    111.   bumpNormal.xy = EXPAND(tex2D(bumpMap, bumpTC.xy).ga);  
    112.   bumpNormal.z = sqrt(saturate(1 - dot(bumpNormal.xy, bumpNormal.xy)));  
    113.   return bumpNormal;
    114. }
    115.  
    116. float4 HPosToScreenTC(float4 HPos)
    117. {
    118.   float4 ScrTC = HPos;
    119.   ScrTC.xy = (HPos.xy * float2(1,-1) + HPos.ww  ) * 0.5;
    120.  
    121.   ScrTC.xy += 5.0 *HPos.w;
    122.  
    123.   return ScrTC;
    124. }
    125.  
    126. v2f vert ( appdata_base i) {
    127.  
    128. appdata_tan n;
    129. v2f o;
    130.  
    131.   o.Position = mul(glstate.matrix.mvp, i.vertex);
    132.   o.tcBase.xy = i.texcoord.xy;
    133.  
    134.     float3 vScale = float3(VariationTileU, VariationTileV, VariationTileW);
    135.   o.tcDetail0.xyz = i.vertex.xyz * vScale.xyz + float3(0, 0, frac(5 * AnimScale)) + NoiseOffset;
    136.   o.tcDetail1.xyz = i.vertex.xyz * vScale.xyz * 2 + float3(0, 0, frac(5 * AnimScale * 2)) + NoiseOffset;
    137.  
    138.   float3 vEye = normalize( - i.vertex.xyz );
    139.  
    140.   o.vEye.xyz = vEye.xyz;
    141.  
    142.  // float3 worldTangentN = mul((const float3x3)i.vertex, n.tangent);
    143.   //float3 tcRef = reflect( vEye.xyz,  worldTangentN.xyz);
    144.    //o.vReflectionWS.xyz = tcRef;  //-> should be transformed to eye space, but looks weird in non-closed objects
    145.    
    146.     // Output the screen-space texture coordinates
    147.   o.shadowTC = HPosToScreenTC(o.Position);
    148.      
    149.   // Bump strenght setting - used for compensating mipmaps bump strenght removal
    150.   o.tcBase.w = (o.Position.w * 0.5 + 1) * 0.1;
    151.  
    152. return o;
    153.  
    154. }
    155.  
    156.  
    157. float4 frag (v2f IN) : COLOR {
    158.  
    159.  
    160. half fFinalDarkening = 0;  
    161.      
    162.   half4 cBump = half4(0,0,1,1);
    163.   cBump.xyz = GetNormalMap(bumpMapSampler, IN.tcBase.xy);
    164.  
    165.   half3 vEye = normalize(IN.vEye.xyz);  
    166.   half NdotE = saturate( dot(cBump.xyz, vEye.xyz) );
    167.   half fFresnel = GetFresnel(NdotE, 0.5, 5);  
    168.    
    169.   cBump.xy += (tex2D(detailBumpSampler, IN.tcBase.xy * DetailBumpTile).xy*2-1) * BumpScale;  
    170.  
    171.   cBump.w = texCUBE(noiseMapSampler, IN.tcDetail0.xyz).w;
    172.   cBump.w *= texCUBE(noiseMapSampler, IN.tcDetail1.xyz).w;
    173.   cBump.w *= 2;
    174.  
    175.   cBump.w = cBump.w * 2.0 - 1.0;  
    176.   cBump.z = -cBump.w;
    177.      
    178.   // Get main vectors/coeficients        
    179.   half3 vNormal = normalize(cBump.xyz);  
    180.  
    181.              
    182.   // normalize reflection vector and add some perturbation into it
    183.   half3 vReflectionWS = normalize(IN.vReflectionWS.xyz) ;
    184.    
    185.   half3 envMap = GetTexture2D(envMapSampler, vReflectionWS.xy* 0.5 + 0.5 + vNormal.xy * IN.tcBase.w).w;      
    186.   envMap = saturate(envMap *2-1) * fFresnel;
    187.  
    188.   half fOcclShadowAcc = 0;
    189.  
    190.   half4 cFinal = 0;
    191.    
    192.   cFinal.xyz = envMap * fOcclShadowAcc * WetMultiplier;
    193.   cFinal.w = DarkeningAmount;
    194.  
    195. return cBump;
    196. }
    197.  
    198.  
    199. ENDCG
    200.  
    201. }
    202.  
    203.     }
    204.  
    205. }
    206.  
    207.  
    I honestly like some of the results, COLORFUL! But I'd also like to know what I've done wrong and why it's not showing the right results :p

    I've also attached zip file containing the original Crysis WetLayer shader which I was porting as well as the textures originally used for this shader, if you want to look at it.

    Thanks
     

    Attached Files:

  2. Deleted User

    Deleted User

    Guest

    I think I tried it but it still gave weird results lol