raycast opt (#6113)

This commit is contained in:
Yun Hsiao Wu
2020-01-15 17:00:46 +08:00
committed by pandamicro
parent 74bfa051d1
commit 3aec9cb125
2 changed files with 13 additions and 13 deletions
+1 -1
View File
@@ -133,7 +133,7 @@ const ray_aabb = (function () {
const tmin = Math.max(Math.max(Math.min(t1, t2), Math.min(t3, t4)), Math.min(t5, t6));
const tmax = Math.min(Math.min(Math.max(t1, t2), Math.max(t3, t4)), Math.max(t5, t6));
if (tmax < 0 || tmin > tmax) { return 0; }
return tmin;
return tmin > 0 ? tmin : tmax; // ray origin inside aabb
};
})();
+12 -12
View File
@@ -302,15 +302,15 @@ export class RenderScene {
pool.reset();
for (const m of this._models) {
const transform = m.transform;
if (!transform || !m.enabled || m.node.layer & Layers.Enum.IGNORE_RAYCAST || !(m.node.layer & mask) || !m.worldBounds) { 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));
if (!transform || !m.enabled || !(m.node.layer & (mask & ~Layers.Enum.IGNORE_RAYCAST)) || !m.worldBounds) { continue; }
// broadphase
let d = intersect.ray_aabb(worldRay, m.worldBounds);
if (d <= 0 || d >= distance) { continue; }
if (!(m instanceof SkinningModel)) {
if (!(m instanceof SkinningModel && m.uploadedAnim)) {
// 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));
d = Infinity;
for (let i = 0; i < m.subModelNum; ++i) {
const subModel = m.getSubModel(i).subMeshData;
@@ -355,15 +355,15 @@ export class RenderScene {
pool.reset();
const m = model;
const transform = m.transform;
if (!transform || !m.enabled || m.node.layer & Layers.Enum.IGNORE_RAYCAST || !(m.node.layer & mask) || !m.worldBounds) { 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));
if (!transform || !m.enabled || !(m.node.layer & (mask & ~Layers.Enum.IGNORE_RAYCAST)) || !m.worldBounds) { return false; }
// broadphase
let d = intersect.ray_aabb(worldRay, m.worldBounds);
if (d <= 0 || d >= distance) { return false; }
if (!(m instanceof SkinningModel)) {
if (!(m instanceof SkinningModel && m.uploadedAnim)) {
// 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));
d = Infinity;
for (let i = 0; i < m.subModelNum; ++i) {
const subModel = m.getSubModel(i).subMeshData;