ContrastEnhance.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. @script ExecuteInEditMode
  2. @script RequireComponent(Camera)
  3. @script AddComponentMenu("Image Effects/Contrast Enhance (Unsharp Mask)")
  4. class ContrastEnhance extends PostEffectsBase {
  5. public var intensity : float = 0.5;
  6. public var threshhold : float = 0.0;
  7. private var _separableBlurMaterial : Material;
  8. private var _contrastCompositeMaterial : Material;
  9. public var blurSpread : float = 1.0;
  10. public var separableBlurShader : Shader = null;
  11. public var contrastCompositeShader : Shader = null;
  12. function CreateMaterials ()
  13. {
  14. _contrastCompositeMaterial = CheckShaderAndCreateMaterial(contrastCompositeShader,_contrastCompositeMaterial);
  15. _separableBlurMaterial = CheckShaderAndCreateMaterial(separableBlurShader,_separableBlurMaterial);
  16. }
  17. function Start ()
  18. {
  19. CreateMaterials ();
  20. CheckSupport(false);
  21. }
  22. function OnRenderImage (source : RenderTexture, destination : RenderTexture)
  23. {
  24. CreateMaterials ();
  25. var halfRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 2.0, source.height / 2.0, 0);
  26. var quarterRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 4.0, source.height / 4.0, 0);
  27. var secondQuarterRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 4.0, source.height / 4.0, 0);
  28. // do the downsample and stuff
  29. Graphics.Blit (source, halfRezColor);
  30. Graphics.Blit (halfRezColor, quarterRezColor);
  31. // blurring
  32. _separableBlurMaterial.SetVector ("offsets", Vector4 (0.0, (blurSpread * 1.0) / quarterRezColor.height, 0.0, 0.0));
  33. Graphics.Blit (quarterRezColor, secondQuarterRezColor, _separableBlurMaterial);
  34. _separableBlurMaterial.SetVector ("offsets", Vector4 ((blurSpread * 1.0) / quarterRezColor.width, 0.0, 0.0, 0.0));
  35. Graphics.Blit (secondQuarterRezColor, quarterRezColor, _separableBlurMaterial);
  36. // comp
  37. _contrastCompositeMaterial.SetTexture ("_MainTexBlurred", quarterRezColor);
  38. _contrastCompositeMaterial.SetFloat ("intensity", intensity);
  39. _contrastCompositeMaterial.SetFloat ("threshhold", threshhold);
  40. Graphics.Blit (source, destination, _contrastCompositeMaterial);
  41. RenderTexture.ReleaseTemporary (halfRezColor);
  42. RenderTexture.ReleaseTemporary (quarterRezColor);
  43. RenderTexture.ReleaseTemporary (secondQuarterRezColor);
  44. }
  45. }