Skip to content

Commit

Permalink
feat(event-display): use workaround to manage scale in AR
Browse files Browse the repository at this point in the history
  • Loading branch information
9inpachi committed Jun 22, 2021
1 parent 07d8633 commit a5481e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PerspectiveCamera, Scene, Vector3 } from 'three';
import { SceneManager } from './scene-manager';
import { XRManager, XRSessionType } from './xr-manager';

// NOTE: This was created on 28/06/2021
Expand All @@ -11,12 +13,20 @@ import { XRManager, XRSessionType } from './xr-manager';
export class ARManager extends XRManager {
/** Session type to use for AR. */
static readonly SESSION_TYPE: string = 'immersive-ar';
/** Previous values of scene scale, camera near and camera position. */
private previousValues = {
sceneScale: 1,
cameraNear: 10,
cameraPosition: new Vector3(1, 0, 1),
};

/**
* Create the AR manager.
* @param scene The three.js scene.
* @param camera Camera in the scene.
* @override
*/
constructor() {
constructor(private scene: Scene, private camera: PerspectiveCamera) {
super(XRSessionType.AR);
}

Expand All @@ -26,7 +36,38 @@ export class ARManager extends XRManager {
* @param session The AR session.
*/
protected async onXRSessionStarted(session: any) {
this.previousValues.sceneScale = this.scene.scale.x;
this.previousValues.cameraNear = this.camera.near;
this.previousValues.cameraPosition = this.camera.position.clone();
this.scaleScene(0.00001);
this.camera.near = 0.01;
this.camera.position.set(0, 0, 0.1);
this.renderer.xr.setReferenceSpaceType('local');
await super.onXRSessionStarted(session);
}

/**
* Callback when the AR session ends.
* @override
*/
protected onXRSessionEnded() {
this.scaleScene(this.previousValues.sceneScale);
this.camera.near = this.previousValues.cameraNear;
this.camera.position.copy(this.previousValues.cameraPosition);
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);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import {
Camera,
PerspectiveCamera,
Expand All @@ -10,6 +9,7 @@ import {
Mesh,
TubeBufferGeometry,
} from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { RendererManager } from './renderer-manager';
import * as TWEEN from '@tweenjs/tween.js';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Mesh,
MeshBasicMaterial,
Euler,
PerspectiveCamera,
} from 'three';
import { Configuration } from '../../extras/configuration';
import { ControlsManager } from './controls-manager';
Expand Down Expand Up @@ -129,7 +130,10 @@ export class ThreeManager {
// VR manager
this.vrManager = new VRManager();
// AR manager
this.arManager = new ARManager();
this.arManager = new ARManager(
this.sceneManager.getScene(),
this.controlsManager.getMainCamera() as PerspectiveCamera
);
// Coloring manager
this.colorManager = new ColorManager(this.sceneManager);
// Selection manager
Expand Down

0 comments on commit a5481e1

Please sign in to comment.