Skip to content

Commit ee82553

Browse files
committed
fix: Remember each lamp's intensity when fading.
1 parent 46f8da6 commit ee82553

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

VisualPinball.Unity/VisualPinball.Unity/VPT/Light/LightComponent.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ public override void UpdateTransforms()
159159

160160
#region Runtime
161161

162+
private float _value;
162163
private bool _hasLights;
163164
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();
166167
private MaterialPropertyBlock _propBlock;
167168

168169
public bool Enabled {
@@ -195,14 +196,11 @@ private void Awake()
195196

196197
player.RegisterLamp(this);
197198
_unityLights = GetComponentsInChildren<Light>();
198-
_hasLights = _unityLights.Length > 0;
199+
_value = 0;
199200

200-
// remember intensity
201-
if (_hasLights) {
202-
_fullIntensity = _unityLights[0].intensity;
203-
}
204-
// enable at 0
201+
// remember intensities
205202
foreach (var unityLight in _unityLights) {
203+
_fullIntensities[unityLight] = unityLight.intensity;
206204
if (FadeEnabled) {
207205
unityLight.enabled = true;
208206
unityLight.intensity = 0;
@@ -212,14 +210,16 @@ private void Awake()
212210
}
213211
}
214212

215-
// emissive materials
213+
// remember material emissions
216214
_propBlock = new MaterialPropertyBlock();
217215
foreach (var mr in GetComponentsInChildren<MeshRenderer>()) {
218216
var emissiveColor = RenderPipeline.Current.MaterialConverter.GetEmissiveColor(mr.sharedMaterial);
219217
if (emissiveColor?.a > 10f) {
220218
_fullEmissions.Add((mr, (Color)emissiveColor, 0));
221219
}
222220
}
221+
222+
_hasLights = _unityLights.Length > 0 || _fullEmissions.Count > 0;
223223
}
224224

225225
public void FadeTo(float value)
@@ -232,9 +232,10 @@ public void FadeTo(float value)
232232
StartCoroutine(nameof(Fade), value);
233233

234234
} else {
235+
_value = value;
235236
foreach (var unityLight in _unityLights) {
236237
if (value > 0) {
237-
unityLight.intensity = value * _fullIntensity;
238+
unityLight.intensity = value * _fullIntensities[unityLight];
238239
unityLight.enabled = true;
239240

240241
} else {
@@ -279,25 +280,24 @@ private IEnumerator Blink(float blinkIntensity)
279280
private IEnumerator Fade(float value)
280281
{
281282
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);
288286

289287
if (duration == 0) {
288+
_value = value;
290289
foreach (var unityLight in _unityLights) {
291-
unityLight.intensity = b;
290+
unityLight.intensity = _fullIntensities[unityLight] * value;
292291
}
293292
SetEmissions(value);
294293

295294
} else {
296295
while (counter <= duration) {
297296
counter += Time.deltaTime;
298297
var position = counter / duration;
298+
_value = Mathf.Lerp(_value, 1, position);
299299
foreach (var unityLight in _unityLights) {
300-
unityLight.intensity = Mathf.Lerp(a, b, position);
300+
unityLight.intensity = _fullIntensities[unityLight] * _value;
301301
}
302302
yield return FadeEmissions(value, position);
303303
}

0 commit comments

Comments
 (0)