Skip to content

Commit

Permalink
\#4: port drawParticles to shader preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
loganzartman committed Feb 21, 2023
1 parent 3126b4f commit 90872e3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 40 deletions.
11 changes: 0 additions & 11 deletions src/drawParticles.frag.glsl

This file was deleted.

25 changes: 0 additions & 25 deletions src/drawParticles.vert.glsl

This file was deleted.

7 changes: 3 additions & 4 deletions src/renderWebGL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {Params} from './params';
import {GPUState} from './state';
import {memoize} from './util';
import {mat4} from 'gl-matrix';
import drawParticlesVert from './drawParticles.vert.glsl';
import drawParticlesFrag from './drawParticles.frag.glsl';
import {drawParticlesVs, drawParticlesFs} from './shader/drawParticles';

const getCircleVertexBuffer = memoize(
(gl: WebGL2RenderingContext, vertexCount: number) =>
Expand All @@ -27,11 +26,11 @@ const getCircleVAO = memoize(
);

const getDrawParticlesVert = memoize((gl: WebGL2RenderingContext) =>
createShader(gl, {source: drawParticlesVert, type: gl.VERTEX_SHADER})
createShader(gl, {source: drawParticlesVs, type: gl.VERTEX_SHADER})
);

const getDrawParticlesFrag = memoize((gl: WebGL2RenderingContext) =>
createShader(gl, {source: drawParticlesFrag, type: gl.FRAGMENT_SHADER})
createShader(gl, {source: drawParticlesFs, type: gl.FRAGMENT_SHADER})
);

const getDrawParticlesProgram = memoize((gl: WebGL2RenderingContext) =>
Expand Down
37 changes: 37 additions & 0 deletions src/shader/drawParticles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {compile, glsl} from '../glslpp';
import {
densitySampler,
particleRadius,
positionSampler,
projection,
resolution,
velocitySampler,
} from './uniforms';

export const drawParticlesVs = compile(glsl`
in vec2 circleOffset;
out vec4 color;
void main() {
int particleIndex = gl_InstanceID;
ivec2 texCoord = ivec2(particleIndex % ${resolution}.x, particleIndex / ${resolution}.x);
vec2 pos = texelFetch(${positionSampler}, texCoord, 0).xy;
vec2 vel = texelFetch(${velocitySampler}, texCoord, 0).xy;
float density = texelFetch(${densitySampler}, texCoord, 0).x;
vec2 vertexPos = pos + circleOffset * ${particleRadius};
gl_Position = ${projection} * vec4(vertexPos, 0., 1.);
color = vec4(vel * 2. + 0.5, density, 1.);
}
`);

export const drawParticlesFs = compile(glsl`
in vec4 color;
out vec4 outColor;
void main() {
outColor = color;
}
`);
1 change: 1 addition & 0 deletions src/shader/uniforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const pointerDown = new GLSLUniform('pointerDown', 'int');
export const pointerPos = new GLSLUniform('pointerPos', 'vec2');
export const pointerVel = new GLSLUniform('pointerVel', 'vec2');
export const positionSampler = new GLSLUniform('positionSampler', 'sampler2D');
export const projection = new GLSLUniform('projection', 'mat4');
export const resolution = new GLSLUniform('resolution', 'ivec2');
export const restDensity = new GLSLUniform('restDensity', 'float');
export const sigma = new GLSLUniform('sigma', 'float');
Expand Down

0 comments on commit 90872e3

Please sign in to comment.