EnviroCubemapBlur.shader 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2. Shader "Hidden/EnviroCubemapBlur" {
  3. Properties {
  4. _MainTex ("Main", CUBE) = "" {}
  5. _Texel ("Texel", Float) = 0.0078125
  6. _Level ("Level", Float) = 0.
  7. }
  8. CGINCLUDE
  9. #pragma vertex vert
  10. #pragma fragment frag
  11. #include "UnityCG.cginc"
  12. #include "HLSLSupport.cginc"
  13. struct v2f {
  14. half4 pos : SV_POSITION;
  15. half4 uvw : TEXCOORD0;
  16. };
  17. v2f vert(appdata_base v)
  18. {
  19. v2f o;
  20. o.pos = UnityObjectToClipPos(v.vertex);
  21. o.uvw = v.texcoord;
  22. return o;
  23. }
  24. UNITY_DECLARE_TEXCUBE(_MainTex);
  25. half _Level; // Workaround for Metal driver bug: please keep this uniform aligned to 4 bytes (case 899153)
  26. half _Texel;
  27. #define zero half3(0., 0., 0.)
  28. #define one half3(1., 1., 1.)
  29. #define two half3(2., 2., 2.)
  30. half3 fold(half3 st, half3 face)
  31. {
  32. half3 c = min(max(st, -one), one);
  33. half3 f = abs(st - c);
  34. half m = max(max(f.x, f.y), f.z);
  35. return c - m*face;
  36. }
  37. half3 gauss(half d)
  38. {
  39. // compute coefficients for positions 0., 1.*d/.5 and 2.*d/.5
  40. // this assumes a sigma of .5 for a density of 1.
  41. half3 v = half3(0., 1.*d, 2.*d);
  42. return exp(-v*v);
  43. }
  44. half4 frag(v2f i) : SV_Target
  45. {
  46. #if (SHADER_TARGET < 30)
  47. return UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, i.uvw.xyz, _Level);
  48. #else
  49. half3 st;
  50. half3 face = abs(i.uvw.xyz)==one ? i.uvw.xyz : zero;
  51. half3 u = face.zxy*_Texel;
  52. half3 v = face.yzx*_Texel;
  53. half4 s = float4(i.uvw.xyz*(one - abs(face)), 0.);
  54. // modulate coefficients based on position (texel density on projected sphere)
  55. half w = 1. / sqrt(1. + dot(s.xyz, s.xyz));
  56. half3 C = gauss(w*w*w);
  57. half4 s1, s2, s3;
  58. half3 c;
  59. s = 0.;
  60. w = 0.;
  61. // first row
  62. c = C.xyz*C.zzz;
  63. st = i.uvw.xyz - 2.*u - 2.*v;
  64. st = fold(st, face);
  65. s3 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  66. st = i.uvw.xyz - 1.*u - 2.*v;
  67. st = fold(st, face);
  68. s2 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  69. st = i.uvw.xyz - 2.*v;
  70. st = fold(st, face);
  71. s1 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  72. st = i.uvw.xyz + 1.*u - 2.*v;
  73. st = fold(st, face);
  74. s2 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  75. st = i.uvw.xyz + 2.*u - 2.*v;
  76. st = fold(st, face);
  77. s3 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  78. w += c.x + dot(c.yz, two.yz);
  79. s1 = c.x*s1 + c.y*s2;
  80. s += c.z*s3;
  81. s += s1;
  82. // second row
  83. c = C.xyz*C.yyy;
  84. st = i.uvw.xyz + 2.*u - 1.*v;
  85. st = fold(st, face);
  86. s3 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  87. st = i.uvw.xyz + 1.*u - 1.*v;
  88. st = fold(st, face);
  89. s2 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  90. st = i.uvw.xyz - 1.*v;
  91. st = fold(st, face);
  92. s1 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  93. st = i.uvw.xyz - 1.*u - 1.*v;
  94. st = fold(st, face);
  95. s2 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  96. st = i.uvw.xyz - 2.*u - 1.*v;
  97. st = fold(st, face);
  98. s3 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  99. w += c.x + dot(c.yz, two.yz);
  100. s1 = c.x*s1 + c.y*s2;
  101. s += c.z*s3;
  102. s += s1;
  103. // third row
  104. c = C.xyz*C.xxx;
  105. st = i.uvw.xyz - 2.*u;
  106. st = fold(st, face);
  107. s3 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  108. st = i.uvw.xyz - 1.*u;
  109. st = fold(st, face);
  110. s2 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  111. st = i.uvw.xyz;
  112. s1 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  113. st = i.uvw.xyz + 1.*u;
  114. s2 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  115. st = i.uvw.xyz + 2.*u;
  116. st = fold(st, face);
  117. s3 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  118. w += c.x + dot(c.yz, two.yz);
  119. s1 = c.x*s1 + c.y*s2;
  120. s += c.z*s3;
  121. s += s1;
  122. // fourth row
  123. c = C.xyz*C.yyy;
  124. st = i.uvw.xyz + 2.*u + 1.*v;
  125. st = fold(st, face);
  126. s3 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  127. st = i.uvw.xyz + 1.*u + 1.*v;
  128. st = fold(st, face);
  129. s2 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  130. st = i.uvw.xyz + 1.*v;
  131. st = fold(st, face);
  132. s1 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  133. st = i.uvw.xyz - 1.*u + 1.*v;
  134. st = fold(st, face);
  135. s2 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  136. st = i.uvw.xyz - 2.*u + 1.*v;
  137. st = fold(st, face);
  138. s3 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  139. w += c.x + dot(c.yz, two.yz);
  140. s1 = c.x*s1 + c.y*s2;
  141. s += c.z*s3;
  142. s += s1;
  143. // fifth row
  144. c = C.xyz*C.zzz;
  145. st = i.uvw.xyz - 2.*u + 2.*v;
  146. st = fold(st, face);
  147. s3 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  148. st = i.uvw.xyz - 1.*u + 2.*v;
  149. st = fold(st, face);
  150. s2 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  151. st = i.uvw.xyz + 2.*v;
  152. st = fold(st, face);
  153. s1 = UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  154. st = i.uvw.xyz + 1.*u + 2.*v;
  155. st = fold(st, face);
  156. s2 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  157. st = i.uvw.xyz + 2.*u + 2.*v;
  158. st = fold(st, face);
  159. s3 += UNITY_SAMPLE_TEXCUBE_LOD(_MainTex, st, _Level);
  160. w += c.x + dot(c.yz, two.yz);
  161. s1 = c.x*s1 + c.y*s2;
  162. s += c.z*s3;
  163. s += s1;
  164. //return half4(C.zzz, 1.);
  165. return s/w;
  166. #endif
  167. }
  168. ENDCG
  169. SubShader {
  170. Tags { "RenderType"="Opaque" }
  171. LOD 200
  172. Pass {
  173. ZTest Always
  174. Blend Off
  175. AlphaTest off
  176. Cull Off
  177. ZWrite Off
  178. Fog { Mode off }
  179. CGPROGRAM
  180. #pragma target 3.0
  181. ENDCG
  182. }
  183. }
  184. SubShader {
  185. Tags { "RenderType"="Opaque" }
  186. LOD 200
  187. Pass {
  188. ZTest Always
  189. Blend Off
  190. AlphaTest off
  191. Cull Off
  192. ZWrite Off
  193. Fog { Mode off }
  194. CGPROGRAM
  195. #pragma target 2.0
  196. ENDCG
  197. }
  198. }
  199. }