BumpSpecular.shader 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Shader "SupGames/PlanarReflection/Bumped Specular"
  2. {
  3. Properties{
  4. _Color("Color", Color) = (1,1,1,1)
  5. _MainTex("Main Texture", 2D) = "white" {}
  6. _BumpTex("Normal Map", 2D) = "bump" {}
  7. _Glossiness("Glossiness", Range(0.01,100)) = 0.03
  8. _SpecColor("Specular Color", Color) = (1,1,1,1)
  9. _Distort("Distort Amount", Range(0.01,10)) = 1
  10. _MaskTex("Mask Texture", 2D) = "white" {}
  11. _BlurAmount("Blur Amount", Range(0,7)) = 1
  12. }
  13. SubShader{
  14. Tags {"RenderType" = "Opaque"}
  15. LOD 150
  16. Pass {
  17. Tags { "LightMode" = "ForwardBase" }
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. #pragma fragmentoption ARB_precision_hint_fastest
  22. #pragma shader_feature_local BLUR
  23. #pragma shader_feature_local VRon
  24. #pragma multi_compile_fwdbase novertexlight
  25. #pragma multi_compile_instancing
  26. #pragma multi_compile_fog
  27. #include "UnityCG.cginc"
  28. #include "AutoLight.cginc"
  29. UNITY_DECLARE_SCREENSPACE_TEXTURE(_ReflectionTex);
  30. #ifdef VRon
  31. UNITY_DECLARE_SCREENSPACE_TEXTURE(_ReflectionTexRight);
  32. #endif
  33. UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
  34. UNITY_DECLARE_SCREENSPACE_TEXTURE(_BumpTex);
  35. UNITY_DECLARE_SCREENSPACE_TEXTURE(_MaskTex);
  36. fixed _RefAlpha;
  37. fixed _Glossiness;
  38. fixed _Distort;
  39. fixed _BlurAmount;
  40. fixed4 _MainTex_ST;
  41. fixed4 _BumpTex_ST;
  42. fixed4 _Color;
  43. fixed4 _SpecColor;
  44. fixed _ReflectionAlpha;
  45. fixed4 _ReflectionTex_TexelSize;
  46. fixed4 _LightColor0;
  47. struct appdata
  48. {
  49. fixed4 vertex : POSITION;
  50. fixed4 uv : TEXCOORD0;
  51. fixed3 normal : NORMAL;
  52. fixed4 tangent : TANGENT;
  53. UNITY_VERTEX_INPUT_INSTANCE_ID
  54. };
  55. struct v2f
  56. {
  57. fixed4 pos : SV_POSITION;
  58. fixed4 uv : TEXCOORD0;
  59. fixed2 fogCoord : TEXCOORD1;
  60. fixed4 normal : TEXCOORD2;
  61. fixed4 tangent : TEXCOORD3;
  62. fixed4 bitangent : TEXCOORD4;
  63. #if defined(BLUR)
  64. fixed4 offset : TEXCOORD5;
  65. #endif
  66. LIGHTING_COORDS(6, 7)
  67. UNITY_VERTEX_INPUT_INSTANCE_ID
  68. UNITY_VERTEX_OUTPUT_STEREO
  69. };
  70. v2f vert(appdata v)
  71. {
  72. v2f o;
  73. UNITY_SETUP_INSTANCE_ID(v);
  74. UNITY_INITIALIZE_OUTPUT(v2f, o);
  75. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  76. o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
  77. fixed3 viewDirection = _WorldSpaceCameraPos - mul(unity_ObjectToWorld, v.vertex).xyz;
  78. o.normal = fixed4(normalize(mul(fixed4(v.normal, 0.0h), unity_WorldToObject).xyz), viewDirection.x);
  79. o.tangent = fixed4(normalize(mul(unity_ObjectToWorld, fixed4(v.tangent.xyz, 0.0h)).xyz), viewDirection.y);
  80. o.bitangent = fixed4(cross(o.normal.xyz, o.tangent.xyz) * v.tangent.w * unity_WorldTransformParams.w, viewDirection.z);
  81. o.pos = UnityObjectToClipPos(v.vertex);
  82. fixed4 scrPos = ComputeNonStereoScreenPos(o.pos);
  83. o.uv.zw = scrPos.xy;
  84. o.fogCoord.y = scrPos.w;
  85. #if defined(BLUR)
  86. fixed2 offset = _ReflectionTex_TexelSize.xy * _BlurAmount;
  87. o.offset = fixed4(-offset, offset);
  88. #endif
  89. TRANSFER_VERTEX_TO_FRAGMENT(o);
  90. UNITY_TRANSFER_FOG(o, o.pos);
  91. return o;
  92. }
  93. fixed4 frag(v2f i) : SV_Target
  94. {
  95. UNITY_SETUP_INSTANCE_ID(i);
  96. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  97. fixed4 encodedNormal = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_BumpTex, _BumpTex_ST.xy * i.uv + _BumpTex_ST.zw);
  98. fixed3 localCoords = UnpackNormal(encodedNormal);
  99. fixed3 viewDirection = fixed3(i.normal.w, i.tangent.w, i.bitangent.w);
  100. fixed3 normalDirection = normalize(mul(localCoords, fixed3x3(i.tangent.xyz, i.bitangent.xyz, i.normal.xyz)));
  101. fixed3 diffuseReflection = UNITY_LIGHTMODEL_AMBIENT.rgb + _LightColor0.rgb * dot(normalDirection, _WorldSpaceLightPos0.xyz)* LIGHT_ATTENUATION(i);
  102. fixed3 specularReflection = _SpecColor.rgb * _LightColor0.rgb * pow(saturate(dot(normalDirection, normalize(_WorldSpaceLightPos0.xyz + viewDirection))), _Glossiness);
  103. fixed4 color = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv.xy);
  104. fixed4 mask = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MaskTex, i.uv.xy);
  105. fixed4 bump = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_BumpTex, i.uv.xy);
  106. i.uv.z += (bump.x - 0.5h) * _Distort;
  107. i.uv.w += (0.5h - bump.y) * _Distort;
  108. i.uv.zw /= i.fogCoord.y;
  109. fixed4 reflection = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTex, i.uv.zw);
  110. #if defined(BLUR)
  111. i.offset /= i.fogCoord.y;
  112. i.offset = fixed4(i.uv.zz + i.offset.xz, i.uv.ww + i.offset.yw);
  113. reflection += UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTex, i.offset.xz);
  114. reflection += UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTex, i.offset.xw);
  115. reflection += UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTex, i.offset.yz);
  116. reflection += UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTex, i.offset.yw);
  117. reflection *= 0.2h;
  118. #endif
  119. #ifdef VRon
  120. fixed4 reflectionr = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTexRight, i.uv.zw);
  121. #ifdef BLUR
  122. reflectionr += UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTexRight, i.offset.xz);
  123. reflectionr += UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTexRight, i.offset.xw);
  124. reflectionr += UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTexRight, i.offset.yz);
  125. reflectionr += UNITY_SAMPLE_SCREENSPACE_TEXTURE(_ReflectionTexRight, i.offset.yw);
  126. reflectionr *= 0.2h;
  127. #endif
  128. reflection = lerp(reflection, reflectionr, unity_StereoEyeIndex);
  129. #endif
  130. color = fixed4(specularReflection * (1.0h - color.a) + diffuseReflection * color.rgb, 1.0h);
  131. UNITY_APPLY_FOG(i.fogCoord.x, color);
  132. return (lerp(color, reflection, _RefAlpha * mask.r) + lerp(reflection, color, 1 - _RefAlpha * mask.r)) * _Color * 0.5h;
  133. }
  134. ENDCG
  135. }
  136. }
  137. }