From e75b394008f04adbfdb1444f6931d751c647a338 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Sun, 12 Jan 2020 15:09:32 +0800 Subject: [PATCH] fix convertToUINode (#6085) --- cocos/core/3d/framework/camera-component.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cocos/core/3d/framework/camera-component.ts b/cocos/core/3d/framework/camera-component.ts index 312db52030..d912d30f2b 100644 --- a/cocos/core/3d/framework/camera-component.ts +++ b/cocos/core/3d/framework/camera-component.ts @@ -28,6 +28,7 @@ */ import { RenderTexture } from '../../assets/render-texture'; +import { UITransformComponent } from '../../components'; import { Component } from '../../components/component'; import { ccclass, executeInEditMode, menu, property } from '../../data/class-decorator'; import { ray } from '../../geom-utils'; @@ -38,9 +39,8 @@ import { CameraDefaultMask } from '../../pipeline/define'; import { Camera } from '../../renderer'; import { SKYBOX_FLAG } from '../../renderer/scene/camera'; import { Root } from '../../root'; -import { Layers, Scene, Node } from '../../scene-graph'; +import { Layers, Node, Scene } from '../../scene-graph'; import { Enum } from '../../value-types'; -import { UITransformComponent } from '../../components'; const _temp_vec3_1 = new Vec3(); @@ -444,22 +444,24 @@ export class CameraComponent extends Component { * uiNode.position = out; * ``` */ - public convertToUINode(wpos: Vec3, uiNode: Node, out?: Vec3){ + public convertToUINode (wpos: Vec3, uiNode: Node, out?: Vec3){ if (!out) { out = new Vec3(); } + if (!this._camera) { return out; } this.worldToScreen(wpos, _temp_vec3_1); - _temp_vec3_1.x = _temp_vec3_1.x / cc.view.getScaleX(); - _temp_vec3_1.y = _temp_vec3_1.y / cc.view.getScaleY(); const cmp = uiNode.getComponent('cc.UITransformComponent') as UITransformComponent; + const designSize = cc.view._designResolutionSize; + const xoffset = _temp_vec3_1.x - this._camera!.width * 0.5; + const yoffset = _temp_vec3_1.y - this._camera!.height * 0.5; + _temp_vec3_1.x = xoffset / cc.view.getScaleX() + designSize.width * 0.5; + _temp_vec3_1.y = yoffset / cc.view.getScaleY() + designSize.height * 0.5; - if (!cmp) { - return out; + if (cmp) { + cmp.convertToNodeSpaceAR(_temp_vec3_1, out); } - cmp.convertToNodeSpaceAR(_temp_vec3_1, out); - return out; }