Skip to content

Commit 1fda06e

Browse files
committed
Add an option to set webcam threshold colors from UI
1 parent 2df4199 commit 1fda06e

File tree

1 file changed

+94
-2
lines changed

1 file changed

+94
-2
lines changed

Packages/webxr-interactions/Runtime/Scripts/PlayWebcam.cs

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,52 @@
33

44
public class PlayWebcam : MonoBehaviour
55
{
6+
[SerializeField]
7+
private string thresholdMinName = "_ThresholdMin";
8+
[SerializeField]
9+
private string thresholdMaxName = "_ThresholdMax";
10+
611
private WebCamTexture webcamTexture;
712
private Renderer _renderer;
813
private bool started = false;
914

15+
private Material material;
16+
private bool hasThresholdProperties = false;
17+
private int thresholdMinID = 0;
18+
private int thresholdMaxID = 0;
19+
private Color thresholdMinColor;
20+
private Color thresholdMaxColor;
21+
1022
private int defaultWidth = 1280;
1123
private int defaultHeight = 720;
1224

1325
void Awake()
1426
{
15-
_renderer = GetComponent<Renderer>();
27+
TrySetupRenderer();
28+
}
29+
30+
void TrySetupRenderer()
31+
{
32+
if (_renderer == null)
33+
{
34+
_renderer = GetComponent<Renderer>();
35+
material = Instantiate(_renderer.sharedMaterial);
36+
_renderer.sharedMaterial = material;
37+
hasThresholdProperties = false;
38+
thresholdMinID = material.shader.FindPropertyIndex(thresholdMinName);
39+
if (thresholdMinID > -1)
40+
{
41+
thresholdMinID = material.shader.GetPropertyNameId(thresholdMinID);
42+
}
43+
thresholdMaxID = material.shader.FindPropertyIndex(thresholdMaxName);
44+
if (thresholdMaxID > -1)
45+
{
46+
thresholdMaxID = material.shader.GetPropertyNameId(thresholdMaxID);
47+
}
48+
thresholdMinColor = material.GetColor(thresholdMinID);
49+
thresholdMaxColor = material.GetColor(thresholdMaxID);
50+
hasThresholdProperties = true;
51+
}
1652
}
1753

1854
void Start()
@@ -38,7 +74,7 @@ void Play()
3874
}
3975
float ratio = (float)defaultWidth / (float)defaultHeight;
4076
transform.localScale = new Vector3(ratio, 1f, 1f);
41-
_renderer.material.mainTexture = webcamTexture;
77+
material.mainTexture = webcamTexture;
4278
}
4379
webcamTexture.Play();
4480
StartCoroutine(SetScale());
@@ -67,4 +103,60 @@ void OnDisable()
67103
StopAllCoroutines();
68104
webcamTexture.Stop();
69105
}
106+
107+
private void TrySetColor(string value, ref Color color, int propertyID, int colorLetter)
108+
{
109+
if (hasThresholdProperties && int.TryParse(value, out int intValue))
110+
{
111+
switch (colorLetter)
112+
{
113+
case 0:
114+
color.r = (float)Mathf.Clamp(intValue, 0, 255) / 255f;
115+
break;
116+
case 1:
117+
color.g = (float)Mathf.Clamp(intValue, 0, 255) / 255f;
118+
break;
119+
case 2:
120+
color.b = (float)Mathf.Clamp(intValue, 0, 255) / 255f;
121+
break;
122+
}
123+
material.SetColor(propertyID, color);
124+
}
125+
}
126+
127+
public void TrySetMinR(string value)
128+
{
129+
TrySetupRenderer();
130+
TrySetColor(value, ref thresholdMinColor, thresholdMinID, 0);
131+
}
132+
133+
public void TrySetMinG(string value)
134+
{
135+
TrySetupRenderer();
136+
TrySetColor(value, ref thresholdMinColor, thresholdMinID, 1);
137+
}
138+
139+
public void TrySetMinB(string value)
140+
{
141+
TrySetupRenderer();
142+
TrySetColor(value, ref thresholdMinColor, thresholdMinID, 2);
143+
}
144+
145+
public void TrySetMaxR(string value)
146+
{
147+
TrySetupRenderer();
148+
TrySetColor(value, ref thresholdMaxColor, thresholdMaxID, 0);
149+
}
150+
151+
public void TrySetMaxG(string value)
152+
{
153+
TrySetupRenderer();
154+
TrySetColor(value, ref thresholdMaxColor, thresholdMaxID, 1);
155+
}
156+
157+
public void TrySetMaxB(string value)
158+
{
159+
TrySetupRenderer();
160+
TrySetColor(value, ref thresholdMaxColor, thresholdMaxID, 2);
161+
}
70162
}

0 commit comments

Comments
 (0)