Skip to content

Limit Scope of Default Uniforms #5280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 73 additions & 55 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -1233,72 +1233,90 @@ p5.RendererGL.prototype.getTexture = function(img) {
p5.RendererGL.prototype._setStrokeUniforms = function(strokeShader) {
strokeShader.bindShader();

// set the uniform values
strokeShader.setUniform('uMaterialColor', this.curStrokeColor);
strokeShader.setUniform('uStrokeWeight', this.curStrokeWeight);
if (strokeShader !== this.userStrokeShader) {
// set the uniform values
strokeShader.setUniform('uMaterialColor', this.curStrokeColor);
strokeShader.setUniform('uStrokeWeight', this.curStrokeWeight);
strokeShader.setUniform('uViewport', this._viewport);
if (this._curCamera.cameraType === 'default') {
// strokes scale up as they approach camera, default
strokeShader.setUniform('uPerspective', 1);
} else {
// strokes have uniform scale regardless of distance from camera
strokeShader.setUniform('uPerspective', 0);
}
}
};

p5.RendererGL.prototype._setFillUniforms = function(fillShader) {
fillShader.bindShader();

// TODO: optimize
fillShader.setUniform('uMaterialColor', this.curFillColor);
fillShader.setUniform('isTexture', !!this._tex);
if (this._tex) {
fillShader.setUniform('uSampler', this._tex);
}
fillShader.setUniform('uTint', this._tint);
if (fillShader !== this.userFillShader) {
// TODO: optimize
fillShader.setUniform('uMaterialColor', this.curFillColor);
fillShader.setUniform('isTexture', !!this._tex);
if (this._tex) {
fillShader.setUniform('uSampler', this._tex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this prevent people from using the uSampler uniform? For instance, at the moment, you can use uSampler as a uniform in your shader and have p5 send you whatever texture was bound with the texture() function. I think we wouldn't want to take that functionality away.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just based off of the code, I think if a user has set texture(someTexture), then this._tex will be set, and the uSampler uniform will still get set for you here. A test to confirm that would be great though

}
fillShader.setUniform('uTint', this._tint);

fillShader.setUniform('uSpecular', this._useSpecularMaterial);
fillShader.setUniform('uEmissive', this._useEmissiveMaterial);
fillShader.setUniform('uShininess', this._useShininess);
fillShader.setUniform('uSpecular', this._useSpecularMaterial);
fillShader.setUniform('uEmissive', this._useEmissiveMaterial);
fillShader.setUniform('uShininess', this._useShininess);

fillShader.setUniform('uUseLighting', this._enableLighting);
fillShader.setUniform('uUseLighting', this._enableLighting);

const pointLightCount = this.pointLightDiffuseColors.length / 3;
fillShader.setUniform('uPointLightCount', pointLightCount);
fillShader.setUniform('uPointLightLocation', this.pointLightPositions);
fillShader.setUniform(
'uPointLightDiffuseColors',
this.pointLightDiffuseColors
);
fillShader.setUniform(
'uPointLightSpecularColors',
this.pointLightSpecularColors
);
const pointLightCount = this.pointLightDiffuseColors.length / 3;
fillShader.setUniform('uPointLightCount', pointLightCount);
fillShader.setUniform('uPointLightLocation', this.pointLightPositions);
fillShader.setUniform(
'uPointLightDiffuseColors',
this.pointLightDiffuseColors
);
fillShader.setUniform(
'uPointLightSpecularColors',
this.pointLightSpecularColors
);

const directionalLightCount = this.directionalLightDiffuseColors.length / 3;
fillShader.setUniform('uDirectionalLightCount', directionalLightCount);
fillShader.setUniform('uLightingDirection', this.directionalLightDirections);
fillShader.setUniform(
'uDirectionalDiffuseColors',
this.directionalLightDiffuseColors
);
fillShader.setUniform(
'uDirectionalSpecularColors',
this.directionalLightSpecularColors
);
const directionalLightCount = this.directionalLightDiffuseColors.length / 3;
fillShader.setUniform('uDirectionalLightCount', directionalLightCount);
fillShader.setUniform(
'uLightingDirection',
this.directionalLightDirections
);
fillShader.setUniform(
'uDirectionalDiffuseColors',
this.directionalLightDiffuseColors
);
fillShader.setUniform(
'uDirectionalSpecularColors',
this.directionalLightSpecularColors
);

// TODO: sum these here...
const ambientLightCount = this.ambientLightColors.length / 3;
fillShader.setUniform('uAmbientLightCount', ambientLightCount);
fillShader.setUniform('uAmbientColor', this.ambientLightColors);

const spotLightCount = this.spotLightDiffuseColors.length / 3;
fillShader.setUniform('uSpotLightCount', spotLightCount);
fillShader.setUniform('uSpotLightAngle', this.spotLightAngle);
fillShader.setUniform('uSpotLightConc', this.spotLightConc);
fillShader.setUniform('uSpotLightDiffuseColors', this.spotLightDiffuseColors);
fillShader.setUniform(
'uSpotLightSpecularColors',
this.spotLightSpecularColors
);
fillShader.setUniform('uSpotLightLocation', this.spotLightPositions);
fillShader.setUniform('uSpotLightDirection', this.spotLightDirections);
// TODO: sum these here...
const ambientLightCount = this.ambientLightColors.length / 3;
fillShader.setUniform('uAmbientLightCount', ambientLightCount);
fillShader.setUniform('uAmbientColor', this.ambientLightColors);

const spotLightCount = this.spotLightDiffuseColors.length / 3;
fillShader.setUniform('uSpotLightCount', spotLightCount);
fillShader.setUniform('uSpotLightAngle', this.spotLightAngle);
fillShader.setUniform('uSpotLightConc', this.spotLightConc);
fillShader.setUniform(
'uSpotLightDiffuseColors',
this.spotLightDiffuseColors
);
fillShader.setUniform(
'uSpotLightSpecularColors',
this.spotLightSpecularColors
);
fillShader.setUniform('uSpotLightLocation', this.spotLightPositions);
fillShader.setUniform('uSpotLightDirection', this.spotLightDirections);

fillShader.setUniform('uConstantAttenuation', this.constantAttenuation);
fillShader.setUniform('uLinearAttenuation', this.linearAttenuation);
fillShader.setUniform('uQuadraticAttenuation', this.quadraticAttenuation);
fillShader.setUniform('uConstantAttenuation', this.constantAttenuation);
fillShader.setUniform('uLinearAttenuation', this.linearAttenuation);
fillShader.setUniform('uQuadraticAttenuation', this.quadraticAttenuation);
}

fillShader.bindTextures();
};
Expand Down
11 changes: 0 additions & 11 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ p5.Shader.prototype.bindShader = function() {
this._bound = true;

this._setMatrixUniforms();

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

Expand Down Expand Up @@ -262,15 +260,6 @@ p5.Shader.prototype.unbindTextures = function() {

p5.Shader.prototype._setMatrixUniforms = function() {
this.setUniform('uProjectionMatrix', this._renderer.uPMatrix.mat4);
if (this.isStrokeShader()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think moving this to the set stroke uniforms makes sense!

if (this._renderer._curCamera.cameraType === 'default') {
// strokes scale up as they approach camera, default
this.setUniform('uPerspective', 1);
} else {
// strokes have uniform scale regardless of distance from camera
this.setUniform('uPerspective', 0);
}
}
this.setUniform('uModelViewMatrix', this._renderer.uMVMatrix.mat4);
this.setUniform('uViewMatrix', this._renderer._curCamera.cameraMatrix.mat4);
if (this.uniforms.uNormalMatrix) {
Expand Down