Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. Shader "Custom/Scope Overlay Refraction"
  2. {
  3. Properties
  4. {
  5. [Header(Main Texture)]
  6. [Space(10)]
  7. _Color(" Main Color", Color) = (1, 1, 1, 1)
  8. _SpecColor(" Specular Color", Color) = (1, 1, 1, 1)
  9. _Smoothness(" Smoothness", Range(0, 1)) = 1
  10. _Metallic(" Metallic", Range(0, 1)) = 0
  11. _MainTex(" Main Texture", 2D) = "white" {}
  12. [Normal]
  13. _BumpMap(" Normalmap", 2D) = "bump" {}
  14. _NormSmoothness(" Normal Smoothness", Range(0, 1)) = 1
  15. _ParallaxMap(" Heightmap", 2D) = "bump" {}
  16. [Normal]
  17. _Parallax(" Height", Range(0.005, 10)) = 0.02
  18. [Space(10)]
  19. [Header(Reticle Overlay)]
  20. [Space(10)]
  21. _DecalReticle(" Decal - Reticle Overlay", 2D) = "white" {}
  22. _DecalReticleSmoothness(" Reticle Reflection", Range(0, 1)) = 1
  23. [Space(10)]
  24. [Header(Shadow Overlay)]
  25. [Space(10)]
  26. _DecalShadow(" Decal - Parallax Shadow Overlay", 2D) = "white" {}
  27. _DecalShadowSmoothness(" Parallax Shadow Reflection", Range(0, 1)) = 1
  28. [Space(10)]
  29. [Header(Parallax Offsets)]
  30. [Space(10)]
  31. _EyeReliefDistance(" Eye Relief Distance", Float) = 0.5
  32. _EyeReliefNoBlurZone(" No Blur Zone", Float) = 0.2
  33. _ScopePictureBlurMultiplier(" Scope Blur Multiplier", Float) = 0.01
  34. _MainTexOffsetMultiplier(" Main Texture Offset Multiplier", Float) = 0.01
  35. _ReticleOffsetMultiplier(" Reticle Offset Multiplier", Float) = 0.02
  36. _ShadowOffsetMultiplier(" Parallax Shadow Offset Multiplier", Float) = 0.05
  37. _ShadowScaleMultiplier(" Parallax Shadow Scale Multiplier", Float) = 0.5
  38. _RefractionFadeInStart(" Refraction Fade-In Start angle", Float) = 7
  39. _RefractionFadeInEnd(" Refraction Fade-In End angle", Float) = 100
  40. [Space(5)]
  41. _CameraDistance(" Camera Distance (Set via script)", Float) = 1
  42. [Space(5)]
  43. _XOffset(" X Offset (Set via script)", Float) = 0
  44. _YOffset(" Y Offset (Set via script)", Float) = 0
  45. _InverseOfRenderViewportScale(" Offset for render scale", Float) = 1
  46. _Test1 ("Test 1", Float) = 1
  47. _Test2("Test 2", Float) = 1
  48. _Test3("Test 3", Float) = 1
  49. _Test4("Test 4", Float) = 1
  50. }
  51.  
  52. SubShader
  53. {
  54. ColorMask RGB
  55. Cull Off
  56.  
  57. Tags{"RenderType" = "Opaque" }
  58. LOD 600
  59.  
  60. CGPROGRAM
  61. #pragma surface surf Standard noshadow
  62. #pragma target 3.0
  63.  
  64. sampler2D _MainTex;
  65. UNITY_DECLARE_TEX2D(_DecalReticle);
  66. UNITY_DECLARE_TEX2D_NOSAMPLER (_DecalShadow);
  67. UNITY_DECLARE_TEX2D_NOSAMPLER (_BumpMap);
  68. UNITY_DECLARE_TEX2D_NOSAMPLER (_ParallaxMap);
  69. half4 _Color;
  70. fixed _Parallax;
  71. fixed _Shininess;
  72. fixed _MainTexOffsetMultiplier;
  73. fixed _ShadowOffsetMultiplier;
  74. fixed _ShadowScaleMultiplier;
  75. fixed _ReticleOffsetMultiplier;
  76. fixed _CameraDistance;
  77. fixed _XOffset;
  78. fixed _YOffset;
  79. fixed _NormSmoothness;
  80. fixed _EyeReliefDistance;
  81. fixed _EyeReliefNoBlurZone;
  82. fixed _ScopePictureBlurMultiplier;
  83. fixed _DecalReticleSmoothness;
  84. fixed _DecalShadowSmoothness;
  85. fixed _DecalShadowDistanceScale;
  86. fixed _Smoothness;
  87. fixed _Metallic;
  88. float _InverseOfRenderViewportScale;
  89.  
  90. struct Input
  91. {
  92. float2 uv_MainTex;
  93. float2 uv_ParallaxMap;
  94. float2 uv_DecalReticle;
  95. float2 uv_DecalShadow;
  96. float2 uv_BumpMap;
  97. float3 viewDir;
  98. };
  99.  
  100. float normpdf(float x, float sigma)
  101. {
  102. return 0.39894*exp(-0.5*x*x / (sigma*sigma)) / sigma;
  103. }
  104.  
  105. half4 blur(sampler2D tex, float2 uv, float blurAmount)
  106. {
  107. half4 col = tex2D(tex, uv);
  108. const int mSize = 11;
  109. const int iter = (mSize - 1) / 2;
  110. for (int i = -iter; i <= iter; ++i) {
  111. for (int j = -iter; j <= iter; ++j) {
  112. col += tex2D(tex, float2(uv.x + i * blurAmount, uv.y + j * blurAmount)) * normpdf(float(i), 7);
  113. }
  114. }
  115.  
  116. return col / mSize;
  117. }
  118.  
  119. void surf(Input IN, inout SurfaceOutputStandard o)
  120. {
  121. half4 norm = UNITY_SAMPLE_TEX2D_SAMPLER(_BumpMap, _DecalReticle, (IN.uv_BumpMap));
  122. norm.r = (0.5 * (1 - _NormSmoothness)) + (norm.r * _NormSmoothness);
  123. norm.g = (0.5 * (1 - _NormSmoothness)) + (norm.g * _NormSmoothness);
  124. norm.b = (1 - _NormSmoothness) + (norm.b * _NormSmoothness);
  125. norm.a = (0.5 * (1 - _NormSmoothness)) + (norm.a * _NormSmoothness);
  126. half3 normFinal = UnpackNormal(norm).rgb;
  127.  
  128. float scale = pow(_CameraDistance + 1 - _EyeReliefDistance, _ShadowScaleMultiplier / _EyeReliefDistance);
  129.  
  130. IN.uv_DecalShadow *= float2(scale, scale);
  131. IN.uv_DecalShadow += float2((1 - scale) / 2, (1 - scale) / 2);
  132. IN.uv_DecalShadow = float2(IN.uv_DecalShadow.x + (_XOffset * _ShadowOffsetMultiplier * scale), IN.uv_DecalShadow.y + (_YOffset * _ShadowOffsetMultiplier * scale));
  133.  
  134. half h = UNITY_SAMPLE_TEX2D_SAMPLER(_ParallaxMap, _DecalReticle, IN.uv_ParallaxMap).w;
  135. float2 offset = ParallaxOffset(h, _Parallax, IN.viewDir);
  136.  
  137. IN.uv_DecalShadow += offset;
  138. half4 DecalShadow = UNITY_SAMPLE_TEX2D_SAMPLER(_DecalShadow, _DecalReticle, IN.uv_DecalShadow);
  139.  
  140. IN.uv_DecalReticle += offset;
  141. IN.uv_DecalReticle = float2(IN.uv_DecalReticle.x + (_XOffset * -_ReticleOffsetMultiplier), IN.uv_DecalReticle.y + (_YOffset * -_ReticleOffsetMultiplier));
  142. half4 reticle = UNITY_SAMPLE_TEX2D(_DecalReticle, IN.uv_DecalReticle);
  143.  
  144. //IN.uv_MainTex = UnityStereoTransformScreenSpaceTex(IN.uv_MainTex);
  145.  
  146. IN.uv_MainTex += offset;
  147. IN.uv_MainTex = float2(IN.uv_MainTex.x + (_XOffset * -_MainTexOffsetMultiplier), IN.uv_MainTex.y + (_YOffset * -_MainTexOffsetMultiplier));
  148. //IN.uv_MainTex *= _InverseOfRenderViewportScale;
  149. half4 tex = blur(_MainTex, IN.uv_MainTex, clamp(abs(_CameraDistance - _EyeReliefDistance) - (_EyeReliefNoBlurZone / 2), 0, 1) * _ScopePictureBlurMultiplier);
  150. half4 c = tex * _Color;
  151.  
  152. half oneMinusDecalShadowAlpha = 1 - DecalShadow.a;
  153. half oneMinusReticleAlpha = 1 - reticle.a;
  154.  
  155. o.Albedo = (c.rgb * c.a * oneMinusDecalShadowAlpha * oneMinusReticleAlpha) + (DecalShadow.rgb * DecalShadow.a) +(reticle.rgb * reticle.a * oneMinusDecalShadowAlpha);
  156. o.Emission = c.rgb * oneMinusReticleAlpha * oneMinusDecalShadowAlpha * _SpecColor;
  157.  
  158. o.Alpha = c.a;
  159. o.Normal = normFinal;
  160. o.Smoothness = (_Smoothness * oneMinusDecalShadowAlpha * _DecalReticleSmoothness) + (_Smoothness * DecalShadow.a * _DecalShadowSmoothness);
  161. o.Metallic = _Metallic;
  162. }
  163.  
  164. ENDCG
  165.  
  166. GrabPass
  167. {
  168. "_Pass1"
  169. }
  170.  
  171. Pass
  172. {
  173. CGPROGRAM
  174.  
  175. #pragma vertex vert
  176. #pragma fragment frag
  177. #include "UnityCG.cginc"
  178.  
  179. float _InverseOfRenderViewportScale;
  180. float _Test1;
  181. float _Test2;
  182. float _Test3;
  183. float _Test4;
  184.  
  185. struct vertInput
  186. {
  187. float4 vertex : POSITION;
  188. float3 normal : NORMAL;
  189. };
  190.  
  191. struct vertOutput
  192. {
  193. float4 pos : SV_POSITION;
  194. float3 normalDir : TEXCOORD0;
  195. float4 grabPos : TEXCOORD2;
  196. float3 viewDir : TEXCOORD1;
  197. };
  198.  
  199. vertOutput vert(vertInput input)
  200. {
  201. float4 someTest = float4 (input.vertex.x * _Test1, input.vertex.y * _Test2, input.vertex.z * _Test3, input.vertex.w * _Test4);
  202. vertOutput output;
  203.  
  204. float4x4 modelMatrix = unity_ObjectToWorld;
  205. float4x4 modelMatrixInverse = unity_WorldToObject;
  206.  
  207. output.viewDir = mul(modelMatrix, input.vertex).xyz - _WorldSpaceCameraPos;
  208. output.normalDir = normalize(mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);
  209.  
  210. //position to draw on screen
  211. output.pos = UnityObjectToClipPos(input.vertex);
  212.  
  213. //this is where we grab from
  214. output.grabPos = ComputeGrabScreenPos(UnityObjectToClipPos(input.vertex));
  215.  
  216. return output;
  217. }
  218.  
  219. sampler2D _Pass1;
  220. fixed _XOffset;
  221. fixed _YOffset;
  222. fixed _RefractionFadeInStart;
  223. fixed _RefractionFadeInEnd;
  224.  
  225. float4 frag(vertOutput input) : COLOR
  226. {
  227. float refractiveIndex = 1.5;
  228. float3 refractedDir = refract(normalize(input.viewDir),
  229. normalize(input.normalDir), 1.0 / refractiveIndex);
  230. fixed maxAngle = clamp(max(abs(_XOffset), abs(_YOffset)) - _RefractionFadeInStart, 0, 90) * (90 / (_RefractionFadeInEnd - _RefractionFadeInStart));
  231. maxAngle /= 90;
  232.  
  233. return ((tex2Dproj(_Pass1, input.grabPos) * (1 - maxAngle)) + (UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, refractedDir) * maxAngle));
  234. }
  235. ENDCG
  236. }
  237. }
  238. FallBack "Standard"
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement