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

Fix Gizmo Scale Error #237

Merged
merged 2 commits into from
Dec 12, 2023
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
48 changes: 24 additions & 24 deletions packages/gizmo/src/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,38 +179,38 @@ export class Group {
}

/**
* set group's world matrix
* @param value the new world matrix for the group
* 从上个状态的矩阵变换到目标矩阵
* from 矩阵计算所有节点的在本次变换中的 local 姿态
* to 矩阵计算所有节点的在本次变换后的 world 姿态
* @param from - 初始矩阵
* @param to - 目标矩阵
*/
setWorldMatrix(value: Matrix): void {
if (this.getWorldMatrix()) {
const { _worldMatrix: worldMatrix } = this;
if (!Matrix.equals(worldMatrix, value)) {
// old worldMatrix.
const { _tempMat0: groupWorldInvMat, _tempMat1: nodeMat } = Group;
Matrix.invert(worldMatrix, groupWorldInvMat);
// new worldMatrix.
worldMatrix.copyFrom(value);
const { _entities: entities } = this;
// update entities worldMatrix
for (let i = entities.length - 1; i >= 0; i--) {
const nodeTrans = entities[i].transform;
// get entity's localMatrix.
Matrix.multiply(groupWorldInvMat, nodeTrans.worldMatrix, nodeMat);
// update entity's worldMatrix.
Matrix.multiply(worldMatrix, nodeMat, nodeMat);
nodeTrans.worldMatrix = nodeMat;
}
}
this._dirtyFlag = GroupDirtyFlag.None;
applyTransform(from: Matrix, to: Matrix): void {
const { _entities: entities } = this;
if (this._entities.length <= 0) {
return;
}
if (Matrix.equals(from, to)) {
return;
}
// old worldMatrix.
const { _tempMat0: groupWorldInvMat, _tempMat1: nodeMat } = Group;
Matrix.invert(from, groupWorldInvMat);
// update entities worldMatrix
for (let i = entities.length - 1; i >= 0; i--) {
const nodeTrans = entities[i].transform;
// get entity's localMatrix.
Matrix.multiply(groupWorldInvMat, nodeTrans.worldMatrix, nodeMat);
// update entity's worldMatrix.
Matrix.multiply(to, nodeMat, nodeMat);
nodeTrans.worldMatrix = nodeMat;
}
}

/**
* force update group dirty flag
* @param flag - group dirty flag
*/

setDirtyFlagTrue(flag: GroupDirtyFlag): void {
this._dirtyFlag |= flag;
this._gizmoTransformDirty = true;
Expand Down
13 changes: 10 additions & 3 deletions packages/gizmo/src/Rotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class RotateControl extends GizmoComponent {
private _rotateHelperPlaneMesh = GizmoMesh.createCircle(this.engine);

private _selectedAxis: axisType;
private _preMatrix: Matrix = new Matrix();
private _startMatrix: Matrix = new Matrix();
private _startInvMatrix: Matrix = new Matrix();

Expand Down Expand Up @@ -152,6 +153,7 @@ export class RotateControl extends GizmoComponent {
startHelperRenderer.castShadows = false;
startHelperRenderer.mesh = this._startLineMesh;
startHelperRenderer.setMaterial(Utils.yellowMaterial);
startHelperRenderer.priority = 90;

// rotate end line
this._endLineHelperEntity = this._gizmoRotateHelperEntity.createChild("lineHelperE");
Expand All @@ -160,6 +162,7 @@ export class RotateControl extends GizmoComponent {
endHelperRenderer.castShadows = false;
endHelperRenderer.mesh = this._endLineMesh;
endHelperRenderer.setMaterial(Utils.yellowMaterial);
endHelperRenderer.priority = 90;

// rotate plane
this._rotateHelperPlaneEntity = this._gizmoRotateHelperEntity.createChild("rotateHelperPlane");
Expand All @@ -170,6 +173,7 @@ export class RotateControl extends GizmoComponent {
// @ts-ignore
this._rotateHelperPlaneMesh._enableVAO = false;
planeHelperRenderer.setMaterial(Utils.rotatePlaneMaterial);
planeHelperRenderer.priority = 90;
this._rotateHelperPlaneEntity.isActive = false;
}

Expand Down Expand Up @@ -205,6 +209,7 @@ export class RotateControl extends GizmoComponent {
} = this;

group.getWorldMatrix(startMat);
this._preMatrix.copyFrom(startMat);
Matrix.invert(startMat, this._startInvMatrix);

const s = this._getGizmoScale();
Expand Down Expand Up @@ -261,8 +266,8 @@ export class RotateControl extends GizmoComponent {
GizmoMesh.updateCircle(this._rotateHelperPlaneMesh, startP, localAxis, rad);

Matrix.rotateAxisAngle(startMat, localAxis, rad, mat);
group.setWorldMatrix(mat);

group.applyTransform(this._preMatrix, mat);
this._preMatrix.copyFrom(mat);
const d = (rad / Math.PI) * 180;
this._endLineHelperEntity.transform.setRotation(d * localAxis.x, d * localAxis.y, d * localAxis.z);
break;
Expand All @@ -281,7 +286,9 @@ export class RotateControl extends GizmoComponent {
Vector3.transformNormal(tempVec, this._startInvMatrix, tempVec);
const angle = pointer.deltaPosition.length() * this._speedFactor;
Matrix.rotateAxisAngle(startMat, tempVec, angle, startMat);
group.setWorldMatrix(startMat);
group.applyTransform(this._preMatrix, startMat);
this._preMatrix.copyFrom(startMat);

Matrix.invert(startMat, this._startInvMatrix);
break;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/gizmo/src/Scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export class ScaleControl extends GizmoComponent {
private _scaleControlMap: Array<AxisProps> = [];

private _selectedAxis: axisType;

private _preMatrix: Matrix = new Matrix();
private _startGroupMatrix: Matrix = new Matrix();
private _startInvMatrix: Matrix = new Matrix();
private _startPoint: Vector3 = new Vector3();
Expand Down Expand Up @@ -56,6 +58,7 @@ export class ScaleControl extends GizmoComponent {
this._selectedAxis = axisType[axisName];
// get gizmo start worldPosition
this._group.getWorldMatrix(this._startGroupMatrix);
this._preMatrix.copyFrom(this._startGroupMatrix);
Matrix.invert(this._startGroupMatrix, this._startInvMatrix);
const { _startPoint, _scaleFactor } = this;

Expand Down Expand Up @@ -107,7 +110,8 @@ export class ScaleControl extends GizmoComponent {
}

Matrix.scale(this._startGroupMatrix, scaleVec, mat);
this._group.setWorldMatrix(mat);
this._group.applyTransform(this._preMatrix, mat);
this._preMatrix.copyFrom(mat);
}

onMoveEnd(): void {
Expand Down
6 changes: 5 additions & 1 deletion packages/gizmo/src/Translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export class TranslateControl extends GizmoComponent {
private _translateControlMap: Array<AxisProps>;

private _selectedAxis: axisType;

private _preMatrix: Matrix = new Matrix();
private _startGroupMatrix: Matrix = new Matrix();
private _startInvMatrix: Matrix = new Matrix();
private _startScale: number = 1;
Expand Down Expand Up @@ -61,6 +63,7 @@ export class TranslateControl extends GizmoComponent {
this._selectedAxis = axisType[axisName];
// get gizmo start worldPosition
this._group.getWorldMatrix(this._startGroupMatrix);
this._preMatrix.copyFrom(this._startGroupMatrix);
Matrix.invert(this._startGroupMatrix, this._startInvMatrix);

// get start scale
Expand Down Expand Up @@ -100,7 +103,8 @@ export class TranslateControl extends GizmoComponent {
mat.elements[14] = subVec.z * localAxis.z;

Matrix.multiply(this._startGroupMatrix, mat, mat);
this._group.setWorldMatrix(mat);
this._group.applyTransform(this._preMatrix, mat);
this._preMatrix.copyFrom(mat);
}

onMoveEnd(): void {
Expand Down
Loading