From ea700afb50eef1eb8dc6da157d107d38c076db04 Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Sat, 4 Jan 2020 11:22:14 +0800 Subject: [PATCH] fix coordinate replace function (#6025) save --- cocos/core/3d/framework/camera-component.ts | 13 +++++++++++++ cocos/core/utils/coordinates-converts-utils.ts | 17 ++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/cocos/core/3d/framework/camera-component.ts b/cocos/core/3d/framework/camera-component.ts index 714c1a5d8e..312db52030 100644 --- a/cocos/core/3d/framework/camera-component.ts +++ b/cocos/core/3d/framework/camera-component.ts @@ -431,6 +431,19 @@ export class CameraComponent extends Component { return out; } + /** + * @zh 3D 节点转 UI 本地节点坐标。 + * 注意:千万不要设置负责做转换的 uiNode 和最终设置位置的 uiNode 是同一个 node,否则可能出现跳动现象。 + * @param wpos 3D 节点事件坐标 + * @param uiNode UI 节点 + * @param out 返回在当前传入的 UI 节点下的偏移量 + * + * @example + * ```typescript + * this.convertToUINode(target.worldPosition, uiNode.parent, out); + * uiNode.position = out; + * ``` + */ public convertToUINode(wpos: Vec3, uiNode: Node, out?: Vec3){ if (!out) { out = new Vec3(); diff --git a/cocos/core/utils/coordinates-converts-utils.ts b/cocos/core/utils/coordinates-converts-utils.ts index 2579555db1..5d8f601487 100644 --- a/cocos/core/utils/coordinates-converts-utils.ts +++ b/cocos/core/utils/coordinates-converts-utils.ts @@ -7,6 +7,8 @@ import { Vec3 } from '../math'; import { Node } from '../scene-graph'; import { replaceProperty } from './deprecated'; +const _vec3 = new Vec3(); + /** * @en * Conversion of non-UI nodes to UI Node (Local) Space coordinate system. @@ -60,12 +62,17 @@ const convertUtils = { export { convertUtils }; cc.pipelineUtils = convertUtils; -replaceProperty(cc, 'cc', [ +replaceProperty(cc.pipelineUtils, 'cc.pipelineUtils', [ { - name: 'pipelineUtils', - newName: 'CameraComponent.convertToUINode', - customGetter: () => { - return CameraComponent; + 'name': 'WorldNode3DToLocalNodeUI', + 'newName': 'convertToUINode', + 'targetName': 'cc.CameraComponent.prototype', + 'customFunction': function (...args: any[]) { + const camera = args[0] as CameraComponent; + const out = args[3] || _vec3; + camera.convertToUINode(args[1], args[2], out); + out.add(args[2].position); + return args[3] || out.clone(); }, }, ]);