Skip to content

Commit 3300755

Browse files
committed
better colors
1 parent 30b8039 commit 3300755

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/pages/particle-life-gpu/draw.wgsl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ const positions = array<vec2f, 6>(
1717
vec2f(-1, 1),
1818
);
1919

20-
const colors = array<vec4f, 6>(
21-
vec4f(1.0, 0.0, 0.0, 1.0), // red
22-
vec4f(0.0, 1.0, 0.0, 1.0), // green
23-
vec4f(0.5, 0.5, 1.0, 1.0), // blue
24-
vec4f(1.0, 1.0, 0.0, 1.0), // yellow
25-
vec4f(1.0, 0.0, 1.0, 1.0), // magenta
26-
vec4f(0.0, 1.0, 1.0, 1.0), // cyan
20+
const colors = array<vec4f, 8>(
21+
vec4f(0.72, 0.07, 0.02, 1.0), // red
22+
vec4f(0.12, 0.67, 0.89, 1.0), // cyan
23+
vec4f(0.84, 1.0, 0.19, 1.0), // yellow
24+
vec4f(0.53, 0.05, 0.87, 1.0), // indigo
25+
vec4f(0.04, 0.56, 0.04, 1.0), // green
26+
vec4f(0.56, 0.56, 0.77, 1.0), // lavender
27+
vec4f(1.0, 0.65, 0.0, 1.0), // orange
28+
vec4f(0.93, 0.51, 0.93, 1.0), // violet
2729
);
2830

2931
struct VertexOutput {

src/pages/particle-life-gpu/index.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import drawShader from './draw.wgsl?raw'
88
import binShader from './bin-fill.wgsl?raw'
99
import updateShader from './update.wgsl?raw'
1010

11+
// const particleCount = 200_000
12+
const particleCount = 100_000
13+
// const particleCount = 20_000
14+
1115
export const meta: RouteMeta = {
1216
title: 'Particle Life GPU',
1317
tags: ['simulation', 'webgpu', 'particles'],
@@ -34,12 +38,14 @@ export default function ParticleLifeGPUPage() {
3438
controller.abort()
3539
}
3640
}, [supported])
41+
42+
const [numberFormat] = useState(() => new Intl.NumberFormat('en-US'))
3743

3844
return (
3945
<div className={styles.main}>
4046
<div className={styles.head}>
4147
<Head />
42-
{supported && <pre>{fps} FPS</pre>}
48+
{supported && <pre>{numberFormat.format(particleCount)} particles, {fps} fps</pre>}
4349
{!supported && <pre>Your browser does not support WebGPU.</pre>}
4450
</div>
4551
<canvas ref={canvasRef} />
@@ -90,9 +96,8 @@ async function start(
9096
const toBinX = widthDivisions / width
9197
const toBinY = heightDivisions / height
9298
const binCount = widthDivisions * heightDivisions
93-
// const particleCount = 200_000
94-
const particleCount = 100_000
95-
// const particleCount = 20_000
99+
100+
const numberOfColors = 8
96101

97102
const particlePositionBuffer = device.createBuffer({
98103
label: 'particle position storage buffer',
@@ -118,7 +123,7 @@ async function start(
118123
{
119124
const staticStorageArray = new Uint32Array(particleColorBuffer.getMappedRange())
120125
for (let i = 0; i < particleCount; i++) {
121-
staticStorageArray[i] = i % 6
126+
staticStorageArray[i] = i % numberOfColors
122127
}
123128
particleColorBuffer.unmap()
124129
onAbort(() => particleColorBuffer.destroy())
@@ -140,16 +145,16 @@ async function start(
140145
}
141146
const particleInteractionsBuffer = device.createBuffer({
142147
label: 'particle interactions storage buffer',
143-
size: 6 * 6 * Float32Array.BYTES_PER_ELEMENT,
148+
size: numberOfColors * numberOfColors * Float32Array.BYTES_PER_ELEMENT,
144149
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
145150
mappedAtCreation: true,
146151
})
147152
{
148153
const staticStorageArray = new Float32Array(particleInteractionsBuffer.getMappedRange())
149-
for (let i = 0; i < 6; i++) {
150-
for (let j = 0; j < 6; j++) {
151-
staticStorageArray[i * 6 + j] = Math.random() * 2 - 1
152-
// staticStorageArray[i * 6 + j] = i === j ? 1 : 0
154+
for (let i = 0; i < numberOfColors; i++) {
155+
for (let j = 0; j < numberOfColors; j++) {
156+
staticStorageArray[i * numberOfColors + j] = Math.random() * 2 - 1
157+
// staticStorageArray[i * numberOfColors + j] = i === j ? 1 : 0
153158
}
154159
}
155160
particleInteractionsBuffer.unmap()
@@ -612,7 +617,7 @@ async function start(
612617
colorAttachments: [
613618
{
614619
view: null! as GPUTextureView,
615-
clearValue: [0.1, 0.1, 0.1, 1],
620+
clearValue: [0.06, 0.06, 0.08, 1],
616621
loadOp: 'clear',
617622
storeOp: 'store',
618623
},

0 commit comments

Comments
 (0)