Procházet zdrojové kódy

New BABYLON.PickingInfo.prototype.getNormal function

David Catuhe před 11 roky
rodič
revize
535dafabd8

+ 23 - 0
Babylon/Collisions/babylon.pickingInfo.js

@@ -11,4 +11,27 @@ var BABYLON = BABYLON || {};
     BABYLON.PickingInfo.prototype.distance = 0;
     BABYLON.PickingInfo.prototype.pickedPoint = null;
     BABYLON.PickingInfo.prototype.pickedMesh = null;
+    BABYLON.PickingInfo.prototype.bu = 0;
+    BABYLON.PickingInfo.prototype.bv = 0;
+    BABYLON.PickingInfo.prototype.faceId = -1;
+
+    // Methods
+    BABYLON.PickingInfo.prototype.getNormal = function() {
+        if (!this.pickedMesh) {
+            return null;
+        }
+
+        var indices = this.pickedMesh.getIndices();
+        var normals = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
+
+        var normal0 = BABYLON.Vector3.FromArray(normals , indices[this.faceId]);
+        var normal1 = BABYLON.Vector3.FromArray(normals, indices[this.faceId + 1]);
+        var normal2 = BABYLON.Vector3.FromArray(normals, indices[this.faceId + 2]);
+
+        normal0 = normal0.scale(this.bu);
+        normal1 = normal1.scale(this.bv);
+        normal2 = normal2.scale(1.0 - this.bu - this.bv);
+
+        return new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
+    };
 })();

+ 10 - 7
Babylon/Mesh/babylon.mesh.js

@@ -786,7 +786,7 @@ var BABYLON = BABYLON || {};
 
         this._generatePointsArray();
 
-        var distance = Number.MAX_VALUE;
+        var intersectInfo = null;
 
         for (var index = 0; index < this.subMeshes.length; index++) {
             var subMesh = this.subMeshes[index];
@@ -795,11 +795,11 @@ var BABYLON = BABYLON || {};
             if (this.subMeshes.length > 1 && !subMesh.canIntersects(ray))
                 continue;
 
-            var currentDistance = subMesh.intersects(ray, this._positions, this._indices, fastCheck);
+            var currentIntersectInfo = subMesh.intersects(ray, this._positions, this._indices, fastCheck);
 
-            if (currentDistance > 0) {
-                if (fastCheck || currentDistance < distance) {
-                    distance = currentDistance;
+            if (currentIntersectInfo) {
+                if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
+                    intersectInfo = currentIntersectInfo;
 
                     if (fastCheck) {
                         break;
@@ -808,13 +808,13 @@ var BABYLON = BABYLON || {};
             }
         }
 
-        if (distance >= 0 && distance < Number.MAX_VALUE) {
+        if (intersectInfo) {
             // Get picked point
             var world = this.getWorldMatrix();
             var worldOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, world);
             var direction = ray.direction.clone();
             direction.normalize();
-            direction = direction.scale(distance);
+            direction = direction.scale(intersectInfo.distance);
             var worldDirection = BABYLON.Vector3.TransformNormal(direction, world);
 
             var pickedPoint = worldOrigin.add(worldDirection);
@@ -824,6 +824,9 @@ var BABYLON = BABYLON || {};
             pickingInfo.distance = BABYLON.Vector3.Distance(worldOrigin, pickedPoint);
             pickingInfo.pickedPoint = pickedPoint;
             pickingInfo.pickedMesh = this;
+            pickingInfo.bu = intersectInfo.bu;
+            pickingInfo.bv = intersectInfo.bv;
+            pickingInfo.faceId = intersectInfo.faceId;
             return pickingInfo;
         }
 

+ 7 - 9
Babylon/Mesh/babylon.subMesh.js

@@ -87,7 +87,7 @@ var BABYLON = BABYLON || {};
     };
 
     BABYLON.SubMesh.prototype.intersects = function (ray, positions, indices, fastCheck) {
-        var distance = Number.MAX_VALUE;
+        var intersectInfo = null;
 
         // Triangles test
         for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
@@ -95,11 +95,12 @@ var BABYLON = BABYLON || {};
             var p1 = positions[indices[index + 1]];
             var p2 = positions[indices[index + 2]];
 
-            var currentDistance = ray.intersectsTriangle(p0, p1, p2);
+            var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
 
-            if (currentDistance > 0) {
-                if (fastCheck || currentDistance < distance) {
-                    distance = currentDistance;
+            if (currentIntersectInfo) {
+                if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
+                    intersectInfo = currentIntersectInfo;
+                    intersectInfo.faceId = index / 3;
 
                     if (fastCheck) {
                         break;
@@ -108,10 +109,7 @@ var BABYLON = BABYLON || {};
             }
         }
 
-        if (distance > 0 && distance < Number.MAX_VALUE)
-            return distance;
-
-        return 0;
+        return intersectInfo;
     };
 
     // Clone    

+ 9 - 4
Babylon/Tools/babylon.math.js

@@ -125,7 +125,7 @@ var BABYLON = BABYLON || {};
         var det = BABYLON.Vector3.Dot(this._edge1, this._pvec);
 
         if (det === 0) {
-            return 0;
+            return null;
         }
 
         var invdet = 1 / det;
@@ -135,7 +135,7 @@ var BABYLON = BABYLON || {};
         var bu = BABYLON.Vector3.Dot(this._tvec, this._pvec) * invdet;
 
         if (bu < 0 || bu > 1.0) {
-            return 0;
+            return null;
         }
 
         BABYLON.Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
@@ -143,10 +143,15 @@ var BABYLON = BABYLON || {};
         var bv = BABYLON.Vector3.Dot(this.direction, this._qvec) * invdet;
 
         if (bv < 0 || bu + bv > 1.0) {
-            return 0;
+            return null;
         }
 
-        return BABYLON.Vector3.Dot(this._edge2, this._qvec) * invdet;
+        return {
+            bu: bu,
+            bv: bv,
+            distance: BABYLON.Vector3.Dot(this._edge2, this._qvec) * invdet
+        };
+
     };
 
     // Statics

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 2 - 2
babylon.1.9.0.js