@pmndrs/viverse 0.1.19 → 0.2.0

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.
Files changed (57) hide show
  1. package/dist/animation/bvh-bone-map.json +24 -0
  2. package/dist/animation/bvh.d.ts +4 -0
  3. package/dist/animation/bvh.js +8 -0
  4. package/dist/animation/default.d.ts +1 -0
  5. package/dist/animation/default.js +18 -0
  6. package/dist/animation/fbx.d.ts +2 -2
  7. package/dist/animation/gltf.d.ts +2 -2
  8. package/dist/animation/index.d.ts +16 -17
  9. package/dist/animation/index.js +118 -67
  10. package/dist/animation/mask.d.ts +3 -0
  11. package/dist/animation/mask.js +3 -0
  12. package/dist/camera.d.ts +3 -7
  13. package/dist/camera.js +16 -24
  14. package/dist/index.d.ts +3 -2
  15. package/dist/index.js +3 -2
  16. package/dist/input/index.d.ts +6 -7
  17. package/dist/input/index.js +5 -4
  18. package/dist/input/keyboard.d.ts +3 -4
  19. package/dist/input/keyboard.js +8 -10
  20. package/dist/input/pointer-capture.d.ts +3 -3
  21. package/dist/input/pointer-capture.js +11 -12
  22. package/dist/input/pointer-lock.d.ts +3 -3
  23. package/dist/input/pointer-lock.js +10 -10
  24. package/dist/input/screen-joystick.d.ts +6 -10
  25. package/dist/input/screen-joystick.js +29 -36
  26. package/dist/input/screen-jump-button.d.ts +1 -1
  27. package/dist/model/index.d.ts +10 -13
  28. package/dist/model/index.js +26 -29
  29. package/dist/physics/index.d.ts +2 -5
  30. package/dist/physics/index.js +7 -16
  31. package/dist/simple-character/defaults.d.ts +2 -0
  32. package/dist/simple-character/defaults.js +2 -0
  33. package/dist/simple-character/index.d.ts +101 -0
  34. package/dist/simple-character/index.js +109 -0
  35. package/dist/simple-character/state/index.d.ts +6 -0
  36. package/dist/simple-character/state/index.js +6 -0
  37. package/dist/simple-character/state/jump-down.d.ts +3 -0
  38. package/dist/simple-character/state/jump-down.js +25 -0
  39. package/dist/simple-character/state/jump-forward.d.ts +5 -0
  40. package/dist/simple-character/state/jump-forward.js +39 -0
  41. package/dist/simple-character/state/jump-loop.d.ts +3 -0
  42. package/dist/simple-character/state/jump-loop.js +23 -0
  43. package/dist/simple-character/state/jump-start.d.ts +4 -0
  44. package/dist/simple-character/state/jump-start.js +30 -0
  45. package/dist/simple-character/state/jump-up.d.ts +5 -0
  46. package/dist/simple-character/state/jump-up.js +38 -0
  47. package/dist/simple-character/state/movement.d.ts +3 -0
  48. package/dist/simple-character/state/movement.js +59 -0
  49. package/dist/simple-character/update-input-velocity.d.ts +5 -0
  50. package/dist/simple-character/update-input-velocity.js +25 -0
  51. package/dist/simple-character/update-rotation.d.ts +6 -0
  52. package/dist/simple-character/update-rotation.js +40 -0
  53. package/dist/utils.d.ts +12 -5
  54. package/dist/utils.js +28 -39
  55. package/package.json +2 -2
  56. package/dist/simple-character.d.ts +0 -106
  57. package/dist/simple-character.js +0 -342
@@ -6,7 +6,7 @@ export type PointerCaptureInputOptions = {
6
6
  /**
7
7
  * @requires to manually execute `domElement.setPointerCapture(pointerId)` on pointerdown
8
8
  */
9
- export declare class PointerCaptureInput implements Input {
9
+ export declare class PointerCaptureInput implements Input<PointerCaptureInputOptions> {
10
10
  private readonly domElement;
11
11
  private readonly abortController;
12
12
  private deltaZoom;
@@ -14,7 +14,7 @@ export declare class PointerCaptureInput implements Input {
14
14
  private deltaPitch;
15
15
  private activePointers;
16
16
  private lastPinchDist;
17
- constructor(domElement: HTMLElement, options?: PointerCaptureInputOptions);
18
- get<T>(field: InputField<T>): T | undefined;
17
+ constructor(domElement: HTMLElement);
18
+ get<T>(field: InputField<T>, options: PointerCaptureInputOptions): T | undefined;
19
19
  dispose(): void;
20
20
  }
@@ -10,7 +10,7 @@ export class PointerCaptureInput {
10
10
  deltaPitch = 0;
11
11
  activePointers = new Map();
12
12
  lastPinchDist = null;
13
- constructor(domElement, options = {}) {
13
+ constructor(domElement) {
14
14
  this.domElement = domElement;
15
15
  domElement.addEventListener('pointerdown', (event) => {
16
16
  this.domElement.setPointerCapture(event.pointerId);
@@ -31,16 +31,14 @@ export class PointerCaptureInput {
31
31
  const pts = Array.from(this.activePointers.values());
32
32
  if (this.lastPinchDist != null) {
33
33
  const d = Math.hypot(pts[0].x - pts[1].x, pts[0].y - pts[1].y);
34
- const zoomSpeed = options.pointerCaptureZoomSpeed ?? 0.0001;
35
- this.deltaZoom += (this.lastPinchDist - d) * zoomSpeed;
34
+ this.deltaZoom += this.lastPinchDist - d;
36
35
  this.lastPinchDist = d;
37
36
  }
38
37
  event.preventDefault();
39
38
  return;
40
39
  }
41
- const rotationSpeed = options.pointerCaptureRotationSpeed ?? 0.4;
42
- this.deltaYaw -= (rotationSpeed * event.movementX) / window.innerHeight;
43
- this.deltaPitch -= (rotationSpeed * event.movementY) / window.innerHeight;
40
+ this.deltaYaw -= event.movementX / window.innerHeight;
41
+ this.deltaPitch -= event.movementY / window.innerHeight;
44
42
  }, {
45
43
  signal: this.abortController.signal,
46
44
  });
@@ -64,25 +62,26 @@ export class PointerCaptureInput {
64
62
  });
65
63
  domElement.addEventListener('wheel', (event) => {
66
64
  event.preventDefault();
67
- const zoomSpeed = options.pointerCaptureZoomSpeed ?? 0.0001;
68
- this.deltaZoom += event.deltaY * zoomSpeed;
65
+ this.deltaZoom += event.deltaY;
69
66
  }, {
70
67
  signal: this.abortController.signal,
71
68
  });
72
69
  }
73
- get(field) {
70
+ get(field, options) {
71
+ const rotationSpeed = options.pointerCaptureRotationSpeed ?? 0.4;
72
+ const zoomSpeed = options.pointerCaptureZoomSpeed ?? 0.0001;
74
73
  let result;
75
74
  switch (field) {
76
75
  case DeltaPitchField:
77
- result = this.deltaPitch;
76
+ result = (this.deltaPitch * rotationSpeed);
78
77
  this.deltaPitch = 0;
79
78
  break;
80
79
  case DeltaYawField:
81
- result = this.deltaYaw;
80
+ result = (this.deltaYaw * rotationSpeed);
82
81
  this.deltaYaw = 0;
83
82
  break;
84
83
  case DeltaZoomField:
85
- result = this.deltaZoom;
84
+ result = (this.deltaZoom * zoomSpeed);
86
85
  this.deltaZoom = 0;
87
86
  break;
88
87
  }
@@ -6,12 +6,12 @@ export type PointerLockInputOptions = {
6
6
  /**
7
7
  * @requires to manually execute `domElement.requestPointerLock()`
8
8
  */
9
- export declare class PointerLockInput implements Input {
9
+ export declare class PointerLockInput implements Input<PointerLockInputOptions> {
10
10
  private readonly abortController;
11
11
  private deltaZoom;
12
12
  private deltaYaw;
13
13
  private deltaPitch;
14
- constructor(domElement: HTMLElement, options?: PointerLockInputOptions);
15
- get<T>(field: InputField<T>): T | undefined;
14
+ constructor(domElement: HTMLElement);
15
+ get<T>(field: InputField<T>, options: PointerLockInputOptions): T | undefined;
16
16
  dispose(): void;
17
17
  }
@@ -7,16 +7,15 @@ export class PointerLockInput {
7
7
  deltaZoom = 0;
8
8
  deltaYaw = 0;
9
9
  deltaPitch = 0;
10
- constructor(domElement, options = {}) {
10
+ constructor(domElement) {
11
11
  domElement.addEventListener('pointermove', (event) => {
12
12
  if (document.pointerLockElement != domElement) {
13
13
  return;
14
14
  }
15
- const rotationSpeed = options.pointerLockRotationSpeed ?? 0.4;
16
15
  // Compute based on domElement bounds instead of window.innerHeight
17
16
  const rect = domElement.getBoundingClientRect();
18
- this.deltaYaw -= (rotationSpeed * event.movementX) / rect.height;
19
- this.deltaPitch -= (rotationSpeed * event.movementY) / rect.height;
17
+ this.deltaYaw -= event.movementX / rect.height;
18
+ this.deltaPitch -= event.movementY / rect.height;
20
19
  }, {
21
20
  signal: this.abortController.signal,
22
21
  });
@@ -24,26 +23,27 @@ export class PointerLockInput {
24
23
  if (document.pointerLockElement != domElement) {
25
24
  return;
26
25
  }
27
- const zoomSpeed = options.pointerLockZoomSpeed ?? 0.0001;
28
- this.deltaZoom += event.deltaY * zoomSpeed;
26
+ this.deltaZoom += event.deltaY;
29
27
  event.preventDefault();
30
28
  }, {
31
29
  signal: this.abortController.signal,
32
30
  });
33
31
  }
34
- get(field) {
32
+ get(field, options) {
33
+ const rotationSpeed = options.pointerLockRotationSpeed ?? 0.4;
34
+ const zoomSpeed = options.pointerLockZoomSpeed ?? 0.0001;
35
35
  let result;
36
36
  switch (field) {
37
37
  case DeltaPitchField:
38
- result = this.deltaPitch;
38
+ result = (this.deltaPitch * rotationSpeed);
39
39
  this.deltaPitch = 0;
40
40
  break;
41
41
  case DeltaYawField:
42
- result = this.deltaYaw;
42
+ result = (this.deltaYaw * rotationSpeed);
43
43
  this.deltaYaw = 0;
44
44
  break;
45
45
  case DeltaZoomField:
46
- result = this.deltaZoom;
46
+ result = (this.deltaZoom * zoomSpeed);
47
47
  this.deltaZoom = 0;
48
48
  break;
49
49
  }
@@ -3,19 +3,15 @@ export type ScreenJoystickInputOptions = {
3
3
  screenJoystickRunDistancePx?: number;
4
4
  screenJoystickDeadZonePx?: number;
5
5
  };
6
- export declare class ScreenJoystickInput implements Input {
7
- private readonly options;
6
+ export declare class ScreenJoystickInput implements Input<ScreenJoystickInputOptions> {
8
7
  readonly root: HTMLDivElement;
9
8
  private readonly handle;
10
- private moveX;
11
- private moveY;
12
- private running;
13
- private readonly joystickRadius;
14
- private joyCenterX;
15
- private joyCenterY;
16
9
  private pointerId;
17
- constructor(domElement: HTMLElement, options?: ScreenJoystickInputOptions);
18
- get<T>(field: InputField<T>): T | undefined;
10
+ private distanceToCenter;
11
+ private clampedX;
12
+ private clampedY;
13
+ constructor(domElement: HTMLElement);
14
+ get<T>(field: InputField<T>, options: ScreenJoystickInputOptions): T | undefined;
19
15
  dispose(): void;
20
16
  private updateHandle;
21
17
  private resetHandle;
@@ -1,19 +1,15 @@
1
1
  import { MoveForwardField, MoveBackwardField, MoveLeftField, MoveRightField, RunField, } from './index.js';
2
2
  const DefaultDeadZonePx = 24;
3
3
  const DefaultRunDistancePx = 46;
4
+ const JoystickRadius = 56;
4
5
  export class ScreenJoystickInput {
5
- options;
6
6
  root;
7
7
  handle;
8
- moveX = 0;
9
- moveY = 0;
10
- running = false;
11
- joystickRadius = 56;
12
- joyCenterX = 0;
13
- joyCenterY = 0;
14
8
  pointerId;
15
- constructor(domElement, options = {}) {
16
- this.options = options;
9
+ distanceToCenter = 0;
10
+ clampedX = 0;
11
+ clampedY = 0;
12
+ constructor(domElement) {
17
13
  const parent = domElement.parentElement ?? domElement;
18
14
  const joy = document.createElement('div');
19
15
  joy.className = 'viverse-joystick mobile-only';
@@ -58,9 +54,9 @@ export class ScreenJoystickInput {
58
54
  joy.setPointerCapture(e.pointerId);
59
55
  this.pointerId = e.pointerId;
60
56
  const rect = joy.getBoundingClientRect();
61
- this.joyCenterX = rect.left + rect.width / 2;
62
- this.joyCenterY = rect.top + rect.height / 2;
63
- this.updateHandle(e.clientX - this.joyCenterX, e.clientY - this.joyCenterY);
57
+ const joyCenterX = rect.left + rect.width / 2;
58
+ const joyCenterY = rect.top + rect.height / 2;
59
+ this.updateHandle(e.clientX - joyCenterX, e.clientY - joyCenterY);
64
60
  };
65
61
  const onPointerMove = (e) => {
66
62
  if (this.pointerId == null) {
@@ -68,7 +64,10 @@ export class ScreenJoystickInput {
68
64
  }
69
65
  e.preventDefault();
70
66
  e.stopPropagation();
71
- this.updateHandle(e.clientX - this.joyCenterX, e.clientY - this.joyCenterY);
67
+ const rect = joy.getBoundingClientRect();
68
+ const joyCenterX = rect.left + rect.width / 2;
69
+ const joyCenterY = rect.top + rect.height / 2;
70
+ this.updateHandle(e.clientX - joyCenterX, e.clientY - joyCenterY);
72
71
  };
73
72
  const onPointerEnd = (e) => {
74
73
  if (this.pointerId != e.pointerId) {
@@ -84,18 +83,22 @@ export class ScreenJoystickInput {
84
83
  joy.addEventListener('pointerup', onPointerEnd);
85
84
  joy.addEventListener('pointercancel', onPointerEnd);
86
85
  }
87
- get(field) {
86
+ get(field, options) {
88
87
  switch (field) {
89
88
  case MoveForwardField:
90
- return Math.max(0, this.moveY);
91
89
  case MoveBackwardField:
92
- return Math.max(0, -this.moveY);
90
+ const moveY = this.distanceToCenter <= (options.screenJoystickDeadZonePx ?? DefaultDeadZonePx)
91
+ ? 0
92
+ : -this.clampedY / JoystickRadius;
93
+ return field === MoveForwardField ? Math.max(0, moveY) : Math.max(0, -moveY);
93
94
  case MoveLeftField:
94
- return Math.max(0, -this.moveX);
95
95
  case MoveRightField:
96
- return Math.max(0, this.moveX);
96
+ const moveX = this.distanceToCenter <= (options.screenJoystickDeadZonePx ?? DefaultDeadZonePx)
97
+ ? 0
98
+ : this.clampedX / JoystickRadius;
99
+ return field === MoveLeftField ? Math.max(0, moveX) : Math.max(0, moveX);
97
100
  case RunField:
98
- return this.running;
101
+ return (this.distanceToCenter > (options.screenJoystickRunDistancePx ?? DefaultRunDistancePx));
99
102
  }
100
103
  return undefined;
101
104
  }
@@ -103,25 +106,15 @@ export class ScreenJoystickInput {
103
106
  this.root.remove();
104
107
  }
105
108
  updateHandle(dx, dy) {
106
- const len = Math.hypot(dx, dy) || 1;
107
- const max = this.joystickRadius;
108
- const clampedX = (dx / len) * Math.min(len, max);
109
- const clampedY = (dy / len) * Math.min(len, max);
110
- this.handle.style.transform = `translate(-50%,-50%) translate(${clampedX}px, ${clampedY}px)`;
111
- if (len <= (this.options.screenJoystickDeadZonePx ?? DefaultDeadZonePx)) {
112
- this.moveX = 0;
113
- this.moveY = 0;
114
- }
115
- else {
116
- this.moveX = clampedX / max;
117
- this.moveY = -clampedY / max;
118
- }
119
- this.running = len > (this.options.screenJoystickRunDistancePx ?? DefaultRunDistancePx);
109
+ this.distanceToCenter = Math.hypot(dx, dy) || 1;
110
+ this.clampedX = (dx / this.distanceToCenter) * Math.min(this.distanceToCenter, JoystickRadius);
111
+ this.clampedY = (dy / this.distanceToCenter) * Math.min(this.distanceToCenter, JoystickRadius);
112
+ this.handle.style.transform = `translate(-50%,-50%) translate(${this.clampedX}px, ${this.clampedY}px)`;
120
113
  }
121
114
  resetHandle() {
122
115
  this.handle.style.transform = 'translate(-50%,-50%)';
123
- this.moveX = 0;
124
- this.moveY = 0;
125
- this.running = false;
116
+ this.distanceToCenter = 0;
117
+ this.clampedX = 0;
118
+ this.clampedY = 0;
126
119
  }
127
120
  }
@@ -1,5 +1,5 @@
1
1
  import { Input, InputField } from './index.js';
2
- export declare class ScreenJumpButtonInput implements Input {
2
+ export declare class ScreenJumpButtonInput implements Input<{}> {
3
3
  readonly root: HTMLDivElement;
4
4
  private lastJumpTime;
5
5
  constructor(domElement: HTMLElement);
@@ -1,4 +1,5 @@
1
- import { Quaternion } from 'three';
1
+ import { AnimationAction, AnimationMixer, Object3D, Quaternion } from 'three';
2
+ import { CharacterAnimationMask } from '../animation/index.js';
2
3
  export { VRMHumanBoneName } from '@pixiv/three-vrm';
3
4
  export * from './vrm.js';
4
5
  export type CharacterModelOptions = {
@@ -17,16 +18,12 @@ export type CharacterModelOptions = {
17
18
  * @default true
18
19
  */
19
20
  readonly receiveShadow?: boolean;
20
- } | boolean;
21
- export declare function clearCharacterModelCache(options?: CharacterModelOptions): void;
22
- export declare function loadCharacterModel(options?: CharacterModelOptions): Promise<((import("@pixiv/three-vrm").VRM & {
23
- scene: import("three").Object3D<import("three").Object3DEventMap & {
24
- dispose: {};
25
- }>;
26
- }) | (import("three/examples/jsm/Addons.js").GLTF & {
27
- scene: import("three").Object3D<import("three").Object3DEventMap & {
28
- dispose: {};
29
- }>;
30
- })) & {
21
+ };
22
+ export declare function flattenCharacterModelOptions(options: Exclude<CharacterModelOptions, false> | undefined): Parameters<typeof loadCharacterModel>;
23
+ export type CharacterModel = {
24
+ mixer: AnimationMixer;
25
+ scene: Object3D;
26
+ currentAnimations: Map<CharacterAnimationMask | undefined, AnimationAction>;
31
27
  boneRotationOffset?: Quaternion;
32
- }> | undefined;
28
+ };
29
+ export declare function loadCharacterModel(url?: string, type?: Exclude<CharacterModelOptions, boolean>['type'], boneRotationOffset?: Quaternion, castShadow?: boolean, receiveShadow?: boolean): Promise<CharacterModel>;
@@ -1,17 +1,33 @@
1
- import { Euler, Quaternion } from 'three';
2
- import { loadVrmCharacterModel } from './vrm.js';
3
- import { cached, clearCache } from '../utils.js';
1
+ import { AnimationMixer, Euler, Quaternion } from 'three';
4
2
  import { loadGltfCharacterModel } from './gltf.js';
3
+ import { loadVrmCharacterModel } from './vrm.js';
5
4
  export { VRMHumanBoneName } from '@pixiv/three-vrm';
6
5
  export * from './vrm.js';
7
- async function uncachedLoadCharacterModel(type, url, boneRotationOffset, castShadow = true, receiveShadow = true) {
6
+ export function flattenCharacterModelOptions(options) {
7
+ if (options == null) {
8
+ return [];
9
+ }
10
+ return [options.url, options.type, options.boneRotationOffset, options.castShadow, options.receiveShadow];
11
+ }
12
+ export async function loadCharacterModel(url, type, boneRotationOffset, castShadow = true, receiveShadow = true) {
8
13
  let result;
9
- if (type == null || url == null) {
14
+ if (url == null) {
10
15
  //prepare loading the default model
11
16
  type = 'gltf';
12
17
  url = (await import('../assets/mannequin.js')).url;
13
18
  boneRotationOffset = new Quaternion().setFromEuler(new Euler(Math.PI, 0, Math.PI / 2, 'ZYX'));
14
19
  }
20
+ if (type == null) {
21
+ if (url.endsWith('.gltf') || url.endsWith('.glb')) {
22
+ type = 'gltf';
23
+ }
24
+ if (url.endsWith('.vrm')) {
25
+ type = 'vrm';
26
+ }
27
+ if (type == null) {
28
+ throw new Error(`Unable to infer model type from url "${url}. Please specify the type of the model manually."`);
29
+ }
30
+ }
15
31
  switch (type) {
16
32
  case 'vrm':
17
33
  result = await loadVrmCharacterModel(url);
@@ -30,28 +46,9 @@ async function uncachedLoadCharacterModel(type, url, boneRotationOffset, castSha
30
46
  obj.receiveShadow = true;
31
47
  }
32
48
  });
33
- return result;
34
- }
35
- function getCharacterModelDependencies(options = true) {
36
- if (options === false) {
37
- return undefined;
38
- }
39
- if (options === true) {
40
- return [undefined, undefined, undefined, undefined, undefined];
41
- }
42
- return [options.type, options.url, options.boneRotationOffset, options.castShadow, options.receiveShadow];
43
- }
44
- export function clearCharacterModelCache(options) {
45
- const dependencies = getCharacterModelDependencies(options);
46
- if (dependencies == null) {
47
- return;
48
- }
49
- clearCache(uncachedLoadCharacterModel, dependencies);
50
- }
51
- export function loadCharacterModel(options) {
52
- const dependencies = getCharacterModelDependencies(options);
53
- if (dependencies == null) {
54
- return undefined;
55
- }
56
- return cached(uncachedLoadCharacterModel, dependencies);
49
+ const restPose = result.scene.clone();
50
+ restPose.visible = false;
51
+ restPose.traverse((object) => (object.name = `rest_${object.name}`));
52
+ result.scene.add(restPose);
53
+ return Object.assign(result, { mixer: new AnimationMixer(result.scene), currentAnimations: new Map() });
57
54
  }
@@ -32,9 +32,7 @@ export type BvhCharacterPhysicsOptions = {
32
32
  * assumes the target object origin is at its bottom
33
33
  */
34
34
  export declare class BvhCharacterPhysics {
35
- private readonly character;
36
35
  private readonly world;
37
- private disposed;
38
36
  private readonly stateVelocity;
39
37
  readonly inputVelocity: Vector3;
40
38
  private notGroundedSeconds;
@@ -42,14 +40,13 @@ export declare class BvhCharacterPhysics {
42
40
  private readonly aabbox;
43
41
  private radius;
44
42
  get isGrounded(): boolean;
45
- constructor(character: Object3D, world: BvhPhysicsWorld);
43
+ constructor(world: BvhPhysicsWorld);
46
44
  applyVelocity(velocity: Vector3): void;
47
45
  /**
48
46
  * @param delta in seconds
49
47
  */
50
- update(fullDelta: number, options?: BvhCharacterPhysicsOptions): void;
48
+ update(model: Object3D, fullDelta: number, options?: BvhCharacterPhysicsOptions): void;
51
49
  private updateBoundingShapes;
52
- dispose(): void;
53
50
  shapecastCapsule(position: Vector3, maxGroundSlope: number, options: Exclude<BvhCharacterPhysicsOptions, boolean>): boolean;
54
51
  }
55
52
  export * from './world.js';
@@ -11,9 +11,7 @@ const YAxis = new Vector3(0, 1, 0);
11
11
  * assumes the target object origin is at its bottom
12
12
  */
13
13
  export class BvhCharacterPhysics {
14
- character;
15
14
  world;
16
- disposed = false;
17
15
  stateVelocity = new Vector3();
18
16
  inputVelocity = new Vector3();
19
17
  notGroundedSeconds = 0;
@@ -23,8 +21,7 @@ export class BvhCharacterPhysics {
23
21
  get isGrounded() {
24
22
  return this.notGroundedSeconds < 0.2;
25
23
  }
26
- constructor(character, world) {
27
- this.character = character;
24
+ constructor(world) {
28
25
  this.world = world;
29
26
  }
30
27
  applyVelocity(velocity) {
@@ -33,16 +30,13 @@ export class BvhCharacterPhysics {
33
30
  /**
34
31
  * @param delta in seconds
35
32
  */
36
- update(fullDelta, options = true) {
33
+ update(model, fullDelta, options = true) {
37
34
  if (options === false) {
38
35
  return;
39
36
  }
40
37
  if (options === true) {
41
38
  options = {};
42
39
  }
43
- if (this.disposed) {
44
- return;
45
- }
46
40
  //at max catch up to 1 second of physics in one update call (running at less then 1fps is unplayable anyways)
47
41
  fullDelta = Math.min(1, fullDelta);
48
42
  const updatesPerSecond = options.updatesPerSecond ?? 60;
@@ -52,10 +46,10 @@ export class BvhCharacterPhysics {
52
46
  const partialDelta = Math.min(fullDelta, physicsDelta);
53
47
  fullDelta -= physicsDelta;
54
48
  //compute global position and inverted parent matrix so that we can compute the position in global space and re-assign it to the local chracter space
55
- if (this.character.parent != null) {
56
- this.character.parent.updateWorldMatrix(true, false);
57
- position.copy(this.character.position).applyMatrix4(this.character.parent.matrixWorld);
58
- invertedParentMatrix.copy(this.character.parent.matrixWorld).invert();
49
+ if (model.parent != null) {
50
+ model.parent.updateWorldMatrix(true, false);
51
+ position.copy(model.position).applyMatrix4(model.parent.matrixWorld);
52
+ invertedParentMatrix.copy(model.parent.matrixWorld).invert();
59
53
  }
60
54
  else {
61
55
  invertedParentMatrix.identity();
@@ -70,7 +64,7 @@ export class BvhCharacterPhysics {
70
64
  this.notGroundedSeconds = 0;
71
65
  }
72
66
  if (!isGrounded || this.inputVelocity.lengthSq() > 0) {
73
- this.character.position.copy(collisionFreePosition).applyMatrix4(invertedParentMatrix);
67
+ model.position.copy(collisionFreePosition).applyMatrix4(invertedParentMatrix);
74
68
  }
75
69
  //compute new velocity
76
70
  // apply gravity
@@ -101,9 +95,6 @@ export class BvhCharacterPhysics {
101
95
  this.aabbox.min.addScalar(-this.radius);
102
96
  this.aabbox.max.addScalar(this.radius);
103
97
  }
104
- dispose() {
105
- this.disposed = true;
106
- }
107
98
  shapecastCapsule(position, maxGroundSlope, options) {
108
99
  this.updateBoundingShapes(options);
109
100
  let grounded = false;
@@ -0,0 +1,2 @@
1
+ export declare const DefaultCrossFadeDuration = 0.1;
2
+ export declare const DefaultJumDelay = 0.2;
@@ -0,0 +1,2 @@
1
+ export const DefaultCrossFadeDuration = 0.1;
2
+ export const DefaultJumDelay = 0.2;
@@ -0,0 +1,101 @@
1
+ import { Group, Object3D, Object3DEventMap, AnimationAction } from 'three';
2
+ import { CharacterAnimationOptions } from '../animation/index.js';
3
+ import { CharacterCameraBehavior, SimpleCharacterCameraBehaviorOptions } from '../camera.js';
4
+ import { Input, ScreenJoystickInputOptions, LocomotionKeyboardInputOptions, PointerCaptureInputOptions, PointerLockInputOptions, InputSystem } from '../input/index.js';
5
+ import { CharacterModelOptions, CharacterModel } from '../model/index.js';
6
+ import { BvhCharacterPhysicsOptions, BvhCharacterPhysics, BvhPhysicsWorld } from '../physics/index.js';
7
+ export type SimpleCharacterState = {
8
+ camera: Object3D;
9
+ model?: CharacterModel;
10
+ physics: BvhCharacterPhysics;
11
+ inputSystem: InputSystem;
12
+ lastJump: number;
13
+ };
14
+ export type SimpleCharacterMovementOptions = {
15
+ /**
16
+ * @default true
17
+ */
18
+ jump?: {
19
+ /**
20
+ * @default 0.2
21
+ */
22
+ delay?: number;
23
+ /**
24
+ * @default 0.1
25
+ */
26
+ bufferTime?: number;
27
+ /**
28
+ * @default 8
29
+ */
30
+ speed?: number;
31
+ } | boolean;
32
+ /**
33
+ * @default true
34
+ */
35
+ walk?: {
36
+ speed?: number;
37
+ } | boolean;
38
+ /**
39
+ * @default true
40
+ */
41
+ run?: {
42
+ speed?: number;
43
+ } | boolean;
44
+ };
45
+ export type SimpleCharacterAnimationOptions = {
46
+ readonly walk?: CharacterAnimationOptions;
47
+ readonly run?: CharacterAnimationOptions;
48
+ readonly idle?: CharacterAnimationOptions;
49
+ readonly jumpUp?: CharacterAnimationOptions;
50
+ readonly jumpLoop?: CharacterAnimationOptions;
51
+ readonly jumpDown?: CharacterAnimationOptions;
52
+ readonly jumpForward?: CharacterAnimationOptions;
53
+ /**
54
+ * @default "movement"
55
+ */
56
+ yawRotationBasdOn?: 'camera' | 'movement';
57
+ /**
58
+ * @default 10
59
+ */
60
+ maxYawRotationSpeed?: number;
61
+ /**
62
+ * @default 0.1
63
+ */
64
+ crossFadeDuration?: number;
65
+ };
66
+ export type SimpleCharacterInputOptions = ScreenJoystickInputOptions & PointerCaptureInputOptions & PointerLockInputOptions & LocomotionKeyboardInputOptions;
67
+ export type SimpleCharacterOptions = {
68
+ readonly input?: ReadonlyArray<Input | {
69
+ new (domElement: HTMLElement): Input;
70
+ }>;
71
+ inputOptions?: SimpleCharacterInputOptions;
72
+ movement?: SimpleCharacterMovementOptions;
73
+ readonly model?: CharacterModelOptions | boolean;
74
+ physics?: BvhCharacterPhysicsOptions;
75
+ cameraBehavior?: SimpleCharacterCameraBehaviorOptions;
76
+ readonly animation?: SimpleCharacterAnimationOptions;
77
+ };
78
+ export declare class SimpleCharacter extends Group<Object3DEventMap & {
79
+ loaded: {};
80
+ }> implements SimpleCharacterState {
81
+ readonly camera: Object3D;
82
+ private readonly world;
83
+ private readonly options;
84
+ readonly cameraBehavior: CharacterCameraBehavior;
85
+ readonly physics: BvhCharacterPhysics;
86
+ readonly inputSystem: InputSystem;
87
+ readonly currentAnimationRef: {
88
+ current?: AnimationAction;
89
+ };
90
+ model?: CharacterModel;
91
+ private readonly updateTimeline;
92
+ private readonly graph;
93
+ private readonly abortController;
94
+ lastJump: number;
95
+ constructor(camera: Object3D, world: BvhPhysicsWorld, domElement: HTMLElement, options?: SimpleCharacterOptions);
96
+ private init;
97
+ update(delta: number): void;
98
+ dispose(): void;
99
+ }
100
+ export * from './update-input-velocity.js';
101
+ export * from './update-rotation.js';