From 283edb8d22a2c53fa9e2f1f28e505943fdf91ded Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Thu, 16 May 2019 11:34:17 +0800 Subject: [PATCH] =?UTF-8?q?Add=20method=20'=20raycastUI=20'=E3=80=81'=20ra?= =?UTF-8?q?ycastUINode=20'=20to=20rander-scene;=20(#4422)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add method ' getComputeAABB ' to ui-transform-component; Add UI layer; --- .../ui/components/ui-transfrom-component.ts | 34 ++++++++-- cocos/renderer/scene/render-scene.ts | 65 ++++++++++++++++++- cocos/scene-graph/layers.ts | 1 + 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/cocos/3d/ui/components/ui-transfrom-component.ts b/cocos/3d/ui/components/ui-transfrom-component.ts index f1bc6338b4..33abca8895 100644 --- a/cocos/3d/ui/components/ui-transfrom-component.ts +++ b/cocos/3d/ui/components/ui-transfrom-component.ts @@ -4,6 +4,7 @@ import { EventType } from '../../../core/platform/event-manager/event-enum'; import { EventListener, ILinstenerMask } from '../../../core/platform/event-manager/event-listener'; import { Mat4, Rect, Size, Vec2, Vec3 } from '../../../core/value-types'; import * as vmath from '../../../core/vmath'; +import { aabb } from '../../geom-utils'; import { CanvasComponent } from './canvas-component'; const _vec2a = new Vec2(); @@ -35,7 +36,7 @@ export class UITransformComponent extends Component { } let clone: Size; - if (CC_EDITOR){ + if (CC_EDITOR) { clone = new Size(this._contentSize); } @@ -77,7 +78,7 @@ export class UITransformComponent extends Component { } set height (value) { - if (this.contentSize.height === value){ + if (this.contentSize.height === value) { return; } @@ -149,6 +150,7 @@ export class UITransformComponent extends Component { public __preload () { this.node.uiTransfromComp = this; + this.node.layer = cc.Layers.UI; } public onDestroy () { @@ -165,7 +167,7 @@ export class UITransformComponent extends Component { * node.setContentSize(cc.size(100, 100)); * node.setContentSize(100, 100); */ - public setContentSize (size: Size|number, height?: number) { + public setContentSize (size: Size | number, height?: number) { const locContentSize = this._contentSize; let clone: Size; if (height === undefined) { @@ -174,7 +176,7 @@ export class UITransformComponent extends Component { return; } - if (CC_EDITOR){ + if (CC_EDITOR) { clone = new Size(this._contentSize); } @@ -193,10 +195,10 @@ export class UITransformComponent extends Component { locContentSize.height = height; } - if (CC_EDITOR){ + if (CC_EDITOR) { // @ts-ignore this.node.emit(EventType.SIZE_CHANGED, clone); - }else{ + } else { this.node.emit(EventType.SIZE_CHANGED); } } @@ -425,7 +427,25 @@ export class UITransformComponent extends Component { return rect; } - private _getVisibility (){ + /** + * compute the corresponding aabb in world space for raycast + */ + public getComputeAABB (out?: aabb) { + const rectWorld = this.getBoundingBoxToWorld(); + const px = rectWorld.center.x; + const py = rectWorld.center.y; + const pz = this.node.getWorldPosition().z; + const w = rectWorld.width / 2; + const h = rectWorld.height / 2; + const l = 0.01; + if (out != null) { + aabb.set(out, px, py, pz, w, h, l); + } else { + return new aabb(px, py, pz, w, h, l); + } + } + + private _getVisibility () { let visibility = -1; let parent = this.node; // 获取被渲染相机的 visibility diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index d4601bf62e..303258e618 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -1,5 +1,5 @@ import { IBArray } from '../../3d/assets/mesh'; -import { intersect, ray, triangle } from '../../3d/geom-utils'; +import { aabb, intersect, ray, triangle } from '../../3d/geom-utils'; import { RecyclePool } from '../../3d/memop'; import { Root } from '../../core/root'; import { Mat4, Vec3 } from '../../core/value-types'; @@ -96,7 +96,7 @@ export class RenderScene { constructor (root: Root) { this._root = root; - this._ambient = new Ambient (this); + this._ambient = new Ambient(this); this._defaultMainLightNode = new Node('Main Light'); this._mainLight = new DirectionalLight(this, 'Main Light', this._defaultMainLightNode); this._mainLight.illuminance = Ambient.SUN_ILLUM; @@ -247,6 +247,61 @@ export class RenderScene { results.length = pool.length; return results; } + + /** + * Cast a ray into the scene, record all the intersected ui aabb in the result array + * @param worldRay - the testing ray + * @param mask - the layer mask to filter the ui aabb + * @returns the results array + */ + public raycastUI (worldRay: ray, mask: number = Layers.UI): IRaycastResult[] { + poolUI.reset(); + const canvasComs = cc.director.getScene().getComponentsInChildren(cc.CanvasComponent); + if (canvasComs != null) { + const canvasNode = canvasComs[0].node as Node; + if (canvasNode != null && canvasNode.active) { + this._raycastUINodeRecursiveChildren(worldRay, mask, canvasNode); + } + } + resultUIs.length = poolUI.length; + return resultUIs; + } + + /** + * Before you raycast the ui node, make sure the node is not null + * @param worldRay - the testing ray + * @param mask - the layer mask to filter the models + * @param uiNode - the ui node + * @returns IRaycastResult | undefined + */ + public raycastUINode (worldRay: ray, mask: number = Layers.UI, uiNode: Node) { + const uiTransfrom = uiNode.uiTransfromComp; + if (uiTransfrom == null || !cc.Layers.check(uiNode.layer, mask)) { return; } + uiTransfrom.getComputeAABB(aabbUI); + // broadphase + distance = intersect.ray_aabb(worldRay, aabbUI); + + if (distance <= 0) { + return; + } else { + const r = poolUI.add(); + r.node = uiNode; + r.distance = distance * vec3.magnitude(vec3.mul(v3, worldRay.d, uiNode._scale)); + return r; + } + } + + private _raycastUINodeRecursiveChildren (worldRay: ray, mask: number, parent: Node) { + const result = this.raycastUINode(worldRay, mask, parent); + if (result != null) { + resultUIs[poolUI.length - 1] = result; + } + for (const node of parent.children) { + if (node != null && node.active) { + this._raycastUINodeRecursiveChildren(worldRay, mask, node); + } + } + } } const modelRay = ray.create(); @@ -258,6 +313,12 @@ const pool = new RecyclePool(() => { return { node: null!, distance: Infinity }; }, 8); const results: IRaycastResult[] = []; +/** UI raycast result pool */ +const aabbUI = new aabb(); +const poolUI = new RecyclePool(() => { + return { node: null!, distance: Infinity }; +}, 8); +const resultUIs: IRaycastResult[] = []; const narrowphase = (vb: Float32Array, ib: IBArray, pm: GFXPrimitiveMode, sides?: boolean) => { distance = Infinity; diff --git a/cocos/scene-graph/layers.ts b/cocos/scene-graph/layers.ts index 47ac5ad384..cc260d40c8 100644 --- a/cocos/scene-graph/layers.ts +++ b/cocos/scene-graph/layers.ts @@ -9,6 +9,7 @@ export class Layers { public static IgnoreRaycast = (1 << 1); public static Gizmos = (1 << 2); public static Editor = (1 << 3); + public static UI = (1 << 4); // masks public static All = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor]); public static RaycastMask = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor, Layers.IgnoreRaycast]);