three-player-controller 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -73,6 +73,7 @@ export function playerController(): {
73
73
  reset: (pos?: THREE.Vector3) => void;
74
74
  update: (dt?: number) => void;
75
75
  destroy: () => void;
76
+ getposition: () => THREE.Vector3;
76
77
  };
77
78
  ```
78
79
 
@@ -88,6 +89,8 @@ export function playerController(): {
88
89
  每帧调用。
89
90
  - `destroy()`
90
91
  销毁控制器。
92
+ - `getposition()`
93
+ 获取人物当前位置。
91
94
 
92
95
  ---
93
96
 
@@ -139,6 +142,9 @@ type PlayerControllerOptions = {
139
142
  minCamDistance?: number;
140
143
  maxCamDistance?: number;
141
144
  colliderMeshUrl?: string;
145
+ isShowMobileControls?: boolean;
146
+ thirdMouseMode?: number;
147
+ enableZoom?: boolean;
142
148
  };
143
149
  ```
144
150
 
@@ -161,6 +167,7 @@ type PlayerControllerOptions = {
161
167
  | `colliderMeshUrl` | `string` | 自制碰撞体模型路径,默认`""` |
162
168
  | `isShowMobileControls` | `boolean` | 移动端运行时,是否自动显示移动端控制器,默认`true` |
163
169
  | `thirdMouseMode` | `[0, 1, 2, 3]` | 第三人称视角下的不同鼠标控制模式 ,默认`1`(0: 隐藏鼠标控制朝向及视角,1: 隐藏鼠标仅控制视角,2: 显示鼠标拖拽控制朝向及视角, 3: 显示鼠标拖拽仅控制视角) |
170
+ | `enableZoom` | `boolean` | 第三人称模式下是否允许缩放,默认`false` |
164
171
 
165
172
  ---
166
173
 
package/dist/index.d.mts CHANGED
@@ -29,6 +29,7 @@ type PlayerControllerOptions = {
29
29
  colliderMeshUrl?: string;
30
30
  isShowMobileControls?: boolean;
31
31
  thirdMouseMode?: 0 | 1 | 2 | 3;
32
+ enableZoom?: boolean;
32
33
  };
33
34
  declare function playerController(): {
34
35
  init: (opts: PlayerControllerOptions, callback?: () => void) => Promise<void>;
package/dist/index.d.ts CHANGED
@@ -29,6 +29,7 @@ type PlayerControllerOptions = {
29
29
  colliderMeshUrl?: string;
30
30
  isShowMobileControls?: boolean;
31
31
  thirdMouseMode?: 0 | 1 | 2 | 3;
32
+ enableZoom?: boolean;
32
33
  };
33
34
  declare function playerController(): {
34
35
  init: (opts: PlayerControllerOptions, callback?: () => void) => Promise<void>;
package/dist/index.js CHANGED
@@ -60,7 +60,6 @@ var PlayerController = class {
60
60
  constructor() {
61
61
  // ==================== 基本配置与参数 ====================
62
62
  this.loader = new import_GLTFLoader.GLTFLoader();
63
- // 0: 普通 1: 飞行 2: 车辆
64
63
  // ==================== 玩家基本属性 ====================
65
64
  this.playerRadius = 45;
66
65
  this.playerHeight = 180;
@@ -351,6 +350,7 @@ var PlayerController = class {
351
350
  this._maxCamDistance = (opts.maxCamDistance ?? 440) * s;
352
351
  this.orginMaxCamDistance = this._maxCamDistance;
353
352
  this.thirdMouseMode = opts.thirdMouseMode ?? 1;
353
+ this.enableZoom = opts.enableZoom ?? false;
354
354
  const isMobileDevice = () => navigator.maxTouchPoints && navigator.maxTouchPoints > 0 || "ontouchstart" in window || /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
355
355
  this.isShowMobileControls = (opts.isShowMobileControls ?? true) && isMobileDevice();
356
356
  if (this.isShowMobileControls) {
@@ -561,6 +561,7 @@ var PlayerController = class {
561
561
  this.player.attach(this.camera);
562
562
  this.camera.position.set(0, 40 * this.playerModel.scale, 30 * this.playerModel.scale);
563
563
  this.camera.rotation.set(0, Math.PI, 0);
564
+ this.controls.enableZoom = false;
564
565
  } else {
565
566
  this.scene.attach(this.camera);
566
567
  const worldPos = this.player.position.clone();
@@ -569,6 +570,7 @@ var PlayerController = class {
569
570
  const offset = new THREE.Vector3(Math.cos(angle) * 400 * this.playerModel.scale, 200 * this.playerModel.scale, Math.sin(angle) * 400 * this.playerModel.scale);
570
571
  this.camera.position.copy(worldPos).add(offset);
571
572
  this.controls.target.copy(worldPos);
573
+ this.controls.enableZoom = this.enableZoom;
572
574
  }
573
575
  this.setPointerLock();
574
576
  }
@@ -582,6 +584,8 @@ var PlayerController = class {
582
584
  setPointerLock() {
583
585
  if ((this.thirdMouseMode === 0 || this.thirdMouseMode === 1) && !this.isFirstPerson || this.isFirstPerson) {
584
586
  document.body.requestPointerLock();
587
+ } else {
588
+ document.exitPointerLock();
585
589
  }
586
590
  }
587
591
  /**
@@ -603,9 +607,10 @@ var PlayerController = class {
603
607
  * 设置控制器
604
608
  */
605
609
  setControls() {
606
- this.controls.enabled = !(this.thirdMouseMode === 0 || this.thirdMouseMode === 1);
610
+ this.controls.enableZoom = this.enableZoom;
607
611
  this.controls.rotateSpeed = this.mouseSensity * 0.05;
608
612
  this.controls.maxPolarAngle = Math.PI * (300 / 360);
613
+ this.controls.mouseButtons = { LEFT: 0, MIDDLE: 1, RIGHT: 2 };
609
614
  }
610
615
  /**
611
616
  * 重置控制器
@@ -929,28 +934,30 @@ var PlayerController = class {
929
934
  this.controls.target.copy(lookTarget);
930
935
  this.camera.position.add(lookTarget);
931
936
  this.controls.update();
932
- this._personToCam.subVectors(this.camera.position, this.player.position);
933
- const origin = this.player.position.clone().add(new THREE.Vector3(0, 0, 0));
934
- const direction = this._personToCam.clone().normalize();
935
- const desiredDist = this._personToCam.length();
936
- this._raycasterPersonToCam.set(origin, direction);
937
- this._raycasterPersonToCam.far = desiredDist;
938
- const intersects2 = this._raycasterPersonToCam.intersectObject(this.collider, false);
939
- if (intersects2.length > 0) {
940
- const hit = intersects2[0];
941
- const safeDist = Math.max(hit.distance - this._camEpsilon, this._minCamDistance);
942
- const targetCamPos = origin.clone().add(direction.clone().multiplyScalar(safeDist));
943
- this.camera.position.lerp(targetCamPos, this._camCollisionLerp);
944
- } else {
945
- this._raycasterPersonToCam.far = this._maxCamDistance;
946
- const intersectsMaxDis = this._raycasterPersonToCam.intersectObject(this.collider, false);
947
- let safeDist = this._maxCamDistance;
948
- if (intersectsMaxDis.length) {
949
- const hitMax = intersectsMaxDis[0];
950
- safeDist = hitMax.distance - this._camEpsilon;
937
+ if (!this.enableZoom) {
938
+ this._personToCam.subVectors(this.camera.position, this.player.position);
939
+ const origin = this.player.position.clone().add(new THREE.Vector3(0, 0, 0));
940
+ const direction = this._personToCam.clone().normalize();
941
+ const desiredDist = this._personToCam.length();
942
+ this._raycasterPersonToCam.set(origin, direction);
943
+ this._raycasterPersonToCam.far = desiredDist;
944
+ const intersects2 = this._raycasterPersonToCam.intersectObject(this.collider, false);
945
+ if (intersects2.length > 0) {
946
+ const hit = intersects2[0];
947
+ const safeDist = Math.max(hit.distance - this._camEpsilon, this._minCamDistance);
948
+ const targetCamPos = origin.clone().add(direction.clone().multiplyScalar(safeDist));
949
+ this.camera.position.lerp(targetCamPos, this._camCollisionLerp);
950
+ } else {
951
+ this._raycasterPersonToCam.far = this._maxCamDistance;
952
+ const intersectsMaxDis = this._raycasterPersonToCam.intersectObject(this.collider, false);
953
+ let safeDist = this._maxCamDistance;
954
+ if (intersectsMaxDis.length) {
955
+ const hitMax = intersectsMaxDis[0];
956
+ safeDist = hitMax.distance - this._camEpsilon;
957
+ }
958
+ const targetCamPos = origin.clone().add(direction.clone().multiplyScalar(safeDist));
959
+ this.camera.position.lerp(targetCamPos, this._camCollisionLerp);
951
960
  }
952
- const targetCamPos = origin.clone().add(direction.clone().multiplyScalar(safeDist));
953
- this.camera.position.lerp(targetCamPos, this._camCollisionLerp);
954
961
  }
955
962
  }
956
963
  if (this.player.position.y < this.boundingBoxMinY - 1) {