Skip to content

Commit 497b23c

Browse files
committed
Update internal strands shaders to use manual uniform names
1 parent 1fbbedd commit 497b23c

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

src/core/filterShaders.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import * as constants from './constants';
22

33
/*
4-
* Creates p5.strands filter shaders for cross-platform compatibility
4+
* Creates p5.strands filter shaders for cross-platform compatibility.
5+
*
6+
* NOTE: These work a little differently than p5.js web editor shaders work!
7+
* Firstly, it uses instance mode, so we have to explicitly pass in context
8+
* variables in an argument to your callback and as a second argument to `modify`.
9+
* Secondly, always manually specify uniform names, as variable names will change
10+
* in minified builds.
511
*/
612
export function makeFilterShader(renderer, operation, p5) {
713
switch (operation) {
@@ -26,7 +32,7 @@ export function makeFilterShader(renderer, operation, p5) {
2632

2733
case constants.THRESHOLD:
2834
return renderer.baseFilterShader().modify(({ p5 }) => {
29-
const filterParameter = p5.uniformFloat();
35+
const filterParameter = p5.uniformFloat('filterParameter');
3036
p5.getColor((inputs, canvasContent) => {
3137
const color = p5.getTexture(canvasContent, inputs.texCoord);
3238
// weighted grayscale with luminance values
@@ -39,7 +45,7 @@ export function makeFilterShader(renderer, operation, p5) {
3945

4046
case constants.POSTERIZE:
4147
return renderer.baseFilterShader().modify(({ p5 }) => {
42-
const filterParameter = p5.uniformFloat();
48+
const filterParameter = p5.uniformFloat('filterParameter');
4349
const quantize = (color, n) => {
4450
// restrict values to N options/bins
4551
// and floor each channel to nearest value
@@ -61,8 +67,8 @@ export function makeFilterShader(renderer, operation, p5) {
6167

6268
case constants.BLUR:
6369
return renderer.baseFilterShader().modify(({ p5 }) => {
64-
const radius = p5.uniformFloat();
65-
const direction = p5.uniformVec2();
70+
const radius = p5.uniformFloat('radius');
71+
const direction = p5.uniformVec2('direction');
6672

6773
// This isn't a real Gaussian weight, it's a quadratic weight
6874
const quadWeight = (x, e) => {

src/core/p5.Renderer3D.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,13 +1912,13 @@ export class Renderer3D extends Renderer {
19121912
if (!this.sphereMapping) {
19131913
const p5 = this._pInst;
19141914
this.sphereMapping = this.baseFilterShader().modify(({ p5 }) => {
1915-
const uEnvMap = p5.uniformTexture();
1916-
const uFovY = p5.uniformFloat();
1917-
const uAspect = p5.uniformFloat();
1915+
const uEnvMap = p5.uniformTexture('uEnvMap');
1916+
const uFovY = p5.uniformFloat('uFovY');
1917+
const uAspect = p5.uniformFloat('uAspect');
19181918
// Hack: we don't have matrix uniforms yet; use three vectors
1919-
const uN1 = p5.uniformVec3();
1920-
const uN2 = p5.uniformVec3();
1921-
const uN3 = p5.uniformVec3();
1919+
const uN1 = p5.uniformVec3('uN1');
1920+
const uN2 = p5.uniformVec3('uN2');
1921+
const uN3 = p5.uniformVec3('uN3');
19221922
p5.getColor((inputs) => {
19231923
const uFovX = uFovY * uAspect;
19241924
const angleY = p5.mix(uFovY/2.0, -uFovY/2.0, inputs.texCoord.y);

src/webgl/p5.Shader.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,20 @@ class Shader {
243243
* then be passed into your function as an argument. If you are
244244
* using instance mode, you will need to pass your sketch object in this way.
245245
*
246+
* If you are also using a build system for your sketch, variable names may be changed as
247+
* part of minification. When creating a uniform, you can pass the name of the uniform in
248+
* as a first parameter to ensure it doesn't get changed.
249+
*
246250
* ```js example
247251
* new p5((sketch) => {
248252
* let myShader;
249253
*
250254
* sketch.setup = function() {
251255
* sketch.createCanvas(200, 200, sketch.WEBGL);
252256
* myShader = sketch.baseMaterialShader().modify(({ sketch }) => {
257+
* let b = uniformFloat('b');
253258
* sketch.getPixelInputs((inputs) => {
254-
* inputs.color = [inputs.texCoord, 0, 1];
259+
* inputs.color = [inputs.texCoord, b, 1];
255260
* return inputs;
256261
* });
257262
* }, { sketch });
@@ -260,6 +265,7 @@ class Shader {
260265
* sketch.draw = function() {
261266
* sketch.background(255);
262267
* sketch.noStroke();
268+
* myShader.setUniform('b', 0.5);
263269
* sketch.shader(myShader); // Apply the custom shader
264270
* sketch.plane(sketch.width, sketch.height); // Draw a plane with the shader applied
265271
* }

0 commit comments

Comments
 (0)