Ice.shader 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. Shader "Custom/Ice" {
  2. Properties {
  3. _Color ("Color", Color) = (1,1,1,1)
  4. _FresnelColor ("_FresnelColor", Color) = (1,1,1)
  5. _MainTex ("Albedo (RGB)", 2D) = "white" {}
  6. _Glossiness ("Smoothness", Range(0,1)) = 0.5
  7. _Metallic ("Metallic", Range(0,1)) = 0.0
  8. _BumpAmt ("Distortion", range (0,1000)) = 10
  9. _BumpMap ("Normalmap", 2D) = "bump" {}
  10. _Fresnel ("Fresnel", Range(1.0,12.0)) = 3.0
  11. _marchDistance ("March Distance", Float) = 3.0
  12. _numSteps ("Steps", Float) = 4.0
  13. _Ramp ("Ramp", 2D) = "white" {}
  14. _InnerRamp ("_InnerRamp", 2D) = "white" {}
  15. }
  16. SubShader {
  17. Tags { "Queue"="Transparent" "RenderType"="Transparent" }
  18. LOD 200
  19. GrabPass {
  20. Name "BASE"
  21. Tags { "LightMode" = "Always" }
  22. }
  23. Pass {
  24. Name "BASE"
  25. CGPROGRAM
  26. #pragma vertex vert
  27. #pragma fragment frag
  28. #pragma multi_compile_fog
  29. #include "UnityCG.cginc"
  30. struct appdata_t {
  31. float4 vertex : POSITION;
  32. float2 texcoord: TEXCOORD0;
  33. };
  34. struct v2f {
  35. float4 vertex : SV_POSITION;
  36. float4 uvgrab : TEXCOORD0;
  37. float2 uvbump : TEXCOORD1;
  38. float2 uvmain : TEXCOORD2;
  39. UNITY_FOG_COORDS(3)
  40. };
  41. float _BumpAmt;
  42. float4 _BumpMap_ST;
  43. float4 _MainTex_ST;
  44. v2f vert (appdata_t v)
  45. {
  46. v2f o;
  47. o.vertex = UnityObjectToClipPos(v.vertex);
  48. #if UNITY_UV_STARTS_AT_TOP
  49. float scale = -1.0;
  50. #else
  51. float scale = 1.0;
  52. #endif
  53. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  54. o.uvgrab.zw = o.vertex.zw;
  55. o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
  56. o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
  57. UNITY_TRANSFER_FOG(o,o.vertex);
  58. return o;
  59. }
  60. sampler2D _GrabTexture;
  61. float4 _GrabTexture_TexelSize;
  62. sampler2D _BumpMap;
  63. sampler2D _MainTex;
  64. half4 frag (v2f i) : SV_Target
  65. {
  66. // calculate perturbed coordinates
  67. half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
  68. float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
  69. i.uvgrab.xy = offset + i.uvgrab.xy;
  70. half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  71. //half4 tint = tex2D(_MainTex, i.uvmain);
  72. //col *= tint;
  73. UNITY_APPLY_FOG(i.fogCoord, col);
  74. return col;
  75. }
  76. ENDCG
  77. }
  78. //Blend SrcBlend OneMinusSrcAlpha
  79. CGPROGRAM
  80. // Physically based Standard lighting model, and enable shadows on all light types
  81. //#pragma surface surf Standard alpha:fade fullforwardshadows
  82. #pragma surface surf RampSpec alpha:fade fullforwardshadows
  83. // Use shader model 3.0 target, to get nicer looking lighting
  84. #pragma target 3.0
  85. #include "UnityPBSLighting.cginc"
  86. sampler2D _MainTex;
  87. sampler2D _BumpMap;
  88. sampler2D _Ramp, _InnerRamp;
  89. struct Input {
  90. float2 uv_MainTex;
  91. float2 uv_BumpMap;
  92. float3 viewDir;
  93. };
  94. half _Glossiness;
  95. half _Metallic;
  96. fixed4 _Color;
  97. float _Fresnel;
  98. fixed3 _FresnelColor;
  99. fixed _marchDistance, _numSteps;
  100. half3 RampShading (float3 normal, half3 lightDir, half3 viewDir, half3 lightCol) {
  101. half NdotL = dot (normal, lightDir);
  102. half diff = NdotL * 0.5 + 0.5;
  103. half3 ramp = tex2D (_Ramp, float2(diff, 0.5)).rgb;
  104. half3 c;
  105. c.rgb = ramp * lightCol;
  106. return c;
  107. }
  108. half4 LightingRampSpec(SurfaceOutputStandardSpecular s, half3 viewDir, UnityGI gi)
  109. {
  110. s.Normal = normalize(s.Normal);
  111. // energy conservation
  112. half oneMinusReflectivity;
  113. s.Albedo = EnergyConservationBetweenDiffuseAndSpecular(s.Albedo, s.Specular, /*out*/ oneMinusReflectivity);
  114. // shader relies on pre-multiply alpha-blend (_SrcBlend = One, _DstBlend = OneMinusSrcAlpha)
  115. // this is necessary to handle transparency in physically correct way - only diffuse component gets affected by alpha
  116. half outputAlpha;
  117. s.Albedo = PreMultiplyAlpha(s.Albedo, s.Alpha, oneMinusReflectivity, /*out*/ outputAlpha);
  118. half4 c = UNITY_BRDF_PBS(s.Albedo, s.Specular, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, gi.light, gi.indirect);
  119. c.rgb += RampShading(s.Normal, gi.light.dir, viewDir, gi.light.color) * _Color;
  120. //c.rgb += SubsurfaceShadingSimple(_InternalColor, s.Normal, viewDir, s.Alpha*_SSS, gi.light.dir, gi.light.color);
  121. c.a = outputAlpha;
  122. return c;
  123. }
  124. inline void LightingRampSpec_GI(
  125. SurfaceOutputStandardSpecular s,
  126. UnityGIInput data,
  127. inout UnityGI gi)
  128. {
  129. #if defined(UNITY_PASS_DEFERRED) && UNITY_ENABLE_REFLECTION_BUFFERS
  130. gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal);
  131. #else
  132. Unity_GlossyEnvironmentData g = UnityGlossyEnvironmentSetup(s.Smoothness, data.worldViewDir, s.Normal, s.Specular);
  133. gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal, g);
  134. #endif
  135. }
  136. void surf(Input IN, inout SurfaceOutputStandardSpecular o) {
  137. // Albedo comes from a texture tinted by color
  138. fixed4 c = _Color;
  139. //half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg;
  140. //Inner structure parallax
  141. float3 InnerStructure = float3(0, 0, 0);
  142. float2 UV = IN.uv_MainTex;
  143. float offset = 1;
  144. for (float d = 0.0; d < _marchDistance; d += _marchDistance / _numSteps)
  145. {
  146. UV -= (IN.viewDir*d)/_numSteps * tex2D (_MainTex, IN.uv_MainTex).g;
  147. float4 Ldensity = tex2D(_MainTex, UV).r;
  148. InnerStructure += saturate(Ldensity[0])*tex2D(_InnerRamp, float2(1/_numSteps * offset, 0.5));
  149. offset ++;
  150. }
  151. // Metallic and smoothness come from slider variables
  152. o.Normal = UnpackScaleNormal (tex2D (_BumpMap, IN.uv_BumpMap), 0.2);
  153. half rim = saturate(dot (normalize(IN.viewDir), o.Normal));
  154. half rim2 = 1 - saturate(dot (normalize(IN.viewDir), o.Normal));
  155. float fresnel = pow(rim2, _Fresnel);// + pow(rim2, _Fresnel);
  156. o.Alpha = clamp(c.a + fresnel + InnerStructure, 0, 1) + 0.2;
  157. o.Albedo = _Color + InnerStructure * _FresnelColor;
  158. o.Specular = _Metallic;
  159. o.Smoothness = _Glossiness;
  160. //o.Emission = SubsurfaceShadingSimple(_InternalColor, o.Normal, IN.viewDir, c.a*_SSS, IN.lightDir, _LightColor0);
  161. }
  162. ENDCG
  163. }
  164. FallBack "Diffuse"
  165. }