瀏覽代碼

Fixing issue with texture exporter with 3dsMax

David catuhe 9 年之前
父節點
當前提交
17912c9931

+ 6 - 0
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Texture.cs

@@ -65,6 +65,12 @@ namespace Max2Babylon
 
             var texMap = stdMat.GetSubTexmap(index);
 
+            if (texMap == null)
+            {
+                RaiseWarning("Texture channel " + index + " activated but no texture found.");
+                return null;
+            }
+
             // Fallout
             if (texMap.ClassName == "Falloff") // This is the only way I found to detect it. This is crappy but it works
             {

File diff suppressed because it is too large
+ 509 - 506
dist/preview release/babylon.d.ts


File diff suppressed because it is too large
+ 7 - 7
dist/preview release/babylon.js


+ 102 - 33
dist/preview release/babylon.max.js

@@ -27814,7 +27814,29 @@ var BABYLON;
                             var tmpQ = new CANNON.Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
                             body.quaternion = body.quaternion.mult(tmpQ);
                         }
-                        //TODO - If the body is heightmap-based, this won't work correctly. find a good fix.
+                        if (registeredMesh.heightmap) {
+                            //calculate the correct body position:
+                            var rotationQuaternion = mesh.rotationQuaternion;
+                            mesh.rotationQuaternion = new BABYLON.Quaternion();
+                            mesh.computeWorldMatrix(true);
+                            //get original center with no rotation
+                            var center = mesh.getBoundingInfo().boundingBox.center.clone();
+                            var oldPivot = mesh.getPivotMatrix() || BABYLON.Matrix.Translation(0, 0, 0);
+                            //rotation is back
+                            mesh.rotationQuaternion = rotationQuaternion;
+                            //calculate the new center using a pivot (since Cannon.js doesn't center height maps)
+                            var p = BABYLON.Matrix.Translation(mesh.getBoundingInfo().boundingBox.extendSize.x, 0, -mesh.getBoundingInfo().boundingBox.extendSize.z);
+                            mesh.setPivotMatrix(p);
+                            mesh.computeWorldMatrix(true);
+                            //calculate the translation
+                            var translation = mesh.getBoundingInfo().boundingBox.center.subtract(center).subtract(mesh.position).negate();
+                            body.position = new CANNON.Vec3(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
+                            //add it inverted to the delta 
+                            registeredMesh.delta = mesh.getBoundingInfo().boundingBox.center.subtract(center);
+                            registeredMesh.delta.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
+                            mesh.setPivotMatrix(oldPivot);
+                            mesh.computeWorldMatrix(true);
+                        }
                         return;
                     }
                 }
@@ -27902,9 +27924,10 @@ var BABYLON;
         CannonJSPlugin.prototype._createConvexPolyhedron = function (rawVerts, rawFaces, mesh) {
             var verts = [], faces = [];
             mesh.computeWorldMatrix(true);
+            //reuse this variable
+            var transformed = BABYLON.Vector3.Zero();
             // Get vertices
             for (var i = 0; i < rawVerts.length; i += 3) {
-                var transformed = BABYLON.Vector3.Zero();
                 BABYLON.Vector3.TransformNormalFromFloatsToRef(rawVerts[i], rawVerts[i + 1], rawVerts[i + 2], mesh.getWorldMatrix(), transformed);
                 verts.push(new CANNON.Vec3(transformed.x, transformed.y, transformed.z));
             }
@@ -27915,41 +27938,51 @@ var BABYLON;
             var shape = new CANNON.ConvexPolyhedron(verts, faces);
             return shape;
         };
-        CannonJSPlugin.prototype._createHeightmap = function (mesh) {
+        CannonJSPlugin.prototype._createHeightmap = function (mesh, pointDepth) {
             var pos = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
-            if (pos[0] !== pos[pos.length - 1]) {
-                console.log("ERROR");
-                return;
-            }
             var matrix = [];
-            //dimension
-            var dim = -pos[0];
-            //array size
-            var arraySize = -1;
-            for (var i = 0; i < pos.length; i = i + 3) {
-                if (pos[i + 2] === pos[2]) {
-                    arraySize++;
-                }
-                else {
-                    break;
-                }
-            }
-            //calculate element size
+            //For now pointDepth will not be used and will be automatically calculated.
+            //Future reference - try and find the best place to add a reference to the pointDepth variable.
+            var arraySize = pointDepth || ~~(Math.sqrt(pos.length / 3) - 1);
+            var dim = Math.min(mesh.getBoundingInfo().boundingBox.extendSize.x, mesh.getBoundingInfo().boundingBox.extendSize.z);
             var elementSize = dim * 2 / arraySize;
-            //calculate the matrix for cannon's heightfield
+            var minY = mesh.getBoundingInfo().boundingBox.extendSize.y;
             for (var i = 0; i < pos.length; i = i + 3) {
                 var x = Math.round((pos[i + 0]) / elementSize + arraySize / 2);
                 var z = Math.round(((pos[i + 2]) / elementSize - arraySize / 2) * -1);
+                var y = pos[i + 1] + minY;
                 if (!matrix[x]) {
                     matrix[x] = [];
                 }
-                matrix[x][z] = pos[i + 1];
+                if (!matrix[x][z]) {
+                    matrix[x][z] = y;
+                }
+                matrix[x][z] = Math.max(y, matrix[x][z]);
+            }
+            for (var x = 0; x <= arraySize; ++x) {
+                if (!matrix[x]) {
+                    var loc = 1;
+                    while (!matrix[(x + loc) % arraySize]) {
+                        loc++;
+                    }
+                    matrix[x] = matrix[(x + loc) % arraySize].slice();
+                }
+                for (var z = 0; z <= arraySize; ++z) {
+                    if (!matrix[x][z]) {
+                        var loc = 1;
+                        var newValue;
+                        while (newValue === undefined) {
+                            newValue = matrix[x][(z + loc++) % arraySize];
+                        }
+                        matrix[x][z] = newValue;
+                    }
+                }
             }
             var shape = new CANNON.Heightfield(matrix, {
                 elementSize: elementSize
             });
             //For future reference, needed for body transformation
-            shape.dim = dim;
+            shape.minY = minY;
             return shape;
         };
         CannonJSPlugin.prototype._addMaterial = function (friction, restitution) {
@@ -27990,19 +28023,37 @@ var BABYLON;
                 //-90 DEG in X, precalculated
                 var tmpQ = new CANNON.Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
                 body.quaternion = body.quaternion.mult(tmpQ);
-                //Invert!
+                //Invert! (Precalculated, 90 deg in X)
                 deltaRotation = new BABYLON.Quaternion(0.7071067811865475, 0, 0, 0.7071067811865475);
             }
             //If it is a heightfield, if should be centered.
             if (shape.type === CANNON.Shape.types.HEIGHTFIELD) {
-                body.position = new CANNON.Vec3(-shape.dim, 0, shape.dim).vadd(mesh.position);
+                //calculate the correct body position:
+                var rotationQuaternion = mesh.rotationQuaternion;
+                mesh.rotationQuaternion = new BABYLON.Quaternion();
+                mesh.computeWorldMatrix(true);
+                //get original center with no rotation
+                var center = mesh.getBoundingInfo().boundingBox.center.clone();
+                var oldPivot = mesh.getPivotMatrix() || BABYLON.Matrix.Translation(0, 0, 0);
+                //rotation is back
+                mesh.rotationQuaternion = rotationQuaternion;
+                //calculate the new center using a pivot (since Cannon.js doesn't center height maps)
+                var p = BABYLON.Matrix.Translation(mesh.getBoundingInfo().boundingBox.extendSize.x, 0, -mesh.getBoundingInfo().boundingBox.extendSize.z);
+                mesh.setPivotMatrix(p);
+                mesh.computeWorldMatrix(true);
+                //calculate the translation
+                var translation = mesh.getBoundingInfo().boundingBox.center.subtract(center).subtract(mesh.position).negate();
+                body.position = new CANNON.Vec3(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
                 //add it inverted to the delta 
-                deltaPosition = mesh.position.add(new BABYLON.Vector3(shape.dim, 0, -shape.dim));
+                deltaPosition = mesh.getBoundingInfo().boundingBox.center.subtract(center);
+                deltaPosition.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
+                mesh.setPivotMatrix(oldPivot);
+                mesh.computeWorldMatrix(true);
             }
             //add the shape
             body.addShape(shape);
             this._world.add(body);
-            this._registeredMeshes.push({ mesh: mesh, body: body, material: material, delta: deltaPosition, deltaRotation: deltaRotation });
+            this._registeredMeshes.push({ mesh: mesh, body: body, material: material, delta: deltaPosition, deltaRotation: deltaRotation, heightmap: shape.type === CANNON.Shape.types.HEIGHTFIELD });
             return body;
         };
         CannonJSPlugin.prototype.registerMeshesAsCompound = function (parts, options) {
@@ -35818,7 +35869,7 @@ var BABYLON;
 var BABYLON;
 (function (BABYLON) {
     var SolidParticleSystem = (function () {
-        function SolidParticleSystem(name, scene) {
+        function SolidParticleSystem(name, scene, options) {
             // public members  
             this.particles = new Array();
             this.nbParticles = 0;
@@ -35830,6 +35881,7 @@ var BABYLON;
             this._colors = new Array();
             this._uvs = new Array();
             this._index = 0; // indices index
+            this._updatable = true;
             this._shapeCounter = 0;
             this._copy = new BABYLON.SolidParticle(null, null, null, null, null);
             this._color = new BABYLON.Color4(0, 0, 0, 0);
@@ -35864,10 +35916,15 @@ var BABYLON;
             this.name = name;
             this._scene = scene;
             this._camera = scene.activeCamera;
+            if (options && options.updatable) {
+                this._updatable = options.updatable;
+            }
+            else {
+                this._updatable = true;
+            }
         }
         // build the SPS mesh : returns the mesh
-        SolidParticleSystem.prototype.buildMesh = function (updatable) {
-            if (updatable === void 0) { updatable = true; }
+        SolidParticleSystem.prototype.buildMesh = function () {
             if (this.nbParticles === 0) {
                 var triangle = BABYLON.MeshBuilder.CreateDisc("", { radius: 1, tessellation: 3 }, this._scene);
                 this.addShape(triangle, 1);
@@ -35890,14 +35947,14 @@ var BABYLON;
                 vertexData.set(this._colors32, BABYLON.VertexBuffer.ColorKind);
             }
             var mesh = new BABYLON.Mesh(name, this._scene);
-            vertexData.applyToMesh(mesh, updatable);
+            vertexData.applyToMesh(mesh, this._updatable);
             this.mesh = mesh;
             // free memory
             this._positions = null;
             this._normals = null;
             this._uvs = null;
             this._colors = null;
-            if (!updatable) {
+            if (!this._updatable) {
                 this.particles.length = 0;
             }
             return mesh;
@@ -36015,7 +36072,9 @@ var BABYLON;
             // particles
             for (var i = 0; i < nb; i++) {
                 this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors, this.nbParticles + i, i, options);
-                this._addParticle(this.nbParticles + i, this._positions.length, modelShape, this._shapeCounter, i);
+                if (this._updatable) {
+                    this._addParticle(this.nbParticles + i, this._positions.length, modelShape, this._shapeCounter, i);
+                }
                 this._index += shape.length;
             }
             this.nbParticles += nb;
@@ -36227,6 +36286,16 @@ var BABYLON;
         // dispose the SPS
         SolidParticleSystem.prototype.dispose = function () {
             this.mesh.dispose();
+            // drop references to internal big arrays for the GC
+            this._positions = null;
+            this._indices = null;
+            this._normals = null;
+            this._uvs = null;
+            this._colors = null;
+            this._positions32 = null;
+            this._normals32 = null;
+            this._uvs32 = null;
+            this._colors32 = null;
         };
         Object.defineProperty(SolidParticleSystem.prototype, "computeParticleRotation", {
             // getters

File diff suppressed because it is too large
+ 15 - 15
dist/preview release/babylon.noworker.js


二進制
exporters/3ds Max/Max2Babylon-0.21.zip


+ 0 - 1
materialsLibrary/gulpfile.js

@@ -1,5 +1,4 @@
 var gulp = require("gulp");
-var uglify = require("gulp-uglify");
 var typescript = require("gulp-typescript");
 var srcToVariable = require("./gulp-srcToVariable");
 var merge2 = require("merge2");

+ 24 - 6
src/Particles/babylon.solidParticleSystem.js

@@ -1,7 +1,7 @@
 var BABYLON;
 (function (BABYLON) {
     var SolidParticleSystem = (function () {
-        function SolidParticleSystem(name, scene) {
+        function SolidParticleSystem(name, scene, options) {
             // public members  
             this.particles = new Array();
             this.nbParticles = 0;
@@ -13,6 +13,7 @@ var BABYLON;
             this._colors = new Array();
             this._uvs = new Array();
             this._index = 0; // indices index
+            this._updatable = true;
             this._shapeCounter = 0;
             this._copy = new BABYLON.SolidParticle(null, null, null, null, null);
             this._color = new BABYLON.Color4(0, 0, 0, 0);
@@ -47,10 +48,15 @@ var BABYLON;
             this.name = name;
             this._scene = scene;
             this._camera = scene.activeCamera;
+            if (options && options.updatable) {
+                this._updatable = options.updatable;
+            }
+            else {
+                this._updatable = true;
+            }
         }
         // build the SPS mesh : returns the mesh
-        SolidParticleSystem.prototype.buildMesh = function (updatable) {
-            if (updatable === void 0) { updatable = true; }
+        SolidParticleSystem.prototype.buildMesh = function () {
             if (this.nbParticles === 0) {
                 var triangle = BABYLON.MeshBuilder.CreateDisc("", { radius: 1, tessellation: 3 }, this._scene);
                 this.addShape(triangle, 1);
@@ -73,14 +79,14 @@ var BABYLON;
                 vertexData.set(this._colors32, BABYLON.VertexBuffer.ColorKind);
             }
             var mesh = new BABYLON.Mesh(name, this._scene);
-            vertexData.applyToMesh(mesh, updatable);
+            vertexData.applyToMesh(mesh, this._updatable);
             this.mesh = mesh;
             // free memory
             this._positions = null;
             this._normals = null;
             this._uvs = null;
             this._colors = null;
-            if (!updatable) {
+            if (!this._updatable) {
                 this.particles.length = 0;
             }
             return mesh;
@@ -198,7 +204,9 @@ var BABYLON;
             // particles
             for (var i = 0; i < nb; i++) {
                 this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors, this.nbParticles + i, i, options);
-                this._addParticle(this.nbParticles + i, this._positions.length, modelShape, this._shapeCounter, i);
+                if (this._updatable) {
+                    this._addParticle(this.nbParticles + i, this._positions.length, modelShape, this._shapeCounter, i);
+                }
                 this._index += shape.length;
             }
             this.nbParticles += nb;
@@ -410,6 +418,16 @@ var BABYLON;
         // dispose the SPS
         SolidParticleSystem.prototype.dispose = function () {
             this.mesh.dispose();
+            // drop references to internal big arrays for the GC
+            this._positions = null;
+            this._indices = null;
+            this._normals = null;
+            this._uvs = null;
+            this._colors = null;
+            this._positions32 = null;
+            this._normals32 = null;
+            this._uvs32 = null;
+            this._colors32 = null;
         };
         Object.defineProperty(SolidParticleSystem.prototype, "computeParticleRotation", {
             // getters

+ 2 - 1
src/Particles/babylon.solidParticleSystem.ts

@@ -59,7 +59,7 @@ module BABYLON {
         private _cosYaw: number = 0.0;
 
 
-        constructor(name: string, scene: Scene, options?: {updatable?: boolean}) {
+        constructor(name: string, scene: Scene, options?: { updatable?: boolean }) {
             this.name = name;
             this._scene = scene;
             this._camera = scene.activeCamera;
@@ -566,3 +566,4 @@ module BABYLON {
 
 
 
+

+ 16 - 15
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -141,13 +141,13 @@
             //For now pointDepth will not be used and will be automatically calculated.
             //Future reference - try and find the best place to add a reference to the pointDepth variable.
             var arraySize = pointDepth || ~~(Math.sqrt(pos.length / 3) - 1);
-    
+
             var dim = Math.min(mesh.getBoundingInfo().boundingBox.extendSize.x, mesh.getBoundingInfo().boundingBox.extendSize.z);
-    
+
             var elementSize = dim * 2 / arraySize;
-    
+
             var minY = mesh.getBoundingInfo().boundingBox.extendSize.y;
-    
+
             for (var i = 0; i < pos.length; i = i + 3) {
                 var x = Math.round((pos[i + 0]) / elementSize + arraySize / 2);
                 var z = Math.round(((pos[i + 2]) / elementSize - arraySize / 2) * -1);
@@ -160,8 +160,8 @@
                 }
                 matrix[x][z] = Math.max(y, matrix[x][z]);
             }
-    
-    
+
+
             for (var x = 0; x <= arraySize; ++x) {
                 if (!matrix[x]) {
                     var loc = 1;
@@ -179,7 +179,7 @@
                             newValue = matrix[x][(z + loc++) % arraySize];
                         }
                         matrix[x][z] = newValue;
-    
+
                     }
                 }
             }
@@ -257,7 +257,7 @@
                 
                 //get original center with no rotation
                 var center = mesh.getBoundingInfo().boundingBox.center.clone();
-        
+
                 var oldPivot = mesh.getPivotMatrix() || Matrix.Translation(0, 0, 0);
                 
                 //rotation is back
@@ -270,12 +270,12 @@
         
                 //calculate the translation
                 var translation = mesh.getBoundingInfo().boundingBox.center.subtract(center).subtract(mesh.position).negate();
-                
+
                 body.position = new CANNON.Vec3(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
                 //add it inverted to the delta 
                 deltaPosition = mesh.getBoundingInfo().boundingBox.center.subtract(center);
                 deltaPosition.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
-                
+
                 mesh.setPivotMatrix(oldPivot);
                 mesh.computeWorldMatrix(true);
             }
@@ -371,8 +371,8 @@
                         var tmpQ = new CANNON.Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
                         body.quaternion = body.quaternion.mult(tmpQ);
                     }
-                    
-                    if(registeredMesh.heightmap) {
+
+                    if (registeredMesh.heightmap) {
                         //calculate the correct body position:
                         var rotationQuaternion = mesh.rotationQuaternion;
                         mesh.rotationQuaternion = new BABYLON.Quaternion();
@@ -380,7 +380,7 @@
                         
                         //get original center with no rotation
                         var center = mesh.getBoundingInfo().boundingBox.center.clone();
-                
+
                         var oldPivot = mesh.getPivotMatrix() || Matrix.Translation(0, 0, 0);
                         
                         //rotation is back
@@ -393,12 +393,12 @@
                 
                         //calculate the translation
                         var translation = mesh.getBoundingInfo().boundingBox.center.subtract(center).subtract(mesh.position).negate();
-                        
+
                         body.position = new CANNON.Vec3(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
                         //add it inverted to the delta 
                         registeredMesh.delta = mesh.getBoundingInfo().boundingBox.center.subtract(center);
                         registeredMesh.delta.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
-                        
+
                         mesh.setPivotMatrix(oldPivot);
                         mesh.computeWorldMatrix(true);
                     }
@@ -458,3 +458,4 @@
 
 
 
+