Skip to content
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

Add back WebGL matrices via getters #7588

Merged
merged 1 commit into from
Mar 1, 2025
Merged
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
56 changes: 21 additions & 35 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
82 changes: 81 additions & 1 deletion test/unit/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});