SunShafts.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. @script ExecuteInEditMode
  2. @script RequireComponent (Camera)
  3. @script AddComponentMenu ("Image Effects/Sun Shafts")
  4. enum SunShaftsResolution {
  5. Low = 0,
  6. Normal = 1,
  7. High = 2,
  8. }
  9. class SunShafts extends PostEffectsBase
  10. {
  11. public var resolution : SunShaftsResolution;
  12. public var sunTransform : Transform;
  13. public var radialBlurIterations : int = 2;
  14. public var sunColor : Color = Color.white;
  15. public var sunShaftBlurRadius : float = 0.0164;
  16. public var sunShaftIntensity : float = 1.25;
  17. public var useSkyBoxAlpha : float = 0.75;
  18. public var maxRadius : float = 1.25;
  19. public var useDepthTexture : boolean = true;
  20. public var clearShader : Shader;
  21. private var _clearMaterial : Material;
  22. public var depthDecodeShader : Shader;
  23. private var _encodeDepthRGBA8Material : Material;
  24. public var depthBlurShader : Shader;
  25. private var _radialDepthBlurMaterial : Material;
  26. public var sunShaftsShader : Shader;
  27. private var _sunShaftsMaterial : Material;
  28. public var simpleClearShader : Shader;
  29. private var _simpleClearMaterial : Material;
  30. public var compShader : Shader;
  31. private var _compMaterial : Material;
  32. function CreateMaterials ()
  33. {
  34. _clearMaterial = CheckShaderAndCreateMaterial(clearShader,_clearMaterial);
  35. _sunShaftsMaterial = CheckShaderAndCreateMaterial(sunShaftsShader,_sunShaftsMaterial);
  36. _encodeDepthRGBA8Material = CheckShaderAndCreateMaterial(depthDecodeShader,_encodeDepthRGBA8Material);
  37. _radialDepthBlurMaterial = CheckShaderAndCreateMaterial(depthBlurShader,_radialDepthBlurMaterial);
  38. _simpleClearMaterial = CheckShaderAndCreateMaterial(simpleClearShader,_simpleClearMaterial);
  39. _compMaterial = CheckShaderAndCreateMaterial(compShader,_compMaterial);
  40. }
  41. function Start ()
  42. {
  43. CreateMaterials();
  44. CheckSupport(useDepthTexture);
  45. if(useDepthTexture) {
  46. GetComponent.<Camera>().depthTextureMode |= DepthTextureMode.Depth;
  47. }
  48. }
  49. function OnRenderImage (source : RenderTexture, destination : RenderTexture)
  50. {
  51. CreateMaterials ();
  52. var divider : float = 4.0;
  53. if(resolution == SunShaftsResolution.Normal)
  54. divider = 2.0;
  55. if(resolution == SunShaftsResolution.High)
  56. divider = 1.0;
  57. // get render targets
  58. var secondQuarterRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / divider, source.height / divider, 0);
  59. var lrDepthBuffer : RenderTexture = RenderTexture.GetTemporary(source.width / divider, source.height / divider, 0);
  60. // save the color buffer
  61. Graphics.Blit (source, destination);
  62. // mask skybox (some pixels are clip()'ped, others are kept ...)
  63. if(!useDepthTexture) {
  64. var tmpBuffer : RenderTexture = RenderTexture.GetTemporary(source.width, source.height, 0);
  65. RenderTexture.active = tmpBuffer;
  66. GL.ClearWithSkybox(false, GetComponent.<Camera>());
  67. _compMaterial.SetTexture("_Skybox", tmpBuffer);
  68. Graphics.Blit (source, source, _compMaterial);
  69. RenderTexture.ReleaseTemporary(tmpBuffer);
  70. }
  71. else
  72. Graphics.Blit (source, source, _clearMaterial); // don't care about source :-)
  73. // get depth values
  74. _encodeDepthRGBA8Material.SetFloat("noSkyBoxMask", 1.0-useSkyBoxAlpha);
  75. _encodeDepthRGBA8Material.SetFloat("dontUseSkyboxBrightness", 0.0);
  76. Graphics.Blit (source, lrDepthBuffer, _encodeDepthRGBA8Material);
  77. // black small pixel border to get rid of clamping annoyances
  78. DrawBorder(lrDepthBuffer,_simpleClearMaterial);
  79. var v : Vector3 = Vector3.one * 0.5;
  80. if (sunTransform)
  81. v = GetComponent.<Camera>().WorldToViewportPoint (sunTransform.position);
  82. else {
  83. v = Vector3(0.5, 0.5, 0.0);
  84. }
  85. // radial depth blur now
  86. _radialDepthBlurMaterial.SetVector ("blurRadius4", Vector4 (1.0, 1.0, 0.0, 0.0) * sunShaftBlurRadius );
  87. _radialDepthBlurMaterial.SetVector ("sunPosition", Vector4 (v.x, v.y, v.z, maxRadius));
  88. if (radialBlurIterations<1)
  89. radialBlurIterations = 1;
  90. for (var it2 : int = 0; it2 < radialBlurIterations; it2++ ) {
  91. Graphics.Blit (lrDepthBuffer, secondQuarterRezColor, _radialDepthBlurMaterial);
  92. Graphics.Blit (secondQuarterRezColor, lrDepthBuffer, _radialDepthBlurMaterial);
  93. }
  94. // composite now
  95. _sunShaftsMaterial.SetFloat ("sunShaftIntensity", sunShaftIntensity);
  96. if (v.z >= 0.0)
  97. _sunShaftsMaterial.SetVector ("sunColor", Vector4 (sunColor.r,sunColor.g,sunColor.b,sunColor.a));
  98. else
  99. _sunShaftsMaterial.SetVector ("sunColor", Vector4 (0.0,0.0,0.0,0.0)); // no backprojection !
  100. _sunShaftsMaterial.SetTexture("_ColorBuffer", source);
  101. Graphics.Blit(lrDepthBuffer, destination, _sunShaftsMaterial);
  102. RenderTexture.ReleaseTemporary (lrDepthBuffer);
  103. RenderTexture.ReleaseTemporary (secondQuarterRezColor);
  104. }
  105. }