4 namespace UnityStandardAssets.ImageEffects
7 [RequireComponent (typeof(Camera))]
8 [AddComponentMenu (
"Image Effects/Edge Detection/Crease Shading")]
11 public float intensity = 0.5f;
12 public int softness = 1;
13 public float spread = 1.0f;
15 public Shader blurShader = null;
16 private Material blurMaterial = null;
18 public Shader depthFetchShader = null;
19 private Material depthFetchMaterial = null;
21 public Shader creaseApplyShader = null;
22 private Material creaseApplyMaterial = null;
25 public override bool CheckResources ()
29 blurMaterial = CheckShaderAndCreateMaterial (blurShader, blurMaterial);
30 depthFetchMaterial = CheckShaderAndCreateMaterial (depthFetchShader, depthFetchMaterial);
31 creaseApplyMaterial = CheckShaderAndCreateMaterial (creaseApplyShader, creaseApplyMaterial);
38 void OnRenderImage (RenderTexture source, RenderTexture destination)
40 if (CheckResources()==
false)
42 Graphics.Blit (source, destination);
46 int rtW = source.width;
47 int rtH = source.height;
49 float widthOverHeight = (1.0f * rtW) / (1.0f * rtH);
50 float oneOverBaseSize = 1.0f / 512.0f;
52 RenderTexture hrTex = RenderTexture.GetTemporary (rtW, rtH, 0);
53 RenderTexture lrTex1 = RenderTexture.GetTemporary (rtW/2, rtH/2, 0);
55 Graphics.Blit (source,hrTex, depthFetchMaterial);
56 Graphics.Blit (hrTex, lrTex1);
58 for(
int i = 0; i < softness; i++)
60 RenderTexture lrTex2 = RenderTexture.GetTemporary (rtW/2, rtH/2, 0);
61 blurMaterial.SetVector (
"offsets",
new Vector4 (0.0f, spread * oneOverBaseSize, 0.0f, 0.0f));
62 Graphics.Blit (lrTex1, lrTex2, blurMaterial);
63 RenderTexture.ReleaseTemporary (lrTex1);
66 lrTex2 = RenderTexture.GetTemporary (rtW/2, rtH/2, 0);
67 blurMaterial.SetVector (
"offsets",
new Vector4 (spread * oneOverBaseSize / widthOverHeight, 0.0f, 0.0f, 0.0f));
68 Graphics.Blit (lrTex1, lrTex2, blurMaterial);
69 RenderTexture.ReleaseTemporary (lrTex1);
73 creaseApplyMaterial.SetTexture (
"_HrDepthTex", hrTex);
74 creaseApplyMaterial.SetTexture (
"_LrDepthTex", lrTex1);
75 creaseApplyMaterial.SetFloat (
"intensity", intensity);
76 Graphics.Blit (source,destination, creaseApplyMaterial);
78 RenderTexture.ReleaseTemporary (hrTex);
79 RenderTexture.ReleaseTemporary (lrTex1);