CreaseShading.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.ImageEffects
  4. {
  5. [ExecuteInEditMode]
  6. [RequireComponent (typeof(Camera))]
  7. [AddComponentMenu ("Image Effects/Edge Detection/Crease Shading")]
  8. public class CreaseShading : PostEffectsBase
  9. {
  10. public float intensity = 0.5f;
  11. public int softness = 1;
  12. public float spread = 1.0f;
  13. public Shader blurShader = null;
  14. private Material blurMaterial = null;
  15. public Shader depthFetchShader = null;
  16. private Material depthFetchMaterial = null;
  17. public Shader creaseApplyShader = null;
  18. private Material creaseApplyMaterial = null;
  19. public override bool CheckResources ()
  20. {
  21. CheckSupport (true);
  22. blurMaterial = CheckShaderAndCreateMaterial (blurShader, blurMaterial);
  23. depthFetchMaterial = CheckShaderAndCreateMaterial (depthFetchShader, depthFetchMaterial);
  24. creaseApplyMaterial = CheckShaderAndCreateMaterial (creaseApplyShader, creaseApplyMaterial);
  25. if (!isSupported)
  26. ReportAutoDisable ();
  27. return isSupported;
  28. }
  29. void OnRenderImage (RenderTexture source, RenderTexture destination)
  30. {
  31. if (CheckResources()==false)
  32. {
  33. Graphics.Blit (source, destination);
  34. return;
  35. }
  36. int rtW = source.width;
  37. int rtH = source.height;
  38. float widthOverHeight = (1.0f * rtW) / (1.0f * rtH);
  39. float oneOverBaseSize = 1.0f / 512.0f;
  40. RenderTexture hrTex = RenderTexture.GetTemporary (rtW, rtH, 0);
  41. RenderTexture lrTex1 = RenderTexture.GetTemporary (rtW/2, rtH/2, 0);
  42. Graphics.Blit (source,hrTex, depthFetchMaterial);
  43. Graphics.Blit (hrTex, lrTex1);
  44. for(int i = 0; i < softness; i++)
  45. {
  46. RenderTexture lrTex2 = RenderTexture.GetTemporary (rtW/2, rtH/2, 0);
  47. blurMaterial.SetVector ("offsets", new Vector4 (0.0f, spread * oneOverBaseSize, 0.0f, 0.0f));
  48. Graphics.Blit (lrTex1, lrTex2, blurMaterial);
  49. RenderTexture.ReleaseTemporary (lrTex1);
  50. lrTex1 = lrTex2;
  51. lrTex2 = RenderTexture.GetTemporary (rtW/2, rtH/2, 0);
  52. blurMaterial.SetVector ("offsets", new Vector4 (spread * oneOverBaseSize / widthOverHeight, 0.0f, 0.0f, 0.0f));
  53. Graphics.Blit (lrTex1, lrTex2, blurMaterial);
  54. RenderTexture.ReleaseTemporary (lrTex1);
  55. lrTex1 = lrTex2;
  56. }
  57. creaseApplyMaterial.SetTexture ("_HrDepthTex", hrTex);
  58. creaseApplyMaterial.SetTexture ("_LrDepthTex", lrTex1);
  59. creaseApplyMaterial.SetFloat ("intensity", intensity);
  60. Graphics.Blit (source,destination, creaseApplyMaterial);
  61. RenderTexture.ReleaseTemporary (hrTex);
  62. RenderTexture.ReleaseTemporary (lrTex1);
  63. }
  64. }
  65. }