Procházet zdrojové kódy

PickingInfo.getNormal can now use either vertices normals or vertices positions

David Catuhe před 10 roky
rodič
revize
6bb5fd13f5

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 17 - 21
dist/preview release - alpha/babylon.2.2.js


+ 23 - 10
dist/preview release - alpha/babylon.2.2.max.js

@@ -7227,24 +7227,37 @@ var BABYLON;
             this.subMeshId = 0;
         }
         // Methods
-        PickingInfo.prototype.getNormal = function (useWorldCoordinates) {
+        PickingInfo.prototype.getNormal = function (useWorldCoordinates, useVerticesNormals) {
             if (useWorldCoordinates === void 0) { useWorldCoordinates = false; }
+            if (useVerticesNormals === void 0) { useVerticesNormals = true; }
             if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
                 return null;
             }
             var indices = this.pickedMesh.getIndices();
-            var normals = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
-            var normal0 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
-            var normal1 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
-            var normal2 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
-            normal0 = normal0.scale(this.bu);
-            normal1 = normal1.scale(this.bv);
-            normal2 = normal2.scale(1.0 - this.bu - this.bv);
-            var result = new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
+            var result;
+            if (useVerticesNormals) {
+                var normals = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
+                var normal0 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
+                var normal1 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
+                var normal2 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
+                normal0 = normal0.scale(this.bu);
+                normal1 = normal1.scale(this.bv);
+                normal2 = normal2.scale(1.0 - this.bu - this.bv);
+                result = new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
+            }
+            else {
+                var positions = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
+                var vertex1 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3] * 3);
+                var vertex2 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3 + 1] * 3);
+                var vertex3 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3 + 2] * 3);
+                var p1p2 = vertex1.subtract(vertex2);
+                var p3p2 = vertex3.subtract(vertex2);
+                result = BABYLON.Vector3.Cross(p1p2, p3p2);
+            }
             if (useWorldCoordinates) {
                 result = BABYLON.Vector3.TransformNormal(result, this.pickedMesh.getWorldMatrix());
             }
-            return result;
+            return BABYLON.Vector3.Normalize(result);
         };
         PickingInfo.prototype.getTextureCoordinates = function () {
             if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 17 - 21
dist/preview release - alpha/babylon.2.2.noworker.js


+ 1 - 0
dist/preview release - alpha/what's new.md

@@ -3,6 +3,7 @@
     - Meshes can now be attached to bones. See [documentation here](http://babylondoc.azurewebsites.net/page.php?p=22421) and [sample here](http://www.babylonjs-playground.com/#11BH6Z#18) [deltakosh](https://github.com/deltakosh)
     - HDR Rendering pipeline. See [demo here]() [julien-moreau](https://github.com/julien-moreau)
   - **Updates**
+    - PickingInfo.getNormal can now use either vertices normals or vertices positions [deltakosh](https://github.com/deltakosh)
     - Meshes can now support uv2, uv4, uv5 and uv6 for ShaderMaterials [deltakosh](https://github.com/deltakosh)
     - Panning support for ArcRotateCamera [julien-moreau](https://github.com/julien-moreau)
     - Vertex color and diffuse color can now be mixed [deltakosh](https://github.com/deltakosh)

+ 23 - 10
src/Collisions/babylon.pickingInfo.js

@@ -23,24 +23,37 @@ var BABYLON;
             this.subMeshId = 0;
         }
         // Methods
-        PickingInfo.prototype.getNormal = function (useWorldCoordinates) {
+        PickingInfo.prototype.getNormal = function (useWorldCoordinates, useVerticesNormals) {
             if (useWorldCoordinates === void 0) { useWorldCoordinates = false; }
+            if (useVerticesNormals === void 0) { useVerticesNormals = true; }
             if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
                 return null;
             }
             var indices = this.pickedMesh.getIndices();
-            var normals = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
-            var normal0 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
-            var normal1 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
-            var normal2 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
-            normal0 = normal0.scale(this.bu);
-            normal1 = normal1.scale(this.bv);
-            normal2 = normal2.scale(1.0 - this.bu - this.bv);
-            var result = new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
+            var result;
+            if (useVerticesNormals) {
+                var normals = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
+                var normal0 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
+                var normal1 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
+                var normal2 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
+                normal0 = normal0.scale(this.bu);
+                normal1 = normal1.scale(this.bv);
+                normal2 = normal2.scale(1.0 - this.bu - this.bv);
+                result = new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
+            }
+            else {
+                var positions = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
+                var vertex1 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3] * 3);
+                var vertex2 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3 + 1] * 3);
+                var vertex3 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3 + 2] * 3);
+                var p1p2 = vertex1.subtract(vertex2);
+                var p3p2 = vertex3.subtract(vertex2);
+                result = BABYLON.Vector3.Cross(p1p2, p3p2);
+            }
             if (useWorldCoordinates) {
                 result = BABYLON.Vector3.TransformNormal(result, this.pickedMesh.getWorldMatrix());
             }
-            return result;
+            return BABYLON.Vector3.Normalize(result);
         };
         PickingInfo.prototype.getTextureCoordinates = function () {
             if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {

+ 27 - 10
src/Collisions/babylon.pickingInfo.ts

@@ -18,27 +18,44 @@
         public subMeshId = 0;
 
         // Methods
-        public getNormal(useWorldCoordinates = false): Vector3 {
+        public getNormal(useWorldCoordinates = false, useVerticesNormals = true): Vector3 {
             if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
                 return null;
             }
 
             var indices = this.pickedMesh.getIndices();
-            var normals = this.pickedMesh.getVerticesData(VertexBuffer.NormalKind);
+            var result: Vector3;
 
-            var normal0 = Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
-            var normal1 = Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
-            var normal2 = Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
+            if (useVerticesNormals) {
+                var normals = this.pickedMesh.getVerticesData(VertexBuffer.NormalKind);
 
-            normal0 = normal0.scale(this.bu);
-            normal1 = normal1.scale(this.bv);
-            normal2 = normal2.scale(1.0 - this.bu - this.bv);
+                var normal0 = Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
+                var normal1 = Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
+                var normal2 = Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
+
+                normal0 = normal0.scale(this.bu);
+                normal1 = normal1.scale(this.bv);
+                normal2 = normal2.scale(1.0 - this.bu - this.bv);
+
+                result = new Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
+            } else {
+                var positions = this.pickedMesh.getVerticesData(VertexBuffer.PositionKind);
+
+                var vertex1 = Vector3.FromArray(positions, indices[this.faceId * 3] * 3);
+                var vertex2 = Vector3.FromArray(positions, indices[this.faceId * 3 + 1] * 3);
+                var vertex3 = Vector3.FromArray(positions, indices[this.faceId * 3 + 2] * 3);
+
+                var p1p2 = vertex1.subtract(vertex2);
+                var p3p2 = vertex3.subtract(vertex2);
+
+                result = BABYLON.Vector3.Cross(p1p2, p3p2);
+            }
 
-            var result = new Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
             if (useWorldCoordinates) {
                 result = Vector3.TransformNormal(result, this.pickedMesh.getWorldMatrix());
             }
-            return result;
+
+            return BABYLON.Vector3.Normalize(result);
         }
 
         public getTextureCoordinates(): Vector2 {