Skip to content

Commit 37e94ad

Browse files
committed
materials: Add material emissive intensity to API.
1 parent ee82553 commit 37e94ad

File tree

3 files changed

+64
-63
lines changed

3 files changed

+64
-63
lines changed

VisualPinball.Unity/VisualPinball.Unity/Rendering/IMaterialConverter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,9 @@ public interface IMaterialConverter
6969
void SetEmissiveColor(MaterialPropertyBlock propBlock, Color color);
7070

7171
Color? GetEmissiveColor(Material material);
72+
73+
void SetEmissiveIntensity(Material material, MaterialPropertyBlock propBlock, float intensity);
74+
75+
float GetEmissiveIntensity(Material material);
7276
}
7377
}

VisualPinball.Unity/VisualPinball.Unity/Rendering/Standard/StandardMaterialConverter.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,17 @@ public void SetEmissiveColor(MaterialPropertyBlock propBlock, Color color)
161161
public Color? GetEmissiveColor(Material material)
162162
{
163163
// standard has no emissive color
164-
165164
return null;
166165
}
166+
167+
public void SetEmissiveIntensity(Material material, MaterialPropertyBlock propBlock, float intensity)
168+
{
169+
// standard has no emissive materials
170+
}
171+
172+
public float GetEmissiveIntensity(Material material)
173+
{
174+
return 1;
175+
}
167176
}
168177
}

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

Lines changed: 50 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ public class LightComponent : MainRenderableComponent<LightData>, ILampDeviceCom
9393
public IApiLamp GetApi(Player player) => _api ??= new LightApi(gameObject, player);
9494
public IEnumerable<Light> LightSources => GetComponentsInChildren<Light>();
9595

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;
10297

10398
public LampStatus LampStatus => State;
10499

@@ -160,28 +155,31 @@ public override void UpdateTransforms()
160155
#region Runtime
161156

162157
private float _value;
158+
private Color _color;
163159
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();
167162
private MaterialPropertyBlock _propBlock;
168163

169164
public bool Enabled {
170165
set {
171166
StopAllCoroutines();
172-
foreach (var unityLight in _unityLights) {
173-
unityLight.enabled = value;
174-
}
167+
SetLightIntensity(value ? 1 : 0);
168+
SetMaterialIntensity(value ? 1 : 0);
175169
}
176170
}
177171

178172
public Color Color {
179-
get => _unityLights[0].color;
173+
get => _color;
180174
set {
181-
foreach (var unityLight in _unityLights) {
175+
_color = value;
176+
foreach (var (unityLight, _) in _lights) {
182177
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);
185183
}
186184
}
187185
}
@@ -195,12 +193,13 @@ private void Awake()
195193
}
196194

197195
player.RegisterLamp(this);
198-
_unityLights = GetComponentsInChildren<Light>();
196+
var lights = GetComponentsInChildren<Light>();
199197
_value = 0;
198+
_color = lights.FirstOrDefault()?.color ?? Color.white;
200199

201200
// 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));
204203
if (FadeEnabled) {
205204
unityLight.enabled = true;
206205
unityLight.intensity = 0;
@@ -211,15 +210,16 @@ private void Awake()
211210
}
212211

213212
// remember material emissions
214-
_propBlock = new MaterialPropertyBlock();
213+
_propBlock = new MaterialPropertyBlock(); // this is just something we can recycle
215214
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));
219218
}
219+
// todo set to 0 initially
220220
}
221221

222-
_hasLights = _unityLights.Length > 0 || _fullEmissions.Count > 0;
222+
_hasLights = _lights.Count > 0 || _materials.Count > 0;
223223
}
224224

225225
public void FadeTo(float value)
@@ -233,17 +233,8 @@ public void FadeTo(float value)
233233

234234
} else {
235235
_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);
247238
}
248239
}
249240

@@ -286,48 +277,45 @@ private IEnumerator Fade(float value)
286277

287278
if (duration == 0) {
288279
_value = value;
289-
foreach (var unityLight in _unityLights) {
290-
unityLight.intensity = _fullIntensities[unityLight] * value;
291-
}
292-
SetEmissions(value);
280+
SetLightIntensity(value);
281+
SetMaterialIntensity(value);
293282

294283
} else {
295284
while (counter <= duration) {
296285
counter += Time.deltaTime;
297286
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);
303289
}
304290
}
305291
}
306292

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)
314294
{
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);
323298
yield return null;
324299
}
325300

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)
327315
{
328-
foreach (var (mr, color, lastValue) in _fullEmissions) {
316+
foreach (var (mr, intensity) in _materials) {
329317
mr.GetPropertyBlock(_propBlock);
330-
RenderPipeline.Current.MaterialConverter.SetEmissiveColor(_propBlock, value * color * 0.05f);
318+
RenderPipeline.Current.MaterialConverter.SetEmissiveIntensity(mr.sharedMaterial, _propBlock, value * intensity);
331319
mr.SetPropertyBlock(_propBlock);
332320
}
333321
}

0 commit comments

Comments
 (0)