From bd9dec78c04c57e8f2b879f531e1cc1cfc2b7576 Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Wed, 3 Apr 2019 16:16:35 +0800 Subject: [PATCH] collation coordinate transformation (#4128) --- cocos/3d/ui/components/layout-component.ts | 4 +- .../3d/ui/components/scroll-bar-component.ts | 15 +++- cocos/3d/ui/components/slider-component.ts | 13 +-- .../ui/components/ui-transfrom-component.ts | 82 +++++-------------- cocos/pipeline/index.ts | 1 + cocos/pipeline/utils.ts | 54 ++++++++++++ 6 files changed, 92 insertions(+), 77 deletions(-) create mode 100644 cocos/pipeline/utils.ts diff --git a/cocos/3d/ui/components/layout-component.ts b/cocos/3d/ui/components/layout-component.ts index 617d8c7785..7e33df07be 100644 --- a/cocos/3d/ui/components/layout-component.ts +++ b/cocos/3d/ui/components/layout-component.ts @@ -932,14 +932,14 @@ export class LayoutComponent extends Component { vec3.set(_tempPos, allChildrenBoundingBox.x, allChildrenBoundingBox.y, 0); const leftBottomInParentSpace = new Vec3(); - parentTransform.convertToNodeSpaceAR(leftBottomInParentSpace, _tempPos); + parentTransform.convertToNodeSpaceAR(_tempPos, leftBottomInParentSpace); vec3.set(leftBottomInParentSpace, leftBottomInParentSpace.x - this._paddingLeft, leftBottomInParentSpace.y - this._paddingBottom, leftBottomInParentSpace.z); vec3.set(_tempPos, allChildrenBoundingBox.x + allChildrenBoundingBox.width, allChildrenBoundingBox.y + allChildrenBoundingBox.height, 0); const rightTopInParentSpace = new Vec3(); - parentTransform.convertToNodeSpaceAR(rightTopInParentSpace, _tempPos); + parentTransform.convertToNodeSpaceAR(_tempPos, rightTopInParentSpace); vec3.set(rightTopInParentSpace, rightTopInParentSpace.x + this._paddingRight, rightTopInParentSpace.y + this._paddingTop, rightTopInParentSpace.z); const newSize = cc.size(parseFloat((rightTopInParentSpace.x - leftBottomInParentSpace.x).toFixed(2)), diff --git a/cocos/3d/ui/components/scroll-bar-component.ts b/cocos/3d/ui/components/scroll-bar-component.ts index 3c7c130d14..b5152194d7 100644 --- a/cocos/3d/ui/components/scroll-bar-component.ts +++ b/cocos/3d/ui/components/scroll-bar-component.ts @@ -292,8 +292,15 @@ export class ScrollBarComponent extends Component { return ZERO; } - const worldSpacePos = content.uiTransfromComp!.convertToWorldSpace(ZERO); - const scrollViewSpacePos = this._scrollView.node.uiTransfromComp!.convertToNodeSpace(worldSpacePos); + let ap = content.getAnchorPoint(); + let contentSize = content.getContentSize(); + const scrollViewSpacePos = new Vec3(- ap.x * contentSize.width, - ap.y * contentSize.height, 0); + content.uiTransfromComp!.convertToWorldSpaceAR(scrollViewSpacePos, scrollViewSpacePos); + ap = this._scrollView.node.getAnchorPoint(); + contentSize = this._scrollView.node.getContentSize(); + scrollViewSpacePos.x += ap.x * contentSize.width; + scrollViewSpacePos.y += ap.y * contentSize.height; + this._scrollView.node.uiTransfromComp!.convertToNodeSpaceAR(scrollViewSpacePos, scrollViewSpacePos); return scrollViewSpacePos; } @@ -327,9 +334,9 @@ export class ScrollBarComponent extends Component { const handleParent = this.handle!.node.parent!; vec3.set(_tempPos_1, -barSize.width * barAnchor.x, -barSize.height * barAnchor.y, 0); - const leftBottomWorldPosition = this.node!.uiTransfromComp!.convertToWorldSpaceAR(_tempPos_2, _tempPos_1); + const leftBottomWorldPosition = this.node!.uiTransfromComp!.convertToWorldSpaceAR(_tempPos_1, _tempPos_2); let fixupPosition = new Vec3(); - handleParent.uiTransfromComp!.convertToNodeSpaceAR(fixupPosition, leftBottomWorldPosition); + handleParent.uiTransfromComp!.convertToNodeSpaceAR(leftBottomWorldPosition, fixupPosition); if (this.direction === Direction.HORIZONTAL) { fixupPosition = new Vec3(fixupPosition.x, fixupPosition.y + (barSize.height - handleSize.height) / 2, 0); diff --git a/cocos/3d/ui/components/slider-component.ts b/cocos/3d/ui/components/slider-component.ts index ac7127014b..e9de2c2c4a 100644 --- a/cocos/3d/ui/components/slider-component.ts +++ b/cocos/3d/ui/components/slider-component.ts @@ -24,15 +24,15 @@ THE SOFTWARE. ****************************************************************************/ -import { EventHandler as ComponentEventHandler } from '../../../components/component-event-handler'; import { Component } from '../../../components/component'; +import { EventHandler as ComponentEventHandler } from '../../../components/component-event-handler'; import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator'; import { EventTouch } from '../../../core/platform/event-manager/CCEvent'; import Touch from '../../../core/platform/event-manager/CCTouch'; +import { EventType } from '../../../core/platform/event-manager/event-enum'; import { clamp01 } from '../../../core/utils/misc'; import { Enum, Vec3 } from '../../../core/value-types/index'; import { vec3 } from '../../../core/vmath'; -import { EventType } from '../../../core/platform/event-manager/event-enum'; import { SpriteComponent } from './sprite-component'; const _tempPos = new Vec3(); @@ -196,7 +196,7 @@ export class SliderComponent extends Component { this._touchHandle = true; const touhPos = event.touch!.getUILocation(); vec3.set(this._touchPos, touhPos.x, touhPos.y, 0); - this._handle.node.uiTransfromComp.convertToNodeSpaceAR(this._offset, this._touchPos); + this._handle.node.uiTransfromComp.convertToNodeSpaceAR(this._touchPos, this._offset); event.propagationStopped = true; } @@ -257,7 +257,7 @@ export class SliderComponent extends Component { const touchPos = touch.getUILocation(); vec3.set(this._touchPos, touchPos.x, touchPos.y, 0); - const localTouchPos = this.node.uiTransfromComp!.convertToNodeSpaceAR(_tempPos, this._touchPos); + const localTouchPos = this.node.uiTransfromComp!.convertToNodeSpaceAR(this._touchPos, _tempPos); if (this.direction === Direction.Horizontal) { this.progress = clamp01(0.5 + (localTouchPos.x - this._offset.x) / this.node.width!); } else { @@ -277,11 +277,6 @@ export class SliderComponent extends Component { } this._handle.node.setPosition(this._handlelocalPos); - // const worldSpacePos = this.node.uiTransfromComp!.convertToWorldSpaceAR(handlelocalPos); - // const transform: UITransformComponent | null = this._handle.node.parent && this._handle.node.parent.getComponent(cc.UITransformComponent); - // if (transform) { - // this._handle.node.setPosition(transform.convertToNodeSpaceAR(worldSpacePos)); - // } } } diff --git a/cocos/3d/ui/components/ui-transfrom-component.ts b/cocos/3d/ui/components/ui-transfrom-component.ts index 5767280682..572174c3c5 100644 --- a/cocos/3d/ui/components/ui-transfrom-component.ts +++ b/cocos/3d/ui/components/ui-transfrom-component.ts @@ -3,7 +3,7 @@ import { ccclass, executeInEditMode, executionOrder, menu, property } from '../. import { EventType } from '../../../core/platform/event-manager/event-enum'; import { EventListener } from '../../../core/platform/event-manager/event-listener'; import { Mat4, Rect, Size, Vec2, Vec3 } from '../../../core/value-types'; -import * as math from '../../../core/vmath/index'; +import * as vmath from '../../../core/vmath'; import { CanvasComponent } from './canvas-component'; const _vec2a = new Vec2(); @@ -222,12 +222,12 @@ export class UITransformComponent extends Component { const center = cc.visibleRect.center; _mat4_temp.m12 = center.x - (_mat4_temp.m00 * m12 + _mat4_temp.m04 * m13); _mat4_temp.m13 = center.y - (_mat4_temp.m01 * m12 + _mat4_temp.m05 * m13); - math.mat4.invert(_mat4_temp, _mat4_temp); - math.vec2.transformMat4(cameraPt, point, _mat4_temp); + vmath.mat4.invert(_mat4_temp, _mat4_temp); + vmath.vec2.transformMat4(cameraPt, point, _mat4_temp); this.node.getWorldMatrix(_worldMatrix); - math.mat4.invert(_mat4_temp, _worldMatrix); - math.vec2.transformMat4(testPt, cameraPt, _mat4_temp); + vmath.mat4.invert(_mat4_temp, _worldMatrix); + vmath.vec2.transformMat4(testPt, cameraPt, _mat4_temp); testPt.x += this._anchorPoint.x * w; testPt.y += this._anchorPoint.y * h; @@ -251,90 +251,48 @@ export class UITransformComponent extends Component { } } - /** - * !#en Converts a Point to node (local) space coordinates then add the anchor point position. - * So the return position will be related to the left bottom corner of the node's bounding box. - * This equals to the API behavior of cocos2d-x, you probably want to use convertToNodeSpaceAR instead - * !#zh 将一个点转换到节点 (局部) 坐标系,并加上锚点的坐标。
- * 也就是说返回的坐标是相对于节点包围盒左下角的坐标。
- * 这个 API 的设计是为了和 cocos2d-x 中行为一致,更多情况下你可能需要使用 convertToNodeSpaceAR。 - * @method convertToNodeSpace - * @param {Vec3} worldPoint - * @return {Vec3} - * @example - * var newVec2 = node.convertToNodeSpace(cc.v3(100, 100)); - */ - public convertToNodeSpace (worldPoint: Vec3) { - this.node.getWorldMatrix(_worldMatrix); - math.mat4.invert(_mat4_temp, _worldMatrix); - const out = new Vec3(); - math.vec3.transformMat4(out, worldPoint, _mat4_temp); - out.x += this._anchorPoint.x * this._contentSize.width; - out.y += this._anchorPoint.y * this._contentSize.height; - return out; - } - - /** - * !#en Converts a Point related to the left bottom corner of the node's bounding box to world space coordinates. - * This equals to the API behavior of cocos2d-x, you probably want to use convertToWorldSpaceAR instead - * !#zh 将一个相对于节点左下角的坐标位置转换到世界空间坐标系。 - * 这个 API 的设计是为了和 cocos2d-x 中行为一致,更多情况下你可能需要使用 convertToWorldSpaceAR - * @method convertToWorldSpace - * @param {Vec3} nodePoint - * @return {Vec3} - * @example - * var newVec3 = node.convertToWorldSpace(cc.v3(100, 100)); - */ - public convertToWorldSpace (nodePoint: Vec3) { - this.node.getWorldMatrix(_worldMatrix); - const out = new Vec3( - nodePoint.x - this._anchorPoint.x * this._contentSize.width, - nodePoint.y - this._anchorPoint.y * this._contentSize.height, - 0, - ); - return math.vec3.transformMat4(out, out, _worldMatrix); - } - /** * !#en - * Converts a Point to node (local) space coordinates in which the anchor point is the origin position. + * Converts a UI Point to UI Node (Local) Space coordinates in which the anchor point is the origin position. + * Conversion of non-UI nodes to UI Node (Local) Space coordinate system, please go cc.pipelineUtils.ConvertWorldToUISpaceAR. * !#zh - * 将一个点转换到节点 (局部) 空间坐标系,这个坐标系以锚点为原点。 + * 将一个 UI 节点转换到另一个 UI 节点 (局部) 空间坐标系,这个坐标系以锚点为原点。 + * 非 UI 节点转换到 UI 节点(局部) 空间坐标系,请走 cc.pipelineUtils.ConvertWorldToUISpaceAR * @method convertToNodeSpaceAR * @param {Vec3} worldPoint + * @param {Vec3} out * @return {Vec3} * @example * var newVec2 = node.convertToNodeSpaceAR(cc.v2(100, 100)); */ - public convertToNodeSpaceAR (out: Vec3, worldPoint: Vec3) { - const matrix = new Mat4(); - this.node.getWorldMatrix(matrix); - math.mat4.invert(_mat4_temp, matrix); + public convertToNodeSpaceAR (worldPoint: Vec3, out?: Vec3) { + this.node.getWorldMatrix(_worldMatrix); + vmath.mat4.invert(_mat4_temp, _worldMatrix); if (!out) { out = new Vec3(); } - return math.vec2.transformMat4(out, worldPoint, _mat4_temp); + return vmath.vec3.transformMat4(out, worldPoint, _mat4_temp); } /** * !#en * Converts a Point in node coordinates to world space coordinates. * !#zh - * 将节点坐标系下的一个点转换到世界空间坐标系。 + * 将当前节点坐标系下的一个点转换到世界坐标系。 * @method convertToWorldSpaceAR * @param {Vec2} nodePoint * @return {Vec2} * @example * var newVec2 = node.convertToWorldSpaceAR(cc.v2(100, 100)); */ - public convertToWorldSpaceAR (out: Vec3, nodePoint: Vec3) { + public convertToWorldSpaceAR (nodePoint: Vec3, out?: Vec3) { this.node.getWorldMatrix(_worldMatrix); if (!out) { out = new Vec3(); } - return math.vec2.transformMat4(out, nodePoint, _worldMatrix); + return vmath.vec3.transformMat4(out, nodePoint, _worldMatrix); } /** @@ -348,7 +306,7 @@ export class UITransformComponent extends Component { * var boundingBox = node.getBoundingBox(); */ public getBoundingBox () { - math.mat4.fromRTS(_matrix, this.node.getRotation(), this.node.getPosition(), this.node.getScale()); + vmath.mat4.fromRTS(_matrix, this.node.getRotation(), this.node.getPosition(), this.node.getScale()); const width = this._contentSize.width; const height = this._contentSize.height; const rect = new Rect( @@ -381,7 +339,7 @@ export class UITransformComponent extends Component { } public getBoundingBoxTo (parentMat: Mat4) { - math.mat4.fromRTS(_matrix, this.node.getRotation(), this.node.getPosition(), this.node.getScale()); + vmath.mat4.fromRTS(_matrix, this.node.getRotation(), this.node.getPosition(), this.node.getScale()); const width = this._contentSize.width; const height = this._contentSize.height; const rect = new Rect( @@ -390,7 +348,7 @@ export class UITransformComponent extends Component { width, height); - math.mat4.mul(_worldMatrix, parentMat, _matrix); + vmath.mat4.mul(_worldMatrix, parentMat, _matrix); rect.transformMat4(rect, _worldMatrix); // query child's BoundingBox diff --git a/cocos/pipeline/index.ts b/cocos/pipeline/index.ts index 7c6da1eb92..6f5e87e2f7 100644 --- a/cocos/pipeline/index.ts +++ b/cocos/pipeline/index.ts @@ -1,2 +1,3 @@ import './define'; import './render-view'; +import './utils'; diff --git a/cocos/pipeline/utils.ts b/cocos/pipeline/utils.ts new file mode 100644 index 0000000000..6289d5ecd8 --- /dev/null +++ b/cocos/pipeline/utils.ts @@ -0,0 +1,54 @@ +import { CameraComponent } from '../3d'; +import { UITransformComponent } from '../3d/ui/components/ui-transfrom-component'; +import { Vec3 } from '../core'; +import { Node } from '../scene-graph'; + +const _temp_vec3_1 = new Vec3(); + +/** + * !#en + * Conversion of non-UI nodes to UI Node (Local) Space coordinate system. + * !#zh + * 非 UI 节点转换到 UI 节点(局部) 空间坐标系 + * @method convertToNodeSpaceAR + * @param {CameraComponent} mainCamera + * @param {Vec3} wpos + * @param {Node} uiNode + * @return {Vec3} + */ +export function WorldNode3DToLocalNodeUI (mainCamera: CameraComponent, wpos: Vec3, uiNode: Node, out?: Vec3) { + if (!out) { + out = new Vec3(); + } + + mainCamera.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(UITransformComponent); + + if (!cmp){ + return out; + } + + cmp.convertToNodeSpaceAR(_temp_vec3_1, out); + const targetPos = uiNode.getPosition(); + out.addSelf(targetPos); + + return out; +} + +export function WorldNode3DToWorldNodeUI (mainCamera: CameraComponent, wpos: Vec3, out?: Vec3){ + if (!out) { + out = new Vec3(); + } + + mainCamera.worldToScreen(wpos, out); + out.x = out.x / cc.view.getScaleX(); + out.y = out.y / cc.view.getScaleY(); + return out; +} + +cc.pipelineUtils = { + WorldNode3DToLocalNodeUI, + WorldNode3DToWorldNodeUI, +};