-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Automatically keep track of transformations #7342
Comments
Related, this PR #7113 is adding support for |
For WebGL I'd say to go with the multiplied result of the model, view, and projection matrices that are typically used for vertex shader transforms. Also, although the people who use the matrix directly would be more technically inclined, it'd still be best to make things consistent across both the |
I've implemented a p5.prototype.screenToWorld = function(screenPosition) {
const renderer = this._renderer;
if (renderer.drawingContext instanceof CanvasRenderingContext2D) {
// Handle 2D context
const inverseTransformMatrix = new DOMMatrix()
.scale(renderer._pInst.pixelDensity())
.multiply(renderer.drawingContext.getTransform().inverse());
const worldCoordinates = inverseTransformMatrix.transformPoint(
new DOMPoint(screenPosition.x, screenPosition.y)
);
return new p5.Vector(worldCoordinates.x, worldCoordinates.y);
} else {
// Handle WebGL context (3D)
const normalizedDeviceCoordinates = new p5.Vector(
(screenPosition.x / this.width) * 2 - 1,
(screenPosition.y / this.height) * -2 + 1,
screenPosition.z || 0
);
const inversePMatrix = renderer.states.uPMatrix.invert();
const inverseMVMatrix = renderer.calculateCombinedMatrix().invert();
const cameraCoordinates = inversePMatrix.multiplyPoint(normalizedDeviceCoordinates);
const worldCoordinates = inverseMVMatrix.multiplyPoint(cameraCoordinates);
return new p5.Vector(worldCoordinates.x, worldCoordinates.y, worldCoordinates.z);
}
}; The p5.js/src/webgl/p5.RendererGL.js Line 938 in acb5ac4
could also be enhanced with an optional camera transformation parameter as suggested by @davepagurek . Let me know if I can proceed with this and submit a PR for the issue, @RandomGamingDev . Thankyou! |
It looks great! You should proceed with the issue. After that, all that'll be left is returning attributes individually. |
Thanks, I'll submit a PR soon! |
I apologize for the delay in working on this issue due to some health issue, just in my recovery phase will get back to working on this super soon, till then If anyone wants to work on this, you can, I would try giving my reviews for that too. |
Increasing access
This would mean that transformations can be fully managed by p5.js, which not only means increased ease of control for the user, but also not having to have 2 sets of data for the same thing, one on p5.js's side and one on the user's side.
Most appropriate sub-area of p5.js?
Feature request details
While this is already possible in
P2D
mode withdrawingContext.getTransform();
it doesn't apply to WebGL which doesn't useCanvasRenderingContext2D
. Also, the transform returned withdrawingContext.getTransform();
is a transformation matrix, which many people won't know how to use.The way this would be done is through an official p5.js functions that return the transformation matrix, transform or untransform points, or return attributes that are decided as essential (the rest can be taken from the transformation matrix).
The text was updated successfully, but these errors were encountered: