From f712984d82df36ea6578b81e6ee0820301ead26e Mon Sep 17 00:00:00 2001 From: Dave Pagurek Date: Sat, 1 Mar 2025 15:10:19 -0500 Subject: [PATCH] Add back WebGL matrices via getters --- src/webgl/p5.RendererGL.js | 56 ++++++++-------------- test/unit/webgl/p5.RendererGL.js | 82 +++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 36 deletions(-) diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 8e2f1e4317..92b5c369e2 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -550,41 +550,6 @@ class RendererGL extends Renderer { super.endShape(mode, count); } - legacyEndShape( - mode, - isCurve, - isBezier, - isQuadratic, - isContour, - shapeKind, - count = 1 - ) { - this.shapeBuilder.endShape( - mode, - isCurve, - isBezier, - isQuadratic, - isContour, - shapeKind - ); - - if (this.geometryBuilder) { - this.geometryBuilder.addImmediate( - this.shapeBuilder.geometry, - this.shapeBuilder.shapeMode - ); - } else if (this.states.fillColor || this.states.strokeColor) { - this._drawGeometry(this.shapeBuilder.geometry, { - mode: this.shapeBuilder.shapeMode, - count, - }); - } - } - - legacyVertex(...args) { - this.shapeBuilder.vertex(...args); - } - vertexProperty(...args) { this.currentShape.vertexProperty(...args); } @@ -1051,6 +1016,27 @@ class RendererGL extends Renderer { this.clear(..._col._getRGBA()); } + ////////////////////////////////////////////// + // Positioning + ////////////////////////////////////////////// + + get uModelMatrix() { + return this.states.uModelMatrix; + } + + get uViewMatrix() { + return this.states.uViewMatrix; + } + + get uPMatrix() { + return this.states.uPMatrix; + } + + get uMVMatrix() { + const m = this.uModelMatrix.copy(); + m.mult(this.uViewMatrix); + return m; + } /** * Get a matrix from world-space to screen-space diff --git a/test/unit/webgl/p5.RendererGL.js b/test/unit/webgl/p5.RendererGL.js index 2e4b6a5f8e..e33f4accb0 100644 --- a/test/unit/webgl/p5.RendererGL.js +++ b/test/unit/webgl/p5.RendererGL.js @@ -2702,5 +2702,85 @@ suite('p5.RendererGL', function() { expect(logs.join('\n')).to.match(/One of the geometries has a custom vertex property 'aCustom' with fewer values than vertices./); } ); - }) + }); + + suite('Matrix getters', function() { + test('uModelMatrix', function() { + p5.registerAddon(function (p5, fn) { + fn.checkModelMatrix = function() { + assert.deepEqual( + [...this._renderer.uModelMatrix.mat4], + [ + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 5, 0, 0, 1 + ] + ); + } + }); + myp5.createCanvas(50, 50, myp5.WEBGL); + myp5.translate(5, 0); + myp5.camera(0, 0, 500, 0, 0, 0); + myp5.checkModelMatrix(); + }); + + test('uViewMatrix', function() { + p5.registerAddon(function (p5, fn) { + fn.checkViewMatrix = function() { + assert.deepEqual( + [...this._renderer.uViewMatrix.mat4], + [ + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, -500, 1 + ] + ); + } + }); + myp5.createCanvas(50, 50, myp5.WEBGL); + myp5.translate(5, 0); + myp5.camera(0, 0, 500, 0, 0, 0); + myp5.checkViewMatrix(); + }); + + test('uMVMatrix', function() { + p5.registerAddon(function (p5, fn) { + fn.checkMVMatrix = function() { + assert.deepEqual( + [...this._renderer.uMVMatrix.mat4], + [ + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 5, 0, -500, 1 + ] + ); + } + }); + myp5.createCanvas(50, 50, myp5.WEBGL); + myp5.translate(5, 0); + myp5.camera(0, 0, 500, 0, 0, 0); + myp5.checkMVMatrix(); + }); + + test('uPMatrix', function() { + p5.registerAddon(function (p5, fn) { + fn.checkPMatrix = function() { + assert.deepEqual( + [...this._renderer.uPMatrix.mat4], + [ + 32, 0, 0, 0, + 0, -32, 0, 0, + 0, 0, -1.0202020406723022, -1, + 0, 0, -161.6161651611328, 0 + ] + ); + } + }); + myp5.createCanvas(50, 50, myp5.WEBGL); + myp5.checkPMatrix(); + }); + }); });