Skip to content

Commit

Permalink
freeze frame bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
jerzakm committed Feb 14, 2024
1 parent 74e697e commit db54522
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-berries-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@threejs-kit/instanced-sprite-mesh": patch
---

freeze frame bugfix
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,24 @@
$: {
if ($textureStore && mesh.material && !initialized && mesh) {
mesh.castShadow = true;
for (let i = 0; i < count; i++) {
// mesh.playmode.setAt(i, 'PAUSE');
// mesh.frame.setAt(i, 28);
mesh.frame.setAt(i, 7, 'death');
}
initialized = true;
}
}
window.addEventListener('keydown', () => {
mesh.frame.unsetAll();
for (let i = 0; i < count; i++) {
// mesh.frame.unsetAt(i);
}
});
let dirtyInstanceMatrix = false;
const tempMatrix = new Matrix4();
Expand All @@ -143,7 +158,7 @@
};
const setAnimation = (instanceId: number, animationId: SpriteAnimations) => {
mesh.play(animationId, true, 'REVERSE').at(instanceId);
mesh.play(animationId, true, 'FORWARD').at(instanceId);
};
setContext('instanced-sprite-ctx', {
Expand Down
8 changes: 4 additions & 4 deletions apps/playground/src/routes/instanced-sprite/Scene.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@
<PlayerUpdater />
</AnimatedInstancedSprite> -->

<!-- {#await spritesheet then { spritesheet, texture }}
{#await spritesheet then { spritesheet, texture }}
<AnimatedInstancedSprite {spritesheet} {texture} fps={10} loop={true} {count}>
<FlyerUpdater />
</AnimatedInstancedSprite>
{/await}
<!--
{#await flyerSpritesheet then { spritesheet, texture }}
<AnimatedInstancedSprite {spritesheet} {texture} fps={10} loop={true} count={count * 25}>
<FlyerUpdater />
</AnimatedInstancedSprite>
{/await} -->

{#await countdownSpritesheet then { spritesheet, texture }}
<!-- {#await countdownSpritesheet then { spritesheet, texture }}
<AnimatedInstancedSprite {spritesheet} {texture} fps={1} loop={true} count={count * 25}>
<FlyerUpdater />
</AnimatedInstancedSprite>
{/await}
{/await} -->

<Sky elevation={0.15} />
<!-- <T.AmbientLight /> -->
Expand Down
9 changes: 8 additions & 1 deletion packages/instanced-sprite-mesh/src/InstancedSpriteMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,16 @@ export class InstancedSpriteMesh<
const frameMeta = this.spritesheet?.animations[animation][frameId][0];
id = frameMeta;
}

this.compute.utils.updateFrameAt(instanceId, id);
},
unsetAt: (instanceId: number) => {
this.compute.utils.updateFrameAt(instanceId, -10);
},
unsetAll: () => {
for (let id = 0; id < this.count; id++) {
this.compute.utils.updateFrameAt(id, -10);
}
},
};
}

Expand Down
30 changes: 11 additions & 19 deletions packages/instanced-sprite-mesh/src/animationRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ const animProgressCompute = /*glsl*/ `
vec4 instructions = texture2D( instructionsTexture, uv);
// FREEZE FRAME - return to save calculations?
if(instructions.a >=10.){
progressValue.r = instructions.a - 10.;
progressValue.a = instructions.x;
progressValue.g = progressValue.g;
gl_FragColor = progressValue;
return;
}
progressValue.b = 0.;
Expand Down Expand Up @@ -102,18 +111,15 @@ const animProgressCompute = /*glsl*/ `
float frameId = floor(animLength * frameTimedId);
float spritesheetFrameId = readData(frameId, 2.f + animationId, spritesheetData).r;
if(instructions.a >=10.){
spritesheetFrameId = instructions.a - 10.;
}
// Picked sprite frame that goes to material
progressValue.r = spritesheetFrameId;
progressValue.a = instructions.x;
progressValue.g = frameTimedId;
gl_FragColor = progressValue;
gl_FragColor = progressValue;
}
`;

Expand Down Expand Up @@ -230,15 +236,6 @@ export const initAnimationRunner = (
const index = instanceId * 4;
progressDataTexture.image.data[index + 3] = frameId + 10;
needsUpdate = true;
needsClearFrameSet = true;
};

const clearFrameSet = () => {
for (let i = 0; i < instanceCount; i++) {
const index = i * 4;
progressDataTexture.image.data[index + 3] = 0;
}
needsUpdate = true;
};

const update = () => {
Expand All @@ -247,11 +244,6 @@ export const initAnimationRunner = (
needsUpdate = false;
}
gpuCompute.compute();

if (needsClearFrameSet) {
clearFrameSet();
needsClearFrameSet = false;
}
};

// todo make this nicer after deciding on api
Expand Down

0 comments on commit db54522

Please sign in to comment.