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

Add RectTool #313

Open
wants to merge 1 commit into
base: dev/1.4
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions packages/gizmo/src/Gizmo.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import {
Camera,
Component,
Entity,
Ray,
Layer,
PointerButton,
Vector3,
MathUtil,
Script,
Matrix,
MeshRenderer,
Pointer,
PointerButton,
PointerPhase,
Ray,
Script,
Vector2,
Component,
MeshRenderer,
Matrix
Vector3
} from "@galacean/engine";
import { FramebufferPicker } from "@galacean/engine-toolkit-framebuffer-picker";
import { Group, GroupDirtyFlag } from "./Group";
import { RectControl } from "./Rect";
import { RotateControl } from "./Rotate";
import { ScaleControl } from "./Scale";
import { TranslateControl } from "./Translate";
import { RotateControl } from "./Rotate";
import { GizmoComponent } from "./Type";
import { Utils } from "./Utils";
import { State } from "./enums/GizmoState";
import { Group, GroupDirtyFlag } from "./Group";
import { FramebufferPicker } from "@galacean/engine-toolkit-framebuffer-picker";
/**
* Gizmo controls, including translate, rotate, scale
*/
Expand Down Expand Up @@ -141,6 +142,7 @@ export class Gizmo extends Script {
this._createGizmoControl(State.translate, TranslateControl);
this._createGizmoControl(State.rotate, RotateControl);
this._createGizmoControl(State.scale, ScaleControl);
this._createGizmoControl(State.rect, RectControl);

this.layer = Layer.Layer31;
this.state = this._type;
Expand Down
28 changes: 22 additions & 6 deletions packages/gizmo/src/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class Group {
private _coordinateType: CoordinateType = CoordinateType.Local;
private _dirtyFlag: GroupDirtyFlag = GroupDirtyFlag.All;

get entities(): Entity[] {
return this._entities;
}

/**
* get anchor type
* @return anchor type, pivot or center
Expand Down Expand Up @@ -178,6 +182,15 @@ export class Group {
}
}

/**
* 获取主要的 Entity,即第一个选中的 Entity
* @return Entity
*/
getPrimaryEntity(): Entity {
const { _entities: entities } = this;
return entities.length > 0 ? entities[0] : null;
}

/**
* 从上个状态的矩阵变换到目标矩阵
* from 矩阵计算所有节点的在本次变换中的 local 姿态
Expand All @@ -187,7 +200,7 @@ export class Group {
*/
applyTransform(from: Matrix, to: Matrix): void {
const { _entities: entities } = this;
if (this._entities.length <= 0) {
if (entities.length <= 0) {
return;
}
if (Matrix.equals(from, to)) {
Expand Down Expand Up @@ -288,8 +301,9 @@ export class Group {
(e[12] = tempVec3.x), (e[13] = tempVec3.y), (e[14] = tempVec3.z);
break;
case AnchorType.Pivot:
// align to the first entity
const worldE = this._entities[0].transform.worldMatrix.elements;
// align to the primary entity
const primaryEntity = this.getPrimaryEntity();
const worldE = primaryEntity.transform.worldMatrix.elements;
Comment on lines +304 to +306
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Verify primary entity existence before access.

Both _updateAnchor and _updateCoordinate methods access the primary entity's properties without checking if it exists, which could lead to null pointer exceptions.

-const primaryEntity = this.getPrimaryEntity();
-const worldE = primaryEntity.transform.worldMatrix.elements;
+const primaryEntity = this.getPrimaryEntity();
+if (!primaryEntity) {
+  return;
+}
+const worldE = primaryEntity.transform.worldMatrix.elements;

Also applies to: 319-321

(e[12] = worldE[12]), (e[13] = worldE[13]), (e[14] = worldE[14]);
break;
}
Expand All @@ -302,8 +316,9 @@ export class Group {
const { elements: e } = this._worldMatrix;
switch (this._coordinateType) {
case CoordinateType.Local:
// align to the first entity
const wE = this._entities[0].transform.worldMatrix.elements;
// align to the primary entity
const primaryEntity = this.getPrimaryEntity();
const wE = primaryEntity.transform.worldMatrix.elements;
const sx = 1 / Math.sqrt(wE[0] ** 2 + wE[1] ** 2 + wE[2] ** 2);
const sy = 1 / Math.sqrt(wE[4] ** 2 + wE[5] ** 2 + wE[6] ** 2);
const sz = 1 / Math.sqrt(wE[8] ** 2 + wE[9] ** 2 + wE[10] ** 2);
Expand All @@ -327,9 +342,10 @@ export class Group {
tempBoundBox.max.set(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
const { _entities: entities } = this;
let isEffective = false;
const renderers = [];
for (let i = entities.length - 1; i >= 0; i--) {
const entity = entities[i];
const renderers = entity.getComponentsIncludeChildren(Renderer, []);
entity.getComponentsIncludeChildren(Renderer, renderers);
for (let j = renderers.length - 1; j >= 0; j--) {
const renderer = renderers[j];
if (renderer.entity.isActiveInHierarchy) {
Expand Down
Loading
Loading