Refactored private variables rig platform.
This commit is contained in:
parent
4cb50e5c6a
commit
263879d215
@ -29,10 +29,9 @@ export function buildRig(xr: WebXRDefaultExperience): Mesh {
|
|||||||
for (const cam of scene.cameras) {
|
for (const cam of scene.cameras) {
|
||||||
cam.parent = cameratransform;
|
cam.parent = cameratransform;
|
||||||
}
|
}
|
||||||
//cameratransform.rotation.set(0, Math.PI, 0);
|
|
||||||
});
|
});
|
||||||
//rigMesh.material = buildStandardMaterial("rigMaterial", scene, "#2222ff");
|
|
||||||
rigMesh.setAbsolutePosition(new Vector3(0, .01, 4));
|
rigMesh.setAbsolutePosition(new Vector3(0, .01, 5));
|
||||||
rigMesh.isPickable = false;
|
rigMesh.isPickable = false;
|
||||||
const axis = new AxesViewer(scene, .25);
|
const axis = new AxesViewer(scene, .25);
|
||||||
axis.zAxis.rotation.y = Math.PI;
|
axis.zAxis.rotation.y = Math.PI;
|
||||||
|
|||||||
@ -13,127 +13,125 @@ const LEFT = "left";
|
|||||||
|
|
||||||
|
|
||||||
export class Rigplatform {
|
export class Rigplatform {
|
||||||
private logger = log.getLogger('Rigplatform');
|
|
||||||
private readonly controllers: Controllers;
|
|
||||||
private readonly diagramManager: DiagramManager;
|
|
||||||
private readonly scene: Scene;
|
|
||||||
private readonly velocityArray = [0.01, 0.1, 1, 2, 5];
|
|
||||||
private readonly xr: WebXRDefaultExperience;
|
|
||||||
|
|
||||||
private rightController: Right;
|
|
||||||
private leftController: Left;
|
|
||||||
private turning: boolean = false;
|
|
||||||
private velocity: Vector3 = Vector3.Zero();
|
|
||||||
private velocityIndex: number = 2;
|
|
||||||
private turnVelocity: number = 0;
|
|
||||||
|
|
||||||
private registered = false;
|
|
||||||
private yRotation: number = 0;
|
|
||||||
|
|
||||||
private _flyMode: boolean = true;
|
|
||||||
|
|
||||||
public static instance: Rigplatform;
|
public static instance: Rigplatform;
|
||||||
|
|
||||||
public turnSnap: number = 0;
|
public turnSnap: number = 0;
|
||||||
public rigMesh: Mesh;
|
public rigMesh: Mesh;
|
||||||
|
|
||||||
|
private _logger = log.getLogger('Rigplatform');
|
||||||
|
private readonly _controllers: Controllers;
|
||||||
|
private readonly _diagramManager: DiagramManager;
|
||||||
|
private readonly _scene: Scene;
|
||||||
|
private readonly _velocityArray = [0.01, 0.1, 1, 2, 5];
|
||||||
|
private readonly _xr: WebXRDefaultExperience;
|
||||||
|
|
||||||
|
private _rightController: Right;
|
||||||
|
private _leftController: Left;
|
||||||
|
private _turning: boolean = false;
|
||||||
|
private _velocity: Vector3 = Vector3.Zero();
|
||||||
|
private _velocityIndex: number = 2;
|
||||||
|
private _turnVelocity: number = 0;
|
||||||
|
private _registered = false;
|
||||||
|
private _yRotation: number = 0;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
xr: WebXRDefaultExperience,
|
xr: WebXRDefaultExperience,
|
||||||
diagramManager: DiagramManager
|
diagramManager: DiagramManager
|
||||||
) {
|
) {
|
||||||
this.scene = DefaultScene.Scene;
|
this._scene = DefaultScene.Scene;
|
||||||
this.diagramManager = diagramManager;
|
this._diagramManager = diagramManager;
|
||||||
this.controllers = diagramManager.controllers;
|
this._controllers = diagramManager.controllers;
|
||||||
this.xr = xr;
|
this._xr = xr;
|
||||||
this.rigMesh = buildRig(xr);
|
this.rigMesh = buildRig(xr);
|
||||||
this.fixRotation();
|
this._fixRotation();
|
||||||
this.initializeControllers();
|
this._initializeControllers();
|
||||||
this.registerVelocityObserver();
|
this._registerVelocityObserver();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _flyMode: boolean = true;
|
||||||
|
|
||||||
public set flyMode(value: boolean) {
|
public set flyMode(value: boolean) {
|
||||||
this._flyMode = value;
|
this._flyMode = value;
|
||||||
if (this._flyMode) {
|
if (this._flyMode) {
|
||||||
this.rigMesh.physicsBody.setGravityFactor(.01);
|
this.rigMesh.physicsBody.setGravityFactor(.01);
|
||||||
this.logger.debug('flymode');
|
this._logger.debug('flymode');
|
||||||
} else {
|
} else {
|
||||||
this.rigMesh.physicsBody.setGravityFactor(1);
|
this.rigMesh.physicsBody.setGravityFactor(1);
|
||||||
this.logger.debug('walkmode');
|
this._logger.debug('walkmode');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public forwardback(val: number) {
|
public forwardback(val: number) {
|
||||||
this.velocity.z = (val * this.velocityArray[this.velocityIndex]) * -1;
|
this._velocity.z = (val * this._velocityArray[this._velocityIndex]) * -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public leftright(val: number) {
|
public leftright(val: number) {
|
||||||
this.velocity.x = (val * this.velocityArray[this.velocityIndex]);
|
this._velocity.x = (val * this._velocityArray[this._velocityIndex]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public updown(val: number) {
|
public updown(val: number) {
|
||||||
this.velocity.y = (val * this.velocityArray[this.velocityIndex]) * -1;
|
this._velocity.y = (val * this._velocityArray[this._velocityIndex]) * -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public turn(val: number) {
|
public turn(val: number) {
|
||||||
const snap = this.turnSnap;
|
const snap = this.turnSnap;
|
||||||
|
|
||||||
if (snap && snap > 0) {
|
if (snap && snap > 0) {
|
||||||
if (!this.turning) {
|
if (!this._turning) {
|
||||||
if (Math.abs(val) > .1) {
|
if (Math.abs(val) > .1) {
|
||||||
this.turning = true;
|
this._turning = true;
|
||||||
this.yRotation += Angle.FromDegrees(Math.sign(val) * snap).radians();
|
this._yRotation += Angle.FromDegrees(Math.sign(val) * snap).radians();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Math.abs(val) < .1) {
|
if (Math.abs(val) < .1) {
|
||||||
this.turning = false;
|
this._turning = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Math.abs(val) > .1) {
|
if (Math.abs(val) > .1) {
|
||||||
this.turnVelocity = val;
|
this._turnVelocity = val;
|
||||||
} else {
|
} else {
|
||||||
this.turnVelocity = 0;
|
this._turnVelocity = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerVelocityObserver() {
|
private _registerVelocityObserver() {
|
||||||
this.scene.onBeforeRenderObservable.add(() => {
|
this._scene.onBeforeRenderObservable.add(() => {
|
||||||
const vel = this.velocity.applyRotationQuaternion(this.scene.activeCamera.absoluteRotation);
|
const vel = this._velocity.applyRotationQuaternion(this._scene.activeCamera.absoluteRotation);
|
||||||
if (!this._flyMode) {
|
if (!this._flyMode) {
|
||||||
vel.y = 0;
|
vel.y = 0;
|
||||||
}
|
}
|
||||||
if (vel.length() > 0) {
|
if (vel.length() > 0) {
|
||||||
this.logger.debug('Velocity', this.velocity, vel, this.scene.activeCamera.absoluteRotation);
|
this._logger.debug('Velocity', this._velocity, vel, this._scene.activeCamera.absoluteRotation);
|
||||||
}
|
}
|
||||||
this.rigMesh.physicsBody.setLinearVelocity(vel);
|
this.rigMesh.physicsBody.setLinearVelocity(vel);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerObserver() {
|
private _registerObserver() {
|
||||||
if (this.registered) {
|
if (this._registered) {
|
||||||
this.logger.warn('observer already registered, clearing and re registering');
|
this._logger.warn('observer already registered, clearing and re registering');
|
||||||
this.controllers.controllerObservable.clear();
|
this._controllers.controllerObservable.clear();
|
||||||
this.registered = false;
|
this._registered = false;
|
||||||
}
|
}
|
||||||
if (!this.registered) {
|
if (!this._registered) {
|
||||||
this.registered = true;
|
this._registered = true;
|
||||||
this.controllers.controllerObservable.add((event: ControllerEvent) => {
|
this._controllers.controllerObservable.add((event: ControllerEvent) => {
|
||||||
this.logger.debug(event);
|
this._logger.debug(event);
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case ControllerEventType.INCREASE_VELOCITY:
|
case ControllerEventType.INCREASE_VELOCITY:
|
||||||
if (this.velocityIndex < this.velocityArray.length - 1) {
|
if (this._velocityIndex < this._velocityArray.length - 1) {
|
||||||
this.velocityIndex++;
|
this._velocityIndex++;
|
||||||
} else {
|
} else {
|
||||||
this.velocityIndex = 0;
|
this._velocityIndex = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ControllerEventType.DECREASE_VELOCITY:
|
case ControllerEventType.DECREASE_VELOCITY:
|
||||||
if (this.velocityIndex > 0) {
|
if (this._velocityIndex > 0) {
|
||||||
this.velocityIndex--;
|
this._velocityIndex--;
|
||||||
} else {
|
} else {
|
||||||
this.velocityIndex = this.velocityArray.length - 1;
|
this._velocityIndex = this._velocityArray.length - 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ControllerEventType.TURN:
|
case ControllerEventType.TURN:
|
||||||
@ -151,66 +149,66 @@ export class Rigplatform {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ControllerEventType.MOTION:
|
case ControllerEventType.MOTION:
|
||||||
this.logger.debug(JSON.stringify(event));
|
this._logger.debug(JSON.stringify(event));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.logger.warn('observer already registered');
|
this._logger.warn('observer already registered');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private initializeControllers() {
|
private _initializeControllers() {
|
||||||
this.xr.input.onControllerAddedObservable.add((source) => {
|
this._xr.input.onControllerAddedObservable.add((source) => {
|
||||||
this.registerObserver();
|
this._registerObserver();
|
||||||
switch (source.inputSource.handedness) {
|
switch (source.inputSource.handedness) {
|
||||||
case RIGHT:
|
case RIGHT:
|
||||||
if (!this.rightController) {
|
if (!this._rightController) {
|
||||||
this.rightController = new Right(source, this.xr, this.diagramManager);
|
this._rightController = new Right(source, this._xr, this._diagramManager);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LEFT:
|
case LEFT:
|
||||||
if (!this.leftController) {
|
if (!this._leftController) {
|
||||||
this.leftController = new Left(source, this.xr, this.diagramManager);
|
this._leftController = new Left(source, this._xr, this._diagramManager);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//this.xr.baseExperience.camera.position = new Vector3(0, 0, 0);
|
//this.xr.baseExperience.camera.position = new Vector3(0, 0, 0);
|
||||||
});
|
});
|
||||||
this.xr.input.onControllerRemovedObservable.add((source) => {
|
this._xr.input.onControllerRemovedObservable.add((source) => {
|
||||||
switch (source.inputSource.handedness) {
|
switch (source.inputSource.handedness) {
|
||||||
case RIGHT:
|
case RIGHT:
|
||||||
if (this.rightController) {
|
if (this._rightController) {
|
||||||
this.rightController = null;
|
this._rightController = null;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LEFT:
|
case LEFT:
|
||||||
if (this.leftController) {
|
if (this._leftController) {
|
||||||
this.leftController = null;
|
this._leftController = null;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
this.logger.debug('controller removed', source);
|
this._logger.debug('controller removed', source);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private fixRotation() {
|
private _fixRotation() {
|
||||||
if (!this.scene) {
|
if (!this._scene) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.scene.onAfterPhysicsObservable.add(() => {
|
this._scene.onAfterPhysicsObservable.add(() => {
|
||||||
const turnSnap = this.turnSnap;
|
const turnSnap = this.turnSnap;
|
||||||
if (turnSnap && turnSnap > 0) {
|
if (turnSnap && turnSnap > 0) {
|
||||||
const q = this.rigMesh.rotationQuaternion;
|
const q = this.rigMesh.rotationQuaternion;
|
||||||
this.rigMesh.physicsBody.setAngularVelocity(Vector3.Zero());
|
this.rigMesh.physicsBody.setAngularVelocity(Vector3.Zero());
|
||||||
if (q) {
|
if (q) {
|
||||||
const e = q.toEulerAngles();
|
const e = q.toEulerAngles();
|
||||||
e.y += this.yRotation;
|
e.y += this._yRotation;
|
||||||
q.copyFrom(Quaternion.FromEulerAngles(0, e.y, 0));
|
q.copyFrom(Quaternion.FromEulerAngles(0, e.y, 0));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.rigMesh.physicsBody.setAngularVelocity(Vector3.Up().scale(this.turnVelocity));
|
this.rigMesh.physicsBody.setAngularVelocity(Vector3.Up().scale(this._turnVelocity));
|
||||||
}
|
}
|
||||||
}, -1, false, this, false);
|
}, -1, false, this, false);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user