Skip to content

Commit 4c6e866

Browse files
authored
Merge pull request #239 from De-Panther/spectator_camera_support
Added Spectator Camera
2 parents 76fca35 + 5ade72a commit 4c6e866

File tree

12 files changed

+713
-162
lines changed

12 files changed

+713
-162
lines changed

Build/Build/Build.data

1.29 KB
Binary file not shown.

Build/Build/Build.framework.js

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Build/Build/Build.wasm

1.74 KB
Binary file not shown.

Packages/webxr-interactions/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- Spectator Camera.
11+
912
## [0.15.0] - 2022-06-04
1013
### Changed
1114
- Requires WebXR Export 0.15.0.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using UnityEngine;
2+
3+
namespace WebXR.Interactions
4+
{
5+
/// <summary>
6+
/// Controls the Spectator Camera, enable render it when in XR mode.
7+
/// Notice that the Depth of the attached Camera should be higher than the other cameras on the scene.
8+
/// </summary>
9+
[RequireComponent(typeof(Camera))]
10+
public class SpectatorCamera : MonoBehaviour
11+
{
12+
private Camera _camera;
13+
[SerializeField]
14+
private bool enableInXR = false;
15+
private WebXRState currentXRState = WebXRState.NORMAL;
16+
17+
private void Awake()
18+
{
19+
_camera = GetComponent<Camera>();
20+
}
21+
22+
private void OnEnable()
23+
{
24+
WebXRManager.OnXRChange += HandleOnXRChange;
25+
currentXRState = WebXRManager.Instance.XRState;
26+
TryUpdateCameraState();
27+
}
28+
29+
private void OnDisable()
30+
{
31+
WebXRManager.OnXRChange -= HandleOnXRChange;
32+
}
33+
34+
private void OnPreRender()
35+
{
36+
WebXRManager.Instance.PreRenderSpectatorCamera();
37+
}
38+
39+
private void HandleOnXRChange(WebXRState state, int viewsCount, Rect leftRect, Rect rightRect)
40+
{
41+
currentXRState = state;
42+
TryUpdateCameraState();
43+
}
44+
45+
private void TryUpdateCameraState()
46+
{
47+
bool newState = enableInXR && currentXRState != WebXRState.NORMAL;
48+
if (_camera.enabled != newState)
49+
{
50+
_camera.enabled = newState;
51+
}
52+
}
53+
54+
public void EnableCameraInXR(bool value)
55+
{
56+
enableInXR = value;
57+
TryUpdateCameraState();
58+
}
59+
}
60+
}

Packages/webxr-interactions/Runtime/Scripts/SpectatorCamera.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)