3 using Random = UnityEngine.Random;
5 namespace UnityStandardAssets.ImageEffects
8 [RequireComponent (typeof(Camera))]
9 [AddComponentMenu(
"Image Effects/Noise/Noise and Scratches")]
15 public bool monochrome =
true;
16 private bool rgbFallback =
false;
20 public float grainIntensityMin = 0.1f;
22 public float grainIntensityMax = 0.2f;
26 public float grainSize = 2.0f;
30 public float scratchIntensityMin = 0.05f;
32 public float scratchIntensityMax = 0.25f;
36 public float scratchFPS = 10.0f;
39 public float scratchJitter = 0.01f;
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;
48 private float scratchTimeLeft = 0.0f;
49 private float scratchX, scratchY;
51 protected void Start ()
54 if (!SystemInfo.supportsImageEffects) {
59 if ( shaderRGB == null || shaderYUV == null )
61 Debug.Log(
"Noise shaders are not set up! Disabling noise effect." );
66 if ( !shaderRGB.isSupported )
68 else if ( !shaderYUV.isSupported )
73 protected Material material {
75 if ( m_MaterialRGB == null ) {
76 m_MaterialRGB =
new Material( shaderRGB );
77 m_MaterialRGB.hideFlags = HideFlags.HideAndDontSave;
79 if ( m_MaterialYUV == null && !rgbFallback ) {
80 m_MaterialYUV =
new Material( shaderYUV );
81 m_MaterialYUV.hideFlags = HideFlags.HideAndDontSave;
83 return (!rgbFallback && !monochrome) ? m_MaterialYUV : m_MaterialRGB;
87 protected void OnDisable() {
89 DestroyImmediate( m_MaterialRGB );
91 DestroyImmediate( m_MaterialYUV );
94 private void SanitizeParameters()
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 );
106 void OnRenderImage (RenderTexture source, RenderTexture destination)
108 SanitizeParameters();
110 if ( scratchTimeLeft <= 0.0f )
112 scratchTimeLeft = Random.value * 2 / scratchFPS;
113 scratchX = Random.value;
114 scratchY = Random.value;
116 scratchTimeLeft -= Time.deltaTime;
118 Material mat = material;
120 mat.SetTexture(
"_GrainTex", grainTexture);
121 mat.SetTexture(
"_ScratchTex", scratchTexture);
122 float grainScale = 1.0f / grainSize;
123 mat.SetVector(
"_GrainOffsetScale",
new Vector4(
126 (
float)Screen.width / (
float)grainTexture.width * grainScale,
127 (
float)Screen.height / (
float)grainTexture.height * grainScale
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
135 mat.SetVector(
"_Intensity",
new Vector4(
136 Random.Range(grainIntensityMin, grainIntensityMax),
137 Random.Range(scratchIntensityMin, scratchIntensityMax),
139 Graphics.Blit (source, destination, mat);