UnityMol  0.9.6-875
UnityMol viewer / In developement
NoiseAndScratches.cs
Go to the documentation of this file.
1 using System;
2 using UnityEngine;
3 using Random = UnityEngine.Random;
4 
5 namespace UnityStandardAssets.ImageEffects
6 {
7  [ExecuteInEditMode]
8  [RequireComponent (typeof(Camera))]
9  [AddComponentMenu("Image Effects/Noise/Noise and Scratches")]
10  public class NoiseAndScratches : MonoBehaviour
11  {
15  public bool monochrome = true;
16  private bool rgbFallback = false;
17 
18  // Noise grain takes random intensity from Min to Max.
19  [Range(0.0f,5.0f)]
20  public float grainIntensityMin = 0.1f;
21  [Range(0.0f, 5.0f)]
22  public float grainIntensityMax = 0.2f;
23 
25  [Range(0.1f, 50.0f)]
26  public float grainSize = 2.0f;
27 
28  // Scratches take random intensity from Min to Max.
29  [Range(0.0f, 5.0f)]
30  public float scratchIntensityMin = 0.05f;
31  [Range(0.0f, 5.0f)]
32  public float scratchIntensityMax = 0.25f;
33 
35  [Range(1.0f, 30.0f)]
36  public float scratchFPS = 10.0f;
38  [Range(0.0f, 1.0f)]
39  public float scratchJitter = 0.01f;
40 
41  public Texture grainTexture;
42  public Texture scratchTexture;
43  public Shader shaderRGB;
44  public Shader shaderYUV;
45  private Material m_MaterialRGB;
46  private Material m_MaterialYUV;
47 
48  private float scratchTimeLeft = 0.0f;
49  private float scratchX, scratchY;
50 
51  protected void Start ()
52  {
53  // Disable if we don't support image effects
54  if (!SystemInfo.supportsImageEffects) {
55  enabled = false;
56  return;
57  }
58 
59  if ( shaderRGB == null || shaderYUV == null )
60  {
61  Debug.Log( "Noise shaders are not set up! Disabling noise effect." );
62  enabled = false;
63  }
64  else
65  {
66  if ( !shaderRGB.isSupported ) // disable effect if RGB shader is not supported
67  enabled = false;
68  else if ( !shaderYUV.isSupported ) // fallback to RGB if YUV is not supported
69  rgbFallback = true;
70  }
71  }
72 
73  protected Material material {
74  get {
75  if ( m_MaterialRGB == null ) {
76  m_MaterialRGB = new Material( shaderRGB );
77  m_MaterialRGB.hideFlags = HideFlags.HideAndDontSave;
78  }
79  if ( m_MaterialYUV == null && !rgbFallback ) {
80  m_MaterialYUV = new Material( shaderYUV );
81  m_MaterialYUV.hideFlags = HideFlags.HideAndDontSave;
82  }
83  return (!rgbFallback && !monochrome) ? m_MaterialYUV : m_MaterialRGB;
84  }
85  }
86 
87  protected void OnDisable() {
88  if ( m_MaterialRGB )
89  DestroyImmediate( m_MaterialRGB );
90  if ( m_MaterialYUV )
91  DestroyImmediate( m_MaterialYUV );
92  }
93 
94  private void SanitizeParameters()
95  {
96  grainIntensityMin = Mathf.Clamp( grainIntensityMin, 0.0f, 5.0f );
97  grainIntensityMax = Mathf.Clamp( grainIntensityMax, 0.0f, 5.0f );
98  scratchIntensityMin = Mathf.Clamp( scratchIntensityMin, 0.0f, 5.0f );
99  scratchIntensityMax = Mathf.Clamp( scratchIntensityMax, 0.0f, 5.0f );
100  scratchFPS = Mathf.Clamp( scratchFPS, 1, 30 );
101  scratchJitter = Mathf.Clamp( scratchJitter, 0.0f, 1.0f );
102  grainSize = Mathf.Clamp( grainSize, 0.1f, 50.0f );
103  }
104 
105  // Called by the camera to apply the image effect
106  void OnRenderImage (RenderTexture source, RenderTexture destination)
107  {
108  SanitizeParameters();
109 
110  if ( scratchTimeLeft <= 0.0f )
111  {
112  scratchTimeLeft = Random.value * 2 / scratchFPS; // we have sanitized it earlier, won't be zero
113  scratchX = Random.value;
114  scratchY = Random.value;
115  }
116  scratchTimeLeft -= Time.deltaTime;
117 
118  Material mat = material;
119 
120  mat.SetTexture("_GrainTex", grainTexture);
121  mat.SetTexture("_ScratchTex", scratchTexture);
122  float grainScale = 1.0f / grainSize; // we have sanitized it earlier, won't be zero
123  mat.SetVector("_GrainOffsetScale", new Vector4(
124  Random.value,
125  Random.value,
126  (float)Screen.width / (float)grainTexture.width * grainScale,
127  (float)Screen.height / (float)grainTexture.height * grainScale
128  ));
129  mat.SetVector("_ScratchOffsetScale", new Vector4(
130  scratchX + Random.value*scratchJitter,
131  scratchY + Random.value*scratchJitter,
132  (float)Screen.width / (float) scratchTexture.width,
133  (float)Screen.height / (float) scratchTexture.height
134  ));
135  mat.SetVector("_Intensity", new Vector4(
136  Random.Range(grainIntensityMin, grainIntensityMax),
137  Random.Range(scratchIntensityMin, scratchIntensityMax),
138  0, 0 ));
139  Graphics.Blit (source, destination, mat);
140  }
141  }
142 }
void OnRenderImage(RenderTexture source, RenderTexture destination)