Skip to content

Commit

Permalink
Solves issue processing#7059
Browse files Browse the repository at this point in the history
  • Loading branch information
Garima3110 committed Jul 4, 2024
1 parent 32ac1e4 commit fc28f28
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/core/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,49 @@ function exitFullscreen() {
}
}


/**
* Converts 3D world coordinates to 2D screen coordinates.
*
* This function takes a 3D vector and converts its coordinates
* from the world space to screen space. This is useful for placing
* 2D elements in a 3D scene or for determining the screen position
* of 3D objects.
*
* @method worldToScreen
* @param {p5.Vector} worldPosition The 3D coordinates in the world space.
* @return {p5.Vector} A vector containing the 2D screen coordinates.
* @example
* <div>
* <code>
* </code>
* </div>
* @alt
*
*/
p5.prototype.worldToScreen = function(worldPosition) {
const renderer = this._renderer;
if (renderer.drawingContext instanceof CanvasRenderingContext2D) {
// Handle 2D context
const transformMatrix = new DOMMatrix()
.scale(1 / pixelDensity())
.multiply(renderer.drawingContext.getTransform());
const screenCoordinates = transformMatrix.transformPoint(
new DOMPoint(worldPosition.x, worldPosition.y));
return createVector(screenCoordinates.x, screenCoordinates.y);
} else{
// Handle WebGL context
const cameraCoordinates = renderer.uMVMatrix.multiplyPoint(worldPosition);
const normalizedDeviceCoordinates =
renderer.uPMatrix.multiplyAndNormalizePoint(cameraCoordinates);
const screenX = (0.5 + 0.5 * normalizedDeviceCoordinates.x) * this.width;
const screenY = (0.5 - 0.5 * normalizedDeviceCoordinates.y) * this.height;
const screenZ = (0.5 + 0.5 * normalizedDeviceCoordinates.z);
return createVector(screenX, screenY, screenZ);
}
};


/**
* Returns the sketch's current
* <a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_URL" target="_blank">URL</a>
Expand Down

0 comments on commit fc28f28

Please sign in to comment.