Crease.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. @script ExecuteInEditMode
  2. @script RequireComponent (Camera)
  3. @script AddComponentMenu ("Image Effects/Crease")
  4. class Crease extends PostEffectsBase {
  5. public var intensity : float = 0.5;
  6. public var softness : int = 1;
  7. public var spread : float = 1.0;
  8. public var blurShader : Shader;
  9. private var _blurMaterial : Material = null;
  10. public var depthFetchShader : Shader;
  11. private var _depthFetchMaterial : Material = null;
  12. public var creaseApplyShader : Shader;
  13. private var _creaseApplyMaterial : Material = null;
  14. function CreateMaterials ()
  15. {
  16. _blurMaterial = CheckShaderAndCreateMaterial(blurShader, _blurMaterial);
  17. _depthFetchMaterial = CheckShaderAndCreateMaterial(depthFetchShader,_depthFetchMaterial);
  18. _creaseApplyMaterial = CheckShaderAndCreateMaterial(creaseApplyShader,_creaseApplyMaterial);
  19. }
  20. function Start ()
  21. {
  22. CreateMaterials();
  23. CheckSupport(true);
  24. }
  25. function OnEnable() {
  26. GetComponent.<Camera>().depthTextureMode |= DepthTextureMode.Depth;
  27. }
  28. function OnRenderImage (source : RenderTexture, destination : RenderTexture)
  29. {
  30. CreateMaterials ();
  31. var hrTex : RenderTexture = RenderTexture.GetTemporary (source.width, source.height, 0);
  32. var lrTex1 : RenderTexture = RenderTexture.GetTemporary (source.width / 2, source.height / 2, 0);
  33. var lrTex2 : RenderTexture = RenderTexture.GetTemporary (source.width / 2, source.height / 2, 0);
  34. Graphics.Blit(source,hrTex,_depthFetchMaterial);
  35. Graphics.Blit(hrTex,lrTex1);
  36. for(var i : int = 0; i < softness; i++) {
  37. _blurMaterial.SetVector ("offsets", Vector4 (0.0, (spread) / lrTex1.height, 0.0, 0.0));
  38. Graphics.Blit (lrTex1, lrTex2, _blurMaterial);
  39. _blurMaterial.SetVector ("offsets", Vector4 ((spread) / lrTex1.width, 0.0, 0.0, 0.0));
  40. Graphics.Blit (lrTex2, lrTex1, _blurMaterial);
  41. }
  42. _creaseApplyMaterial.SetTexture("_HrDepthTex",hrTex);
  43. _creaseApplyMaterial.SetTexture("_LrDepthTex",lrTex1);
  44. _creaseApplyMaterial.SetFloat("intensity",intensity);
  45. Graphics.Blit(source,destination,_creaseApplyMaterial);
  46. RenderTexture.ReleaseTemporary(hrTex);
  47. RenderTexture.ReleaseTemporary(lrTex1);
  48. RenderTexture.ReleaseTemporary(lrTex2);
  49. }
  50. }