diff --git a/cocos/core/components/ui-base/ui-transfrom-component.ts b/cocos/core/components/ui-base/ui-transfrom-component.ts index e7d720f5ad..b27512e3ef 100644 --- a/cocos/core/components/ui-base/ui-transfrom-component.ts +++ b/cocos/core/components/ui-base/ui-transfrom-component.ts @@ -35,6 +35,7 @@ import { Mat4, Rect, Size, Vec2, Vec3 } from '../../../core/math'; import { aabb } from '../../geom-utils'; import { CanvasComponent } from './canvas-component'; import { director } from '../../director'; +import { Layers } from '../../scene-graph/layers'; const _vec2a = new Vec2(); const _vec2b = new Vec2(); @@ -186,7 +187,7 @@ export class UITransformComponent extends Component { public __preload () { this.node.uiTransfromComp = this; - this.node.layer = cc.Layers.Enum.UI_2D; + this.node.layer = Layers.Enum.UI_2D; } public onDestroy () { diff --git a/cocos/core/renderer/index.ts b/cocos/core/renderer/index.ts index 1f8a19fab1..67400ff5f4 100644 --- a/cocos/core/renderer/index.ts +++ b/cocos/core/renderer/index.ts @@ -18,3 +18,5 @@ export { Model } from './scene/model'; export { SkinningModel } from './models/skinning-model'; export * from './models/joints-texture-utils'; + +import './scene/deprecated'; \ No newline at end of file diff --git a/cocos/core/renderer/scene/deprecated.ts b/cocos/core/renderer/scene/deprecated.ts new file mode 100644 index 0000000000..99aca16e10 --- /dev/null +++ b/cocos/core/renderer/scene/deprecated.ts @@ -0,0 +1,17 @@ +import { replaceProperty } from "../../utils"; +import { RenderScene } from "./render-scene"; + +replaceProperty(RenderScene.prototype, 'RenderScene.prototype', [ + { + 'name': 'raycastUI', + 'newName': 'raycastUI2D' + }, + { + 'name': 'raycastUINode', + 'newName': 'raycastUI2DNode' + }, + { + 'name': 'raycast', + 'newName': 'raycastModels' + } +]) \ No newline at end of file diff --git a/cocos/core/renderer/scene/render-scene.ts b/cocos/core/renderer/scene/render-scene.ts index aec7078236..f02b47362f 100644 --- a/cocos/core/renderer/scene/render-scene.ts +++ b/cocos/core/renderer/scene/render-scene.ts @@ -224,33 +224,40 @@ export class RenderScene { } /** - * Cast a ray into the scene, record all the intersected models in the result array + * @en + * Cast a ray into the scene, record all the intersected models in the result array * @param worldRay the testing ray * @param mask the layer mask to filter the models - * @returns the results array + * @param distance the max distance , Infinity by default + * @zh + * 传入一条射线检测场景中所有的 3D 模型。 + * @param worldRay 世界射线 + * @param mask 用于标记所有要检测的层,默认为 Default + * @param distance 射线检测的最大距离, 默认为 Infinity + * @returns IRaycastResult[] */ - public raycast (worldRay: ray, mask: number = Layers.RaycastMask): IRaycastResult[] { + public raycastModels (worldRay: ray, mask = Layers.Enum.DEFAULT, distance = Infinity): IRaycastResult[] { pool.reset(); for (const m of this._models) { const transform = m.transform; - if (!transform || !m.enabled || !cc.Layers.check(m.node.layer, mask) || !m.modelBounds) { continue; } + if (!transform || !m.enabled || m.node.layer & Layers.Enum.IGNORE_RAYCAST || !(m.node.layer & mask) || !m.modelBounds) { continue; } // transform ray back to model space Mat4.invert(m4, transform.getWorldMatrix(m4)); Vec3.transformMat4(modelRay.o, worldRay.o, m4); Vec3.normalize(modelRay.d, Vec3.transformMat4Normal(modelRay.d, worldRay.d, m4)); // broadphase - distance = intersect.ray_aabb(modelRay, m.modelBounds); - if (distance <= 0) { continue; } + const d = intersect.ray_aabb(modelRay, m.modelBounds); + if (d <= 0 || d > distance) { continue; } for (let i = 0; i < m.subModelNum; ++i) { const subModel = m.getSubModel(i).subMeshData; if (subModel && subModel.geometricInfo) { const { positions: vb, indices: ib, doubleSided: sides } = subModel.geometricInfo; - narrowphase(vb, ib!, subModel.primitiveMode, sides as boolean); + narrowphase(vb, ib!, subModel.primitiveMode, sides!, distance); } - if (distance < Infinity) { + if (narrowDis < distance) { const r = pool.add(); r.node = m.node; - r.distance = distance * Vec3.multiply(v3, modelRay.d, transform.worldScale).length(); + r.distance = narrowDis * Vec3.multiply(v3, modelRay.d, transform.worldScale).length(); results[pool.length - 1] = r; } } @@ -260,19 +267,72 @@ export class RenderScene { } /** + * @en + * Before you raycast the model, make sure the model is not null + * @param worldRay the testing ray + * @param mask the layer mask to filter the models + * @param distance the max distance , Infinity by default + * @zh + * 传入一条射线和一个 3D 模型进行射线检测。 + * @param worldRay 世界射线 + * @param mask 用于标记所有要检测的层,默认为 Default + * @param distance 射线检测的最大距离, 默认为 Infinity + * @returns IRaycastResult[] + */ + public raycastModel (worldRay: ray, model: Model, mask = Layers.Enum.DEFAULT, distance = Infinity): IRaycastResult[] { + if (CC_PREVIEW) { + if (model == null) console.error(" 检测前请保证 model 不为 null "); + } + pool.reset(); + results.length = 0; + const m = model; + const transform = m.transform; + if (!transform || !m.enabled || m.node.layer & Layers.Enum.IGNORE_RAYCAST || !(m.node.layer & mask) || !m.modelBounds) { return results; } + // transform ray back to model space + Mat4.invert(m4, transform.getWorldMatrix(m4)); + Vec3.transformMat4(modelRay.o, worldRay.o, m4); + Vec3.normalize(modelRay.d, Vec3.transformMat4Normal(modelRay.d, worldRay.d, m4)); + // broadphase + const d = intersect.ray_aabb(modelRay, m.modelBounds); + if (d <= 0 || d > distance) { return results; } + for (let i = 0; i < m.subModelNum; ++i) { + const subModel = m.getSubModel(i).subMeshData; + if (subModel && subModel.geometricInfo) { + const { positions: vb, indices: ib, doubleSided: sides } = subModel.geometricInfo; + narrowphase(vb, ib!, subModel.primitiveMode, sides!, distance); + } + if (narrowDis < distance) { + const r = pool.add(); + r.node = m.node; + r.distance = narrowDis * Vec3.multiply(v3, modelRay.d, transform.worldScale).length(); + results[pool.length - 1] = r; + } + } + return results; + } + + /** + * @en * 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 + * @param mask the layer mask to filter all ui2d aabb + * @param distance the max distance , Infinity by default + * @returns IRaycastResult[] + * @zh + * 传入一条射线检测场景中所有的 UI2D Node,mask 标记了要检测的所有的层,默认为 UI2D* + * @param worldRay 世界射线 + * @param mask 用于标记所有要检测的层,默认为 UI2D + * @param distance 射线检测的最大距离, 默认为 Infinity + * @returns IRaycastResult[] */ - public raycastUI (worldRay: ray, mask: number = Layers.Enum.UI): IRaycastResult[] { + public raycastUI2D (worldRay: ray, mask = Layers.Enum.UI_2D, distance = Infinity): IRaycastResult[] { poolUI.reset(); const canvasComs = cc.director.getScene().getComponentsInChildren(cc.CanvasComponent); if (canvasComs != null && canvasComs.length > 0) { for (let i = 0; i < canvasComs.length; i++) { const canvasNode = canvasComs[i].node as Node; if (canvasNode != null && canvasNode.active) { - this._raycastUINodeRecursiveChildren(worldRay, canvasNode, mask); + this._raycastUI2DNodeRecursiveChildren(worldRay, canvasNode, mask, distance); } } } @@ -281,36 +341,67 @@ export class RenderScene { } /** + * @en * 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 + * @param ui2dNode the ui2d node + * @param mask the layer mask to filter the ui2d node aabb + * @param distance the max distance , Infinity by default * @returns IRaycastResult | undefined + * @zh + * 传入一条射线和一个 UI2D Node 进行射线检测。 + * @param worldRay 世界射线 + * @param ui2dNode UI2D 的节点 + * @param mask 用于标记所有要检测的层,默认为 UI2D + * @param distance 射线检测的最大距离, 默认为 Infinity */ - public raycastUINode (worldRay: ray, uiNode: INode, mask: number = Layers.Enum.UI) { - const uiTransfrom = uiNode.uiTransfromComp; - if (uiTransfrom == null || !(uiNode.layer & mask)) { return; } + public raycastUI2DNode (worldRay: ray, ui2dNode: INode, mask = Layers.Enum.UI_2D, distance = Infinity) { + if (CC_PREVIEW) { + if (ui2dNode == null) console.error(" 检测前请保证 uiNode 不为 null "); + } + const uiTransfrom = ui2dNode.uiTransfromComp; + if (uiTransfrom == null || ui2dNode.layer & Layers.Enum.IGNORE_RAYCAST || !(ui2dNode.layer & mask)) { return; } uiTransfrom.getComputeAABB(aabbUI); - distance = intersect.ray_aabb(worldRay, aabbUI); + const d = intersect.ray_aabb(worldRay, aabbUI); - if (distance <= 0) { + if (d <= 0) { return; - } else { + } else if (d < distance) { const r = poolUI.add(); - r.node = uiNode; - r.distance = distance; + r.node = ui2dNode; + r.distance = d; return r; } } - private _raycastUINodeRecursiveChildren (worldRay: ray, parent: INode, mask: number = Layers.Enum.UI) { - const result = this.raycastUINode(worldRay, parent, mask); + // /** + // * @en + // * Cast a ray into the scene, record all the intersected models and ui2d nodes in the result array + // * @param worldRay the testing ray + // * @param mask the layer mask to filter the models + // * @param distance the max distance , Infinity by default + // * @returns IRaycastResult[] + // * @zh + // * 传入一条射线检测场景中所有的 3D 模型和 UI2D Node + // * @param worldRay 世界射线 + // * @param mask mask 用于标记所有要检测的层,默认为 Default | UI2D + // * @param distance 射线检测的最大距离, 默认为 Infinity + // * @returns IRaycastResult[] + // */ + // public raycastAll (worldRay: ray, mask = Layers.Default | Layers.UI2D, distance = Infinity): IRaycastResult[] { + // const r_3d = this.raycastModels(worldRay, mask, distance); + // const r_ui2d = this.raycastUI2D(worldRay, mask, distance); + // return r_3d.concat(r_ui2d); + // } + + private _raycastUI2DNodeRecursiveChildren (worldRay: ray, parent: INode, mask = Layers.Enum.UI_2D, distance = Infinity) { + const result = this.raycastUI2DNode(worldRay, parent, mask, distance); if (result != null) { resultUIs[poolUI.length - 1] = result; } for (const node of parent.children) { if (node != null && node.active) { - this._raycastUINodeRecursiveChildren(worldRay, node, mask); + this._raycastUI2DNodeRecursiveChildren(worldRay, node, mask, distance); } } } @@ -319,7 +410,7 @@ export class RenderScene { const modelRay = ray.create(); const v3 = new Vec3(); const m4 = new Mat4(); -let distance = Infinity; +let narrowDis = Infinity; const tri = triangle.create(); const pool = new RecyclePool(() => { return { node: null!, distance: Infinity }; @@ -332,8 +423,8 @@ const poolUI = new RecyclePool(() => { }, 8); const resultUIs: IRaycastResult[] = []; -const narrowphase = (vb: Float32Array, ib: IBArray, pm: GFXPrimitiveMode, sides: boolean) => { - distance = Infinity; +const narrowphase = (vb: Float32Array, ib: IBArray, pm: GFXPrimitiveMode, sides: boolean, distance = Infinity) => { + narrowDis = distance; if (pm === GFXPrimitiveMode.TRIANGLE_LIST) { const cnt = ib.length; for (let j = 0; j < cnt; j += 3) { @@ -344,8 +435,8 @@ const narrowphase = (vb: Float32Array, ib: IBArray, pm: GFXPrimitiveMode, sides: Vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]); Vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]); const dist = intersect.ray_triangle(modelRay, tri, sides); - if (dist <= 0 || dist > distance) { continue; } - distance = dist; + if (dist <= 0 || dist > narrowDis) { continue; } + narrowDis = dist; } } else if (pm === GFXPrimitiveMode.TRIANGLE_STRIP) { const cnt = ib.length - 2; @@ -359,8 +450,8 @@ const narrowphase = (vb: Float32Array, ib: IBArray, pm: GFXPrimitiveMode, sides: Vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]); rev = ~rev; const dist = intersect.ray_triangle(modelRay, tri, sides); - if (dist <= 0 || dist > distance) { continue; } - distance = dist; + if (dist <= 0 || dist > narrowDis) { continue; } + narrowDis = dist; } } else if (pm === GFXPrimitiveMode.TRIANGLE_FAN) { const cnt = ib.length - 1; @@ -372,8 +463,8 @@ const narrowphase = (vb: Float32Array, ib: IBArray, pm: GFXPrimitiveMode, sides: Vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]); Vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]); const dist = intersect.ray_triangle(modelRay, tri, sides); - if (dist <= 0 || dist > distance) { continue; } - distance = dist; + if (dist <= 0 || dist > narrowDis) { continue; } + narrowDis = dist; } } }; diff --git a/cocos/core/scene-graph/deprecated.ts b/cocos/core/scene-graph/deprecated.ts index 9ab46036c7..083cbd0f5e 100644 --- a/cocos/core/scene-graph/deprecated.ts +++ b/cocos/core/scene-graph/deprecated.ts @@ -32,6 +32,18 @@ removeProperty(Node.prototype, 'Node.prototype', [ } ]); +removeProperty(Layers, 'Layers', [ + { + 'name': 'All', + }, + { + 'name': 'RaycastMask', + }, + { + 'name': 'check', + } +]); + replaceProperty(Layers, 'Layers', [ { name: 'Default', diff --git a/cocos/core/scene-graph/layers.ts b/cocos/core/scene-graph/layers.ts index ffb3fddffa..55c34b37af 100644 --- a/cocos/core/scene-graph/layers.ts +++ b/cocos/core/scene-graph/layers.ts @@ -52,16 +52,6 @@ export class Layers { public static Enum = Enum(layerList); public static BitMask = BitMask(Object.assign({}, layerList)); - /** - * @zh 接受所有用户创建的节点 - */ - public static All = Layers.makeExclusiveMask([Layers.Enum.GIZMOS, Layers.Enum.SCENE_GIZMO, Layers.Enum.EDITOR]); - /** - * @zh 接受所有支持射线检测的节点 - */ - public static RaycastMask = Layers.makeExclusiveMask([Layers.Enum.GIZMOS, Layers.Enum.SCENE_GIZMO, - Layers.Enum.EDITOR, Layers.Enum.IGNORE_RAYCAST]); - /** * @en * Make a layer mask accepting nothing but the listed layers @@ -90,19 +80,6 @@ export class Layers { return ~Layers.makeInclusiveMask(excludes); } - /** - * @en - * Check a layer is accepted by the mask or not - * @zh - * 检查一个层是否被检测器接受 - * @param layer 待检测的层 - * @param mask 层检测器 - * @return 是否通过检测 - */ - public static check (layer: number, mask: number): boolean { - return (layer & mask) === layer; - } - /** * @zh * 添加一个新层,用户可编辑 0 - 19 位为用户自定义层