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 scale fix v1 #6830

Merged
merged 5 commits into from
Jul 18, 2024
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
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
});
camera.addComponent('script');
const orbitCamera = camera.script.create('orbitCamera');
Expand Down
2 changes: 1 addition & 1 deletion src/extras/gizmo/axis-shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const SHADER = {
// store z/w for later use in fragment shader
vZW = gl_Position.zw;
// disable depth clipping
gl_Position.z = 0.0;
// gl_Position.z = 0.0;
#endif
}`,
frag: /* glsl */`
Expand Down
28 changes: 6 additions & 22 deletions src/extras/gizmo/gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,6 @@ class Gizmo extends EventHandler {
*/
static EVENT_RENDERUPDATE = 'render:update';

/**
* Internal device start size.
*
* @type {number}
* @private
*/
_deviceStartSize;

/**
* Internal version of the gizmo size. Defaults to 1.
*
Expand Down Expand Up @@ -343,13 +335,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 @@ -380,11 +365,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.distance(cameraPos);
this._scale = Math.tan(0.5 * this._camera.fov * math.DEG_TO_RAD) * dist * PERS_SCALE_RATIO;
} else {
this._scale = this._camera.orthoHeight * ORTHO_SCALE_RATIO;
}
Expand All @@ -395,14 +379,14 @@ class Gizmo extends EventHandler {
}

_getSelection(x, y) {
const start = this._camera.screenToWorld(x, y, 1);
const start = this._camera.screenToWorld(x, y, this._camera.nearClip);
const end = this._camera.screenToWorld(x, y, this._camera.farClip);
const dir = end.clone().sub(start).normalize();

const selection = [];
for (let i = 0; i < this.intersectData.length; i++) {
const { triData, parent, meshInstances } = this.intersectData[i];
const wtm = parent.getWorldTransform().clone();
const wtm = parent.getWorldTransform();
for (let j = 0; j < triData.length; j++) {
const { tris, ptm, priority } = triData[j];
tmpM1.copy(wtm).mul(ptm);
Expand Down