Skip to content

Commit 390650f

Browse files
committed
docs: use elapsedTime; fix skydiver animation
1 parent 08c8491 commit 390650f

File tree

11 files changed

+53
-43
lines changed

11 files changed

+53
-43
lines changed

apps/examples/src/app/cannon/kinematic-cube/scene.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Box {
6161
injectBeforeRender(({ clock }) => {
6262
const api = boxApi();
6363
if (!api) return;
64-
const t = clock.getElapsedTime();
64+
const t = clock.elapsedTime;
6565
api.position.set(Math.sin(t * 2) * 5, Math.cos(t * 2) * 5, 3);
6666
api.rotation.set(Math.sin(t * 6), Math.cos(t * 6), 0);
6767
});
@@ -71,12 +71,7 @@ export class Box {
7171
@Component({
7272
selector: 'app-instanced-spheres',
7373
template: `
74-
<ngt-instanced-mesh
75-
#instancedMesh
76-
castShadow
77-
receiveShadow
78-
*args="[undefined, undefined, count()]"
79-
>
74+
<ngt-instanced-mesh #instancedMesh castShadow receiveShadow *args="[undefined, undefined, count()]">
8075
<ngt-sphere-geometry *args="[1, 16, 16]">
8176
<ngt-instanced-buffer-attribute attach="attributes.color" *args="[colors(), 3]" />
8277
</ngt-sphere-geometry>

apps/examples/src/app/cannon/monday-morning/scene.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ export class RagDoll {
241241
injectBeforeRender(({ clock }) => {
242242
const [eyes, mouth] = [this.eyes(), this.mouth()];
243243
if (eyes && mouth) {
244-
eyes.nativeElement.position.y = Math.sin(clock.getElapsedTime() * 1) * 0.06;
245-
mouth.meshRef().nativeElement.scale.y = (1 + Math.sin(clock.getElapsedTime())) * 0.6;
244+
eyes.nativeElement.position.y = Math.sin(clock.elapsedTime * 1) * 0.06;
245+
mouth.meshRef().nativeElement.scale.y = (1 + Math.sin(clock.elapsedTime)) * 0.6;
246246
}
247247
});
248248
}

apps/examples/src/app/rapier/wrapper-default.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class Floor {}
4848
4949
<router-outlet #o="outlet" />
5050
51-
@if (o.activatedRoute.snapshot.url[0]?.path !== 'basic') {
51+
@if (o.activatedRoute.snapshot.url[0].path !== 'basic') {
5252
<app-floor />
5353
}
5454
</ng-template>

apps/examples/src/app/soba/instanced-vertex-colors/scene.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class Boxes {
5555
const instanced = this.meshRef()?.nativeElement;
5656
if (!instanced) return;
5757

58-
const time = clock.getElapsedTime();
58+
const time = clock.elapsedTime;
5959
instanced.rotation.x = Math.sin(time / 4);
6060
instanced.rotation.y = Math.sin(time / 2);
6161

apps/examples/src/app/soba/skydiving/skydiver.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
} from '@angular/core';
99
import { injectBeforeRender, NgtArgs } from 'angular-three';
1010
import { injectGLTF, injectTexture } from 'angular-three-soba/loaders';
11-
import { injectAnimations } from 'angular-three-soba/misc';
11+
import { injectAnimations, NgtsAnimationClips } from 'angular-three-soba/misc';
1212
import {
13-
Bone,
1413
BufferGeometry,
1514
DoubleSide,
15+
Group,
1616
Mesh,
1717
MeshStandardMaterial,
1818
ShaderMaterial,
@@ -27,35 +27,36 @@ type SkydiverGLTF = GLTF & {
2727
mixamorigHips: Mesh;
2828
};
2929
materials: {};
30+
animations: NgtsAnimationClips<'animation_0'>;
3031
};
3132

3233
@Component({
3334
selector: 'app-skydiver',
3435
template: `
3536
<ngt-group [dispose]="null">
3637
@if (gltf(); as gltf) {
37-
<ngt-group>
38+
<ngt-group #group>
3839
<ngt-primitive #bone *args="[gltf.nodes.mixamorigHips]" />
3940
<ngt-skinned-mesh
4041
[geometry]="gltf.nodes.skydiver_2.geometry"
4142
[skeleton]="gltf.nodes.skydiver_2.skeleton"
4243
>
43-
@if (textures(); as textures) {
44-
<ngt-mesh-standard-material
45-
[parameters]="{
46-
uniforms: { uTime: { value: 0 }, uClothes: { value: textures.clothes } },
47-
onBeforeCompile,
48-
toneMapped: false,
49-
envMapIntensity: 0.8,
50-
metalnessMap: textures.metallic,
51-
normalMap: textures.normal,
52-
roughnessMap: textures.roughness,
53-
map: textures.baseColor,
54-
normalScale: [-0.2, 0.2],
55-
side: DoubleSide,
56-
}"
57-
/>
58-
}
44+
@let metallic = textures()?.metallic;
45+
@let normal = textures()?.normal;
46+
@let roughness = textures()?.roughness;
47+
@let baseColor = textures()?.baseColor;
48+
49+
<ngt-mesh-standard-material
50+
[toneMapped]="false"
51+
[envMapIntensity]="0.8"
52+
[metalnessMap]="metallic"
53+
[normalMap]="normal"
54+
[roughnessMap]="roughness"
55+
[map]="baseColor"
56+
[normalScale]="[-0.2, 0.2]"
57+
[side]="DoubleSide"
58+
[parameters]="{ onBeforeCompile }"
59+
/>
5960
</ngt-skinned-mesh>
6061
</ngt-group>
6162
}
@@ -87,14 +88,15 @@ export class Skydiver {
8788
},
8889
);
8990

90-
private boneRef = viewChild<ElementRef<Bone>>('bone');
91-
private animations = injectAnimations(this.gltf, this.boneRef);
91+
private groupRef = viewChild<ElementRef<Group>>('group');
92+
private animations = injectAnimations(this.gltf, this.groupRef);
9293

9394
protected onBeforeCompile: MeshStandardMaterial['onBeforeCompile'] = (shader) => {
9495
const gltf = this.gltf();
96+
if (!gltf) return;
9597

9698
Object.assign(shader.uniforms, {
97-
...(gltf?.nodes['skydiver_2'].material?.uniforms || {}),
99+
...gltf.nodes['skydiver_2'].material.uniforms,
98100
});
99101

100102
shader.vertexShader = /* language=glsl glsl */ `
@@ -116,17 +118,30 @@ export class Skydiver {
116118
};
117119

118120
constructor() {
121+
effect(() => {
122+
const gltf = this.gltf();
123+
if (!gltf) return;
124+
125+
const textures = this.textures();
126+
if (!textures) return;
127+
128+
gltf.nodes['skydiver_2'].material.uniforms = {
129+
uTime: { value: 0 },
130+
uClothes: { value: textures.clothes },
131+
};
132+
});
133+
119134
effect((onCleanup) => {
120135
if (!this.animations.isReady) return;
121136
const { actions } = this.animations;
122-
actions['animation_0']?.reset().play();
123-
onCleanup(() => actions['animation_0']?.stop());
137+
actions['animation_0'].reset().play();
138+
onCleanup(() => actions['animation_0'].stop());
124139
});
125140

126141
injectBeforeRender(({ clock }) => {
127142
const skydiver = this.gltf()?.nodes['skydiver_2'];
128143
if (skydiver?.material.uniforms?.['uTime']) {
129-
skydiver.material.uniforms['uTime'].value = clock.getElapsedTime();
144+
skydiver.material.uniforms['uTime'].value = clock.elapsedTime;
130145
}
131146
});
132147
}

libs/soba/src/abstractions/helper.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CameraHelperStory {
2929
injectBeforeRender(({ clock }) => {
3030
const camera = this.perspectiveCamera().cameraRef().nativeElement;
3131

32-
const t = clock.getElapsedTime();
32+
const t = clock.elapsedTime;
3333

3434
camera.lookAt(0, 0, 0);
3535
camera.position.x = Math.sin(t) * 4;

libs/soba/src/materials/custom-shader-material.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class DefaultCustomShaderMaterialStory {
8484
constructor() {
8585
injectBeforeRender(({ clock }) => {
8686
const material = this.materialRef().material();
87-
material.uniforms['uTime'].value = clock.getElapsedTime();
87+
material.uniforms['uTime'].value = clock.elapsedTime;
8888
});
8989
}
9090
}

libs/soba/src/materials/mesh-reflector-material.stories.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class MeshReflectorMaterialStory {
8888

8989
injectBeforeRender(({ clock }) => {
9090
const mesh = this.mesh().nativeElement;
91-
mesh.position.y += Math.sin(clock.getElapsedTime()) / 25;
92-
mesh.rotation.y = clock.getElapsedTime() / 2;
91+
mesh.position.y += Math.sin(clock.elapsedTime) / 25;
92+
mesh.rotation.y = clock.elapsedTime / 2;
9393
});
9494
}
9595
}

libs/soba/src/performances/instances.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Shoe {
8080

8181
injectBeforeRender(({ clock }) => {
8282
const instance = this.instance().positionMeshRef().nativeElement;
83-
const t = clock.getElapsedTime() + this.random() * 10000;
83+
const t = clock.elapsedTime + this.random() * 10000;
8484

8585
instance.rotation.set(Math.cos(t / 4) / 2, Math.sin(t / 4) / 2, Math.cos(t / 1.5) / 2);
8686
instance.position.y = Math.sin(t / 1.5) / 2;

libs/soba/src/performances/points.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class BasicPointsBufferStory {
153153

154154
constructor() {
155155
injectBeforeRender(({ clock }) => {
156-
const elapsedTime = clock.getElapsedTime();
156+
const elapsedTime = clock.elapsedTime;
157157
const t = misc.remap(Math.sin(elapsedTime), [-1, 1], [0, 1]);
158158

159159
buffer.rotate(this.color, { q: q.setFromAxisAngle(rotationAxis, t * 0.01) });

libs/soba/src/staging/contact-shadows.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ContactShadowsStory {
4747

4848
constructor() {
4949
injectBeforeRender(({ clock }) => {
50-
this.sphere().nativeElement.position.y = Math.sin(clock.getElapsedTime()) + 2;
50+
this.sphere().nativeElement.position.y = Math.sin(clock.elapsedTime) + 2;
5151
});
5252
}
5353
}

0 commit comments

Comments
 (0)