tweaks of render-scene raycast (#5406)

* rename UI to UI_3D

* fix getComputeAABB in ui-transform-component

* tweaks of render-scene raycast
This commit is contained in:
jiaxin.lai
2019-10-14 23:05:42 +08:00
committed by pandamicro
parent a722483d2d
commit fc1c066eac
3 changed files with 135 additions and 71 deletions
@@ -42,7 +42,7 @@ const _vec2b = new Vec2();
const _mat4_temp = new Mat4();
const _matrix = new Mat4();
const _worldMatrix = new Mat4();
const _rect = new Rect();
/**
* @zh
* UI 变换组件。
@@ -477,16 +477,26 @@ export class UITransformComponent extends Component {
}
/**
* @en
* compute the corresponding aabb in world space for raycast
* @zh
* 计算出此 UI_2D 节点在世界空间下的 aabb 包围盒
*/
public getComputeAABB (out?: aabb) {
const rectWorld = this.getBoundingBoxToWorld();
const px = rectWorld.center.x;
const py = rectWorld.center.y;
const width = this._contentSize.width;
const height = this._contentSize.height;
_rect.set(
-this._anchorPoint.x * width,
-this._anchorPoint.y * height,
width,
height);
_rect.transformMat4(this.node.worldMatrix);
const px = _rect.x + _rect.width * 0.5;
const py = _rect.y + _rect.height * 0.5;;
const pz = this.node.worldPosition.z;
const w = rectWorld.width / 2;
const h = rectWorld.height / 2;
const l = 0.01;
const w = _rect.width / 2;
const h = _rect.height / 2;
const l = 0.001;
if (out != null) {
aabb.set(out, px, py, pz, w, h, l);
} else {
+21 -4
View File
@@ -5,15 +5,32 @@ import { Layers } from "../../scene-graph";
replaceProperty(RenderScene.prototype, 'RenderScene.prototype', [
{
'name': 'raycastUI',
'newName': 'raycastUI2D'
'newName': 'raycastAllCanvas'
},
{
'name': 'raycastUINode',
'newName': 'raycastUI2DNode'
'name': 'raycastUI2D',
'newName': 'raycastAllCanvas'
},
{
'name': 'raycast',
'newName': 'raycastModels'
'newName': 'raycastAllModels'
},
{
'name': 'raycastModels',
'newName': 'raycastAllModels'
},
{
'name': 'raycastModel',
'newName': 'raycastSingleModel'
},
]);
removeProperty(RenderScene.prototype, 'RenderScene.prototype', [
{
'name': 'raycastUI2DNode'
},
{
'name': 'raycastUINode',
}
]);
+97 -60
View File
@@ -82,6 +82,38 @@ export class RenderScene {
return this._texturePool;
}
/**
* @zh
* 获取 raycastAllCanvas 后的检测结果
*/
get rayResultCanvas () {
return resultCanvas;
}
/**
* @zh
* 获取 raycastAllModels 后的检测结果
*/
get rayResultModels () {
return resultModels;
}
/**
* @zh
* 获取 raycastAll 后的检测结果
*/
get rayResultAll () {
return resultAll;
}
/**
* @zh
* 获取 raycastSingleModel 后的检测结果
*/
get rayResultSingleModel () {
return this.rayResultSingleModel;
}
public static registerCreateFunc (root: Root) {
root._createSceneFun = (_root: Root): RenderScene => new RenderScene(_root);
}
@@ -223,20 +255,51 @@ export class RenderScene {
return this._modelId++;
}
/**
* @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 boolean , ray is hit or not
* @note getter of this.rayResultAll can get recently result
* @zh
* 传入一条射线检测场景中所有的 3D 模型和 UI2D Node
* @param worldRay 世界射线
* @param mask mask 用于标记所有要检测的层,默认为 Default | UI2D
* @param distance 射线检测的最大距离, 默认为 Infinity
* @returns boolean , 射线是否有击中
* @nete 通过 this.rayResultAll 可以获取到最近的结果
*/
public raycastAll (worldRay: ray, mask = Layers.Enum.DEFAULT | Layers.Enum.UI_2D, distance = Infinity): boolean {
const r_3d = this.raycastAllModels(worldRay, mask, distance);
const r_ui2d = this.raycastAllCanvas(worldRay, mask, distance);
const isHit = r_3d || r_ui2d;
resultAll.length = 0;
if (isHit) {
Array.prototype.push.apply(resultAll, resultModels);
Array.prototype.push.apply(resultAll, resultCanvas);
}
return isHit;
}
/**
* @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
* @param distance the max distance , Infinity by default
* @returns boolean , ray is hit or not
* @note getter of this.rayResultModels can get recently result
* @zh
* 传入一条射线检测场景中所有的 3D 模型。
* @param worldRay 世界射线
* @param mask 用于标记所有要检测的层,默认为 Default
* @param distance 射线检测的最大距离, 默认为 Infinity
* @returns IRaycastResult[]
* @returns boolean , 射线是否有击中
* @nete 通过 this.rayResultModels 可以获取到最近的结果
*/
public raycastModels (worldRay: ray, mask = Layers.Enum.DEFAULT, distance = Infinity): IRaycastResult[] {
public raycastAllModels (worldRay: ray, mask = Layers.Enum.DEFAULT, distance = Infinity): boolean {
pool.reset();
for (const m of this._models) {
const transform = m.transform;
@@ -258,43 +321,45 @@ export class RenderScene {
const r = pool.add();
r.node = m.node;
r.distance = narrowDis * Vec3.multiply(v3, modelRay.d, transform.worldScale).length();
results[pool.length - 1] = r;
resultModels[pool.length - 1] = r;
}
}
}
results.length = pool.length;
return results;
resultModels.length = pool.length;
return resultModels.length > 0;
}
/**
* @en
* Before you raycast the model, make sure the model is not null
* @param worldRay the testing ray
* @param model the testing model
* @param mask the layer mask to filter the models
* @param distance the max distance , Infinity by default
* @returns boolean , ray is hit or not
* @zh
* 传入一条射线和一个 3D 模型进行射线检测。
* @param worldRay 世界射线
* @param model 进行检测的模型
* @param mask 用于标记所有要检测的层,默认为 Default
* @param distance 射线检测的最大距离, 默认为 Infinity
* @returns IRaycastResult[]
* @returns boolean , 射线是否有击中
*/
public raycastModel (worldRay: ray, model: Model, mask = Layers.Enum.DEFAULT, distance = Infinity): IRaycastResult[] {
public raycastSingleModel (worldRay: ray, model: Model, mask = Layers.Enum.DEFAULT, distance = Infinity): boolean {
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; }
if (!transform || !m.enabled || m.node.layer & Layers.Enum.IGNORE_RAYCAST || !(m.node.layer & mask) || !m.modelBounds) { return false; }
// 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; }
if (d <= 0 || d > distance) { return false; }
for (let i = 0; i < m.subModelNum; ++i) {
const subModel = m.getSubModel(i).subMeshData;
if (subModel && subModel.geometricInfo) {
@@ -305,27 +370,30 @@ export class RenderScene {
const r = pool.add();
r.node = m.node;
r.distance = narrowDis * Vec3.multiply(v3, modelRay.d, transform.worldScale).length();
results[pool.length - 1] = r;
resultSingleModel[pool.length - 1] = r;
}
}
return results;
resultSingleModel.length = pool.length;
return resultSingleModel.length > 0;
}
/**
* @en
* Cast a ray into the scene, record all the intersected ui aabb in the result array
* Cast a ray into the scene, detect all canvas and its children
* @param worldRay the testing ray
* @param mask the layer mask to filter all ui2d aabb
* @param distance the max distance , Infinity by default
* @returns IRaycastResult[]
* @returns boolean , ray is hit or not
* @note getter of this.rayResultCanvas can get recently result
* @zh
* 传入一条射线检测场景中所有的 UI2D Node,mask 标记了要检测的所有的层,默认为 UI2D*
* 传入一条射线检测场景中所有的 Canvas 以及 Canvas 下的 Node
* @param worldRay 世界射线
* @param mask 用于标记所有要检测的层,默认为 UI2D
* @param distance 射线检测的最大距离, 默认为 Infinity
* @returns IRaycastResult[]
* @returns boolean , 射线是否有击中
* @nete 通过 this.rayResultCanvas 可以获取到最近的结果
*/
public raycastUI2D (worldRay: ray, mask = Layers.Enum.UI_2D, distance = Infinity): IRaycastResult[] {
public raycastAllCanvas (worldRay: ray, mask = Layers.Enum.UI_2D, distance = Infinity): boolean {
poolUI.reset();
const canvasComs = cc.director.getScene().getComponentsInChildren(cc.CanvasComponent);
if (canvasComs != null && canvasComs.length > 0) {
@@ -336,26 +404,11 @@ export class RenderScene {
}
}
}
resultUIs.length = poolUI.length;
return resultUIs;
resultCanvas.length = poolUI.length;
return resultCanvas.length > 0;
}
/**
* @en
* Before you raycast the ui node, make sure the node is not null
* @param worldRay the testing ray
* @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 raycastUI2DNode (worldRay: ray, ui2dNode: INode, mask = Layers.Enum.UI_2D, distance = Infinity) {
private _raycastUI2DNode (worldRay: ray, ui2dNode: INode, mask = Layers.Enum.UI_2D, distance = Infinity) {
if (CC_PREVIEW) {
if (ui2dNode == null) console.error(" 检测前请保证 uiNode 不为 null ");
}
@@ -374,30 +427,10 @@ export class RenderScene {
}
}
// /**
// * @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);
const result = this._raycastUI2DNode(worldRay, parent, mask, distance);
if (result != null) {
resultUIs[poolUI.length - 1] = result;
resultCanvas[poolUI.length - 1] = result;
}
for (const node of parent.children) {
if (node != null && node.active) {
@@ -415,13 +448,17 @@ const tri = triangle.create();
const pool = new RecyclePool<IRaycastResult>(() => {
return { node: null!, distance: Infinity };
}, 8);
const results: IRaycastResult[] = [];
/** UI raycast result pool */
const resultModels: IRaycastResult[] = [];
/** Canavas raycast result pool */
const aabbUI = new aabb();
const poolUI = new RecyclePool<IRaycastResult>(() => {
return { node: null!, distance: Infinity };
}, 8);
const resultUIs: IRaycastResult[] = [];
const resultCanvas: IRaycastResult[] = [];
/** raycast all */
const resultAll: IRaycastResult[] = [];
/** raycast single model */
const resultSingleModel: IRaycastResult[] = [];
const narrowphase = (vb: Float32Array, ib: IBArray, pm: GFXPrimitiveMode, sides: boolean, distance = Infinity) => {
narrowDis = distance;