-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
368 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
apps/playground/src/routes/instanced-sprite/FlyerUpdater.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<script lang="ts"> | ||
import { useFrame, watch, T, useThrelte } from '@threlte/core'; | ||
import { getContext } from 'svelte'; | ||
import { SphereGeometry, Vector2 } from 'three'; | ||
const spriteCtx: any = getContext('instanced-sprite-ctx'); | ||
const { updatePosition, count, animationMap, setAnimation, mesh } = spriteCtx; | ||
//@ts-ignore | ||
watch(animationMap, (anims: Map<string, number>) => { | ||
// console.log(anims.keys()); | ||
}); | ||
// mesh.material.uniforms.animationId.value = 0; | ||
mesh.animation.setGlobal('fly'); | ||
console.log(mesh); | ||
const posX: number[] = new Array(count).fill(0); | ||
const posY: number[] = new Array(count).fill(0); | ||
const posZ: number[] = new Array(count).fill(0); | ||
const spread = 80; | ||
const minCenterDistance = 0.5; | ||
const maxCenterDistance = spread; | ||
const rndPosition: any = () => { | ||
const x = Math.random() * spread - spread / 2; | ||
const y = Math.random() * spread - spread / 2; | ||
/** min distance from 0,0. Recursive reroll if too close */ | ||
if (Math.sqrt(x ** 2 + y ** 2) < minCenterDistance) { | ||
return rndPosition(); | ||
} | ||
return { x, y }; | ||
}; | ||
/** update from 1 because 0 is user controlled and set at 0,0 */ | ||
for (let i = 1; i < count; i++) { | ||
const pos = rndPosition(); | ||
posX[i] = pos.x; | ||
posY[i] = 2 + Math.random() * 10; | ||
posZ[i] = pos.y; | ||
} | ||
type Agent = { | ||
action: 'Idle' | 'Run'; | ||
velocity: [number, number]; | ||
timer: number; | ||
}; | ||
const agents: Agent[] = []; | ||
for (let i = 0; i < count; i++) { | ||
agents.push({ | ||
action: 'Run', | ||
timer: 0.1, | ||
velocity: [0, 1] | ||
}); | ||
} | ||
const velocityHelper = new Vector2(0, 0); | ||
const updateAgents = (delta: number) => { | ||
for (let i = 0; i < agents.length; i++) { | ||
// timer | ||
agents[i].timer -= delta; | ||
// apply velocity | ||
posX[i] += agents[i].velocity[0] * delta; | ||
posZ[i] += agents[i].velocity[1] * delta; | ||
// roll new behaviour when time runs out or agent gets out of bounds | ||
if (i > 0) { | ||
const dist = Math.sqrt((posX[i] || 0) ** 2 + (posZ[i] || 0) ** 2); | ||
if (agents[i].timer < 0 || dist < minCenterDistance || dist > maxCenterDistance) { | ||
const runChance = 0.6 + (agents[i].action === 'Idle' ? 0.3 : 0); | ||
agents[i].action = Math.random() < runChance ? 'Run' : 'Idle'; | ||
agents[i].timer = 5 + Math.random() * 5; | ||
if (agents[i].action === 'Run') { | ||
velocityHelper | ||
.set(Math.random() - 0.5, Math.random() - 0.5) | ||
.normalize() | ||
.multiplyScalar(0.1); | ||
agents[i].velocity = velocityHelper.toArray(); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
useFrame((_, _delta) => { | ||
if ($animationMap.size > 0) { | ||
updateAgents(_delta); | ||
} | ||
for (let i = 0; i < count; i++) { | ||
// $camera.position.set(0 + (posX[0] || 0), 7, 15 + (posZ[0] || 0)) | ||
updatePosition(i, [posX[i] || 0, posY[i] || 0, posZ[i] || 0]); | ||
} | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
6faf273
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
threejs-kit-playground – ./apps/playground
threejs-kit-playground-git-main-jerzakm.vercel.app
threejs-kit-playground-jerzakm.vercel.app
threejs-kit-playground.vercel.app