src/managers/three-manager/xr/ar-manager.ts
AR manager for AR related operations.
Properties |
|
Methods |
|
constructor(scene: Scene, camera: PerspectiveCamera)
|
||||||||||||
Create the AR manager.
Parameters :
|
Static enableDomOverlay |
Type : boolean
|
Default value : true
|
Whether to enable DOM overlay which shows Phoenix overlays on top of the AR scene. |
Private previousValues |
Type : object
|
Default value : {
sceneScale: 1,
cameraNear: 10,
}
|
Previous values of scene scale, camera near and camera position. |
Static Readonly SESSION_TYPE |
Type : string
|
Default value : 'immersive-ar'
|
Session type to use for AR. |
Public cameraGroup |
Type : Group | undefined
|
Inherited from
XRManager
|
Defined in
XRManager:36
|
Group containing the the camera for XR. |
Protected currentXRSession |
Type : any
|
Default value : null
|
Inherited from
XRManager
|
Defined in
XRManager:32
|
Currently active XR session. |
Protected onSessionEnded |
Type : function
|
Inherited from
XRManager
|
Defined in
XRManager:34
|
Callback to call when the XR session ends. |
Protected renderer |
Type : WebGLRenderer
|
Inherited from
XRManager
|
Defined in
XRManager:30
|
Renderer to set the XR session for. |
Protected sessionInit |
Type : function
|
Inherited from
XRManager
|
Defined in
XRManager:28
|
Returns required and optional features when requesting an XR session. |
Protected xrActive |
Type : boolean
|
Default value : false
|
Inherited from
XRManager
|
Defined in
XRManager:26
|
Whether the XR is currently active or not. |
Public xrCamera |
Type : Camera
|
Inherited from
XRManager
|
Defined in
XRManager:38
|
The camera used by XR. |
Protected onXRSessionEnded |
onXRSessionEnded()
|
Inherited from
XRManager
|
Defined in
XRManager:67
|
Callback when the AR session ends.
Returns :
void
|
Protected Async onXRSessionStarted | ||||||||
onXRSessionStarted(session: any)
|
||||||||
Inherited from
XRManager
|
||||||||
Defined in
XRManager:53
|
||||||||
Callback for when the AR session is started.
Parameters :
Returns :
any
|
Private scaleScene | ||||||||
scaleScene(scale: number)
|
||||||||
Scale the three.js scene.
Parameters :
Returns :
void
|
Public endXRSession |
endXRSession()
|
Inherited from
XRManager
|
Defined in
XRManager:99
|
End the current XR session.
Returns :
void
|
Public getCameraGroup | ||||||||
getCameraGroup(camera?: Camera)
|
||||||||
Inherited from
XRManager
|
||||||||
Defined in
XRManager:109
|
||||||||
Get the group containing the camera for XR. XR camera works by adding a Group with Camera to the scene.
Parameters :
Returns :
Group
The camera group used in XR mode. |
Public getXRCamera |
getXRCamera()
|
Inherited from
XRManager
|
Defined in
XRManager:135
|
Get the camera used by XR.
Returns :
Camera
The camera used by XR. |
Public setXRSession | ||||||||||||||||
setXRSession(renderer: WebGLRenderer, onSessionStarted?: () => void, onSessionEnded?: () => void)
|
||||||||||||||||
Inherited from
XRManager
|
||||||||||||||||
Defined in
XRManager:53
|
||||||||||||||||
Set and configure the XR session.
Parameters :
Returns :
void
|
import { PerspectiveCamera, Scene } from 'three';
import { SceneManager } from '../scene-manager';
import { XRManager, XRSessionType } from './xr-manager';
// NOTE: This was created on 28/06/2021
// It might become outdated given how WebXR is still a work in progress
// LAST UPDATED ON 07/07/2021
/**
* AR manager for AR related operations.
*/
export class ARManager extends XRManager {
/** Session type to use for AR. */
static readonly SESSION_TYPE: string = 'immersive-ar';
/** Whether to enable DOM overlay which shows Phoenix overlays on top of the AR scene. */
public static enableDomOverlay: boolean = true;
/** Previous values of scene scale, camera near and camera position. */
private previousValues = {
sceneScale: 1,
cameraNear: 10,
};
/**
* Create the AR manager.
* @param scene The three.js scene.
* @param camera Camera in the scene.
* @override
*/
constructor(
private scene: Scene,
private camera: PerspectiveCamera,
) {
super(XRSessionType.AR);
this.previousValues.sceneScale = scene.scale.x;
this.previousValues.cameraNear = camera.near;
this.sessionInit = () => {
return ARManager.enableDomOverlay
? {
optionalFeatures: ['dom-overlay'],
domOverlay: { root: document.body },
}
: {};
};
}
/**
* Callback for when the AR session is started.
* @param session The AR session.
* @override
*/
protected async onXRSessionStarted(session: any) {
document.body.style.setProperty('background-color', 'transparent');
this.previousValues.sceneScale = this.scene.scale.x;
this.previousValues.cameraNear = this.camera.near;
this.scaleScene(0.00001);
this.camera.near = 0.01;
this.renderer.xr.setReferenceSpaceType('local');
await super.onXRSessionStarted(session);
}
/**
* Callback when the AR session ends.
* @override
*/
protected onXRSessionEnded() {
document.body.style.removeProperty('background-color');
this.scaleScene(this.previousValues.sceneScale);
this.camera.near = this.previousValues.cameraNear;
super.onXRSessionEnded();
}
/**
* Scale the three.js scene.
* @param scale Number to scale the scene to.
*/
private scaleScene(scale: number) {
[
SceneManager.EVENT_DATA_ID,
SceneManager.GEOMETRIES_ID,
SceneManager.LABELS_ID,
].forEach((groupName) => {
this.scene.getObjectByName(groupName)?.scale.setScalar(scale);
});
}
}