@@ -93,12 +93,7 @@ public class LightComponent : MainRenderableComponent<LightData>, ILampDeviceCom
93
93
public IApiLamp GetApi ( Player player ) => _api ??= new LightApi ( gameObject , player ) ;
94
94
public IEnumerable < Light > LightSources => GetComponentsInChildren < Light > ( ) ;
95
95
96
- public Color LampColor {
97
- get {
98
- var src = GetComponentInChildren < Light > ( ) ;
99
- return Color . magenta ; //src == null ? Color.white : src.color;
100
- }
101
- }
96
+ public Color LampColor => _color ;
102
97
103
98
public LampStatus LampStatus => State ;
104
99
@@ -160,28 +155,31 @@ public override void UpdateTransforms()
160
155
#region Runtime
161
156
162
157
private float _value ;
158
+ private Color _color ;
163
159
private bool _hasLights ;
164
- private Light [ ] _unityLights ;
165
- private readonly List < ( Renderer , Color , float ) > _fullEmissions = new ( ) ;
166
- private readonly Dictionary < Light , float > _fullIntensities = new ( ) ;
160
+ private readonly List < ( Light , float ) > _lights = new ( ) ;
161
+ private readonly List < ( Renderer , float ) > _materials = new ( ) ;
167
162
private MaterialPropertyBlock _propBlock ;
168
163
169
164
public bool Enabled {
170
165
set {
171
166
StopAllCoroutines ( ) ;
172
- foreach ( var unityLight in _unityLights ) {
173
- unityLight . enabled = value ;
174
- }
167
+ SetLightIntensity ( value ? 1 : 0 ) ;
168
+ SetMaterialIntensity ( value ? 1 : 0 ) ;
175
169
}
176
170
}
177
171
178
172
public Color Color {
179
- get => _unityLights [ 0 ] . color ;
173
+ get => _color ;
180
174
set {
181
- foreach ( var unityLight in _unityLights ) {
175
+ _color = value ;
176
+ foreach ( var ( unityLight , _) in _lights ) {
182
177
unityLight . color = value ;
183
-
184
- // todo handle insert material color
178
+ }
179
+ foreach ( var ( mr , intensity ) in _materials ) {
180
+ mr . GetPropertyBlock ( _propBlock ) ;
181
+ RenderPipeline . Current . MaterialConverter . SetEmissiveColor ( _propBlock , value * intensity ) ;
182
+ mr . SetPropertyBlock ( _propBlock ) ;
185
183
}
186
184
}
187
185
}
@@ -195,12 +193,13 @@ private void Awake()
195
193
}
196
194
197
195
player . RegisterLamp ( this ) ;
198
- _unityLights = GetComponentsInChildren < Light > ( ) ;
196
+ var lights = GetComponentsInChildren < Light > ( ) ;
199
197
_value = 0 ;
198
+ _color = lights . FirstOrDefault ( ) ? . color ?? Color . white ;
200
199
201
200
// remember intensities
202
- foreach ( var unityLight in _unityLights ) {
203
- _fullIntensities [ unityLight ] = unityLight . intensity ;
201
+ foreach ( var unityLight in lights ) {
202
+ _lights . Add ( ( unityLight , unityLight . intensity ) ) ;
204
203
if ( FadeEnabled ) {
205
204
unityLight . enabled = true ;
206
205
unityLight . intensity = 0 ;
@@ -211,15 +210,16 @@ private void Awake()
211
210
}
212
211
213
212
// remember material emissions
214
- _propBlock = new MaterialPropertyBlock ( ) ;
213
+ _propBlock = new MaterialPropertyBlock ( ) ; // this is just something we can recycle
215
214
foreach ( var mr in GetComponentsInChildren < MeshRenderer > ( ) ) {
216
- var emissiveColor = RenderPipeline . Current . MaterialConverter . GetEmissiveColor ( mr . sharedMaterial ) ;
217
- if ( emissiveColor ? . a > 10f ) {
218
- _fullEmissions . Add ( ( mr , ( Color ) emissiveColor , 0 ) ) ;
215
+ var emissiveIntensity = RenderPipeline . Current . MaterialConverter . GetEmissiveIntensity ( mr . sharedMaterial ) ;
216
+ if ( emissiveIntensity > 0 ) {
217
+ _materials . Add ( ( mr , emissiveIntensity ) ) ;
219
218
}
219
+ // todo set to 0 initially
220
220
}
221
221
222
- _hasLights = _unityLights . Length > 0 || _fullEmissions . Count > 0 ;
222
+ _hasLights = _lights . Count > 0 || _materials . Count > 0 ;
223
223
}
224
224
225
225
public void FadeTo ( float value )
@@ -233,17 +233,8 @@ public void FadeTo(float value)
233
233
234
234
} else {
235
235
_value = value ;
236
- foreach ( var unityLight in _unityLights ) {
237
- if ( value > 0 ) {
238
- unityLight . intensity = value * _fullIntensities [ unityLight ] ;
239
- unityLight . enabled = true ;
240
-
241
- } else {
242
- unityLight . enabled = false ;
243
- }
244
- }
245
-
246
- SetEmissions ( value ) ;
236
+ SetLightIntensity ( value ) ;
237
+ SetMaterialIntensity ( value ) ;
247
238
}
248
239
}
249
240
@@ -286,48 +277,45 @@ private IEnumerator Fade(float value)
286
277
287
278
if ( duration == 0 ) {
288
279
_value = value ;
289
- foreach ( var unityLight in _unityLights ) {
290
- unityLight . intensity = _fullIntensities [ unityLight ] * value ;
291
- }
292
- SetEmissions ( value ) ;
280
+ SetLightIntensity ( value ) ;
281
+ SetMaterialIntensity ( value ) ;
293
282
294
283
} else {
295
284
while ( counter <= duration ) {
296
285
counter += Time . deltaTime ;
297
286
var position = counter / duration ;
298
- _value = Mathf . Lerp ( _value , 1 , position ) ;
299
- foreach ( var unityLight in _unityLights ) {
300
- unityLight . intensity = _fullIntensities [ unityLight ] * _value ;
301
- }
302
- yield return FadeEmissions ( value , position ) ;
287
+ var newValue = Mathf . Lerp ( _value , 1 , position ) ;
288
+ yield return SetIntensity ( newValue ) ;
303
289
}
304
290
}
305
291
}
306
292
307
- /// <summary>
308
- /// Sets the material emissions as a LERP between the current emission and
309
- /// a value for a given position.
310
- /// </summary>
311
- /// <param name="value">Value, between 0 and 1. End position of LERP is this value times full emission.</param>
312
- /// <param name="position">LERP position</param>
313
- private IEnumerator FadeEmissions ( float value , float position )
293
+ private IEnumerator SetIntensity ( float value )
314
294
{
315
- for ( var i = 0 ; i < _fullEmissions . Count ; i ++ ) {
316
- var ( mr , color , lastValue ) = _fullEmissions [ i ] ;
317
- mr . GetPropertyBlock ( _propBlock ) ;
318
- var emission = Mathf . Lerp ( lastValue , value , position ) ;
319
- RenderPipeline . Current . MaterialConverter . SetEmissiveColor ( _propBlock , emission * color * 0.05f ) ;
320
- _fullEmissions [ i ] = ( mr , color , emission ) ;
321
- mr . SetPropertyBlock ( _propBlock ) ;
322
- }
295
+ _value = value ;
296
+ SetLightIntensity ( value ) ;
297
+ SetMaterialIntensity ( value ) ;
323
298
yield return null ;
324
299
}
325
300
326
- private void SetEmissions ( float value )
301
+ private void SetLightIntensity ( float value )
302
+ {
303
+ foreach ( var ( unityLight , intensity ) in _lights ) {
304
+ if ( value > 0 ) {
305
+ unityLight . intensity = intensity * value ;
306
+ unityLight . enabled = true ;
307
+
308
+ } else {
309
+ unityLight . enabled = false ;
310
+ }
311
+ }
312
+ }
313
+
314
+ private void SetMaterialIntensity ( float value )
327
315
{
328
- foreach ( var ( mr , color , lastValue ) in _fullEmissions ) {
316
+ foreach ( var ( mr , intensity ) in _materials ) {
329
317
mr . GetPropertyBlock ( _propBlock ) ;
330
- RenderPipeline . Current . MaterialConverter . SetEmissiveColor ( _propBlock , value * color * 0.05f ) ;
318
+ RenderPipeline . Current . MaterialConverter . SetEmissiveIntensity ( mr . sharedMaterial , _propBlock , value * intensity ) ;
331
319
mr . SetPropertyBlock ( _propBlock ) ;
332
320
}
333
321
}
0 commit comments