Skip to content
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
2 changes: 1 addition & 1 deletion examples/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<!-- Camera (with XR support) -->
<pc-entity name="camera root">
<pc-entity name="camera" position="0 1.75 8">
<pc-camera clear-color="black"></pc-camera>
<pc-camera></pc-camera>
<pc-scripts>
<pc-script name="cameraControls" attributes='{
"focusPoint": "vec3:0,1.75,0",
Expand Down
15 changes: 12 additions & 3 deletions examples/assets/scripts/annotation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,19 @@ export class Annotation extends Script {
* @private
*/
_calculateScreenSpaceScale() {
const DESIRED_PIXEL_SIZE = 25;

const cameraPos = this.camera.entity.getPosition();
const cameraForward = this.camera.entity.forward;
const toAnnotation = this.entity.getPosition().sub(cameraPos);
const distanceToNearPlane = toAnnotation.dot(cameraForward);
return distanceToNearPlane * Math.tan(this.camera.fov * Math.PI / 180) * 0.025;
const distance = toAnnotation.length();

// Get the camera's projection matrix vertical scale factor
const projMatrix = this.camera.projectionMatrix;
const screenHeight = this.app.graphicsDevice.height;

// Calculate world size needed for desired pixel size
const worldSize = (DESIRED_PIXEL_SIZE / screenHeight) * (2 * distance / projMatrix.data[5]);

return worldSize;
}
}