Vignetting.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. @script ExecuteInEditMode
  2. @script RequireComponent (Camera)
  3. @script AddComponentMenu ("Image Effects/Vignette")
  4. class Vignetting extends PostEffectsBase {
  5. public var vignetteIntensity : float = 0.375;
  6. public var chromaticAberrationIntensity : float = 0.0;
  7. public var blurVignette : float = 0.0;
  8. // needed shaders & materials
  9. public var vignetteShader : Shader;
  10. private var _vignetteMaterial : Material;
  11. public var separableBlurShader : Shader;
  12. private var _separableBlurMaterial : Material;
  13. public var chromAberrationShader : Shader;
  14. private var _chromAberrationMaterial : Material;
  15. function Start ()
  16. {
  17. CreateMaterials ();
  18. CheckSupport(false);
  19. }
  20. function CreateMaterials ()
  21. {
  22. _vignetteMaterial = CheckShaderAndCreateMaterial(vignetteShader,_vignetteMaterial);
  23. _separableBlurMaterial = CheckShaderAndCreateMaterial(separableBlurShader,_separableBlurMaterial);
  24. _chromAberrationMaterial = CheckShaderAndCreateMaterial(chromAberrationShader,_chromAberrationMaterial);
  25. }
  26. function OnEnable () { }
  27. function OnRenderImage (source : RenderTexture, destination : RenderTexture)
  28. {
  29. // needed for most of the new and improved image FX
  30. CreateMaterials ();
  31. // get render targets
  32. var color : RenderTexture = RenderTexture.GetTemporary(source.width, source.height, 0);
  33. var halfRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 2.0, source.height / 2.0, 0);
  34. var quarterRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 4.0, source.height / 4.0, 0);
  35. var secondQuarterRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 4.0, source.height / 4.0, 0);
  36. // do the downsample and blur
  37. Graphics.Blit (source, halfRezColor, _chromAberrationMaterial, 0);
  38. Graphics.Blit (halfRezColor, quarterRezColor);
  39. // blur the result to get a nicer bloom radius
  40. for (var it : int = 0; it < 2; it++ ) {
  41. _separableBlurMaterial.SetVector ("offsets", Vector4 (0.0, (1.5 * 1.0) / quarterRezColor.height, 0.0, 0.0));
  42. Graphics.Blit (quarterRezColor, secondQuarterRezColor, _separableBlurMaterial);
  43. _separableBlurMaterial.SetVector ("offsets", Vector4 ((1.5 * 1.0) / quarterRezColor.width, 0.0, 0.0, 0.0));
  44. Graphics.Blit (secondQuarterRezColor, quarterRezColor, _separableBlurMaterial);
  45. }
  46. _vignetteMaterial.SetFloat ("vignetteIntensity", vignetteIntensity);
  47. _vignetteMaterial.SetFloat ("blurVignette", blurVignette);
  48. _vignetteMaterial.SetTexture ("_VignetteTex", quarterRezColor);
  49. Graphics.Blit(source, color,_vignetteMaterial);
  50. _chromAberrationMaterial.SetFloat ("chromaticAberrationIntensity", chromaticAberrationIntensity);
  51. Graphics.Blit (color, destination, _chromAberrationMaterial, 1);
  52. RenderTexture.ReleaseTemporary (color);
  53. RenderTexture.ReleaseTemporary (halfRezColor);
  54. RenderTexture.ReleaseTemporary (quarterRezColor);
  55. RenderTexture.ReleaseTemporary (secondQuarterRezColor);
  56. }
  57. }