@@ -159,10 +159,11 @@ public override void UpdateTransforms()
159
159
160
160
#region Runtime
161
161
162
+ private float _value ;
162
163
private bool _hasLights ;
163
164
private Light [ ] _unityLights ;
164
- private readonly List < ( Renderer , Color , float ) > _fullEmissions = new List < ( Renderer , Color , float ) > ( ) ;
165
- private float _fullIntensity ;
165
+ private readonly List < ( Renderer , Color , float ) > _fullEmissions = new ( ) ;
166
+ private readonly Dictionary < Light , float > _fullIntensities = new ( ) ;
166
167
private MaterialPropertyBlock _propBlock ;
167
168
168
169
public bool Enabled {
@@ -195,14 +196,11 @@ private void Awake()
195
196
196
197
player . RegisterLamp ( this ) ;
197
198
_unityLights = GetComponentsInChildren < Light > ( ) ;
198
- _hasLights = _unityLights . Length > 0 ;
199
+ _value = 0 ;
199
200
200
- // remember intensity
201
- if ( _hasLights ) {
202
- _fullIntensity = _unityLights [ 0 ] . intensity ;
203
- }
204
- // enable at 0
201
+ // remember intensities
205
202
foreach ( var unityLight in _unityLights ) {
203
+ _fullIntensities [ unityLight ] = unityLight . intensity ;
206
204
if ( FadeEnabled ) {
207
205
unityLight . enabled = true ;
208
206
unityLight . intensity = 0 ;
@@ -212,14 +210,16 @@ private void Awake()
212
210
}
213
211
}
214
212
215
- // emissive materials
213
+ // remember material emissions
216
214
_propBlock = new MaterialPropertyBlock ( ) ;
217
215
foreach ( var mr in GetComponentsInChildren < MeshRenderer > ( ) ) {
218
216
var emissiveColor = RenderPipeline . Current . MaterialConverter . GetEmissiveColor ( mr . sharedMaterial ) ;
219
217
if ( emissiveColor ? . a > 10f ) {
220
218
_fullEmissions . Add ( ( mr , ( Color ) emissiveColor , 0 ) ) ;
221
219
}
222
220
}
221
+
222
+ _hasLights = _unityLights . Length > 0 || _fullEmissions . Count > 0 ;
223
223
}
224
224
225
225
public void FadeTo ( float value )
@@ -232,9 +232,10 @@ public void FadeTo(float value)
232
232
StartCoroutine ( nameof ( Fade ) , value ) ;
233
233
234
234
} else {
235
+ _value = value ;
235
236
foreach ( var unityLight in _unityLights ) {
236
237
if ( value > 0 ) {
237
- unityLight . intensity = value * _fullIntensity ;
238
+ unityLight . intensity = value * _fullIntensities [ unityLight ] ;
238
239
unityLight . enabled = true ;
239
240
240
241
} else {
@@ -279,25 +280,24 @@ private IEnumerator Blink(float blinkIntensity)
279
280
private IEnumerator Fade ( float value )
280
281
{
281
282
var counter = 0f ;
282
-
283
- var a = _unityLights [ 0 ] . intensity ;
284
- var b = _fullIntensity * value ;
285
- var duration = a < b
286
- ? FadeSpeedUp * ( _fullIntensity - a ) / _fullIntensity
287
- : FadeSpeedDown * ( 1 - ( _fullIntensity - a ) / _fullIntensity ) ;
283
+ var duration = _value < 1
284
+ ? FadeSpeedUp * ( 1 - _value ) / 1
285
+ : FadeSpeedDown * ( 1 - ( 1 - _value ) / 1 ) ;
288
286
289
287
if ( duration == 0 ) {
288
+ _value = value ;
290
289
foreach ( var unityLight in _unityLights ) {
291
- unityLight . intensity = b ;
290
+ unityLight . intensity = _fullIntensities [ unityLight ] * value ;
292
291
}
293
292
SetEmissions ( value ) ;
294
293
295
294
} else {
296
295
while ( counter <= duration ) {
297
296
counter += Time . deltaTime ;
298
297
var position = counter / duration ;
298
+ _value = Mathf . Lerp ( _value , 1 , position ) ;
299
299
foreach ( var unityLight in _unityLights ) {
300
- unityLight . intensity = Mathf . Lerp ( a , b , position ) ;
300
+ unityLight . intensity = _fullIntensities [ unityLight ] * _value ;
301
301
}
302
302
yield return FadeEmissions ( value , position ) ;
303
303
}
0 commit comments