Browse Source

new pickInfo.getTextureCoordinates() function

David Catuhe 11 years ago
parent
commit
5c3f44d57f

+ 20 - 1
Babylon/Collisions/babylon.pickingInfo.js

@@ -23,7 +23,7 @@
         }
         // Methods
         PickingInfo.prototype.getNormal = function () {
-            if (!this.pickedMesh) {
+            if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
                 return null;
             }
 
@@ -40,6 +40,25 @@
 
             return new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
         };
+
+        PickingInfo.prototype.getTextureCoordinates = function () {
+            if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
+                return null;
+            }
+
+            var indices = this.pickedMesh.getIndices();
+            var uvs = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
+
+            var uv0 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3] * 2);
+            var uv1 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3 + 1] * 2);
+            var uv2 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3 + 2] * 2);
+
+            uv0 = uv0.scale(this.bu);
+            uv1 = uv1.scale(this.bv);
+            uv2 = uv2.scale(1.0 - this.bu - this.bv);
+
+            return new BABYLON.Vector2(uv0.x + uv1.x + uv2.x, uv0.y + uv1.y + uv2.y);
+        };
         return PickingInfo;
     })();
     BABYLON.PickingInfo = PickingInfo;

+ 20 - 1
Babylon/Collisions/babylon.pickingInfo.ts

@@ -17,7 +17,7 @@
 
         // Methods
         public getNormal(): Vector3 {
-            if (!this.pickedMesh) {
+            if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
                 return null;
             }
 
@@ -34,5 +34,24 @@
 
             return new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
         }
+
+        public getTextureCoordinates(): Vector2 {
+            if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
+                return null;
+            }
+
+            var indices = this.pickedMesh.getIndices();
+            var uvs = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
+
+            var uv0 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3] * 2);
+            var uv1 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3 + 1] * 2);
+            var uv2 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3 + 2] * 2);
+
+            uv0 = uv0.scale(this.bu);
+            uv1 = uv1.scale(this.bv);
+            uv2 = uv2.scale(1.0 - this.bu - this.bv);
+
+            return new BABYLON.Vector2(uv0.x + uv1.x + uv2.x, uv0.y + uv1.y + uv2.y);
+        }
     }
 } 

+ 8 - 0
Babylon/Math/babylon.math.js

@@ -326,6 +326,14 @@
             return new Vector2(0, 0);
         };
 
+        Vector2.FromArray = function (array, offset) {
+            if (!offset) {
+                offset = 0;
+            }
+
+            return new Vector2(array[offset], array[offset + 1]);
+        };
+
         Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
             var squared = amount * amount;
             var cubed = amount * squared;

+ 8 - 0
Babylon/Math/babylon.math.ts

@@ -294,6 +294,14 @@
             return new Vector2(0, 0);
         }
 
+        public static FromArray(array: number[], offset?: number): Vector2 {
+            if (!offset) {
+                offset = 0;
+            }
+
+            return new Vector2(array[offset], array[offset + 1]);
+        }
+
         public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
             var squared = amount * amount;
             var cubed = amount * squared;

+ 1 - 1
Babylon/Tools/babylon.tools.ts

@@ -671,4 +671,4 @@ module BABYLON {
             }
         }
     }
-} 
+} 

File diff suppressed because it is too large
+ 2 - 2
babylon.1.13-beta-debug.js


File diff suppressed because it is too large
+ 12 - 12
babylon.1.13-beta.js