@pmndrs/viverse 0.1.20 → 0.2.1

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 (67) hide show
  1. package/dist/animation/bone-map.d.ts +4 -0
  2. package/dist/animation/bone-map.js +11 -0
  3. package/dist/animation/default.d.ts +9 -0
  4. package/dist/animation/default.js +25 -0
  5. package/dist/animation/index.d.ts +12 -19
  6. package/dist/animation/index.js +76 -72
  7. package/dist/animation/mask.d.ts +6 -0
  8. package/dist/animation/mask.js +53 -0
  9. package/dist/camera.d.ts +7 -7
  10. package/dist/camera.js +33 -26
  11. package/dist/index.d.ts +3 -2
  12. package/dist/index.js +3 -2
  13. package/dist/input/action.d.ts +41 -0
  14. package/dist/input/action.js +97 -0
  15. package/dist/input/index.d.ts +12 -27
  16. package/dist/input/index.js +17 -69
  17. package/dist/input/keyboard.d.ts +29 -7
  18. package/dist/input/keyboard.js +84 -72
  19. package/dist/input/pointer-capture.d.ts +6 -11
  20. package/dist/input/pointer-capture.js +10 -30
  21. package/dist/input/pointer-lock.d.ts +6 -11
  22. package/dist/input/pointer-lock.js +8 -29
  23. package/dist/input/screen-joystick.d.ts +12 -11
  24. package/dist/input/screen-joystick.js +39 -49
  25. package/dist/input/screen-jump-button.d.ts +2 -4
  26. package/dist/input/screen-jump-button.js +7 -12
  27. package/dist/model/index.d.ts +9 -13
  28. package/dist/model/index.js +12 -34
  29. package/dist/physics/index.d.ts +2 -5
  30. package/dist/physics/index.js +7 -16
  31. package/dist/simple-character/apply-input-options.d.ts +2 -0
  32. package/dist/simple-character/apply-input-options.js +28 -0
  33. package/dist/simple-character/defaults.d.ts +2 -0
  34. package/dist/simple-character/defaults.js +2 -0
  35. package/dist/simple-character/index.d.ts +116 -0
  36. package/dist/simple-character/index.js +104 -0
  37. package/dist/simple-character/state/index.d.ts +6 -0
  38. package/dist/simple-character/state/index.js +6 -0
  39. package/dist/simple-character/state/jump-down.d.ts +3 -0
  40. package/dist/simple-character/state/jump-down.js +26 -0
  41. package/dist/simple-character/state/jump-forward.d.ts +5 -0
  42. package/dist/simple-character/state/jump-forward.js +41 -0
  43. package/dist/simple-character/state/jump-loop.d.ts +3 -0
  44. package/dist/simple-character/state/jump-loop.js +24 -0
  45. package/dist/simple-character/state/jump-start.d.ts +4 -0
  46. package/dist/simple-character/state/jump-start.js +30 -0
  47. package/dist/simple-character/state/jump-up.d.ts +5 -0
  48. package/dist/simple-character/state/jump-up.js +40 -0
  49. package/dist/simple-character/state/movement.d.ts +3 -0
  50. package/dist/simple-character/state/movement.js +63 -0
  51. package/dist/simple-character/update-input-velocity.d.ts +4 -0
  52. package/dist/simple-character/update-input-velocity.js +25 -0
  53. package/dist/simple-character/update-rotation.d.ts +6 -0
  54. package/dist/simple-character/update-rotation.js +40 -0
  55. package/dist/utils.d.ts +12 -5
  56. package/dist/utils.js +37 -39
  57. package/package.json +2 -2
  58. package/dist/animation/bvh.d.ts +0 -4
  59. package/dist/animation/bvh.js +0 -8
  60. package/dist/animation/fbx.d.ts +0 -4
  61. package/dist/animation/fbx.js +0 -8
  62. package/dist/animation/gltf.d.ts +0 -3
  63. package/dist/animation/gltf.js +0 -8
  64. package/dist/animation/vrma.d.ts +0 -3
  65. package/dist/animation/vrma.js +0 -8
  66. package/dist/simple-character.d.ts +0 -107
  67. package/dist/simple-character.js +0 -344
@@ -0,0 +1,6 @@
1
+ export * from './jump-down.js';
2
+ export * from './jump-loop.js';
3
+ export * from './jump-start.js';
4
+ export * from './jump-forward.js';
5
+ export * from './jump-up.js';
6
+ export * from './movement.js';
@@ -0,0 +1,6 @@
1
+ export * from './jump-down.js';
2
+ export * from './jump-loop.js';
3
+ export * from './jump-start.js';
4
+ export * from './jump-forward.js';
5
+ export * from './jump-up.js';
6
+ export * from './movement.js';
@@ -0,0 +1,3 @@
1
+ import { type GraphTimelineState } from '@pmndrs/timeline';
2
+ import type { SimpleCharacterState, SimpleCharacterOptions } from '../index.js';
3
+ export declare function loadSimpleCharacterJumpDownState<T>(state: SimpleCharacterState, options: SimpleCharacterOptions): Promise<GraphTimelineState<T>>;
@@ -0,0 +1,26 @@
1
+ import { action, timePassed } from '@pmndrs/timeline';
2
+ import { LoopOnce } from 'three';
3
+ import { JumpDownAnimationUrl } from '../../animation/default.js';
4
+ import { flattenCharacterAnimationOptions, loadCharacterAnimation } from '../../animation/index.js';
5
+ import { startAnimation } from '../../utils.js';
6
+ import { DefaultCrossFadeDuration } from '../defaults.js';
7
+ export async function loadSimpleCharacterJumpDownState(state, options) {
8
+ const model = state.model;
9
+ if (model == null) {
10
+ throw new Error(`Unable to load animation without existing model`);
11
+ }
12
+ const jumpDown = model.mixer.clipAction(await loadCharacterAnimation(model, ...flattenCharacterAnimationOptions({ url: JumpDownAnimationUrl, ...options.animation?.jumpDown })));
13
+ jumpDown.loop = LoopOnce;
14
+ jumpDown.clampWhenFinished = true;
15
+ return {
16
+ timeline: () => action({
17
+ init: () => {
18
+ startAnimation(jumpDown, model.currentAnimations, {
19
+ fadeDuration: options.animation?.crossFadeDuration ?? DefaultCrossFadeDuration,
20
+ });
21
+ },
22
+ until: timePassed(150, 'milliseconds'),
23
+ }),
24
+ transitionTo: { finally: 'moving' },
25
+ };
26
+ }
@@ -0,0 +1,5 @@
1
+ import { type GraphTimelineState } from '@pmndrs/timeline';
2
+ import { AnimationAction } from 'three';
3
+ import { SimpleCharacterState, SimpleCharacterOptions } from '../index.js';
4
+ export declare function loadSimpleCharacterJumpForwardAction(state: SimpleCharacterState, options: SimpleCharacterOptions): Promise<AnimationAction>;
5
+ export declare function loadSimpleCharacterJumpForwardState<T>(jumpForward: AnimationAction, state: SimpleCharacterState, options: SimpleCharacterOptions): Promise<GraphTimelineState<T>>;
@@ -0,0 +1,41 @@
1
+ import { action, animationFinished } from '@pmndrs/timeline';
2
+ import { LoopOnce, Vector3 } from 'three';
3
+ import { JumpForwardAnimationUrl } from '../../animation/default.js';
4
+ import { flattenCharacterAnimationOptions, loadCharacterAnimation } from '../../animation/index.js';
5
+ import { startAnimation } from '../../utils.js';
6
+ import { DefaultCrossFadeDuration } from '../defaults.js';
7
+ export async function loadSimpleCharacterJumpForwardAction(state, options) {
8
+ const model = state.model;
9
+ if (model == null) {
10
+ throw new Error(`Unable to load animation without existing model`);
11
+ }
12
+ const jumpForward = model.mixer.clipAction(await loadCharacterAnimation(model, ...flattenCharacterAnimationOptions({
13
+ url: JumpForwardAnimationUrl,
14
+ scaleTime: 0.9,
15
+ ...options.animation?.jumpForward,
16
+ })));
17
+ jumpForward.loop = LoopOnce;
18
+ jumpForward.clampWhenFinished = true;
19
+ return jumpForward;
20
+ }
21
+ export async function loadSimpleCharacterJumpForwardState(jumpForward, state, options) {
22
+ const model = state.model;
23
+ if (model == null) {
24
+ throw new Error(`Unable to load animation state without existing model`);
25
+ }
26
+ return {
27
+ timeline: () => action({
28
+ init: () => {
29
+ startAnimation(jumpForward, model.currentAnimations, {
30
+ fadeDuration: options.animation?.crossFadeDuration ?? DefaultCrossFadeDuration,
31
+ });
32
+ state.lastJump = performance.now() / 1000;
33
+ state.physics.applyVelocity(new Vector3(0, (typeof options.movement?.jump === 'object' ? options.movement?.jump.speed : undefined) ?? 8, 0));
34
+ },
35
+ until: animationFinished(jumpForward),
36
+ }),
37
+ transitionTo: {
38
+ finally: () => (state.physics.isGrounded ? 'moving' : 'jumpLoop'),
39
+ },
40
+ };
41
+ }
@@ -0,0 +1,3 @@
1
+ import { type GraphTimelineState } from '@pmndrs/timeline';
2
+ import type { SimpleCharacterOptions, SimpleCharacterState } from '../index.js';
3
+ export declare function loadSimpleCharacterJumpLoopState<T>(state: SimpleCharacterState, options: SimpleCharacterOptions): Promise<GraphTimelineState<T>>;
@@ -0,0 +1,24 @@
1
+ import { action } from '@pmndrs/timeline';
2
+ import { JumpLoopAnimationUrl } from '../../animation/default.js';
3
+ import { flattenCharacterAnimationOptions, loadCharacterAnimation } from '../../animation/index.js';
4
+ import { startAnimation } from '../../utils.js';
5
+ import { DefaultCrossFadeDuration } from '../defaults.js';
6
+ export async function loadSimpleCharacterJumpLoopState(state, options) {
7
+ const model = state.model;
8
+ if (model == null) {
9
+ throw new Error(`Unable to load animation without existing model`);
10
+ }
11
+ const jumpLoop = model.mixer.clipAction(await loadCharacterAnimation(model, ...flattenCharacterAnimationOptions({ url: JumpLoopAnimationUrl, ...options.animation?.jumpLoop })));
12
+ return {
13
+ timeline: () => action({
14
+ init: () => {
15
+ startAnimation(jumpLoop, model.currentAnimations, {
16
+ fadeDuration: options.animation?.crossFadeDuration ?? DefaultCrossFadeDuration,
17
+ });
18
+ },
19
+ }),
20
+ transitionTo: {
21
+ jumpDown: { whenUpdate: () => state.physics.isGrounded },
22
+ },
23
+ };
24
+ }
@@ -0,0 +1,4 @@
1
+ import { GraphTimelineState } from '@pmndrs/timeline';
2
+ import { AnimationAction } from 'three';
3
+ import type { SimpleCharacterOptions, SimpleCharacterState } from '../index.js';
4
+ export declare function loadSimpleCharacterJumpStartState<T>(jumpUp: AnimationAction, jumpForward: AnimationAction, state: SimpleCharacterState, options: SimpleCharacterOptions): Promise<GraphTimelineState<T>>;
@@ -0,0 +1,30 @@
1
+ import { action, timePassed } from '@pmndrs/timeline';
2
+ import { RunAction } from '../../input/index.js';
3
+ import { startAnimation } from '../../utils.js';
4
+ import { DefaultCrossFadeDuration, DefaultJumDelay } from '../defaults.js';
5
+ export async function loadSimpleCharacterJumpStartState(jumpUp, jumpForward, state, options) {
6
+ const model = state.model;
7
+ if (model == null) {
8
+ throw new Error(`Unable to load animation state without existing model`);
9
+ }
10
+ return {
11
+ timeline: () => action({
12
+ init() {
13
+ startAnimation(jumpUp, model.currentAnimations, {
14
+ fadeDuration: options.animation?.crossFadeDuration ?? DefaultCrossFadeDuration,
15
+ paused: true,
16
+ });
17
+ startAnimation(jumpForward, model.currentAnimations, {
18
+ fadeDuration: options.animation?.crossFadeDuration ?? DefaultCrossFadeDuration,
19
+ paused: true,
20
+ });
21
+ },
22
+ update: () => void state.physics.inputVelocity.multiplyScalar(0.3),
23
+ until: timePassed((typeof options.movement?.jump === 'object' ? options.movement?.jump.delay : undefined) ?? DefaultJumDelay, 'seconds'),
24
+ }),
25
+ transitionTo: {
26
+ jumpDown: { whenUpdate: () => !state.physics.isGrounded },
27
+ finally: () => (RunAction.get() ? 'jumpForward' : 'jumpUp'),
28
+ },
29
+ };
30
+ }
@@ -0,0 +1,5 @@
1
+ import { GraphTimelineState } from '@pmndrs/timeline';
2
+ import { AnimationAction } from 'three';
3
+ import { SimpleCharacterState, SimpleCharacterOptions } from '../index.js';
4
+ export declare function loadSimpleCharacterJumpUpAction(state: SimpleCharacterState, options: SimpleCharacterOptions): Promise<AnimationAction>;
5
+ export declare function loadSimpleCharacterJumpUpState<T>(jumpUp: AnimationAction, state: SimpleCharacterState, options: SimpleCharacterOptions): Promise<GraphTimelineState<T>>;
@@ -0,0 +1,40 @@
1
+ import { action, animationFinished } from '@pmndrs/timeline';
2
+ import { LoopOnce, Vector3 } from 'three';
3
+ import { JumpUpAnimationUrl } from '../../animation/default.js';
4
+ import { flattenCharacterAnimationOptions, loadCharacterAnimation } from '../../animation/index.js';
5
+ import { startAnimation } from '../../utils.js';
6
+ export async function loadSimpleCharacterJumpUpAction(state, options) {
7
+ const model = state.model;
8
+ if (model == null) {
9
+ throw new Error(`Unable to load animation without existing model`);
10
+ }
11
+ const jumpUp = model.mixer.clipAction(await loadCharacterAnimation(model, ...flattenCharacterAnimationOptions({
12
+ url: JumpUpAnimationUrl,
13
+ ...options.animation?.jumpUp,
14
+ })));
15
+ jumpUp.loop = LoopOnce;
16
+ jumpUp.clampWhenFinished = true;
17
+ return jumpUp;
18
+ }
19
+ export async function loadSimpleCharacterJumpUpState(jumpUp, state, options) {
20
+ const model = state.model;
21
+ if (model == null) {
22
+ throw new Error(`Unable to load animation state without existing model`);
23
+ }
24
+ return {
25
+ timeline: () => action({
26
+ init: () => {
27
+ startAnimation(jumpUp, model.currentAnimations, { fadeDuration: 0.1 });
28
+ state.lastJump = performance.now() / 1000;
29
+ state.physics.applyVelocity(new Vector3(0, (typeof options.movement?.jump === 'object' ? options.movement?.jump.speed : undefined) ?? 8, 0));
30
+ },
31
+ until: animationFinished(jumpUp),
32
+ }),
33
+ transitionTo: {
34
+ jumpDown: {
35
+ whenUpdate: (_, _clock, actionTime) => actionTime > 0.3 && state.physics.isGrounded,
36
+ },
37
+ finally: 'jumpLoop',
38
+ },
39
+ };
40
+ }
@@ -0,0 +1,3 @@
1
+ import { type GraphTimelineState } from '@pmndrs/timeline';
2
+ import type { SimpleCharacterOptions, SimpleCharacterState } from '../index.js';
3
+ export declare function loadSimpleCharacterMovingState<T>(state: SimpleCharacterState, options: SimpleCharacterOptions): Promise<GraphTimelineState<T>>;
@@ -0,0 +1,63 @@
1
+ import { action } from '@pmndrs/timeline';
2
+ import { IdleAnimationUrl, RunAnimationUrl, WalkAnimationUrl } from '../../animation/default.js';
3
+ import { flattenCharacterAnimationOptions, loadCharacterAnimation } from '../../animation/index.js';
4
+ import { RunAction } from '../../input/index.js';
5
+ import { shouldJump, startAnimation } from '../../utils.js';
6
+ import { DefaultCrossFadeDuration } from '../defaults.js';
7
+ export async function loadSimpleCharacterMovingState(state, options) {
8
+ const model = state.model;
9
+ if (model == null) {
10
+ throw new Error(`Unable to load animation without existing model`);
11
+ }
12
+ const idle = model.mixer.clipAction(await loadCharacterAnimation(model, ...flattenCharacterAnimationOptions({
13
+ url: IdleAnimationUrl,
14
+ ...options.animation?.idle,
15
+ })));
16
+ const run = model.mixer.clipAction(await loadCharacterAnimation(model, ...flattenCharacterAnimationOptions({
17
+ url: RunAnimationUrl,
18
+ scaleTime: 0.8,
19
+ ...options.animation?.run,
20
+ })));
21
+ const walk = model.mixer.clipAction(await loadCharacterAnimation(model, ...flattenCharacterAnimationOptions({
22
+ scaleTime: 0.5,
23
+ url: WalkAnimationUrl,
24
+ ...options.animation?.walk,
25
+ })));
26
+ return {
27
+ timeline: () => {
28
+ let currentAnimation;
29
+ return action({
30
+ update: () => {
31
+ let nextAnimation;
32
+ if (state.physics.inputVelocity.lengthSq() === 0) {
33
+ nextAnimation = idle;
34
+ }
35
+ else if (RunAction.get() && options.movement?.run != false) {
36
+ nextAnimation = run;
37
+ }
38
+ else if (options.movement?.walk != false) {
39
+ nextAnimation = walk;
40
+ }
41
+ else {
42
+ nextAnimation = idle;
43
+ }
44
+ if (nextAnimation === currentAnimation) {
45
+ return;
46
+ }
47
+ currentAnimation?.fadeOut(options.animation?.crossFadeDuration ?? DefaultCrossFadeDuration);
48
+ startAnimation(nextAnimation, model.currentAnimations, {
49
+ fadeDuration: options.animation?.crossFadeDuration ?? DefaultCrossFadeDuration,
50
+ });
51
+ currentAnimation = nextAnimation;
52
+ },
53
+ });
54
+ },
55
+ transitionTo: {
56
+ jumpStart: {
57
+ whenUpdate: () => options.movement?.jump !== false &&
58
+ shouldJump(state.physics, state.lastJump, options.movement?.jump === true ? undefined : options.movement?.jump?.bufferTime),
59
+ },
60
+ jumpLoop: { whenUpdate: () => !state.physics.isGrounded },
61
+ },
62
+ };
63
+ }
@@ -0,0 +1,4 @@
1
+ import { Object3D } from 'three';
2
+ import type { SimpleCharacterMovementOptions } from './index.js';
3
+ import type { BvhCharacterPhysics } from '../physics/index.js';
4
+ export declare function updateSimpleCharacterInputVelocity(camera: Object3D, physics: BvhCharacterPhysics, options?: SimpleCharacterMovementOptions): void;
@@ -0,0 +1,25 @@
1
+ import { Euler, Quaternion } from 'three';
2
+ import { RunAction, MoveLeftAction, MoveRightAction, MoveForwardAction, MoveBackwardAction } from '../input/index.js';
3
+ const cameraEuler = new Euler();
4
+ const cameraRotation = new Quaternion();
5
+ export function updateSimpleCharacterInputVelocity(camera, physics, options) {
6
+ cameraEuler.setFromQuaternion(camera.getWorldQuaternion(cameraRotation), 'YXZ');
7
+ cameraEuler.x = 0;
8
+ cameraEuler.z = 0;
9
+ let inputSpeed = 0;
10
+ let runOptions = options?.run ?? true;
11
+ if (RunAction.get() && runOptions !== false) {
12
+ runOptions = runOptions === true ? {} : runOptions;
13
+ inputSpeed = runOptions.speed ?? 6;
14
+ }
15
+ let walkOptions = options?.walk ?? true;
16
+ if (inputSpeed === 0 && walkOptions !== false) {
17
+ walkOptions = walkOptions === true ? {} : walkOptions;
18
+ inputSpeed = walkOptions.speed ?? 3;
19
+ }
20
+ physics.inputVelocity
21
+ .set(-MoveLeftAction.get() + MoveRightAction.get(), 0, -MoveForwardAction.get() + MoveBackwardAction.get())
22
+ .normalize()
23
+ .applyEuler(cameraEuler)
24
+ .multiplyScalar(inputSpeed);
25
+ }
@@ -0,0 +1,6 @@
1
+ import { Object3D } from 'three';
2
+ import { BvhCharacterPhysics } from '../physics/index.js';
3
+ import type { SimpleCharacterAnimationOptions } from './index.js';
4
+ export declare function updateSimpleCharacterRotation(delta: number, physics: BvhCharacterPhysics, camera: Object3D, model: {
5
+ scene: Object3D;
6
+ }, options?: SimpleCharacterAnimationOptions): true | undefined;
@@ -0,0 +1,40 @@
1
+ import { Euler, Quaternion, Vector3 } from 'three';
2
+ const NegZAxis = new Vector3(0, 0, -1);
3
+ const _2MathPI = 2 * Math.PI;
4
+ const characterTargetEuler = new Euler();
5
+ const goalTargetEuler = new Euler();
6
+ const inputDirection = new Vector3();
7
+ const quaternion = new Quaternion();
8
+ export function updateSimpleCharacterRotation(delta, physics, camera, model, options) {
9
+ // Character yaw rotation logic
10
+ const basedOn = options?.yawRotationBasdOn ?? 'movement';
11
+ // compute goalTargetEuler
12
+ if (basedOn === 'camera') {
13
+ goalTargetEuler.setFromQuaternion(camera.getWorldQuaternion(quaternion), 'YXZ');
14
+ }
15
+ else {
16
+ //don't rotate if not moving
17
+ if (physics.inputVelocity.lengthSq() === 0) {
18
+ // run forever
19
+ return true;
20
+ }
21
+ inputDirection.copy(physics.inputVelocity).normalize();
22
+ quaternion.setFromUnitVectors(NegZAxis, inputDirection);
23
+ goalTargetEuler.setFromQuaternion(quaternion, 'YXZ');
24
+ }
25
+ // compute currentTargetEuler
26
+ model.scene.getWorldQuaternion(quaternion);
27
+ characterTargetEuler.setFromQuaternion(quaternion, 'YXZ');
28
+ // apply delta yaw rotation
29
+ let deltaYaw = (goalTargetEuler.y - characterTargetEuler.y + _2MathPI) % _2MathPI;
30
+ if (deltaYaw > Math.PI) {
31
+ deltaYaw = deltaYaw - _2MathPI;
32
+ }
33
+ const absDeltaYaw = Math.abs(deltaYaw);
34
+ if (absDeltaYaw < 0.001) {
35
+ // run forever
36
+ return true;
37
+ }
38
+ const yawRotationDirection = deltaYaw / absDeltaYaw;
39
+ model.scene.rotation.y += Math.min((options?.maxYawRotationSpeed ?? 10) * delta, absDeltaYaw) * yawRotationDirection;
40
+ }
package/dist/utils.d.ts CHANGED
@@ -1,7 +1,14 @@
1
- export declare function cached<D extends ReadonlyArray<unknown>, T>(fn: (...deps: D) => Promise<T>, dependencies: D): Promise<T>;
2
- export declare function clearCache(fn: Function, dependencies: Array<unknown>): void;
3
- export declare function extractProxy<K extends string, T extends {
4
- [key in K]?: {};
5
- }>(value: T, key: K): Partial<Exclude<T[K], undefined>>;
1
+ import type { CharacterModel } from './model/index.js';
2
+ import type { BvhCharacterPhysics } from './physics/index.js';
3
+ import type { AnimationAction } from 'three';
6
4
  export declare function getIsMobileMediaQuery(): MediaQueryList | undefined;
7
5
  export declare function isMobile(): boolean;
6
+ export type StartAnimationOptions = {
7
+ fadeDuration?: number;
8
+ sync?: boolean;
9
+ paused?: boolean;
10
+ crossFade?: boolean;
11
+ layer?: string;
12
+ };
13
+ export declare function startAnimation(animation: AnimationAction, currentAnimations: CharacterModel['currentAnimations'], { crossFade, layer, fadeDuration, paused, sync }: StartAnimationOptions): (() => void) | undefined;
14
+ export declare function shouldJump(physics: BvhCharacterPhysics, lastJump: number, bufferTime?: number): boolean;
package/dist/utils.js CHANGED
@@ -1,42 +1,4 @@
1
- function shallowEqual(a, b) {
2
- if (a.length !== b.length)
3
- return false;
4
- for (let i = 0; i < a.length; i++) {
5
- if (a[i] !== b[i])
6
- return false;
7
- }
8
- return true;
9
- }
10
- const cacheMap = new Map();
11
- export function cached(fn, dependencies) {
12
- let cache = cacheMap.get(fn);
13
- if (cache == null) {
14
- cacheMap.set(fn, (cache = []));
15
- }
16
- const entry = cache.find(({ deps }) => shallowEqual(deps, dependencies));
17
- if (entry != null) {
18
- return entry.result;
19
- }
20
- const result = fn(...dependencies);
21
- cache.push({ deps: dependencies, result });
22
- return result;
23
- }
24
- export function clearCache(fn, dependencies) {
25
- const cache = cacheMap.get(fn);
26
- if (cache == null) {
27
- return;
28
- }
29
- const index = cache.findIndex(({ deps }) => shallowEqual(deps, dependencies));
30
- if (index === -1) {
31
- return;
32
- }
33
- cache.splice(index, 1);
34
- }
35
- export function extractProxy(value, key) {
36
- return new Proxy({}, {
37
- get: (_, p) => value[key]?.[p],
38
- });
39
- }
1
+ import { JumpAction } from './input/index.js';
40
2
  export function getIsMobileMediaQuery() {
41
3
  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
42
4
  return undefined;
@@ -46,3 +8,39 @@ export function getIsMobileMediaQuery() {
46
8
  export function isMobile() {
47
9
  return getIsMobileMediaQuery()?.matches ?? false;
48
10
  }
11
+ export function startAnimation(animation, currentAnimations, { crossFade = true, layer, fadeDuration = 0.1, paused = false, sync = false }) {
12
+ animation.reset();
13
+ animation.play();
14
+ animation.paused = paused;
15
+ if (!crossFade) {
16
+ animation.fadeIn(fadeDuration);
17
+ return () => {
18
+ animation.fadeOut(fadeDuration);
19
+ };
20
+ }
21
+ const currentAnimation = currentAnimations.get(layer);
22
+ if (currentAnimation != null && sync) {
23
+ animation.syncWith(currentAnimation);
24
+ }
25
+ if (currentAnimation != null) {
26
+ animation.crossFadeFrom(currentAnimation, fadeDuration);
27
+ }
28
+ else {
29
+ animation.fadeIn(fadeDuration);
30
+ }
31
+ currentAnimations.set(layer, animation);
32
+ }
33
+ export function shouldJump(physics, lastJump, bufferTime = 0.1) {
34
+ if (!physics.isGrounded) {
35
+ return false;
36
+ }
37
+ const lastTimePressed = JumpAction.getLatestTime();
38
+ if (lastTimePressed == null || lastJump > lastTimePressed) {
39
+ return false;
40
+ }
41
+ //last jump must be more then 0.3 second ago, if not, we dont jump, this is to give the character time to get off the ground
42
+ if (lastJump > performance.now() / 1000 - 0.3) {
43
+ return false;
44
+ }
45
+ return performance.now() / 1000 - lastTimePressed < bufferTime;
46
+ }
package/package.json CHANGED
@@ -21,12 +21,12 @@
21
21
  "peerDependencies": {
22
22
  "three": "*"
23
23
  },
24
- "version": "0.1.20",
24
+ "version": "0.2.1",
25
25
  "type": "module",
26
26
  "dependencies": {
27
27
  "@pixiv/three-vrm": "^3.4.2",
28
28
  "@pixiv/three-vrm-animation": "^3.4.2",
29
- "@pmndrs/timeline": "^0.2.6",
29
+ "@pmndrs/timeline": "^0.3.7",
30
30
  "@viverse/sdk": "1.2.10-alpha.0",
31
31
  "three-mesh-bvh": "^0.9.1"
32
32
  },
@@ -1,4 +0,0 @@
1
- import { AnimationClip } from 'three';
2
- import { loadCharacterModel } from '../model/index.js';
3
- import type { VRMHumanBoneName } from '@pixiv/three-vrm';
4
- export declare function loadVrmModelBvhAnimations(model: Exclude<Awaited<ReturnType<typeof loadCharacterModel>>, undefined>, url: string, removeXZMovement: boolean, boneMap?: Record<string, VRMHumanBoneName>): Promise<Array<AnimationClip>>;
@@ -1,8 +0,0 @@
1
- import { BVHLoader } from 'three/examples/jsm/Addons.js';
2
- import { bvhBoneMap, fixModelAnimationClip } from './index.js';
3
- const loader = new BVHLoader();
4
- export async function loadVrmModelBvhAnimations(model, url, removeXZMovement, boneMap) {
5
- const clipScene = await loader.loadAsync(url);
6
- fixModelAnimationClip(model, clipScene.clip, undefined, removeXZMovement, boneMap ?? bvhBoneMap);
7
- return [clipScene.clip];
8
- }
@@ -1,4 +0,0 @@
1
- import { AnimationClip } from 'three';
2
- import { loadCharacterModel } from '../model/index.js';
3
- import type { VRMHumanBoneName } from '@pixiv/three-vrm';
4
- export declare function loadVrmModelFbxAnimations(model: Exclude<Awaited<ReturnType<typeof loadCharacterModel>>, undefined>, url: string, removeXZMovement: boolean, boneMap?: Record<string, VRMHumanBoneName>): Promise<Array<AnimationClip>>;
@@ -1,8 +0,0 @@
1
- import { FBXLoader } from 'three/examples/jsm/Addons.js';
2
- import { fixModelAnimationClip } from './index.js';
3
- const loader = new FBXLoader();
4
- export async function loadVrmModelFbxAnimations(model, url, removeXZMovement, boneMap) {
5
- const clipScene = await loader.loadAsync(url);
6
- clipScene.animations.forEach((clip) => fixModelAnimationClip(model, clip, clipScene, removeXZMovement, boneMap));
7
- return clipScene.animations;
8
- }
@@ -1,3 +0,0 @@
1
- import { AnimationClip } from 'three';
2
- import { loadCharacterModel, VRMHumanBoneName } from '../model/index.js';
3
- export declare function loadVrmModelGltfAnimations(model: Exclude<Awaited<ReturnType<typeof loadCharacterModel>>, undefined>, url: string, removeXZMovement: boolean, boneMap?: Record<string, VRMHumanBoneName>): Promise<Array<AnimationClip>>;
@@ -1,8 +0,0 @@
1
- import { GLTFLoader } from 'three/examples/jsm/Addons.js';
2
- import { fixModelAnimationClip } from './index.js';
3
- const loader = new GLTFLoader();
4
- export async function loadVrmModelGltfAnimations(model, url, removeXZMovement, boneMap) {
5
- const { animations, scene: clipScene } = await loader.loadAsync(url);
6
- animations.forEach((clip) => fixModelAnimationClip(model, clip, clipScene, removeXZMovement, boneMap));
7
- return animations;
8
- }
@@ -1,3 +0,0 @@
1
- import { VRM } from '@pixiv/three-vrm';
2
- import { AnimationClip } from 'three';
3
- export declare function loadVrmModelVrmaAnimations(vrm: VRM, url: string, removeXZMovement: boolean): Promise<Array<AnimationClip>>;
@@ -1,8 +0,0 @@
1
- import { createVRMAnimationClip } from '@pixiv/three-vrm-animation';
2
- import { vrmaLoader } from '../index.js';
3
- export async function loadVrmModelVrmaAnimations(vrm, url, removeXZMovement) {
4
- const animations = await vrmaLoader.loadAsync(url);
5
- const vrmAnimations = animations.userData.vrmAnimations;
6
- const clips = vrmAnimations.map((vrmAnimation) => createVRMAnimationClip(vrmAnimation, vrm));
7
- return clips;
8
- }