UnityMol  0.9.6-875
UnityMol viewer / In developement
CreaseShading.cs
Go to the documentation of this file.
1 using System;
2 using UnityEngine;
3 
4 namespace UnityStandardAssets.ImageEffects
5 {
6  [ExecuteInEditMode]
7  [RequireComponent (typeof(Camera))]
8  [AddComponentMenu ("Image Effects/Edge Detection/Crease Shading")]
10  {
11  public float intensity = 0.5f;
12  public int softness = 1;
13  public float spread = 1.0f;
14 
15  public Shader blurShader = null;
16  private Material blurMaterial = null;
17 
18  public Shader depthFetchShader = null;
19  private Material depthFetchMaterial = null;
20 
21  public Shader creaseApplyShader = null;
22  private Material creaseApplyMaterial = null;
23 
24 
25  public override bool CheckResources ()
26  {
27  CheckSupport (true);
28 
29  blurMaterial = CheckShaderAndCreateMaterial (blurShader, blurMaterial);
30  depthFetchMaterial = CheckShaderAndCreateMaterial (depthFetchShader, depthFetchMaterial);
31  creaseApplyMaterial = CheckShaderAndCreateMaterial (creaseApplyShader, creaseApplyMaterial);
32 
33  if (!isSupported)
34  ReportAutoDisable ();
35  return isSupported;
36  }
37 
38  void OnRenderImage (RenderTexture source, RenderTexture destination)
39  {
40  if (CheckResources()==false)
41  {
42  Graphics.Blit (source, destination);
43  return;
44  }
45 
46  int rtW = source.width;
47  int rtH = source.height;
48 
49  float widthOverHeight = (1.0f * rtW) / (1.0f * rtH);
50  float oneOverBaseSize = 1.0f / 512.0f;
51 
52  RenderTexture hrTex = RenderTexture.GetTemporary (rtW, rtH, 0);
53  RenderTexture lrTex1 = RenderTexture.GetTemporary (rtW/2, rtH/2, 0);
54 
55  Graphics.Blit (source,hrTex, depthFetchMaterial);
56  Graphics.Blit (hrTex, lrTex1);
57 
58  for(int i = 0; i < softness; i++)
59  {
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);
64  lrTex1 = lrTex2;
65 
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);
70  lrTex1 = lrTex2;
71  }
72 
73  creaseApplyMaterial.SetTexture ("_HrDepthTex", hrTex);
74  creaseApplyMaterial.SetTexture ("_LrDepthTex", lrTex1);
75  creaseApplyMaterial.SetFloat ("intensity", intensity);
76  Graphics.Blit (source,destination, creaseApplyMaterial);
77 
78  RenderTexture.ReleaseTemporary (hrTex);
79  RenderTexture.ReleaseTemporary (lrTex1);
80  }
81  }
82 }
void OnRenderImage(RenderTexture source, RenderTexture destination)