1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[AddComponentMenu("Image Effects/Color Adjustments/Contrast Stretch")]
public class ContrastStretchEffect : MonoBehaviour
{
/// Adaptation speed - percents per frame, if playing at 30FPS.
/// Default is 0.02 (2% each 1/30s).
public float adaptationSpeed = 0.02f;
/// If our scene is really dark (or really bright), we might not want to
/// stretch its contrast to the full range.
/// limitMinimum=0, limitMaximum=1 is the same as not applying the effect at all.
/// limitMinimum=1, limitMaximum=0 is always stretching colors to full range.
/// The limit on the minimum luminance (0...1) - we won't go above this.
public float limitMinimum = 0.2f;
/// The limit on the maximum luminance (0...1) - we won't go below this.
public float limitMaximum = 0.6f;
// To maintain adaptation levels over time, we need two 1x1 render textures
// and ping-pong between them.
private RenderTexture[] adaptRenderTex = new RenderTexture[2];
private int curAdaptIndex = 0;
// Computes scene luminance (grayscale) image
public Shader shaderLum;
private Material m_materialLum;
protected Material materialLum {
get {
if( m_materialLum == null ) {
m_materialLum = new Material(shaderLum);
m_materialLum.hideFlags = HideFlags.HideAndDontSave;
}
return m_materialLum;
}
}
// Reduces size of the image by 2x2, while computing maximum/minimum values.
// By repeatedly applying this shader, we reduce the initial luminance image
// to 1x1 image with minimum/maximum luminances found.
public Shader shaderReduce;
private Material m_materialReduce;
protected Material materialReduce {
get {
if( m_materialReduce == null ) {
m_materialReduce = new Material(shaderReduce);
m_materialReduce.hideFlags = HideFlags.HideAndDontSave;
}
return m_materialReduce;
}
}
// Adaptation shader - gradually "adapts" minimum/maximum luminances,
// based on currently adapted 1x1 image and the actual 1x1 image of the current scene.
public Shader shaderAdapt;
private Material m_materialAdapt;
protected Material materialAdapt {
get {
if( m_materialAdapt == null ) {
m_materialAdapt = new Material(shaderAdapt);
m_materialAdapt.hideFlags = HideFlags.HideAndDontSave;
}
return m_materialAdapt;
}
}
// Final pass - stretches the color values of the original scene, based on currently
// adpated minimum/maximum values.
public Shader shaderApply;
private Material m_materialApply;
protected Material materialApply {
get {
if( m_materialApply == null ) {
m_materialApply = new Material(shaderApply);
m_materialApply.hideFlags = HideFlags.HideAndDontSave;
}
return m_materialApply;
}
}
void Start()
{
// Disable if we don't support image effects
if (!SystemInfo.supportsImageEffects) {
enabled = false;
return;
}
if (!shaderAdapt.isSupported || !shaderApply.isSupported || !shaderLum.isSupported || !shaderReduce.isSupported) {
enabled = false;
return;
}
}
void OnEnable()
{
for( int i = 0; i < 2; ++i )
{
if( !adaptRenderTex[i] ) {
adaptRenderTex[i] = new RenderTexture(1, 1, 0);
adaptRenderTex[i].hideFlags = HideFlags.HideAndDontSave;
}
}
}
void OnDisable()
{
for( int i = 0; i < 2; ++i )
{
DestroyImmediate( adaptRenderTex[i] );
adaptRenderTex[i] = null;
}
if( m_materialLum )
DestroyImmediate( m_materialLum );
if( m_materialReduce )
DestroyImmediate( m_materialReduce );
if( m_materialAdapt )
DestroyImmediate( m_materialAdapt );
if( m_materialApply )
DestroyImmediate( m_materialApply );
}
/// Apply the filter
void OnRenderImage (RenderTexture source, RenderTexture destination)
{
// Blit to smaller RT and convert to luminance on the way
const int TEMP_RATIO = 1; // 4x4 smaller
RenderTexture rtTempSrc = RenderTexture.GetTemporary(source.width/TEMP_RATIO, source.height/TEMP_RATIO);
Graphics.Blit (source, rtTempSrc, materialLum);
// Repeatedly reduce this image in size, computing min/max luminance values
// In the end we'll have 1x1 image with min/max luminances found.
const int FINAL_SIZE = 1;
//const int FINAL_SIZE = 1;
while( rtTempSrc.width > FINAL_SIZE || rtTempSrc.height > FINAL_SIZE )
{
const int REDUCE_RATIO = 2; // our shader does 2x2 reduction
int destW = rtTempSrc.width / REDUCE_RATIO;
if( destW < FINAL_SIZE ) destW = FINAL_SIZE;
int destH = rtTempSrc.height / REDUCE_RATIO;
if( destH < FINAL_SIZE ) destH = FINAL_SIZE;
RenderTexture rtTempDst = RenderTexture.GetTemporary(destW,destH);
Graphics.Blit (rtTempSrc, rtTempDst, materialReduce);
// Release old src temporary, and make new temporary the source
RenderTexture.ReleaseTemporary( rtTempSrc );
rtTempSrc = rtTempDst;
}
// Update viewer's adaptation level
CalculateAdaptation( rtTempSrc );
// Apply contrast strech to the original scene, using currently adapted parameters
materialApply.SetTexture("_AdaptTex", adaptRenderTex[curAdaptIndex] );
Graphics.Blit (source, destination, materialApply);
RenderTexture.ReleaseTemporary( rtTempSrc );
}
/// Helper function to do gradual adaptation to min/max luminances
private void CalculateAdaptation( Texture curTexture )
{
int prevAdaptIndex = curAdaptIndex;
curAdaptIndex = (curAdaptIndex+1) % 2;
// Adaptation speed is expressed in percents/frame, based on 30FPS.
// Calculate the adaptation lerp, based on current FPS.
float adaptLerp = 1.0f - Mathf.Pow( 1.0f - adaptationSpeed, 30.0f * Time.deltaTime );
const float kMinAdaptLerp = 0.01f;
adaptLerp = Mathf.Clamp( adaptLerp, kMinAdaptLerp, 1 );
materialAdapt.SetTexture("_CurTex", curTexture );
materialAdapt.SetVector("_AdaptParams", new Vector4(
adaptLerp,
limitMinimum,
limitMaximum,
0.0f
));
// clear destination RT so its contents don't need to be restored
Graphics.SetRenderTarget(adaptRenderTex[curAdaptIndex]);
GL.Clear(false, true, Color.black);
Graphics.Blit (
adaptRenderTex[prevAdaptIndex],
adaptRenderTex[curAdaptIndex],
materialAdapt);
}
}
|