3
3
4
4
public class PlayWebcam : MonoBehaviour
5
5
{
6
+ [ SerializeField ]
7
+ private string thresholdMinName = "_ThresholdMin" ;
8
+ [ SerializeField ]
9
+ private string thresholdMaxName = "_ThresholdMax" ;
10
+
6
11
private WebCamTexture webcamTexture ;
7
12
private Renderer _renderer ;
8
13
private bool started = false ;
9
14
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
+
10
22
private int defaultWidth = 1280 ;
11
23
private int defaultHeight = 720 ;
12
24
13
25
void Awake ( )
14
26
{
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
+ }
16
52
}
17
53
18
54
void Start ( )
@@ -38,7 +74,7 @@ void Play()
38
74
}
39
75
float ratio = ( float ) defaultWidth / ( float ) defaultHeight ;
40
76
transform . localScale = new Vector3 ( ratio , 1f , 1f ) ;
41
- _renderer . material . mainTexture = webcamTexture ;
77
+ material . mainTexture = webcamTexture ;
42
78
}
43
79
webcamTexture . Play ( ) ;
44
80
StartCoroutine ( SetScale ( ) ) ;
@@ -67,4 +103,60 @@ void OnDisable()
67
103
StopAllCoroutines ( ) ;
68
104
webcamTexture . Stop ( ) ;
69
105
}
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
+ }
70
162
}
0 commit comments