Skip to content

Commit fd50e04

Browse files
committed
Updated webcam module.
1 parent 2bd7598 commit fd50e04

File tree

1 file changed

+115
-59
lines changed

1 file changed

+115
-59
lines changed

Assets/JCSUnity/Scripts/Webcam/JCS_Webcam.cs

Lines changed: 115 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,26 @@ public class JCS_Webcam
2929

3030
private int mCaptureCounter = 0;
3131

32-
[Header("** Runtime Variables (JCS_Webcam) **")]
33-
34-
[Tooltip("After the screenshot is taken, how fast to resume the webcam?")]
35-
[SerializeField]
36-
private float mResumeTime = 0;
37-
38-
private float mResumeTimer = 0;
32+
private float mResumeTimer = 0.0f;
3933

4034
private bool mResumeTrigger = false;
4135

42-
#if (UNITY_STANDALONE || UNITY_EDITOR)
43-
[Header("- Keys")]
36+
[Header("** Check Variables (JCS_Webcam) **")]
4437

45-
[Tooltip("Key to take gameplay screenshot.")]
38+
[Tooltip("Record down the screen width.")]
4639
[SerializeField]
47-
private KeyCode mTakePic = KeyCode.None;
48-
#endif
40+
private int mWebcamWidth = 0;
4941

50-
[Header("- Image Path")]
51-
52-
[Tooltip("Webcam u take will save into this path.")]
53-
[SerializeField] private string mSavePath = "/JCS_GameData/WebcamShot/"; //Change the path here!
54-
55-
[Tooltip("Webcam resolution width.")]
42+
[Tooltip("Record down the screen height.")]
5643
[SerializeField]
57-
private int mWebcamResolutionWidth = 1920;
44+
private int mWebcamHeight = 0;
45+
46+
[Header("** Runtime Variables (JCS_Webcam) **")]
5847

59-
[Tooltip("Webcam resolution height.")]
48+
[Tooltip("After the screenshot is taken, how fast to resume the webcam.")]
6049
[SerializeField]
61-
private int mWebcamResolutionHeight = 1080;
50+
[Range(0.001f, 5.0f)]
51+
private float mResumeTime = 1.0f;
6252

6353
[Header("- Effect")]
6454

@@ -71,9 +61,28 @@ public class JCS_Webcam
7161

7262
[Tooltip("Delay time to fade out the splash screen.")]
7363
[SerializeField]
74-
private float mDelay = 1.0f;
64+
[Range(0.001f, 5.0f)]
65+
private float mDelayTime = 1.0f;
7566

76-
private float mDelayTimer = 0;
67+
private float mDelayTimer = 0.0f;
68+
69+
#if (UNITY_STANDALONE || UNITY_EDITOR)
70+
[Header("- Keys")]
71+
72+
[Tooltip("Key to take webcam image.")]
73+
[SerializeField]
74+
private KeyCode mTakePicKey = KeyCode.None;
75+
#endif
76+
77+
[Header("- Image")]
78+
79+
[Tooltip("Image save path.")]
80+
[SerializeField]
81+
private string mSavePath = "/JCS_GameData/WebcamShot/"; //Change the path here!
82+
83+
[Tooltip("Default save image file extension.")]
84+
[SerializeField]
85+
private string mSaveExtension = ".png";
7786

7887
[Header("- Sound")]
7988

@@ -87,13 +96,13 @@ public class JCS_Webcam
8796

8897
/* Setter & Getter */
8998

90-
public void Stop() { if (mWebCamTexture != null) this.mWebCamTexture.Stop(); }
91-
public void Play() { this.mWebCamTexture.Play(); }
92-
public void Pause() { this.mWebCamTexture.Pause(); }
9399
public bool isPlaying { get { return this.mWebCamTexture.isPlaying; } }
94-
95-
public int WebcamResolutionWidth { get { return this.mWebcamResolutionWidth; } set { this.mWebcamResolutionWidth = value; } }
96-
public int WebcamResolutionHeight { get { return this.mWebcamResolutionHeight; } set { this.mWebcamResolutionHeight = value; } }
100+
public float ResumeTime { get { return this.mResumeTime; } set { this.mResumeTime = value; } }
101+
public float DelayTime { get { return this.mDelayTime; } set { this.mDelayTime = value; } }
102+
public KeyCode TakePicKey { get { return this.mTakePicKey; } set { this.mTakePicKey = value; } }
103+
public string SavePath { get { return this.mSavePath; } set { this.mSavePath = value; } }
104+
public string SaveExtension { get { return this.mSaveExtension; } set { this.mSaveExtension = value; } }
105+
public AudioClip TakePhotoSound { get { return this.mTakePhotoSound; } set { this.mTakePhotoSound = value; } }
97106

98107
/* Functions */
99108

@@ -103,7 +112,10 @@ protected override void Awake()
103112

104113
if (mSoundPlayer == null)
105114
mSoundPlayer = this.GetComponent<JCS_SoundPlayer>();
115+
}
106116

117+
private void Start()
118+
{
107119
ActiveWebcam();
108120
}
109121

@@ -113,31 +125,8 @@ private void Update()
113125
ProcessInput();
114126
#endif
115127

116-
if (mResumeTrigger)
117-
{
118-
mResumeTimer += Time.deltaTime;
119-
120-
if (mResumeTime < mResumeTimer)
121-
{
122-
if (mWebCamTexture != null)
123-
mWebCamTexture.Play();
124-
125-
// done resume.
126-
mResumeTrigger = false;
127-
}
128-
}
129-
130-
if (mSplashEffectTrigger)
131-
{
132-
mDelayTimer += Time.deltaTime;
133-
134-
if (mDelayTimer > mDelay)
135-
{
136-
mDelayTimer = 0;
137-
JCS_SceneManager.instance.GetJCSWhiteScreen().FadeOut();
138-
mSplashEffectTrigger = false;
139-
}
140-
}
128+
DoSplash();
129+
DoAspect();
141130
}
142131

143132
private void OnApplicationQuit()
@@ -151,6 +140,10 @@ private void OnDestroy()
151140
Stop();
152141
}
153142

143+
public void Stop() { if (mWebCamTexture != null) this.mWebCamTexture.Stop(); }
144+
public void Play() { this.mWebCamTexture.Play(); }
145+
public void Pause() { this.mWebCamTexture.Pause(); }
146+
154147
/// <summary>
155148
/// Active the webcam.
156149
/// </summary>
@@ -166,23 +159,27 @@ public void ActiveWebcam()
166159
return;
167160
}
168161

162+
var scs = JCS_ScreenSettings.instance;
163+
mWebcamWidth = scs.STANDARD_SCREEN_WIDTH;
164+
mWebcamHeight = scs.STANDARD_SCREEN_HEIGHT;
165+
169166
mDeviceName = devices[0].name;
170-
mWebCamTexture = new WebCamTexture(mDeviceName, mWebcamResolutionWidth, mWebcamResolutionHeight, 12);
167+
mWebCamTexture = new WebCamTexture(mDeviceName, mWebcamWidth, mWebcamHeight, 12);
171168
UpdateUnityData();
172169

173-
mWebCamTexture.Play();
170+
Play();
174171
}
175172

176173
/// <summary>
177-
///
174+
/// Take a snapshot and store image to data path.
178175
/// </summary>
179176
public void TakeSnapshotWebcam()
180177
{
181178
// No device detected!!
182179
// cannot take snap shot without the device!!
183180
if (!mDetectDevice)
184181
{
185-
JCS_Debug.LogError("No webcam detected in the current devices.");
182+
JCS_Debug.LogError("No webcam detected in the current devices");
186183
return;
187184
}
188185

@@ -258,11 +255,70 @@ public override void UpdateUnityData()
258255
/// </summary>
259256
private void ProcessInput()
260257
{
261-
if (JCS_Input.GetKeyDown(mTakePic))
258+
if (JCS_Input.GetKeyDown(mTakePicKey))
262259
{
263260
TakeSnapshotWebcam();
264261
}
265262
}
266263
#endif
264+
265+
/// <summary>
266+
/// Do splash effect.
267+
/// </summary>
268+
private void DoSplash()
269+
{
270+
if (mResumeTrigger)
271+
{
272+
mResumeTimer += Time.deltaTime;
273+
274+
if (mResumeTime < mResumeTimer)
275+
{
276+
if (mWebCamTexture != null)
277+
mWebCamTexture.Play();
278+
279+
// done resume.
280+
mResumeTrigger = false;
281+
}
282+
}
283+
284+
if (mSplashEffectTrigger)
285+
{
286+
mDelayTimer += Time.deltaTime;
287+
288+
if (mDelayTimer > mDelayTime)
289+
{
290+
mDelayTimer = 0;
291+
JCS_SceneManager.instance.GetJCSWhiteScreen().FadeOut();
292+
mSplashEffectTrigger = false;
293+
}
294+
}
295+
}
296+
297+
/// <summary>
298+
/// Do aspect ratio calculation to webcam.
299+
/// </summary>
300+
private void DoAspect()
301+
{
302+
if (!mDetectDevice)
303+
{
304+
JCS_Debug.LogError("No webcam detected in the current devices");
305+
return;
306+
}
307+
308+
float xRatio = (float)mWebcamWidth / (float)mWebCamTexture.width;
309+
float width = mWebcamWidth;
310+
float height = (float)mWebCamTexture.height * xRatio;
311+
this.GetRectTransform().sizeDelta = new Vector2(width, height);
312+
313+
float scaleY = mWebCamTexture.videoVerticallyMirrored ? -1f : 1f;
314+
{
315+
Vector3 newScale = this.LocalScale;
316+
newScale.y *= scaleY;
317+
this.LocalScale = newScale;
318+
}
319+
320+
int orient = -this.mWebCamTexture.videoRotationAngle;
321+
this.LocalEulerAngles = new Vector3(0.0f, 0.0f, orient);
322+
}
267323
}
268324
}

0 commit comments

Comments
 (0)