瀏覽代碼

Fix bug in intersectsTriangle
where bu bv is actually bv and bw

Gary Hsu 6 年之前
父節點
當前提交
36147d7178

+ 5 - 5
src/Culling/ray.ts

@@ -197,17 +197,17 @@ export class Ray {
 
         this.origin.subtractToRef(vertex0, tvec);
 
-        var bu = Vector3.Dot(tvec, pvec) * invdet;
+        var bv = Vector3.Dot(tvec, pvec) * invdet;
 
-        if (bu < 0 || bu > 1.0) {
+        if (bv < 0 || bv > 1.0) {
             return null;
         }
 
         Vector3.CrossToRef(tvec, edge1, qvec);
 
-        var bv = Vector3.Dot(this.direction, qvec) * invdet;
+        var bw = Vector3.Dot(this.direction, qvec) * invdet;
 
-        if (bv < 0 || bu + bv > 1.0) {
+        if (bw < 0 || bv + bw > 1.0) {
             return null;
         }
 
@@ -217,7 +217,7 @@ export class Ray {
             return null;
         }
 
-        return new IntersectionInfo(bu, bv, distance);
+        return new IntersectionInfo(1 - bv - bw, bv, distance);
     }
 
     /**

+ 84 - 0
tests/unit/babylon/src/Culling/babylon.ray.tests.ts

@@ -0,0 +1,84 @@
+/**
+ * Describes the test suite.
+ */
+describe('Babylon Ray', function() {
+    let subject: BABYLON.Engine;
+
+    this.timeout(10000);
+
+    /**
+     * Loads the dependencies.
+     */
+    before(function(done) {
+        this.timeout(180000);
+        (BABYLONDEVTOOLS).Loader
+            .useDist()
+            .testMode()
+            .load(function() {
+                // Force apply promise polyfill for consistent behavior between chrome headless, IE11, and other browsers.
+                BABYLON.PromisePolyfill.Apply(true);
+                done();
+            });
+    });
+
+    /**
+     * Create a new engine subject before each test.
+     */
+    beforeEach(function() {
+        subject = new BABYLON.NullEngine({
+            renderHeight: 256,
+            renderWidth: 256,
+            textureSize: 256,
+            deterministicLockstep: false,
+            lockstepMaxSteps: 1
+        });
+
+        // Avoid creating normals in PBR materials.
+        subject.getCaps().standardDerivatives = true;
+    });
+
+    /**
+     * Ray tests.
+     */
+    describe('#ray', () => {
+        it('pickWithRay', () => {
+            const scene = new BABYLON.Scene(subject);
+
+            const vertexData = new BABYLON.VertexData();
+            vertexData.indices = [0, 1, 2];
+            vertexData.positions = [
+                0, 0, 0,
+                1, 0, 0,
+                0, 1, 0,
+            ];
+            vertexData.normals = [
+                0, 0, -1,
+                1, 0, -1,
+                0, 1, -1,
+            ];
+
+            const mesh = new BABYLON.Mesh("triangle", scene);
+
+            const geometry = new BABYLON.Geometry("triangle", scene, vertexData);
+            geometry.applyToMesh(mesh);
+
+            const material = new BABYLON.PBRMaterial("material", scene);
+            material.metallic = 0;
+            material.albedoColor = BABYLON.Color3.Gray();
+
+            mesh.material = material;
+
+            const direction = BABYLON.Vector3.Forward();
+            for (const index of vertexData.indices) {
+                const position = BABYLON.Vector3.FromArray(vertexData.positions, index * 3);
+                const normal = BABYLON.Vector3.FromArray(vertexData.normals, index * 3).normalize();
+                const origin = new BABYLON.Vector3(position.x, position.y, position.z - 1);
+                const ray = new BABYLON.Ray(origin, direction);
+                const hit = scene.pickWithRay(ray);
+                expect(hit.hit, "[0] hit.hit").to.be.true;
+                expect(BABYLON.Vector3.DistanceSquared(hit.pickedPoint, position), "[0] hit.pickedPoint").to.equal(0);
+                expect(BABYLON.Vector3.DistanceSquared(hit.getNormal(), normal), "[0] hit.getNormal()").to.equal(0);
+            }
+        });
+    });
+});

tests/unit/babylon/src/Material/babylon.material.tests.ts → tests/unit/babylon/src/Materials/babylon.material.tests.ts


tests/unit/babylon/src/Mesh/babylon.dictionaryMode.tests.ts → tests/unit/babylon/src/Meshes/babylon.dictionaryMode.tests.ts


tests/unit/babylon/src/Mesh/babylon.geometry.tests.ts → tests/unit/babylon/src/Meshes/babylon.geometry.tests.ts


tests/unit/babylon/src/Mesh/babylon.mesh.vertexData.tests.ts → tests/unit/babylon/src/Meshes/babylon.mesh.vertexData.tests.ts


tests/unit/babylon/src/Mesh/babylon.positionAndRotation.tests.ts → tests/unit/babylon/src/Meshes/babylon.positionAndRotation.tests.ts


tests/unit/babylon/src/Tools/babylon.promise.tests.ts → tests/unit/babylon/src/Misc/babylon.promise.tests.ts


tests/unit/babylon/src/PostProcess/babylon.postProcess.tests.ts → tests/unit/babylon/src/PostProcesses/babylon.postProcess.tests.ts


+ 8 - 7
tests/unit/karma.conf.js

@@ -20,14 +20,15 @@ module.exports = function(config) {
             './tests/unit/babylon/src/babylon.node.tests.js',
             './tests/unit/babylon/src/Animations/babylon.animation.tests.js',
             './tests/unit/babylon/src/Animations/babylon.animationGroup.tests.js',
-            './tests/unit/babylon/src/Loading/babylon.sceneLoader.tests.js',
-            './tests/unit/babylon/src/PostProcess/babylon.postProcess.tests.js',
-            './tests/unit/babylon/src/Material/babylon.material.tests.js',
-            './tests/unit/babylon/src/Mesh/babylon.dictionaryMode.tests.js',
-            './tests/unit/babylon/src/Mesh/babylon.geometry.tests.js',
-            './tests/unit/babylon/src/Mesh/babylon.mesh.vertexData.tests.js',
-            './tests/unit/babylon/src/Tools/babylon.promise.tests.js',
             './tests/unit/babylon/src/Cameras/babylon.pointerInput.tests.js',
+            './tests/unit/babylon/src/Culling/babylon.ray.tests.js',
+            './tests/unit/babylon/src/Loading/babylon.sceneLoader.tests.js',
+            './tests/unit/babylon/src/PostProcesses/babylon.postProcess.tests.js',
+            './tests/unit/babylon/src/Materials/babylon.material.tests.js',
+            './tests/unit/babylon/src/Meshes/babylon.dictionaryMode.tests.js',
+            './tests/unit/babylon/src/Meshes/babylon.geometry.tests.js',
+            './tests/unit/babylon/src/Meshes/babylon.mesh.vertexData.tests.js',
+            './tests/unit/babylon/src/Misc/babylon.promise.tests.js',
             { pattern: 'dist/preview release/**/*.js', watched: false, included: false, served: true },
             { pattern: 'assets/**/*', watched: false, included: false, served: true },
             //{ pattern: 'tests/**/*', watched: false, included: false, served: true },