@@ -45,6 +45,7 @@ public struct Settings {
45
45
[ Range ( - 1 , 1 ) ]
46
46
public float highlights ;
47
47
48
+
48
49
//[Header("Tone Mapping")]
49
50
//[Range(0, 1)]
50
51
//public float strength;
@@ -112,6 +113,7 @@ static class PropertyIDs {
112
113
113
114
internal static readonly int Temperature_f = Shader . PropertyToID ( "_Temperature" ) ;
114
115
internal static readonly int Tint_f = Shader . PropertyToID ( "_Tint" ) ;
116
+ internal static readonly int ColorBalance_v = Shader . PropertyToID ( "_ColorBalance" ) ;
115
117
116
118
internal static readonly int BlackPoint_f = Shader . PropertyToID ( "_BlackPoint" ) ;
117
119
internal static readonly int WhitePoint_f = Shader . PropertyToID ( "_WhitePoint" ) ;
@@ -127,16 +129,25 @@ override protected void UpdateMaterialPerFrame(Material material, Camera camera,
127
129
}
128
130
129
131
override protected void UpdateMaterial ( Material material , Camera camera , VectorInt2 cameraSize ) {
130
-
131
- material . SetFloat ( PropertyIDs . Exposure_f , settings . exposure ) ;
132
- material . SetFloat ( PropertyIDs . Contrast_f , settings . contrast ) ;
133
- material . SetFloat ( PropertyIDs . Saturation_f , settings . saturation ) ;
134
-
135
- material . SetFloat ( PropertyIDs . Temperature_f , settings . temperature ) ;
136
- material . SetFloat ( PropertyIDs . Tint_f , settings . tint ) ;
137
- material . SetFloat ( PropertyIDs . BlackPoint_f , settings . blackPoint ) ;
138
- material . SetFloat ( PropertyIDs . WhitePoint_f , settings . whitePoint ) ;
132
+ const float EPSILON = 1e-4f ;
133
+ var exposure = Mathf . Pow ( 2 , settings . exposure ) ;
134
+ var contrast = Mathf . Max ( EPSILON , settings . contrast + Mathf . Max ( 0 , settings . contrast ) * Mathf . Max ( 0 , settings . contrast ) + 1 ) ;
135
+ var saturation = ( settings . saturation + Mathf . Max ( 0 , settings . saturation ) * Mathf . Max ( 0 , settings . saturation ) + 1 ) / contrast ;
136
+ var temperature = settings . temperature ;
137
+ var tint = settings . tint ;
138
+ var blackPoint = 0 + settings . blackPoint * 0.25f ;
139
+ var whitePoint = 1 + settings . whitePoint * 0.25f ;
140
+
141
+ material . SetFloat ( PropertyIDs . Exposure_f , exposure ) ;
142
+ material . SetFloat ( PropertyIDs . Contrast_f , contrast ) ;
143
+ material . SetFloat ( PropertyIDs . Saturation_f , saturation ) ;
139
144
145
+ //material.SetFloat(PropertyIDs.Temperature_f, settings.temperature);
146
+ //material.SetFloat(PropertyIDs.Tint_f, settings.tint);
147
+ material . SetVector ( PropertyIDs . ColorBalance_v , CalculateColorBalance ( settings . temperature , settings . tint ) ) ;
148
+ material . SetFloat ( PropertyIDs . BlackPoint_f , blackPoint ) ;
149
+ material . SetFloat ( PropertyIDs . WhitePoint_f , whitePoint ) ;
150
+
140
151
material . SetVector ( PropertyIDs . CurveParams_v , settings . GetCurveParams ( ) ) ;
141
152
142
153
//material.SetFloat(PropertyIDs.Strength_f, settings.strength);
@@ -150,6 +161,54 @@ internal override void RenderImage(RenderTexture source, RenderTexture destinati
150
161
public void OnValidate ( ) {
151
162
setMaterialDirty ( ) ;
152
163
}
164
+
165
+ // An analytical model of chromaticity of the standard illuminant, by Judd et al.
166
+ // http://en.wikipedia.org/wiki/Standard_illuminant#Illuminant_series_D
167
+ // Slightly modifed to adjust it with the D65 white point (x=0.31271, y=0.32902).
168
+ float StandardIlluminantY ( float x ) {
169
+ return 2.87f * x - 3 * x * x - 00.27509507f ;
170
+ }
171
+
172
+ readonly Matrix4x4 M_CAT02_XYZ_TO_LMS = new Matrix4x4 ( ) {
173
+ m00 = 0.7328f , m01 = 0.4296f , m02 = - 0.1624f , m03 = 0 ,
174
+ m10 = - 0.7036f , m11 = 1.6975f , m12 = 0.0061f , m13 = 0 ,
175
+ m20 = 0.0030f , m21 = 0.0136f , m22 = 0.9834f , m23 = 0 ,
176
+ m30 = 0 , m31 = 0 , m32 = 0 , m33 = 0
177
+ // 0.7328 , 0.4296 , -0.1624 ,
178
+ // -0.7036 , 1.6975 , 0.0061 ,
179
+ // 0.0030 , 0.0136 , 0.9834
180
+ } ;
181
+
182
+ Vector3 XYZtoLMS ( Vector3 xyz ) {
183
+ return M_CAT02_XYZ_TO_LMS . MultiplyVector ( xyz ) ;
184
+ }
185
+ // CIE xy chromaticity to CAT02 LMS.
186
+ // http://en.wikipedia.org/wiki/LMS_color_space#CAT02
187
+ Vector3 XYtoLMS ( float x , float y ) {
188
+ var xyz = new Vector3 ( x / y , 1 , ( 1 - x - y ) / y ) ;
189
+ return XYZtoLMS ( xyz ) ;
190
+ }
191
+
192
+ Vector3 CalculateColorBalance ( float temperature , float tint ) {
193
+ // Range ~[-1.8;1.8] ; using higher ranges is unsafe
194
+ float t1 = temperature / 0.55f ;
195
+ float t2 = tint / 0.55f ;
196
+
197
+ // Get the CIE xy chromaticity of the reference white point.
198
+ // Note: 0.31271 = x value on the D65 white point
199
+ float x = 0.31271f - t1 * ( t1 < 0 ? 0.1f : 0.05f ) ;
200
+ float y = StandardIlluminantY ( x ) + t2 * 0.05f ;
201
+
202
+ // Calculate the coefficients in the LMS space.
203
+ var w1 = new Vector3 ( 0.949237f , 1.03542f , 1.08728f ) ; // D65 white point
204
+ var w2 = XYtoLMS ( x , y ) ;
205
+ w2 . Set ( 1 / w2 . x , 1 / w2 . y , 1 / w2 . z ) ;
206
+ w2 . Scale ( w1 ) ;
207
+ return w2 ;
208
+ }
209
+
210
+
211
+
153
212
}
154
213
155
214
}
0 commit comments