Skip to content

Commit

Permalink
worker test, remove hueshift
Browse files Browse the repository at this point in the history
  • Loading branch information
jerzakm committed Mar 7, 2024
1 parent 43b5e5f commit f0994c3
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 383 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { onMount } from 'svelte';
import { initDemonBench } from './mainDemons';
import { page } from '$app/stores';
import { initBunBench } from './mainBunnies';
Expand All @@ -15,8 +14,6 @@
onMount(() => {
if (mode === 'bunnies') {
initBunBench(count);
} else {
initDemonBench(count);
}
});
Expand Down Expand Up @@ -44,21 +41,6 @@
</li>
{/each}
</ul>
<ul>
<span style="width: 140px;">Demons (animated):</span>
{#each countOptions as c}
<li>
<a
data-sveltekit-reload
href={`/instanced-sprite-bunny-mark-workerized?count=${c}&mode=demons`}>{c}</a
>
</li>
{/each}
</ul>
{#if mode === 'demons'}
Each demon has a random animation (idle, death, fly, attack) that changes each time it hits the
screen boundary, a separate animation progression and changes flipX based on the direction.
{/if}
</div>

<canvas id="three-canvas" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ onmessage = function (e) {
}
lastTime = currentTime * 1;

// Schedule the next update
setTimeout(update, 0); // roughly 60fps
setTimeout(update, 0);
}
update();
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
DoubleSide,
Matrix4,
MeshBasicMaterial,
MeshStandardMaterial,
Vector2,
type Scene,
type Vector3Tuple,
type WebGLRenderer
Expand Down Expand Up @@ -52,119 +50,30 @@ export const initBunnies = async (renderer: WebGLRenderer, scene: Scene, count:

const sprite = new InstancedSpriteMesh(baseMaterial, count, renderer);

sprite.fps = 9;

sprite.hueShift.setGlobal({
h: 0,
s: 1.1,
v: 1.9
});
sprite.fps = 1;

sprite.spritesheet = spritesheet;
scene.add(sprite);

sprite.castShadow = true;

// UPDATING AND MOVING SPRITES
let dirtyInstanceMatrix = false;

const tempMatrix = new Matrix4();
function updatePosition(id: number, [x, y, z]: Vector3Tuple) {
tempMatrix.makeScale(25, 32, 1);
tempMatrix.setPosition(x, bounds.top - y, z);
sprite.setMatrixAt(id, tempMatrix);
// dirtyInstanceMatrix = true;
}

const gravity = 0.75;

const positionX: number[] = new Array(count).fill(0);
const positionY: number[] = new Array(count).fill(0);
const zIndex: number[] = new Array(count).fill(0);

const speedX: number[] = new Array(count).fill(0);
const speedY: number[] = new Array(count).fill(0);

const { updateAgents } = setupRandomAgents();

const bounds = {
left: 0,
right: window.innerWidth,
bottom: 0,
top: window.innerHeight
};

function setupRandomAgents() {
for (let i = 0; i < count; i++) {
positionX[i] = 0;
positionY[i] = 0;
zIndex[i] = -Math.random() * 10;

speedX[i] = Math.random() * 10;
speedY[i] = Math.random() * 10 - 5;

sprite.animation.setAt(i, bunnies[Math.floor(Math.random() * bunnies.length)]);
}

const updateAgents = (delta: number) => {
for (let i = 0; i < count; i++) {
delta = 1;
// timer
// apply gravity

// apply velocity
positionX[i] += speedX[i] * delta;
positionY[i] += speedY[i] * delta;
speedY[i] += gravity * delta;

// roll new behaviour if bunny gets out of bounds

if (positionX[i] > bounds.right) {
speedX[i] *= -1;
positionX[i] = bounds.right;
} else if (positionX[i] < bounds.left) {
speedX[i] *= -1;
positionX[i] = bounds.left;
}

if (positionY[i] > bounds.top) {
speedY[i] *= -0.85;
positionY[i] = bounds.top;
if (Math.random() > 0.5) {
speedY[i] -= Math.random() * 6;
}
} else if (positionY[i] < bounds.bottom) {
speedY[i] *= -1;
positionY[i] = bounds.top;
}
}

for (let i = 0; i < count; i++) {
updatePosition(i, [positionX[i], positionY[i], zIndex[i]]);
}
};
sprite.update();

return { updateAgents };
for (let i = 0; i < count; i++) {
sprite.animation.setAt(i, bunnies[Math.floor(Math.random() * bunnies.length)]);
}

let initialized = false;

const update = (delta: number) => {
updateAgents(delta);

// sprite.update();

if (dirtyInstanceMatrix) {
sprite.instanceMatrix.needsUpdate = true;
dirtyInstanceMatrix = false;
}

if (!initialized) {
sprite.update();
initialized = true;
}
};

return { update, sprite, updatePosition };
return { sprite, updatePosition };
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const initBunBench = async (count = 100000) => {

let workersToSpawn = navigator.hardwareConcurrency
? Math.floor(navigator.hardwareConcurrency / 2)
: 3;
: 2;

// workersToSpawn = 1;
console.log(`This device appears to have ${navigator.hardwareConcurrency} logical cores.`);
Expand Down Expand Up @@ -156,7 +156,6 @@ export const initBunBench = async (count = 100000) => {
}

bunnies.sprite.instanceMatrix.needsUpdate = true;
// bunnies.update(delta);
renderer.render(scene, camera);
perf.end();
stats.end();
Expand Down
Loading

0 comments on commit f0994c3

Please sign in to comment.