SpecularVertex.shader 4.3 KB

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