Skip to content

Commit

Permalink
Move all uniform setting outside of p5.Shader
Browse files Browse the repository at this point in the history
  • Loading branch information
davepagurek committed Nov 3, 2024
1 parent fcb8abe commit e9f400d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
38 changes: 38 additions & 0 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ class RendererGL extends Renderer {
const shader = this._drawingFilter && this.states.userFillShader
? this.states.userFillShader
: this._getFillShader();
this._setGlobalUniforms(shader);
this._setFillUniforms(shader);

for (const buff of this.buffers.fill) {
Expand All @@ -575,6 +576,7 @@ class RendererGL extends Renderer {
this._useLineColor = geometry.vertexStrokeColors.length > 0;

const shader = this._getStrokeShader();
this._setGlobalUniforms(shader);
this._setStrokeUniforms(shader);

for (const buff of this.buffers.stroke) {
Expand Down Expand Up @@ -613,6 +615,7 @@ class RendererGL extends Renderer {
_drawPoints(vertices, vertexBuffer) {
const gl = this.GL;
const pointShader = this._getPointShader();
this._setGlobalUniforms(pointShader);
this._setPointUniforms(pointShader);

this._bindBuffer(
Expand Down Expand Up @@ -2100,6 +2103,41 @@ class RendererGL extends Renderer {
return new Framebuffer(this, options);
}

_setGlobalUniforms(shader) {
shader.bindShader();

const modelMatrix = this.states.uModelMatrix;
const viewMatrix = this.states.uViewMatrix;
const projectionMatrix = this.states.uPMatrix;
const modelViewMatrix = (modelMatrix.copy()).mult(viewMatrix);
this.states.uMVMatrix = this.calculateCombinedMatrix();

const modelViewProjectionMatrix = modelViewMatrix.copy();
modelViewProjectionMatrix.mult(projectionMatrix);

shader.setUniform(
'uPerspective',
this.states.curCamera.useLinePerspective ? 1 : 0
);
shader.setUniform('uViewMatrix', viewMatrix.mat4);
shader.setUniform('uProjectionMatrix', projectionMatrix.mat4);
shader.setUniform('uModelMatrix', modelMatrix.mat4);
shader.setUniform('uModelViewMatrix', modelViewMatrix.mat4);
shader.setUniform(
'uModelViewProjectionMatrix',
modelViewProjectionMatrix.mat4
);
if (shader.uniforms.uNormalMatrix) {
this.states.uNMatrix.inverseTranspose(this.states.uMVMatrix);
shader.setUniform('uNormalMatrix', this.states.uNMatrix.mat3);
}
if (shader.uniforms.uCameraRotation) {
this.states.curMatrix.inverseTranspose(this.states.uViewMatrix);
shader.setUniform('uCameraRotation', this.states.curMatrix.mat3);
}
shader.setUniform('uViewport', this._viewport);
}

_setStrokeUniforms(strokeShader) {
strokeShader.bindShader();

Expand Down
36 changes: 0 additions & 36 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,10 +737,6 @@ class Shader {
if (!this._bound) {
this.useProgram();
this._bound = true;

this._setMatrixUniforms();

this.setUniform('uViewport', this._renderer._viewport);
}
}

Expand Down Expand Up @@ -790,38 +786,6 @@ class Shader {
}
}

_setMatrixUniforms() {
const modelMatrix = this._renderer.states.uModelMatrix;
const viewMatrix = this._renderer.states.uViewMatrix;
const projectionMatrix = this._renderer.states.uPMatrix;
const modelViewMatrix = (modelMatrix.copy()).mult(viewMatrix);
this._renderer.states.uMVMatrix = this._renderer.calculateCombinedMatrix();

const modelViewProjectionMatrix = modelViewMatrix.copy();
modelViewProjectionMatrix.mult(projectionMatrix);

this.setUniform(
'uPerspective',
this._renderer.states.curCamera.useLinePerspective ? 1 : 0
);
this.setUniform('uViewMatrix', viewMatrix.mat4);
this.setUniform('uProjectionMatrix', projectionMatrix.mat4);
this.setUniform('uModelMatrix', modelMatrix.mat4);
this.setUniform('uModelViewMatrix', modelViewMatrix.mat4);
this.setUniform(
'uModelViewProjectionMatrix',
modelViewProjectionMatrix.mat4
);
if (this.uniforms.uNormalMatrix) {
this._renderer.states.uNMatrix.inverseTranspose(this._renderer.states.uMVMatrix);
this.setUniform('uNormalMatrix', this._renderer.states.uNMatrix.mat3);
}
if (this.uniforms.uCameraRotation) {
this._renderer.states.curMatrix.inverseTranspose(this._renderer.states.uViewMatrix);
this.setUniform('uCameraRotation', this._renderer.states.curMatrix.mat3);
}
}

/**
* @chainable
* @private
Expand Down

0 comments on commit e9f400d

Please sign in to comment.