three-player-controller 0.2.5 → 0.2.51

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
@@ -15,17 +15,17 @@ npm install three-player-controller
15
15
  [glb 场景](https://hh-hang.github.io/three-player-controller/index.html)
16
16
  [3dtiles 场景](https://hh-hang.github.io/three-player-controller/3dtilesScene.html)
17
17
 
18
- ### 控制
18
+ ### 普通控制
19
19
 
20
- ![控制演示](https://github.com/hh-hang/three-player-controller/blob/master/example/public/gif/1.gif)
20
+ ![普通控制演示](https://github.com/hh-hang/three-player-controller/blob/master/example/public/gif/1.gif)
21
21
 
22
- ### 飞行
22
+ ### 飞行控制
23
23
 
24
- ![飞行](https://github.com/hh-hang/three-player-controller/blob/master/example/public/gif/3.gif)
24
+ ![飞行控制演示](https://github.com/hh-hang/three-player-controller/blob/master/example/public/gif/4.gif)
25
25
 
26
- ### 3DTiles 漫游
26
+ ### 移动端控制演示
27
27
 
28
- ![3DTiles 漫游](https://github.com/hh-hang/three-player-controller/blob/master/example/public/gif/4.gif)
28
+ ![移动端控制演示](https://github.com/hh-hang/three-player-controller/blob/master/example/public/gif/5.gif)
29
29
 
30
30
  # 使用
31
31
 
package/dist/index.d.mts CHANGED
@@ -20,6 +20,7 @@ type PlayerControllerOptions = {
20
20
  gravity?: number;
21
21
  jumpHeight?: number;
22
22
  speed?: number;
23
+ rotateY?: number;
23
24
  };
24
25
  initPos?: THREE.Vector3;
25
26
  intDirection?: THREE.Vector3;
@@ -37,6 +38,7 @@ declare function playerController(): {
37
38
  update: (dt?: number) => Promise<void>;
38
39
  destroy: () => void;
39
40
  setInput: (i: any) => void;
41
+ getposition: () => THREE.Vector3;
40
42
  };
41
43
  declare function onAllEvent(): void;
42
44
  declare function offAllEvent(): void;
package/dist/index.d.ts CHANGED
@@ -20,6 +20,7 @@ type PlayerControllerOptions = {
20
20
  gravity?: number;
21
21
  jumpHeight?: number;
22
22
  speed?: number;
23
+ rotateY?: number;
23
24
  };
24
25
  initPos?: THREE.Vector3;
25
26
  intDirection?: THREE.Vector3;
@@ -37,6 +38,7 @@ declare function playerController(): {
37
38
  update: (dt?: number) => Promise<void>;
38
39
  destroy: () => void;
39
40
  setInput: (i: any) => void;
41
+ getposition: () => THREE.Vector3;
40
42
  };
41
43
  declare function onAllEvent(): void;
42
44
  declare function offAllEvent(): void;
package/dist/index.js CHANGED
@@ -134,14 +134,8 @@ var PlayerController = class {
134
134
  this.DIR_UP = new THREE.Vector3(0, 1, 0);
135
135
  this._personToCam = new THREE.Vector3();
136
136
  this._originTmp = new THREE.Vector3();
137
- this._raycaster = new THREE.Raycaster(
138
- new THREE.Vector3(),
139
- new THREE.Vector3(0, -1, 0)
140
- );
141
- this._raycasterPersonToCam = new THREE.Raycaster(
142
- new THREE.Vector3(),
143
- new THREE.Vector3()
144
- );
137
+ this._raycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3(0, -1, 0));
138
+ this._raycasterPersonToCam = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3());
145
139
  // 键盘按下事件
146
140
  this._boundOnKeydown = async (e) => {
147
141
  if (e.ctrlKey && (e.code === "KeyW" || e.code === "KeyA" || e.code === "KeyS" || e.code === "KeyD")) {
@@ -191,8 +185,7 @@ var PlayerController = class {
191
185
  case "KeyF":
192
186
  this.isFlying = !this.isFlying;
193
187
  this.setAnimationByPressed();
194
- if (!this.isFlying && !this.playerIsOnGround)
195
- this.playPersonAnimationByName("jumping");
188
+ if (!this.isFlying && !this.playerIsOnGround) this.playPersonAnimationByName("jumping");
196
189
  break;
197
190
  }
198
191
  };
@@ -340,6 +333,7 @@ var PlayerController = class {
340
333
  this.jumpHeight = opts.playerModel.jumpHeight ? opts.playerModel.jumpHeight * s : 800 * s;
341
334
  this.originPlayerSpeed = opts.playerModel.speed ? opts.playerModel.speed * s : 400 * s;
342
335
  this.playerSpeed = this.originPlayerSpeed;
336
+ this.playerModel.rotateY = opts.playerModel.rotateY ? opts.playerModel.rotateY : 0;
343
337
  this._camCollisionLerp = 0.18;
344
338
  this._camEpsilon = 35 * s;
345
339
  this._minCamDistance = opts.minCamDistance ? opts.minCamDistance * s : 100 * s;
@@ -369,53 +363,32 @@ var PlayerController = class {
369
363
  this.isFirstPerson = !this.isFirstPerson;
370
364
  if (this.isFirstPerson) {
371
365
  this.player.attach(this.camera);
372
- this.camera.position.set(
373
- 0,
374
- 40 * this.playerModel.scale,
375
- 30 * this.playerModel.scale
376
- );
366
+ this.camera.position.set(0, 40 * this.playerModel.scale, 30 * this.playerModel.scale);
377
367
  this.camera.rotation.set(0, Math.PI, 0);
378
368
  this.setPointerLock();
379
369
  } else {
380
370
  this.scene.attach(this.camera);
381
371
  const worldPos = this.player.position.clone();
382
- const dir = new THREE.Vector3(0, 0, -1).applyQuaternion(
383
- this.player.quaternion
384
- );
372
+ const dir = new THREE.Vector3(0, 0, -1).applyQuaternion(this.player.quaternion);
385
373
  const angle = Math.atan2(dir.z, dir.x);
386
- const offset = new THREE.Vector3(
387
- Math.cos(angle) * 400 * this.playerModel.scale,
388
- 200 * this.playerModel.scale,
389
- Math.sin(angle) * 400 * this.playerModel.scale
390
- );
374
+ const offset = new THREE.Vector3(Math.cos(angle) * 400 * this.playerModel.scale, 200 * this.playerModel.scale, Math.sin(angle) * 400 * this.playerModel.scale);
391
375
  this.camera.position.copy(worldPos).add(offset);
392
376
  this.controls.target.copy(worldPos);
393
377
  this.setPointerLock();
394
378
  }
395
379
  }
396
380
  setPointerLock() {
397
- if ((this.thirdMouseMode == 0 || this.thirdMouseMode == 1) && !this.isFirstPerson || this.isFirstPerson)
398
- document.body.requestPointerLock();
381
+ if ((this.thirdMouseMode == 0 || this.thirdMouseMode == 1) && !this.isFirstPerson || this.isFirstPerson) document.body.requestPointerLock();
399
382
  }
400
383
  // 摄像机/控制器设置
401
384
  setCameraPos() {
402
385
  if (this.isFirstPerson) {
403
- this.camera.position.set(
404
- 0,
405
- 40 * this.playerModel.scale,
406
- 30 * this.playerModel.scale
407
- );
386
+ this.camera.position.set(0, 40 * this.playerModel.scale, 30 * this.playerModel.scale);
408
387
  } else {
409
388
  const worldPos = this.player.position.clone();
410
- const dir = new THREE.Vector3(0, 0, -1).applyQuaternion(
411
- this.player.quaternion
412
- );
389
+ const dir = new THREE.Vector3(0, 0, -1).applyQuaternion(this.player.quaternion);
413
390
  const angle = Math.atan2(dir.z, dir.x);
414
- const offset = new THREE.Vector3(
415
- Math.cos(angle) * 400 * this.playerModel.scale,
416
- -100 * this.playerModel.scale,
417
- Math.sin(angle) * 400 * this.playerModel.scale
418
- );
391
+ const offset = new THREE.Vector3(Math.cos(angle) * 400 * this.playerModel.scale, -100 * this.playerModel.scale, Math.sin(angle) * 400 * this.playerModel.scale);
419
392
  this.camera.position.copy(worldPos).add(offset);
420
393
  }
421
394
  this.camera.updateProjectionMatrix();
@@ -447,9 +420,7 @@ var PlayerController = class {
447
420
  // 初始化加载器
448
421
  async initLoader() {
449
422
  const dracoLoader = new import_DRACOLoader.DRACOLoader();
450
- dracoLoader.setDecoderPath(
451
- "https://unpkg.com/three@0.180.0/examples/jsm/libs/draco/gltf/"
452
- );
423
+ dracoLoader.setDecoderPath("https://unpkg.com/three@0.180.0/examples/jsm/libs/draco/gltf/");
453
424
  dracoLoader.setDecoderConfig({ type: "js" });
454
425
  this.loader.setDRACOLoader(dracoLoader);
455
426
  }
@@ -477,18 +448,9 @@ var PlayerController = class {
477
448
  const regs = [
478
449
  [this.playerModel.idleAnim, "idle"],
479
450
  [this.playerModel.walkAnim, "walking"],
480
- [
481
- this.playerModel.leftWalkAnim || this.playerModel.walkAnim,
482
- "left_walking"
483
- ],
484
- [
485
- this.playerModel.rightWalkAnim || this.playerModel.walkAnim,
486
- "right_walking"
487
- ],
488
- [
489
- this.playerModel.backwardAnim || this.playerModel.walkAnim,
490
- "walking_backward"
491
- ],
451
+ [this.playerModel.leftWalkAnim || this.playerModel.walkAnim, "left_walking"],
452
+ [this.playerModel.rightWalkAnim || this.playerModel.walkAnim, "right_walking"],
453
+ [this.playerModel.backwardAnim || this.playerModel.walkAnim, "walking_backward"],
492
454
  [this.playerModel.jumpAnim, "jumping"],
493
455
  [this.playerModel.runAnim, "running"],
494
456
  [this.playerModel.flyIdleAnim || this.playerModel.idleAnim, "flyidle"],
@@ -577,29 +539,22 @@ var PlayerController = class {
577
539
  material.depthWrite = false;
578
540
  const r = this.playerRadius * this.playerModel.scale;
579
541
  const h = this.playerHeight * this.playerModel.scale;
580
- this.player = new THREE.Mesh(
581
- new import_RoundedBoxGeometry.RoundedBoxGeometry(r * 2, h, r * 2, 1, 75),
582
- material
583
- );
542
+ this.player = new THREE.Mesh(new import_RoundedBoxGeometry.RoundedBoxGeometry(r * 2, h, r * 2, 1, 75), material);
584
543
  this.player.geometry.translate(0, -h * 0.25, 0);
585
544
  this.player.capsuleInfo = {
586
545
  radius: r,
587
- segment: new THREE.Line3(
588
- new THREE.Vector3(),
589
- new THREE.Vector3(0, -h * 0.5, 0)
590
- )
546
+ segment: new THREE.Line3(new THREE.Vector3(), new THREE.Vector3(0, -h * 0.5, 0))
591
547
  };
592
548
  this.player.name = "capsule";
593
549
  this.scene.add(this.player);
594
550
  this.reset();
551
+ this.player.rotateY(this.playerModel.rotateY ?? 0);
595
552
  }
596
553
  // 获取法线与Y轴的夹角
597
554
  getAngleWithYAxis(normal) {
598
555
  const yAxis = { x: 0, y: 1, z: 0 };
599
556
  const dotProduct = normal.x * yAxis.x + normal.y * yAxis.y + normal.z * yAxis.z;
600
- const normalMagnitude = Math.sqrt(
601
- normal.x * normal.x + normal.y * normal.y + normal.z * normal.z
602
- );
557
+ const normalMagnitude = Math.sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
603
558
  const yAxisMagnitude = 1;
604
559
  const cosTheta = dotProduct / (normalMagnitude * yAxisMagnitude);
605
560
  return Math.acos(cosTheta);
@@ -608,8 +563,7 @@ var PlayerController = class {
608
563
  async update(delta = clock.getDelta()) {
609
564
  if (!this.isupdate || !this.player || !this.collider) return;
610
565
  delta = Math.min(delta, 1 / 30);
611
- if (!this.isFlying)
612
- this.player.position.addScaledVector(this.playerVelocity, delta);
566
+ if (!this.isFlying) this.player.position.addScaledVector(this.playerVelocity, delta);
613
567
  this.updateMixers(delta);
614
568
  this.camera.getWorldDirection(this.camDir);
615
569
  let angle = Math.atan2(this.camDir.z, this.camDir.x) + Math.PI / 2;
@@ -635,21 +589,11 @@ var PlayerController = class {
635
589
  this.playerSpeed = this.shiftPressed ? this.originPlayerSpeed * 2 : this.originPlayerSpeed;
636
590
  }
637
591
  this.moveDir.normalize().applyAxisAngle(this.upVector, angle);
638
- this.player.position.addScaledVector(
639
- this.moveDir,
640
- this.playerSpeed * delta
641
- );
592
+ this.player.position.addScaledVector(this.moveDir, this.playerSpeed * delta);
642
593
  let playerDistanceFromGround = Infinity;
643
- this._originTmp.set(
644
- this.player.position.x,
645
- this.player.position.y,
646
- this.player.position.z
647
- );
594
+ this._originTmp.set(this.player.position.x, this.player.position.y, this.player.position.z);
648
595
  this._raycaster.ray.origin.copy(this._originTmp);
649
- const intersects = this._raycaster.intersectObject(
650
- this.collider,
651
- false
652
- );
596
+ const intersects = this._raycaster.intersectObject(this.collider, false);
653
597
  if (intersects.length > 0) {
654
598
  playerDistanceFromGround = this.player.position.y - intersects[0].point.y;
655
599
  const normal = intersects[0].normal;
@@ -677,11 +621,7 @@ var PlayerController = class {
677
621
  this.playerIsOnGround = true;
678
622
  } else if (playerDistanceFromGround < minH) {
679
623
  this.playerVelocity.set(0, 0, 0);
680
- this.player.position.set(
681
- this.player.position.x,
682
- intersects[0].point.y + h,
683
- this.player.position.z
684
- );
624
+ this.player.position.set(this.player.position.x, intersects[0].point.y + h, this.player.position.z);
685
625
  this.playerIsOnGround = true;
686
626
  }
687
627
  }
@@ -704,11 +644,7 @@ var PlayerController = class {
704
644
  intersectsTriangle: (tri) => {
705
645
  const triPoint = this.tempVector;
706
646
  const capsulePoint = this.tempVector2;
707
- const distance = tri.closestPointToSegment(
708
- this.tempSegment,
709
- triPoint,
710
- capsulePoint
711
- );
647
+ const distance = tri.closestPointToSegment(this.tempSegment, triPoint, capsulePoint);
712
648
  if (distance < capsuleInfo.radius) {
713
649
  const depth = capsuleInfo.radius - distance;
714
650
  const direction = capsulePoint.sub(triPoint).normalize();
@@ -718,10 +654,7 @@ var PlayerController = class {
718
654
  }
719
655
  });
720
656
  const newPosition = this.tempVector.copy(this.tempSegment.start).applyMatrix4(this.collider.matrixWorld);
721
- const deltaVector = this.tempVector2.subVectors(
722
- newPosition,
723
- this.player.position
724
- );
657
+ const deltaVector = this.tempVector2.subVectors(newPosition, this.player.position);
725
658
  const offset = Math.max(0, deltaVector.length() - 1e-5);
726
659
  deltaVector.normalize().multiplyScalar(offset);
727
660
  this.player.position.add(deltaVector);
@@ -776,24 +709,15 @@ var PlayerController = class {
776
709
  const desiredDist = this._personToCam.length();
777
710
  this._raycasterPersonToCam.set(origin, direction);
778
711
  this._raycasterPersonToCam.far = desiredDist;
779
- const intersects2 = this._raycasterPersonToCam.intersectObject(
780
- this.collider,
781
- false
782
- );
712
+ const intersects2 = this._raycasterPersonToCam.intersectObject(this.collider, false);
783
713
  if (intersects2.length > 0) {
784
714
  const hit = intersects2[0];
785
- const safeDist = Math.max(
786
- hit.distance - this._camEpsilon,
787
- this._minCamDistance
788
- );
715
+ const safeDist = Math.max(hit.distance - this._camEpsilon, this._minCamDistance);
789
716
  const targetCamPos = origin.clone().add(direction.clone().multiplyScalar(safeDist));
790
717
  this.camera.position.lerp(targetCamPos, this._camCollisionLerp);
791
718
  } else {
792
719
  this._raycasterPersonToCam.far = this._maxCamDistance;
793
- const intersectsMaxDis = this._raycasterPersonToCam.intersectObject(
794
- this.collider,
795
- false
796
- );
720
+ const intersectsMaxDis = this._raycasterPersonToCam.intersectObject(this.collider, false);
797
721
  let safeDist = this._maxCamDistance;
798
722
  if (intersectsMaxDis.length) {
799
723
  const hitMax = intersectsMaxDis[0];
@@ -804,34 +728,15 @@ var PlayerController = class {
804
728
  }
805
729
  }
806
730
  if (this.player.position.y < this.boundingBoxMinY - 1) {
807
- this._originTmp.set(
808
- this.player.position.x,
809
- 1e4,
810
- this.player.position.z
811
- );
731
+ this._originTmp.set(this.player.position.x, 1e4, this.player.position.z);
812
732
  this._raycaster.ray.origin.copy(this._originTmp);
813
- const intersects2 = this._raycaster.intersectObject(
814
- this.collider,
815
- false
816
- );
733
+ const intersects2 = this._raycaster.intersectObject(this.collider, false);
817
734
  if (intersects2.length > 0) {
818
735
  console.log("\u73A9\u5BB6\u4E3Abug\u610F\u5916\u6389\u843D");
819
- this.reset(
820
- new THREE.Vector3(
821
- this.player.position.x,
822
- intersects2[0].point.y + 5,
823
- this.player.position.z
824
- )
825
- );
736
+ this.reset(new THREE.Vector3(this.player.position.x, intersects2[0].point.y + 5, this.player.position.z));
826
737
  } else {
827
738
  console.log("\u73A9\u5BB6\u6B63\u5E38\u6389\u843D");
828
- this.reset(
829
- new THREE.Vector3(
830
- this.player.position.x,
831
- this.player.position.y + 15,
832
- this.player.position.z
833
- )
834
- );
739
+ this.reset(new THREE.Vector3(this.player.position.x, this.player.position.y + 15, this.player.position.z));
835
740
  }
836
741
  }
837
742
  }
@@ -883,6 +788,9 @@ var PlayerController = class {
883
788
  window.removeEventListener("mousemove", this._mouseMove);
884
789
  window.removeEventListener("click", this._mouseClick);
885
790
  }
791
+ getPosition() {
792
+ return this.player.position;
793
+ }
886
794
  // 更新模型动画
887
795
  updateMixers(delta) {
888
796
  if (this.personMixer) this.personMixer.update(delta);
@@ -936,8 +844,7 @@ var PlayerController = class {
936
844
  attrMap.set(name, { itemSize, arrayCtor: ctor, examples: 1 });
937
845
  } else {
938
846
  const m = attrMap.get(name);
939
- if (m.itemSize !== itemSize || m.arrayCtor !== ctor)
940
- attrConflict.add(name);
847
+ if (m.itemSize !== itemSize || m.arrayCtor !== ctor) attrConflict.add(name);
941
848
  else m.examples++;
942
849
  }
943
850
  }
@@ -958,10 +865,7 @@ var PlayerController = class {
958
865
  const meta = attrMap.get(name);
959
866
  const len = count * meta.itemSize;
960
867
  const array = new meta.arrayCtor(len);
961
- g.setAttribute(
962
- name,
963
- new THREE.BufferAttribute(array, meta.itemSize)
964
- );
868
+ g.setAttribute(name, new THREE.BufferAttribute(array, meta.itemSize));
965
869
  }
966
870
  }
967
871
  }
@@ -1006,11 +910,7 @@ var PlayerController = class {
1006
910
  const yaw = -dx * speed * this.mouseSensity;
1007
911
  const pitch = -dy * speed * this.mouseSensity;
1008
912
  this.player.rotateY(yaw);
1009
- this.camera.rotation.x = THREE.MathUtils.clamp(
1010
- this.camera.rotation.x + pitch,
1011
- -1.1,
1012
- 1.4
1013
- );
913
+ this.camera.rotation.x = THREE.MathUtils.clamp(this.camera.rotation.x + pitch, -1.1, 1.4);
1014
914
  } else {
1015
915
  const sensitivity = this.mouseSensity;
1016
916
  const deltaX = -dx * speed * sensitivity;
@@ -1026,11 +926,7 @@ var PlayerController = class {
1026
926
  const newX = distance * Math.sin(phi) * Math.sin(theta);
1027
927
  const newY = distance * Math.cos(phi);
1028
928
  const newZ = distance * Math.sin(phi) * Math.cos(theta);
1029
- this.camera.position.set(
1030
- target.x + newX,
1031
- target.y + newY,
1032
- target.z + newZ
1033
- );
929
+ this.camera.position.set(target.x + newX, target.y + newY, target.z + newZ);
1034
930
  this.camera.lookAt(target);
1035
931
  }
1036
932
  }
@@ -1069,8 +965,7 @@ var PlayerController = class {
1069
965
  if (input.toggleFly) {
1070
966
  this.isFlying = !this.isFlying;
1071
967
  this.setAnimationByPressed();
1072
- if (!this.isFlying && !this.playerIsOnGround)
1073
- this.playPersonAnimationByName("jumping");
968
+ if (!this.isFlying && !this.playerIsOnGround) this.playPersonAnimationByName("jumping");
1074
969
  }
1075
970
  }
1076
971
  // 初始化移动端摇杆控制
@@ -1096,17 +991,15 @@ var PlayerController = class {
1096
991
  userSelect: "none"
1097
992
  });
1098
993
  container.appendChild(this.joystickZoneEl);
1099
- ["touchstart", "touchmove", "touchend", "touchcancel"].forEach(
1100
- (evtName) => {
1101
- this.joystickZoneEl?.addEventListener(
1102
- evtName,
1103
- (e) => {
1104
- e.preventDefault();
1105
- },
1106
- { passive: false }
1107
- );
1108
- }
1109
- );
994
+ ["touchstart", "touchmove", "touchend", "touchcancel"].forEach((evtName) => {
995
+ this.joystickZoneEl?.addEventListener(
996
+ evtName,
997
+ (e) => {
998
+ e.preventDefault();
999
+ },
1000
+ { passive: false }
1001
+ );
1002
+ });
1110
1003
  this.joystickManager = nipple.create({
1111
1004
  zone: this.joystickZoneEl,
1112
1005
  mode: "static",
@@ -1156,17 +1049,15 @@ var PlayerController = class {
1156
1049
  userSelect: "none"
1157
1050
  });
1158
1051
  container.appendChild(this.lookAreaEl);
1159
- ["touchstart", "touchmove", "touchend", "touchcancel"].forEach(
1160
- (evtName) => {
1161
- this.lookAreaEl?.addEventListener(
1162
- evtName,
1163
- (e) => {
1164
- e.preventDefault();
1165
- },
1166
- { passive: false }
1167
- );
1168
- }
1169
- );
1052
+ ["touchstart", "touchmove", "touchend", "touchcancel"].forEach((evtName) => {
1053
+ this.lookAreaEl?.addEventListener(
1054
+ evtName,
1055
+ (e) => {
1056
+ e.preventDefault();
1057
+ },
1058
+ { passive: false }
1059
+ );
1060
+ });
1170
1061
  this.lookAreaEl.addEventListener("pointerdown", this.onPointerDown, {
1171
1062
  passive: false
1172
1063
  });
@@ -1309,7 +1200,8 @@ function playerController() {
1309
1200
  reset: (pos) => c.reset(pos),
1310
1201
  update: (dt) => c.update(dt),
1311
1202
  destroy: () => c.destroy(),
1312
- setInput: (i) => c.setInput(i)
1203
+ setInput: (i) => c.setInput(i),
1204
+ getposition: () => c.getPosition()
1313
1205
  };
1314
1206
  }
1315
1207
  function onAllEvent() {