Skip to content

Commit 8b7cb67

Browse files
committed
batched text supports changing colors & emission from variable
1 parent 8167b5a commit 8b7cb67

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

RasterPropMonitor/Auxiliary modules/JSILabel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public enum EmissiveMode
137137
private bool flashOn = true;
138138

139139
[SerializeField] internal JSITextMesh textObj;
140-
static readonly int emissiveFactorIndex = Shader.PropertyToID("_EmissiveFactor");
140+
public static readonly int emissiveFactorIndex = Shader.PropertyToID("_EmissiveFactor");
141141

142142
private List<StringProcessorFormatter> labels = new List<StringProcessorFormatter>();
143143
private int activeLabel = 0;
@@ -146,7 +146,7 @@ public enum EmissiveMode
146146
private int updateCountdown;
147147
private Action<float> del;
148148

149-
RasterPropMonitorComputer rpmComp;
149+
internal RasterPropMonitorComputer rpmComp;
150150
private JSIFlashModule fm;
151151

152152
public override void OnLoad(ConfigNode node)

RasterPropMonitor/Auxiliary modules/PropBatcher.cs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Reflection.Emit;
45
using System.Text;
56
using System.Threading.Tasks;
67
using JetBrains.Annotations;
8+
using TMPro;
79
using UnityEngine;
810

911
namespace JSI
@@ -204,11 +206,13 @@ class LabelBatch
204206
public GameObject batchRoot;
205207
public MeshRenderer renderer;
206208
public MeshFilter meshFilter;
209+
public JSILabel.TextBatchInfo batchInfo;
207210

208211
public List<JSITextMesh> textMeshes = new List<JSITextMesh>();
209212
public bool needsUpdate = true;
213+
210214

211-
public LabelBatch()
215+
public LabelBatch(JSILabel firstLabel)
212216
{
213217
batchRoot = new GameObject("Label Batch Root");
214218
batchRoot.layer = 20;
@@ -217,8 +221,52 @@ public LabelBatch()
217221
renderer = batchRoot.AddComponent<MeshRenderer>();
218222
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
219223
renderer.receiveShadows = true;
220-
renderer.material = new Material(JUtil.LoadInternalShader("RPM/JSILabel"));
224+
renderer.material = firstLabel.textObj.material;
225+
renderer.material.mainTexture = firstLabel.batchInfo.font.material.mainTexture;
221226
meshFilter = batchRoot.AddComponent<MeshFilter>();
227+
228+
batchInfo = firstLabel.batchInfo;
229+
230+
if (batchInfo.variableName != null)
231+
{
232+
firstLabel.rpmComp.RegisterVariableCallback(batchInfo.variableName, VariableChangedCallback);
233+
}
234+
}
235+
236+
public void VariableChangedCallback(float value)
237+
{
238+
Color32 newColor;
239+
float newEmissive; // TODO: flash support...
240+
241+
if (value == 0.0f)
242+
{
243+
newColor = batchInfo.zeroColor;
244+
newEmissive = 0.0f;
245+
}
246+
else if (value < 0.0f)
247+
{
248+
newColor = batchInfo.negativeColor;
249+
newEmissive = 0.0f;
250+
}
251+
else
252+
{
253+
newColor = batchInfo.positiveColor;
254+
newEmissive = 1.0f;
255+
}
256+
257+
foreach (var textMesh in textMeshes)
258+
{
259+
if (!newColor.Compare(textMesh.color))
260+
{
261+
textMesh.color = newColor;
262+
needsUpdate = true;
263+
}
264+
}
265+
266+
if (newEmissive != renderer.material.GetFloat(JSILabel.emissiveFactorIndex))
267+
{
268+
renderer.material.SetFloat(JSILabel.emissiveFactorIndex, newEmissive);
269+
}
222270
}
223271

224272
public void LateUpdate()
@@ -249,10 +297,8 @@ public void AddStaticLabel(JSILabel label)
249297
LabelBatch labelBatch;
250298
if (!labelBatches.TryGetValue(label.batchInfo, out labelBatch))
251299
{
252-
labelBatch = new LabelBatch();
300+
labelBatch = new LabelBatch(label);
253301
labelBatches.Add(label.batchInfo, labelBatch);
254-
labelBatch.renderer.material = label.batchInfo.font.material;
255-
// TODO: hook up variable callback
256302
// TODO: hook up flashing behavior
257303
}
258304

@@ -278,5 +324,17 @@ void LateUpdate()
278324
labelBatch.LateUpdate();
279325
}
280326
}
327+
328+
void OnDestroy()
329+
{
330+
RasterPropMonitorComputer rpmComp = RasterPropMonitorComputer.FindFromProp(internalProp);
331+
foreach (var labelBatch in labelBatches)
332+
{
333+
if (labelBatch.Key.variableName != null)
334+
{
335+
rpmComp.UnregisterVariableCallback(labelBatch.Key.variableName, labelBatch.Value.VariableChangedCallback);
336+
}
337+
}
338+
}
281339
}
282340
}

0 commit comments

Comments
 (0)