浏览代码

Fixed cameras and Uvs

Simon Ferquel 10 年之前
父节点
当前提交
f05195787f

+ 3 - 5
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Camera.cs

@@ -50,7 +50,7 @@ namespace Max2Babylon
             babylonCamera.ellipsoid = cameraNode.MaxNode.GetVector3Property("babylonjs_ellipsoid");
 
             // Position
-            var wm = cameraNode.GetObjectTM(0);
+            var wm = cameraNode.GetLocalTM(0);
             var position = wm.Translation;
             babylonCamera.position = new float[] { position.X, position.Y, position.Z };
 
@@ -62,10 +62,8 @@ namespace Max2Babylon
             }
             else
             {
-                IPoint3 cameraTargetDist = Loader.Global.Point3.Create();
-                gameCamera.CameraTargetDist.GetPropertyValue(cameraTargetDist, 0);
-                var targetPos = position.Add(cameraTargetDist);
-                babylonCamera.target = new float[] { targetPos.X, targetPos.Y, targetPos.Z };
+                var dir = wm.GetRow(3);
+                babylonCamera.target = new float[] { position.X - dir.X, position.Y - dir.Y, position.Z - dir.Z };
             }
 
             // todo : handle animations

+ 20 - 18
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Light.cs

@@ -19,7 +19,7 @@ namespace Max2Babylon
             babylonLight.intensity = 1;
 
             babylonLight.diffuse = new[] { 1.0f, 1.0f, 1.0f };
-            babylonLight.specular = new[] { 1.0f, 1.0f, 1.0f }; 
+            babylonLight.specular = new[] { 1.0f, 1.0f, 1.0f };
 
             babylonScene.LightsList.Add(babylonLight);
         }
@@ -32,15 +32,20 @@ namespace Max2Babylon
             }
 
             var gameLight = lightNode.IGameObject.AsGameLight();
+            var initialized = gameLight.InitializeData;
             var babylonLight = new BabylonLight();
 
             RaiseMessage(lightNode.Name, 1);
             babylonLight.name = lightNode.Name;
             babylonLight.id = lightNode.MaxNode.GetGuid().ToString();
 
-            var initialized = gameLight.InitializeData;
-          
+
             // Type
+
+            var maxLight = (lightNode.MaxNode.ObjectRef as ILightObject);
+            var lightState = Loader.Global.LightState.Create();
+            maxLight.EvalLightState(0, Tools.Forever, lightState);
+
             var directionScale = -1;
 
             switch (gameLight.LightType)
@@ -54,7 +59,7 @@ namespace Max2Babylon
 
                     float fallOff = 0;
                     gameLight.LightFallOff.GetPropertyValue(ref fallOff, 0, true);
-                    babylonLight.angle = (float)(fallOff* Math.PI / 180.0f);
+                    babylonLight.angle = (float)(fallOff * Math.PI / 180.0f);
                     babylonLight.exponent = 1;
                     break;
                 case Autodesk.Max.IGameLight.LightType.Dir:
@@ -82,7 +87,7 @@ namespace Max2Babylon
             }
 
             // Position
-            var wm = lightNode.GetWorldTM(0);
+            var wm = lightNode.GetObjectTM(0);
             var position = wm.Translation;
             babylonLight.position = new float[] { position.X, position.Y, position.Z };
 
@@ -90,7 +95,7 @@ namespace Max2Babylon
             var target = gameLight.LightTarget;
             if (target != null)
             {
-                var targetWm = target.GetWorldTM(0);
+                var targetWm = target.GetObjectTM(0);
                 var targetPosition = targetWm.Translation;
 
                 var direction = targetPosition.Subtract(position);
@@ -98,15 +103,11 @@ namespace Max2Babylon
             }
             else
             {
-                IMatrix3 rotMatrix = Loader.Global.Matrix3.Create();
-                wm.Rotation.MakeMatrix(rotMatrix, true);
-                IPoint3 p = Loader.Global.Point3.Create(1, 0, 0);
-                var dir = rotMatrix.VectorTransform(p);
-                babylonLight.direction = new float[] { dir.X, dir.Y, dir.Z };
+                var dir = wm.GetRow(3);
+                babylonLight.direction = new float[] { position.X - dir.X, position.Y - dir.Y, position.Z - dir.Z };
             }
 
 
-            var maxLight = (lightNode.MaxNode.ObjectRef as ILightObject);
             var maxScene = Loader.Core.RootNode;
             // Exclusion
             var inclusion = maxLight.ExclList.TestFlag(1); //NT_INCLUDE 
@@ -141,19 +142,20 @@ namespace Max2Babylon
                 babylonLight.excludedMeshesIds = excllist.ToArray();
             }
 
-            // Other fields
+            // Other fields 
             babylonLight.intensity = maxLight.GetIntensity(0, Tools.Forever);
 
-            IPoint3 lightColor = Loader.Global.Point3.Create(0,0,0);
-            gameLight.LightColor.GetPropertyValue(lightColor, 0);
-            babylonLight.diffuse =  new float[] { lightColor.X, lightColor.Y, lightColor.Z };
-            babylonLight.specular = new float[] { lightColor.X, lightColor.Y, lightColor.Z };
+
+            babylonLight.diffuse = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
+            babylonLight.specular = lightState.AffectDiffuse ? maxLight.GetRGBColor(0, Tools.Forever).ToArray() : new float[] { 0, 0, 0 };
+
 
             if (maxLight.UseAtten)
             {
                 babylonLight.range = maxLight.GetAtten(0, 1, Tools.Forever);
             }
 
+
             //// Animations
             //var animations = new List<BabylonAnimation>();
 
@@ -177,7 +179,7 @@ namespace Max2Babylon
             //        var direction = targetPosition.Subtract(position);
             //        return direction.ToArraySwitched();
             //    }
-                
+
             //    var dir = wm.GetRow(2).MultiplyBy(directionScale);
             //    return dir.ToArraySwitched();
             //});

+ 41 - 12
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Mesh.cs

@@ -28,7 +28,10 @@ namespace Max2Babylon
             }
 
             var gameMesh = meshNode.IGameObject.AsGameMesh();
-            bool initialized = gameMesh.InitializeData;
+            gameMesh.SetCreateOptimizedNormalList();
+            gameMesh.SetUseWeightedNormals();
+            bool initialized = gameMesh.InitializeData; //needed, the property is in fact a method initializing the exporter that has wrongly been auto 
+            // translated into a property because it has no parameters
 
             var babylonMesh = new BabylonMesh();
 
@@ -90,7 +93,6 @@ namespace Max2Babylon
             //babylonMesh.pivotMatrix = pivotMatrix.ToArray();
 
             // Mesh
-            
 
             RaiseMessage(meshNode.Name, 1);
             
@@ -133,9 +135,21 @@ namespace Max2Babylon
 
                 var vertices = new List<GlobalVertex>();
                 var indices = new List<int>();
-
-                var hasUV = unskinnedMesh.NumberOfTexVerts > 0;
-                var hasUV2 = unskinnedMesh.GetNumberOfMapVerts(2) > 0;
+                var mappingChannels = unskinnedMesh.ActiveMapChannelNum;
+                bool hasUV = false;
+                bool hasUV2 = false;
+                for(int i=0;i< mappingChannels.Count; ++i)
+                {
+                    IntPtr indexer = new IntPtr(i);
+                    var channelNum = mappingChannels[indexer];
+                    if(channelNum == 1)
+                    {
+                        hasUV = true;
+                    }else if(channelNum == 2)
+                    {
+                        hasUV2 = true;
+                    }
+                }
                 var hasColor = unskinnedMesh.NumberOfColorVerts > 0;
                 var hasAlpha = unskinnedMesh.GetNumberOfMapVerts(-2) > 0;
 
@@ -254,7 +268,7 @@ namespace Max2Babylon
 
                 // Buffers
                 babylonMesh.positions = vertices.SelectMany(v => new float[] { v.Position.X, v.Position.Y, v.Position.Z }).ToArray();
-                babylonMesh.normals = vertices.SelectMany(v => new float[] { v.Normal.X, v.Normal.Y, v.Normal.Z }).ToArray();
+                babylonMesh.normals = vertices.SelectMany(v => new float[] { -v.Normal.X, -v.Normal.Y, -v.Normal.Z }).ToArray();
                 if (hasUV)
                 {
                     babylonMesh.uvs = vertices.SelectMany(v => new float[] { v.UV.X, 1-v.UV.Y }).ToArray();
@@ -474,18 +488,33 @@ namespace Max2Babylon
                 Position = mesh.GetVertex(vertexIndex, true),
                 Normal = mesh.GetNormal((int)face.Norm[facePart], true) //vnorms[vertexIndex].GetNormal(verticesAlreadyExported != null ? 1 : faceObject.SmGroup)
             };
-
+          
             if (hasUV)
             {
-                var tvertexIndex = (int)face.TexCoord[facePart];
-                vertex.UV = mesh.GetTexVertex(tvertexIndex);
+                int[] indices = new int[3];
+                unsafe
+                {
+                    fixed(int* indicesPtr = indices)
+                    {
+                        mesh.GetMapFaceIndex(1, face.MeshFaceIndex, new IntPtr(indicesPtr));
+                    }
+                }
+                var texCoord = mesh.GetMapVertex(1, indices[facePart]);
+                vertex.UV = Loader.Global.Point2.Create( texCoord.X, -texCoord.Y);
             }
 
             if (hasUV2)
             {
-                IPoint3 p = Loader.Global.Point3.Create();
-                var tvertexIndex = mesh.GetMapFaceIndex(2, face.MeshFaceIndex, p.GetNativeHandle());
-                vertex.UV2 = Loader.Global.Point2.Create(p.X, p.Y);
+                int[] indices = new int[3];
+                unsafe
+                {
+                    fixed (int* indicesPtr = indices)
+                    {
+                        mesh.GetMapFaceIndex(2, face.MeshFaceIndex, new IntPtr(indicesPtr));
+                    }
+                }
+                var texCoord = mesh.GetMapVertex(2, indices[facePart]);
+                vertex.UV2 = Loader.Global.Point2.Create(texCoord.X, -texCoord.Y);
             }
 
             if (hasColor)