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 orthogonal facing direction translation #6607

Merged
merged 2 commits into from
May 24, 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
4 changes: 2 additions & 2 deletions examples/src/examples/misc/gizmos.controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function controls({ observer, ReactPCUI, React, jsx, fragment }) {
precision: 2
})
),
type === 'scale' &&
(type === 'translate' || type === 'scale') &&
jsx(
LabelGroup,
{ text: 'Center Tolerance' },
Expand Down Expand Up @@ -223,7 +223,7 @@ export function controls({ observer, ReactPCUI, React, jsx, fragment }) {
link: { observer, path: 'gizmo.axisPlaneGap' }
})
),
type === 'scale' &&
(type === 'translate' || type === 'scale') &&
jsx(
LabelGroup,
{ text: 'Center Size' },
Expand Down
51 changes: 50 additions & 1 deletion src/extras/gizmo/axis-shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { BoxGeometry } from '../../scene/geometry/box-geometry.js';
import { CylinderGeometry } from '../../scene/geometry/cylinder-geometry.js';
import { ConeGeometry } from '../../scene/geometry/cone-geometry.js';
import { PlaneGeometry } from '../../scene/geometry/plane-geometry.js';
import { SphereGeometry } from '../../scene/geometry/sphere-geometry.js';
import { TorusGeometry } from '../../scene/geometry/torus-geometry.js';

// constants
Expand All @@ -32,6 +33,7 @@ const GEOMETRIES = {
cone: ConeGeometry,
cylinder: CylinderGeometry,
plane: PlaneGeometry,
sphere: SphereGeometry,
torus: TorusGeometry
};

Expand Down Expand Up @@ -690,4 +692,51 @@ class AxisPlane extends AxisShape {
}
}

export { AxisShape, AxisArrow, AxisBoxCenter, AxisBoxLine, AxisDisk, AxisPlane };
class AxisSphereCenter extends AxisShape {
_size = 0.12;

_tolerance = 0.05;

constructor(device, options = {}) {
super(device, options);

this.triData = [
new TriData(new SphereGeometry(), 2)
];

this._createCenter();
}

_createCenter() {
this._createRoot('sphereCenter');
this._updateTransform();

// box
this._addRenderShadowMesh(this.entity, 'sphere');
}

set size(value) {
this._size = value ?? 1;
this._updateTransform();
}

get size() {
return this._size;
}

set tolerance(value) {
this._tolerance = value;
this._updateTransform();
}

get tolerance() {
return this._tolerance;
}

_updateTransform() {
// intersect/render
this.entity.setLocalScale(this._size, this._size, this._size);
}
}

export { AxisShape, AxisArrow, AxisBoxCenter, AxisBoxLine, AxisDisk, AxisPlane, AxisSphereCenter };
15 changes: 12 additions & 3 deletions src/extras/gizmo/transform-gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ class TransformGizmo extends Gizmo {
face: COLOR_YELLOW.clone()
};

/**
* Internal gizmo starting rotation in world space.
*
* @type {Vec3}
* @protected
*/
_rootStartPos = new Vec3();


/**
* Internal gizmo starting rotation in world space.
*
Expand Down Expand Up @@ -321,6 +330,7 @@ class TransformGizmo extends Gizmo {

this._selectedAxis = this._getAxis(meshInstance);
this._selectedIsPlane = this._getIsPlane(meshInstance);
this._rootStartPos.copy(this.root.getPosition());
this._rootStartRot.copy(this.root.getRotation());
const pointInfo = this._screenToPoint(x, y);
this._selectionStartPoint.copy(pointInfo.point);
Expand Down Expand Up @@ -518,9 +528,8 @@ class TransformGizmo extends Gizmo {

_createPlane(axis, isFacing, isLine) {
const cameraPos = this._camera.entity.getPosition();
const gizmoPos = this.root.getPosition();

const facingDir = tmpV1.sub2(cameraPos, gizmoPos).normalize();
const facingDir = tmpV1.sub2(cameraPos, this._rootStartPos).normalize();
const normal = tmpP1.normal.set(0, 0, 0);

if (isFacing) {
Expand All @@ -538,7 +547,7 @@ class TransformGizmo extends Gizmo {
}
}

return tmpP1.setFromPointNormal(gizmoPos, normal);
return tmpP1.setFromPointNormal(this._rootStartPos, normal);
}

_projectToAxis(point, axis) {
Expand Down
40 changes: 36 additions & 4 deletions src/extras/gizmo/translate-gizmo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Vec3 } from '../../core/math/vec3.js';
import { Quat } from '../../core/math/quat.js';

import { AxisArrow, AxisPlane } from './axis-shapes.js';
import { AxisArrow, AxisPlane, AxisSphereCenter } from './axis-shapes.js';
import { GIZMO_LOCAL } from './gizmo.js';
import { TransformGizmo } from "./transform-gizmo.js";

Expand All @@ -17,6 +17,12 @@ const tmpQ1 = new Quat();
*/
class TranslateGizmo extends TransformGizmo {
_shapes = {
face: new AxisSphereCenter(this._device, {
axis: 'face',
layers: [this._layer.id],
defaultColor: this._meshColors.axis.xyz,
hoverColor: this._meshColors.hover.xyz
}),
yz: new AxisPlane(this._device, {
axis: 'x',
flipAxis: 'y',
Expand Down Expand Up @@ -223,6 +229,32 @@ class TranslateGizmo extends TransformGizmo {
return this._shapes.yz.gap;
}

/**
* Axis center size.
*
* @type {number}
*/
set axisCenterSize(value) {
this._shapes.face.size = value;
}

get axisCenterSize() {
return this._shapes.face.size;
}

/**
* Axis center tolerance.
*
* @type {number}
*/
set axisCenterTolerance(value) {
this._shapes.face.tolerance = value;
}

get axisCenterTolerance() {
return this._shapes.face.tolerance;
}

_setArrowProp(prop, value) {
this._shapes.x[prop] = value;
this._shapes.y[prop] = value;
Expand All @@ -248,7 +280,7 @@ class TranslateGizmo extends TransformGizmo {
const node = this.nodes[i];
if (this._coordSpace === GIZMO_LOCAL) {
tmpV1.copy(pointDelta);
node.parent.getWorldTransform().getScale(tmpV2);
node.parent?.getWorldTransform().getScale(tmpV2);
tmpV2.x = 1 / tmpV2.x;
tmpV2.y = 1 / tmpV2.y;
tmpV2.z = 1 / tmpV2.z;
Expand All @@ -270,7 +302,7 @@ class TranslateGizmo extends TransformGizmo {
const isPlane = this._selectedIsPlane;

const ray = this._createRay(mouseWPos);
const plane = this._createPlane(axis, false, !isPlane);
const plane = this._createPlane(axis, axis === 'face', !isPlane);

const point = new Vec3();
const angle = 0;
Expand All @@ -280,7 +312,7 @@ class TranslateGizmo extends TransformGizmo {
// rotate point back to world coords
tmpQ1.copy(this._rootStartRot).invert().transformVector(point, point);

if (!isPlane) {
if (!isPlane && axis !== 'face') {
this._projectToAxis(point, axis);
}

Expand Down