Skip to content

Commit

Permalink
Added forward getter to gizmo and fixed plane flipping for ortho
Browse files Browse the repository at this point in the history
  • Loading branch information
kpal81xd committed Oct 24, 2024
1 parent a95cea4 commit 54fd4fe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
12 changes: 12 additions & 0 deletions src/extras/gizmo/gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { GIZMOSPACE_LOCAL, GIZMOSPACE_WORLD } from './constants.js';

// temporary variables
const tmpV1 = new Vec3();
const tmpV2 = new Vec3();
const tmpM1 = new Mat4();
const tmpM2 = new Mat4();
const tmpR1 = new Ray();
Expand Down Expand Up @@ -337,6 +338,17 @@ class Gizmo extends EventHandler {
return this._size;
}

/**
* @type {Vec3}
* @protected
*/
get forward() {
if (this._camera.projection === PROJECTION_PERSPECTIVE) {
return tmpV2.copy(this.root.getPosition()).sub(this._camera.entity.getPosition()).normalize();
}
return tmpV2.copy(this._camera.entity.forward);
}

/**
* @param {PointerEvent} e - The pointer event.
* @private
Expand Down
23 changes: 7 additions & 16 deletions src/extras/gizmo/scale-gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ class ScaleGizmo extends TransformGizmo {
*/
_nodeScales = new Map();

/**
* Internal forward vector of the camera in the direction of the gizmo.
*
* @type {Vec3}
* @private
*/
_forward = new Vec3();

/**
* Internal state if transform should use uniform scaling.
*
Expand Down Expand Up @@ -380,39 +372,38 @@ class ScaleGizmo extends TransformGizmo {
* @private
*/
_shapesLookAtCamera() {
this._forward.copy(this.root.getPosition()).sub(this._camera.entity.getPosition()).normalize();

let dot = this._forward.dot(this.root.right);
const forward = this.forward;
let dot = forward.dot(this.root.right);
this._shapes.x.entity.enabled = Math.abs(dot) < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.x.flipped = dot > 0;
}

dot = this._forward.dot(this.root.up);
dot = forward.dot(this.root.up);
this._shapes.y.entity.enabled = Math.abs(dot) < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.y.flipped = dot > 0;
}

dot = this._forward.dot(this.root.forward);
dot = forward.dot(this.root.forward);
this._shapes.z.entity.enabled = Math.abs(dot) < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.z.flipped = dot < 0;
}

tmpV1.cross(this._forward, this.root.right);
tmpV1.cross(forward, this.root.right);
this._shapes.yz.entity.enabled = tmpV1.length() < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.yz.flipped = tmpV2.set(0, +(tmpV1.dot(this.root.forward) > 0), +(tmpV1.dot(this.root.up) > 0));
}

tmpV1.cross(this._forward, this.root.forward);
tmpV1.cross(forward, this.root.forward);
this._shapes.xy.entity.enabled = tmpV1.length() < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.xy.flipped = tmpV2.set(+(tmpV1.dot(this.root.up) > 0), +(tmpV1.dot(this.root.right) < 0), 0);
}

tmpV1.cross(this._forward, this.root.up);
tmpV1.cross(forward, this.root.up);
this._shapes.xz.entity.enabled = tmpV1.length() < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.xz.flipped = tmpV2.set(+(tmpV1.dot(this.root.forward) < 0), 0, +(tmpV1.dot(this.root.right) < 0));
Expand Down
1 change: 0 additions & 1 deletion src/extras/gizmo/transform-gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class TransformGizmo extends Gizmo {
*/
_rootStartPos = new Vec3();


/**
* Internal gizmo starting rotation in world space.
*
Expand Down
23 changes: 7 additions & 16 deletions src/extras/gizmo/translate-gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ class TranslateGizmo extends TransformGizmo {
*/
_nodePositions = new Map();

/**
* Internal forward vector of the camera in the direction of the gizmo.
*
* @type {Vec3}
* @private
*/
_forward = new Vec3();

/**
* @override
*/
Expand Down Expand Up @@ -369,39 +361,38 @@ class TranslateGizmo extends TransformGizmo {
* @private
*/
_shapesLookAtCamera() {
this._forward.copy(this.root.getPosition()).sub(this._camera.entity.getPosition()).normalize();

let dot = this._forward.dot(this.root.right);
const forward = this.forward;
let dot = forward.dot(this.root.right);
this._shapes.x.entity.enabled = Math.abs(dot) < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.x.flipped = dot > 0;
}

dot = this._forward.dot(this.root.up);
dot = forward.dot(this.root.up);
this._shapes.y.entity.enabled = Math.abs(dot) < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.y.flipped = dot > 0;
}

dot = this._forward.dot(this.root.forward);
dot = forward.dot(this.root.forward);
this._shapes.z.entity.enabled = Math.abs(dot) < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.z.flipped = dot < 0;
}

tmpV1.cross(this._forward, this.root.right);
tmpV1.cross(forward, this.root.right);
this._shapes.yz.entity.enabled = tmpV1.length() < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.yz.flipped = tmpV2.set(0, +(tmpV1.dot(this.root.forward) > 0), +(tmpV1.dot(this.root.up) > 0));
}

tmpV1.cross(this._forward, this.root.forward);
tmpV1.cross(forward, this.root.forward);
this._shapes.xy.entity.enabled = tmpV1.length() < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.xy.flipped = tmpV2.set(+(tmpV1.dot(this.root.up) > 0), +(tmpV1.dot(this.root.right) < 0), 0);
}

tmpV1.cross(this._forward, this.root.up);
tmpV1.cross(forward, this.root.up);
this._shapes.xz.entity.enabled = tmpV1.length() < GLANCE_EPSILON;
if (this.flipShapes) {
this._shapes.xz.flipped = tmpV2.set(+(tmpV1.dot(this.root.forward) < 0), 0, +(tmpV1.dot(this.root.right) < 0));
Expand Down

0 comments on commit 54fd4fe

Please sign in to comment.