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

Gizmo fixes #6763

Merged
merged 11 commits into from
Jun 28, 2024
Prev Previous commit
Next Next commit
fixed gizmo scaling based on fov and aspect ratio
  • Loading branch information
kpal81xd committed Jun 27, 2024
commit 2f65e2aecde547b43cd0b99a1111884e37a58d94
3 changes: 2 additions & 1 deletion examples/src/examples/misc/gizmos.example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ data.set('camera', {
});
const camera = new pc.Entity('camera');
camera.addComponent('camera', {
clearColor: new pc.Color(0.1, 0.1, 0.1)
clearColor: new pc.Color(0.1, 0.1, 0.1),
farClip: 1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why won't the example work with the default far clip?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does it you can just see the the axis lines clipping when zooming out

});
camera.addComponent('script');
const orbitCamera = camera.script.create('orbitCamera');
Expand Down
16 changes: 4 additions & 12 deletions src/extras/gizmo/gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,6 @@ class Gizmo extends EventHandler {
return this._size;
}

_getProjFrustumWidth() {
const gizmoPos = this.root.getPosition();
const cameraPos = this._camera.entity.getPosition();
const dist = tmpV1.copy(gizmoPos).sub(cameraPos).dot(this._camera.entity.forward);
return dist * Math.tan(0.5 * this._camera.fov * math.DEG_TO_RAD);
}

_createGizmo() {
this.root = new Entity('gizmo');
this._app.root.addChild(this.root);
Expand Down Expand Up @@ -378,11 +371,10 @@ class Gizmo extends EventHandler {

_updateScale() {
if (this._camera.projection === PROJECTION_PERSPECTIVE) {
let canvasMult = 1;
if (this._device.width > 0 && this._device.height > 0) {
canvasMult = this._deviceStartSize / Math.min(this._device.width, this._device.height);
}
this._scale = this._getProjFrustumWidth() * canvasMult * PERS_SCALE_RATIO;
const gizmoPos = this.root.getPosition();
const cameraPos = this._camera.entity.getPosition();
const dist = gizmoPos.sub(cameraPos).length();
this._scale = Math.tan(0.5 * this._camera.fov * math.DEG_TO_RAD * Math.max(1, 1 / this._camera.aspectRatio)) * dist * PERS_SCALE_RATIO;
} else {
this._scale = this._camera.orthoHeight * ORTHO_SCALE_RATIO;
}
Expand Down