Skip to content

Commit

Permalink
Node.x/y/z are back. (#18228)
Browse files Browse the repository at this point in the history
It will be easier for just set one axis value. For example, while using Tween animation, we want to make a sprite jump up and down forever and move x to 100, it will be hard to do that if Node.x/y/z are absent.
  • Loading branch information
dumganhar authored Jan 22, 2025
1 parent 36d3d58 commit 9b4dfdb
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 1 deletion.
78 changes: 77 additions & 1 deletion cocos/scene-graph/node.jsb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export const Node: typeof JsbNode = jsb.Node;
export type Node = JsbNode;
cclegacy.Node = Node;

const NodeCls: any = Node;
const tempVec3 = new Vec3();

const NodeCls: any = Node;

NodeCls.reserveContentsForAllSyncablePrefabTag = reserveContentsForAllSyncablePrefabTag;

Expand Down Expand Up @@ -860,6 +861,39 @@ Object.defineProperty(nodeProto, 'position', {
},
});

Object.defineProperty(nodeProto, 'x', {
configurable: true,
enumerable: true,
get(): number {
return this._lpos.x;
},
set(v: number) {
this.setPosition(v, this._lpos.y, this._lpos.z);
},
});

Object.defineProperty(nodeProto, 'y', {
configurable: true,
enumerable: true,
get(): number {
return this._lpos.y;
},
set(v: number) {
this.setPosition(this._lpos.x, v, this._lpos.z);
},
});

Object.defineProperty(nodeProto, 'z', {
configurable: true,
enumerable: true,
get(): number {
return this._lpos.z;
},
set(v: number) {
this.setPosition(this._lpos.x, this._lpos.y, v);
},
});

Object.defineProperty(nodeProto, 'rotation', {
configurable: true,
enumerable: true,
Expand Down Expand Up @@ -893,6 +927,48 @@ Object.defineProperty(nodeProto, 'worldPosition', {
},
});

Object.defineProperty(nodeProto, 'worldPositionX', {
configurable: true,
enumerable: true,
get(): number {
this.getWorldPosition(tempVec3);
return tempVec3.x;
},
set(v: number) {
this.getWorldPosition(tempVec3);
tempVec3.x = v;
this.setWorldPosition(tempVec3);
},
});

Object.defineProperty(nodeProto, 'worldPositionY', {
configurable: true,
enumerable: true,
get(): number {
this.getWorldPosition(tempVec3);
return tempVec3.y;
},
set(v: number) {
this.getWorldPosition(tempVec3);
tempVec3.y = v;
this.setWorldPosition(tempVec3);
},
});

Object.defineProperty(nodeProto, 'worldPositionZ', {
configurable: true,
enumerable: true,
get(): number {
this.getWorldPosition(tempVec3);
return tempVec3.z;
},
set(v: number) {
this.getWorldPosition(tempVec3);
tempVec3.z = v;
this.setWorldPosition(tempVec3);
},
});

Object.defineProperty(nodeProto, 'worldRotation', {
configurable: true,
enumerable: true,
Expand Down
99 changes: 99 additions & 0 deletions cocos/scene-graph/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,54 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {
this.setPosition(val as Vec3);
}

/**
* @en Get x axis value in local coordinate system
* @zh 获取本地 x 轴坐标分量
*/
get x (): number {
return this._lpos.x;
}

/**
* @en Set x axis value in local coordinate system
* @zh 设置本地 x 轴坐标分量
*/
set x (val: number) {
this.setPosition(val, this._lpos.y, this._lpos.z);
}

/**
* @en Get y axis value in local coordinate system
* @zh 获取本地 y 轴坐标分量
*/
get y (): number {
return this._lpos.y;
}

/**
* @en Set y axis value in local coordinate system
* @zh 设置本地 y 轴坐标分量
*/
set y (val: number) {
this.setPosition(this._lpos.x, val, this._lpos.z);
}

/**
* @en Get z axis value in local coordinate system
* @zh 获取本地 z 轴坐标分量
*/
get z (): number {
return this._lpos.z;
}

/**
* @en Set z axis value in local coordinate system
* @zh 设置本地 z 轴坐标分量
*/
set z (val: number) {
this.setPosition(this._lpos.x, this._lpos.y, val);
}

/**
* @en Position in world coordinate system
* @zh 世界坐标系下的坐标
Expand All @@ -1606,6 +1654,57 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {
this.setWorldPosition(val as Vec3);
}

/**
* @en Get x axis value in world coordinate system
* @zh 获取世界坐标 x 轴分量
*/
get worldPositionX (): number {
this.updateWorldTransform();
return this._pos.x;
}

/**
* @en Set x axis value in world coordinate system
* @zh 设置世界坐标 x 轴分量
*/
set worldPositionX (val: number) {
this.setWorldPosition(val, this._pos.y, this._pos.z);
}

/**
* @en Get y axis value in world coordinate system
* @zh 获取世界坐标 y 轴分量
*/
get worldPositionY (): number {
this.updateWorldTransform();
return this._pos.y;
}

/**
* @en Set y axis value in world coordinate system
* @zh 设置世界坐标 y 轴分量
*/
set worldPositionY (val: number) {
this.setWorldPosition(this._pos.x, val, this._pos.z);
}

/**
* @en Get z axis value in world coordinate system
* @zh 获取世界坐标 z 轴分量
*/
get worldPositionZ (): number {
this.updateWorldTransform();
return this._pos.z;
}

/**
* @en Set z axis value in world coordinate system
* @zh 设置世界坐标 z 轴分量
*/
set worldPositionZ (val: number) {
this.setWorldPosition(this._pos.x, this._pos.y, val);
}

/**
* @en Rotation in local coordinate system, represented by a quaternion
* @zh 本地坐标系下的旋转,用四元数表示
Expand Down

1 comment on commit 9b4dfdb

@a1076559139
Copy link

Choose a reason for hiding this comment

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

/**x轴缩放 */
scaleX: number,
/**y轴缩放 */
scaleY: number,
/**z轴缩放 */
scaleZ: number,
/**x轴旋转 */
angleX: number,
/**y轴旋转 */
angleY: number,
/**z轴旋转, 等同于angle */
angleZ: number,

这些属性应该同样需要(吐槽一下,编辑器内叫rotation,api叫eulerAngles真的好奇怪,以产品/用户思维来看的话,这就是莫名其妙)

Please sign in to comment.