From 0018f53aa86ad4a80a5b5555f4c95a19b2e35cf3 Mon Sep 17 00:00:00 2001 From: Lucas Dower Date: Mon, 12 Jul 2021 00:47:27 +0100 Subject: [PATCH] Basic cross-product optimisation --- src/math.js | 16 ++++++++++------ src/triangle.js | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/math.js b/src/math.js index 42efec9..fd0874f 100644 --- a/src/math.js +++ b/src/math.js @@ -14,10 +14,6 @@ function ceilTo(value, base) { return Math.ceil(value / base) * base; } -function fastCrossXAxis(vec) { - return Vector3.create(0.0, -vec[2], vec[1]); -} - function fastDotXAxis(vec) { return vec[0]; } @@ -29,15 +25,21 @@ function fastDotYAxis(vec) { function fastDotZAxis(vec) { return vec[2]; } +*/ + +function fastCrossXAxis(vec) { + return new Vector3(0.0, -vec.z, vec.y); +} function fastCrossYAxis(vec) { - return Vector3.create(vec[2], 0.0, -vec[0]); + return new Vector3(vec.z, 0.0, -vec.x); } function fastCrossZAxis(vec) { - return Vector3.create(-vec[1], vec[0], 0.0); + return new Vector3(-vec.y, vec.x, 0.0); } +/* function roundVector3To(vec, round) { vec[0] = roundTo(vec[0], round); vec[1] = roundTo(vec[1], round); @@ -47,11 +49,13 @@ function roundVector3To(vec, round) { module.exports.floorTo = floorTo; module.exports.ceilTo = ceilTo; +*/ module.exports.fastCrossXAxis = fastCrossXAxis; module.exports.fastCrossYAxis = fastCrossYAxis; module.exports.fastCrossZAxis = fastCrossZAxis; +/* module.exports.fastDotXAxis = fastDotXAxis; module.exports.fastDotYAxis = fastDotYAxis; module.exports.fastDotZAxis = fastDotZAxis; diff --git a/src/triangle.js b/src/triangle.js index cba02b5..cf11e5e 100644 --- a/src/triangle.js +++ b/src/triangle.js @@ -1,6 +1,6 @@ const { Vector3 } = require('./vector.js'); const { AABB } = require('./aabb.js'); -const { xAxis, yAxis, zAxis } = require('./math.js'); +const { xAxis, yAxis, zAxis, fastCrossXAxis, fastCrossYAxis, fastCrossZAxis } = require('./math.js'); class Triangle { @@ -52,15 +52,15 @@ class Triangle { const f2 = Vector3.sub(v0, v2); const axis = [ - Vector3.cross(xAxis, f0), - Vector3.cross(xAxis, f1), - Vector3.cross(xAxis, f2), - Vector3.cross(yAxis, f0), - Vector3.cross(yAxis, f1), - Vector3.cross(yAxis, f2), - Vector3.cross(zAxis, f0), - Vector3.cross(zAxis, f1), - Vector3.cross(zAxis, f2), + fastCrossXAxis(f0), + fastCrossXAxis(f1), + fastCrossXAxis(f2), + fastCrossYAxis(f0), + fastCrossYAxis(f1), + fastCrossYAxis(f2), + fastCrossZAxis(f0), + fastCrossZAxis(f1), + fastCrossZAxis(f2), xAxis, yAxis, zAxis,