From 781fda73eb43d18eccd695b2c5a460118e309bab Mon Sep 17 00:00:00 2001 From: harshiltewari2004 Date: Thu, 24 Sep 2026 00:09:56 +0530 Subject: [PATCH 1/4] Add uniformMat2/3/4 aliases for p5.strands matrix uniforms --- src/strands/strands_api.js | 3 +++ test/unit/webgl/p5.Shader.js | 27 +++++++++++++++++++++++++++ test/unit/webgpu/p5.Shader.js | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/strands/strands_api.js b/src/strands/strands_api.js index b873ae916a..fffbbf451e 100644 --- a/src/strands/strands_api.js +++ b/src/strands/strands_api.js @@ -933,6 +933,9 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) { typeAliases.push('Texture'); } else if (/^vec/.test(typeInfo.fnName)) { typeAliases.push(pascalTypeName.replace('Vec', 'Vector')); + } else if (/^mat\dx\d$/.test(typeInfo.fnName)) { + // mat3x3 -> Mat3, so uniformMat3() works alongside uniformMat3x3() + typeAliases.push(pascalTypeName.slice(0, 4)); } } augmentFn( diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index 8ea5f725a5..5b0f3abd82 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -3815,6 +3815,33 @@ suite('p5.Shader', function () { ); }).not.toThrow(); }); + + + suite('matrix uniforms (#8992)', () => { + test('short aliases declare the same uniforms as the NxN names', () => { + myp5.createCanvas(5, 5, myp5.WEBGL); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.uniformMat2('uShort2'); + myp5.uniformMat3('uShort3'); + myp5.uniformMat4('uShort4'); + myp5.uniformMat3x3('uLong3'); + }, + { myp5 } + ); + + expect(() => { + myp5.shader(testShader); + myp5.plane(myp5.width, myp5.height); + }).not.toThrowError(); + + const src = testShader.fragSrc(); + assert.include(src, 'uniform mat2x2 uShort2;'); + assert.include(src, 'uniform mat3x3 uShort3;'); + assert.include(src, 'uniform mat4x4 uShort4;'); + assert.include(src, 'uniform mat3x3 uLong3;'); + }); + }); }); suite('p5.strands error messages', () => { diff --git a/test/unit/webgpu/p5.Shader.js b/test/unit/webgpu/p5.Shader.js index 4003152397..aaa86639ce 100644 --- a/test/unit/webgpu/p5.Shader.js +++ b/test/unit/webgpu/p5.Shader.js @@ -2145,5 +2145,26 @@ suite('WebGPU p5.Shader', function () { }).not.toThrow(); }); }); + + suite('matrix uniforms (#8992)', () => { + test('short aliases declare the same uniforms as the NxN names', async () => { + await myp5.createCanvas(5, 5, myp5.WEBGPU); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.uniformMat2('uShort2'); + myp5.uniformMat3('uShort3'); + myp5.uniformMat4('uShort4'); + myp5.uniformMat3x3('uLong3'); + }, + { myp5 } + ); + + const src = testShader.vertSrc(); + assert.include(src, 'uShort2: mat2x2'); + assert.include(src, 'uShort3: mat3x3'); + assert.include(src, 'uShort4: mat4x4'); + assert.include(src, 'uLong3: mat3x3'); + }); + }); }); }); From f3505158eea5a063eb310fc98a6b2b230cd6f9f1 Mon Sep 17 00:00:00 2001 From: harshiltewari2004 Date: Thu, 24 Sep 2026 00:31:08 +0530 Subject: [PATCH 2/4] Accept p5.Matrix in setUniform --- docs/parameterData.json | 2 +- src/math/Matrices/Matrix.js | 3 +++ src/webgl/p5.Shader.js | 4 +++- test/unit/webgl/p5.Shader.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/parameterData.json b/docs/parameterData.json index 450f251d50..f5cf89c90a 100644 --- a/docs/parameterData.json +++ b/docs/parameterData.json @@ -4482,7 +4482,7 @@ "overloads": [ [ "String", - "Boolean|p5.Vector|p5.Color|Number|Number[]|p5.Image|p5.Graphics|p5.MediaElement|p5.Texture|p5.StorageBuffer" + "Boolean|p5.Vector|p5.Color|p5.Matrix|Number|Number[]|p5.Image|p5.Graphics|p5.MediaElement|p5.Texture|p5.StorageBuffer" ] ] } diff --git a/src/math/Matrices/Matrix.js b/src/math/Matrices/Matrix.js index 91d3ecc263..5acd72ca83 100644 --- a/src/math/Matrices/Matrix.js +++ b/src/math/Matrices/Matrix.js @@ -23,6 +23,9 @@ if (typeof Float32Array !== 'undefined') { export class Matrix extends MatrixInterface { matrix; #sqDimention; + // Duck-typing flag (like isVector / isColor) so code such as + // p5.Shader.setUniform can recognize a p5.Matrix without instanceof + isMatrix = true; constructor(...args) { super(...args); diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index 68dbba20bf..3e4a8192b0 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -935,7 +935,7 @@ class Shader { * @chainable * @param {String} uniformName name of the uniform. Must match the name * used in the vertex and fragment shaders. - * @param {Boolean|p5.Vector|p5.Color|Number|Number[]|p5.Image|p5.Graphics|p5.MediaElement|p5.Texture|p5.StorageBuffer} + * @param {Boolean|p5.Vector|p5.Color|p5.Matrix|Number|Number[]|p5.Image|p5.Graphics|p5.MediaElement|p5.Texture|p5.StorageBuffer} * data value to assign to the uniform. Must match the uniform’s data type. * * @example @@ -1164,6 +1164,8 @@ class Shader { : data.values; } else if (data?.isColor) { data = data._getRGBA([1, 1, 1, 1]); + } else if (data?.isMatrix) { + data = data.matrix; } if (uniform.isArray) { diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index 5b0f3abd82..8d8f7f33a7 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -3841,7 +3841,34 @@ suite('p5.Shader', function () { assert.include(src, 'uniform mat4x4 uShort4;'); assert.include(src, 'uniform mat3x3 uLong3;'); }); - }); + + test('setUniform accepts a p5.Matrix', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.createFilterShader(` + precision highp float; + uniform mat3 uM; + void main() { + gl_FragColor = vec4(uM[0][0], uM[1][0], uM[2][2], 1.0); + } + `); + + // Column-major: index 3 is column 1, row 0 -> uM[1][0] + const m = myp5.createMatrix([ + 1.0, 0.0, 0.0, + 0.5, 0.0, 0.0, + 0.0, 0.0, 0.25 + ]); + + myp5.background(0); + testShader.setUniform('uM', m); + myp5.filter(testShader); + + const pixelColor = myp5.get(25, 25); + assert.approximately(pixelColor[0], 255, 5); // uM[0][0] = 1.0 + assert.approximately(pixelColor[1], 128, 5); // uM[1][0] = 0.5 + assert.approximately(pixelColor[2], 64, 5); // uM[2][2] = 0.25 + }); + }); }); suite('p5.strands error messages', () => { From 4e17285db4eac95583897a0bfbdcdba8082142d7 Mon Sep 17 00:00:00 2001 From: harshiltewari2004 Date: Thu, 24 Sep 2026 00:34:59 +0530 Subject: [PATCH 3/4] Add Mat2/3/4 aliases to generated TypeScript types --- utils/typescript.mjs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/utils/typescript.mjs b/utils/typescript.mjs index e0a850e3d7..919d1f9a17 100644 --- a/utils/typescript.mjs +++ b/utils/typescript.mjs @@ -134,6 +134,9 @@ function processStrandsFunctions() { typeAliases.push('Texture'); } else if (/^vec/.test(typeInfo.fnName)) { typeAliases.push(pascalTypeName.replace('Vec', 'Vector')); + } else if (/^mat\dx\d$/.test(typeInfo.fnName)) { + // mat3x3 -> Mat3, so uniformMat3() works alongside uniformMat3x3() + typeAliases.push(pascalTypeName.slice(0, 4)); } } From 4fa4230708155da08272d8fb4ed299efb123c931 Mon Sep 17 00:00:00 2001 From: harshiltewari2004 Date: Sun, 27 Sep 2026 17:53:38 +0530 Subject: [PATCH 4/4] Drop p5.Matrix from setUniform param types (p5.Matrix is private) --- docs/parameterData.json | 2 +- src/webgl/p5.Shader.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/parameterData.json b/docs/parameterData.json index f5cf89c90a..450f251d50 100644 --- a/docs/parameterData.json +++ b/docs/parameterData.json @@ -4482,7 +4482,7 @@ "overloads": [ [ "String", - "Boolean|p5.Vector|p5.Color|p5.Matrix|Number|Number[]|p5.Image|p5.Graphics|p5.MediaElement|p5.Texture|p5.StorageBuffer" + "Boolean|p5.Vector|p5.Color|Number|Number[]|p5.Image|p5.Graphics|p5.MediaElement|p5.Texture|p5.StorageBuffer" ] ] } diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index 3e4a8192b0..ef055744d4 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -935,7 +935,7 @@ class Shader { * @chainable * @param {String} uniformName name of the uniform. Must match the name * used in the vertex and fragment shaders. - * @param {Boolean|p5.Vector|p5.Color|p5.Matrix|Number|Number[]|p5.Image|p5.Graphics|p5.MediaElement|p5.Texture|p5.StorageBuffer} + * @param {Boolean|p5.Vector|p5.Color|Number|Number[]|p5.Image|p5.Graphics|p5.MediaElement|p5.Texture|p5.StorageBuffer} * data value to assign to the uniform. Must match the uniform’s data type. * * @example