refactor pointercontrols, change VR camera motion behavior

This commit is contained in:
2026-02-27 17:41:48 +02:00
parent 0649d0708c
commit 01c5670b71
2 changed files with 35 additions and 21 deletions
+31 -17
View File
@@ -1,11 +1,13 @@
import { AnimationMixer, Vector3 } from 'three'; import { AnimationMixer, Vector3, Vector2 } from 'three';
import { getBoundingBox, getBoundingBoxSize } from './MeshUtils'; import { getBoundingBox, getBoundingBoxSize } from './MeshUtils';
import { QueryFilterFlags } from '@dimforge/rapier3d'; import { QueryFilterFlags } from '@dimforge/rapier3d';
const zero2 = new Vector2(0,0);
class Hero{ class Hero{
walkDirection = new Vector3() walkDirection = new Vector3()
rotateAngle = new Vector3(0, 1, 0) rotateAngle = new Vector3(0, 1, 0)
#cameraZ = 6 cameraMode = 'rotate';
#cameraZ = 4
get cameraZ(){ get cameraZ(){
return this.#cameraZ; return this.#cameraZ;
@@ -91,15 +93,17 @@ class Hero{
update(delta){ update(delta){
if (this.ready && this.engine.physics.started && !this.disableInput) { if (this.ready && this.engine.physics.started && !this.disableInput) {
this.updateCharacterControls(delta) this.updateCharacterControls(delta)
// if (this.engine.renderer.xr.isPresenting){ if (this.engine.renderer.xr.isPresenting){
// this.camera = this.engine.cameraWorld; this.cameraMode = 'fixed'
// // this.engine.activeObjects.position.x = -this.camera.position.x; //this.camera = this.engine.cameraWorld;
// // this.engine.activeObjects.position.z = -this.camera.position.z; // this.engine.activeObjects.position.x = -this.camera.position.x;
// }else{ // this.engine.activeObjects.position.z = -this.camera.position.z;
// this.camera = this.engine.cameraWorld; }else{
// this.engine.camera.position.set(0,0,0); this.cameraMode = 'rotate'
// this.engine.camera.rotation.set(0,0,0); // this.camera = this.engine.cameraWorld;
// } // this.engine.camera.position.set(0,0,0);
// this.engine.camera.rotation.set(0,0,0);
}
} }
} }
@@ -116,17 +120,22 @@ class Hero{
} }
if (!pc.isLocked && !this.model.visible){ if (!pc.isLocked && !this.model.visible){
this.model.visible = true; this.model.visible = true;
this.#cameraZ = 6 this.#cameraZ = 4
this.engine.camera.fov = 45; this.engine.camera.fov = 45;
this.engine.camera.updateProjectionMatrix(); this.engine.camera.updateProjectionMatrix();
} }
let input = pc.input; let input = pc.input;
if (this.cameraMode == 'fixed'){
let vec = new Vector2(input[0], input[1]);
vec.rotateAround(zero2, this.model.rotation.y - this.cameraDelta);
input = [vec.x, vec.y];
}
let play = this.currentAction || 'idle', velocity = this.walkVelocity; let play = this.currentAction || 'idle', velocity = this.walkVelocity;
this.fadeDuration = 0.2; this.fadeDuration = 0.2;
if (input[1] && pc.running) { if (input[1] && pc.running) {
play = 'run'; play = 'run';
velocity = this.runVelocity
} else if (input[1] > 0) { } else if (input[1] > 0) {
play = 'walk' play = 'walk'
} else if (input[1] < 0) { } else if (input[1] < 0) {
@@ -232,21 +241,26 @@ class Hero{
let cameraPosition = new Vector3().copy(this.camera.position) let cameraPosition = new Vector3().copy(this.camera.position)
let cameraDesiredPosition; let cameraDesiredPosition;
if (true){ if (this.cameraMode == 'rotate'){
cameraDesiredPosition = new Vector3( cameraDesiredPosition = new Vector3(
this.model.position.x + this.#cameraZ* Math.sin(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta), this.model.position.x + this.#cameraZ* Math.sin(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta),
this.model.position.y + (pc.isLocked? this.size.y*0.9 : this.cameraY), this.model.position.y + (pc.isLocked? this.size.y*0.9 : this.cameraY),
this.model.position.z + this.#cameraZ* Math.cos(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta) this.model.position.z + this.#cameraZ* Math.cos(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta)
) )
} else { } else {
// cameraDesiredPosition = new Vector3(
// this.model.position.x,
// this.model.position.y + (pc.isLocked? this.size.y*0.9 : this.cameraY),
// this.model.position.z - this.#cameraZ
// )
cameraDesiredPosition = new Vector3( cameraDesiredPosition = new Vector3(
this.model.position.x, this.model.position.x + this.#cameraZ* Math.sin(0+ Math.PI + this.cameraDelta + this.cameraIdleDelta),
this.model.position.y + (pc.isLocked? this.size.y*0.9 : this.cameraY), this.model.position.y + (pc.isLocked? this.size.y*0.9 : this.cameraY),
this.model.position.z - this.#cameraZ this.model.position.z + this.#cameraZ* Math.cos(0+ Math.PI + this.cameraDelta + this.cameraIdleDelta)
) )
} }
cameraPosition.lerp(cameraDesiredPosition, delta*2) cameraPosition.lerp(cameraDesiredPosition, this.cameraMode == 'fixed' ? 1 : delta*2)
this.camera.position.copy(cameraPosition) this.camera.position.copy(cameraPosition)
if (!pc.isLocked){ if (!pc.isLocked){
this.camera.lookAt( this.camera.lookAt(
+4 -4
View File
@@ -1,4 +1,4 @@
import { Vector3, Controls, Euler } from 'three'; import { Vector3, Vector2, Controls, Euler } from 'three';
const _MOUSE_SENSITIVITY = 0.002; const _MOUSE_SENSITIVITY = 0.002;
@@ -117,15 +117,15 @@ class PointerControls extends Controls {
} }
get cameraLeft(){ get cameraLeft(){
return this.kb['KeyQ'] || false return this.kb['KeyQ'] || this.engine.xrController2?.gamepad?.axes[2] > 0.5 || false
} }
get cameraRight(){ get cameraRight(){
return this.kb['KeyE'] || false return this.kb['KeyE'] || this.engine.xrController2?.gamepad?.axes[2] < -0.5 || false
} }
get moving(){ get moving(){
return this.moveForward || this.moveBackward; return this.moveForward || this.moveBackward || this.moveLeft || this.moveRight;
} }
get rotating(){ get rotating(){