refactor pointerControls, move away from hero to gameEngine

This commit is contained in:
2026-02-21 20:42:29 +02:00
parent 8954770ff7
commit 151e1ff82f
4 changed files with 22 additions and 26 deletions
+8 -3
View File
@@ -8,6 +8,7 @@ import { StereoEffect } from 'three/addons/effects/StereoEffect.js';
// import { MapControls } from 'three/addons/controls/MapControls.js';
// import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
import { TransformControls } from 'three/addons/controls/TransformControls.js';
import { PointerControls } from './PointerControls';
import { ARButton } from 'three/addons/webxr/ARButton.js';
import { XRButton } from 'three/addons/webxr/XRButton.js';
import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
@@ -140,6 +141,8 @@ class GameEngine extends EventManager{
this.transformControls.addEventListener('dragging-changed', function (event) {
controls.enabled = !event.value;
});
this.pointerControls = new PointerControls(this.camera, renderer.domElement);
// controls.enableDamping = true;
// controls.screenSpacePanning = true;
@@ -150,9 +153,9 @@ class GameEngine extends EventManager{
function animate(time) {
let delta = clock.getDelta();
gameEngine.physics?.step();
gameEngine.handleXrAction(gameEngine, delta);
gameEngine.hero?.update(delta);
gameEngine.mixers.forEach(m => m.update(delta));
gameEngine.handleXrAction(gameEngine, delta)
gameEngine.dispatchEvent({type: 'beforeRender'})
this.motionQueue.update(delta);
@@ -190,7 +193,7 @@ class GameEngine extends EventManager{
renderer.domElement.addEventListener('wheel', (event) => {
event.preventDefault();
if (gameEngine.hero){
if (!gameEngine.hero.pointerControls.controls.isLocked){
if (!gameEngine.pointerControls.controls.isLocked){
gameEngine.hero.cameraZ += event.deltaY / 100;
}else{
gameEngine.camera.fov += event.deltaY / 100;
@@ -307,7 +310,9 @@ class GameEngine extends EventManager{
await this.physics.init();
}
handleXrActionGameMode(gameEngine, delta) {}
handleXrActionGameMode(gameEngine, delta) {
}
handleXrActionDesignMode(gameEngine, delta) {
if (gameEngine.xrController2?.gamepad) {
+5 -20
View File
@@ -1,5 +1,4 @@
import { AnimationMixer, Vector3 } from 'three';
import { PointerControls } from './PointerControls';
import { getBoundingBox, getBoundingBoxSize } from './MeshUtils';
import { QueryFilterFlags } from '@dimforge/rapier3d';
@@ -15,7 +14,7 @@ class Hero{
set cameraZ(v){
this.#cameraZ = Math.min(Math.max(v, 1), 12);
if (this.#cameraZ == 1){
this.lockControls().then(()=>{
this.engine.pointerControls.lock(true).then(()=>{
this.#cameraZ = 0;
}).catch(err=>{
console.log(err);
@@ -41,9 +40,8 @@ class Hero{
this.animationsMap = {};
this.source.animations.forEach(a=>{
this.animationsMap[a.name] = this.mixer.clipAction(a);
})
})
this.pointerControls = new PointerControls(engine.camera, this.model, engine.renderer.domElement);
engine.hero = this;
// let bb = this.model.userData.bbox;
@@ -89,15 +87,9 @@ class Hero{
this.ready = true;
}
lockControls(){
return this.pointerControls.lock(true);
}
update(delta){
if (this.ready && this.engine.physics.started && !this.disableInput && !this.engine.renderer.xr.isPresenting) {
let pc = this.pointerControls;
pc.update();
this.updateCharacterControls(delta, pc)
this.updateCharacterControls(delta)
}
}
@@ -107,7 +99,7 @@ class Hero{
}
updateCharacterControls(delta) {
let pc = this.pointerControls;
let pc = this.engine.pointerControls;
if (pc.controls.isLocked && this.model.visible){
this.model.visible = false;
this.camera.rotation.reorder('YZX');
@@ -118,7 +110,7 @@ class Hero{
this.camera.fov = 45;
this.camera.updateProjectionMatrix();
}
let input = this.getInput()
let input = pc.input;
let play = this.currentAction || 'idle', velocity = this.walkVelocity;
this.fadeDuration = 0.2;
@@ -247,13 +239,6 @@ class Hero{
}
}
getInput() {
return [
this.pointerControls.moveLeft * 1 + this.pointerControls.moveRight * -1,
this.pointerControls.moveForward * 1 + this.pointerControls.moveBackward * -1
]
}
idleReset(){
this.actionStart = -1;
//this.currentAction = 'idle'
+8 -2
View File
@@ -2,7 +2,7 @@ import { Vector3 } from 'three';
import { PointerLockControls } from 'three/examples/jsm/Addons.js';
class PointerControls {
constructor(camera, hero, domElement) {
constructor(camera, domElement) {
this.kb = {};
this.dom = domElement;
@@ -14,7 +14,6 @@ class PointerControls {
this.rvelo = 0;
this.camera = camera;
this.hero = hero;
this.click = false;
this.controls = new PointerLockControls(camera, domElement);
@@ -117,6 +116,13 @@ class PointerControls {
return this.moving && this.kb.ShiftLeft;
}
get input() {
return [
this.moveLeft * 1 + this.moveRight * -1,
this.moveForward * 1 + this.moveBackward * -1
]
}
async lock(unadjustedMovement = false){
await this.dom.requestPointerLock( {
unadjustedMovement
+1 -1
View File
@@ -306,7 +306,7 @@ export default {
},
control(){
engine.hero.lockControls();
engine.pointerControls.lock(true);
},
async fullScreen(){