瀏覽代碼

Added PBR exportation support

David Catuhe 9 年之前
父節點
當前提交
d874b5e03b

二進制
Exporters/Unity 5/Unity3D2Babylon/BabylonExport.Entities.dll


+ 14 - 6
Exporters/Unity 5/Unity3D2Babylon/ExporterWindow.cs

@@ -6,6 +6,7 @@ using JsonFx;
 using UnityEditor;
 using UnityEngine;
 using JsonFx.Json;
+using UnityEditor.SceneManagement;
 
 namespace Unity3D2Babylon
 {
@@ -40,7 +41,7 @@ namespace Unity3D2Babylon
 
         void Initialize()
         {
-            title = "Babylon.js";
+            titleContent = new GUIContent("Babylon.js");
         }
 
         void OnGUI()
@@ -63,9 +64,17 @@ namespace Unity3D2Babylon
             EditorGUILayout.Space();
             GUILayout.Label("Collisions options", EditorStyles.boldLabel);
             exportationOptions.ExportCollisions = EditorGUILayout.Toggle("Collisions", exportationOptions.ExportCollisions);
-            exportationOptions.CameraEllipsoid = EditorGUILayout.Vector3Field("Camera's Ellipsoid:", exportationOptions.CameraEllipsoid);
-            exportationOptions.Gravity = EditorGUILayout.Vector3Field("Gravity:", exportationOptions.Gravity);
 
+            EditorGUILayout.BeginHorizontal(GUILayout.ExpandHeight(false));
+            GUILayout.Label("Camera's Ellipsoid");
+            exportationOptions.CameraEllipsoid = EditorGUILayout.Vector3Field("", exportationOptions.CameraEllipsoid, GUILayout.ExpandWidth(false));
+            EditorGUILayout.EndHorizontal();
+            GUILayout.Space(-16);
+            EditorGUILayout.BeginHorizontal();
+            GUILayout.Label("Gravity");
+            exportationOptions.Gravity = EditorGUILayout.Vector3Field("", exportationOptions.Gravity, GUILayout.ExpandWidth(false));
+            EditorGUILayout.EndHorizontal();
+            GUILayout.Space(-16);
             EditorGUILayout.Space();
             GUILayout.Label("Physics options", EditorStyles.boldLabel);
             exportationOptions.ExportPhysics = EditorGUILayout.Toggle("Physics", exportationOptions.ExportPhysics);
@@ -105,8 +114,7 @@ namespace Unity3D2Babylon
         {
             try
             {
-                int pos = EditorApplication.currentScene.LastIndexOf("/", StringComparison.Ordinal);
-                string sceneName = EditorApplication.currentScene.Substring(pos + 1);
+                string sceneName = EditorSceneManager.GetActiveScene().name;
 
                 exportationOptions.DefaultFolder = EditorUtility.SaveFolderPanel("Please select a folder", exportationOptions.DefaultFolder, "");
 
@@ -119,7 +127,7 @@ namespace Unity3D2Babylon
 
                 watch.Start();
 
-                var jsWriter = new JsonWriter();                
+                var jsWriter = new JsonWriter();
                 File.WriteAllText("Unity3D2Babylon.ini", jsWriter.Write(exportationOptions));
                 logs.Clear();
 

+ 5 - 0
Exporters/Unity 5/Unity3D2Babylon/SceneBuilder.Lights.cs

@@ -43,6 +43,11 @@ namespace Unity3D2Babylon
 
         private void ConvertUnityLightToBabylon(Light light, float progress)
         {
+            if (!light.isActiveAndEnabled || light.alreadyLightmapped)
+            {
+                return;
+            }
+
             ExporterWindow.ReportProgress(progress, "Exporting light: " + light.name);
 
             BabylonLight babylonLight = new BabylonLight

+ 40 - 1
Exporters/Unity 5/Unity3D2Babylon/SceneBuilder.Materials.cs

@@ -150,7 +150,7 @@ namespace Unity3D2Babylon
 
             if (!materialsDictionary.ContainsKey(material.name))
             {
-                var bMat = new BabylonMaterial
+                var bMat = new BabylonStandardMaterial
                 {
                     name = material.name,
                     id = Guid.NewGuid().ToString(),
@@ -252,6 +252,45 @@ namespace Unity3D2Babylon
             return materialsDictionary[material.name];
         }
 
+
+        private BabylonMaterial DumpPBRMaterial(Material material, Renderer renderer)
+        {
+            if (!materialsDictionary.ContainsKey(material.name))
+            {
+                var bMat = new BabylonPBRMaterial
+                {
+                    name = material.name,
+                    id = Guid.NewGuid().ToString(),
+                    albedoColor = new float[4]
+                };
+
+                if (material.HasProperty("_Color"))
+                {
+                    bMat.albedoColor = material.color.ToFloat();
+                }
+
+                bMat.albedoTexture = DumpTextureFromMaterial(material, "_MainTex");
+
+                if (material.HasProperty("_Glossiness"))
+                {
+                    bMat.microSurface = material.GetFloat("_Glossiness");
+                }
+
+                if (material.HasProperty("_Metallic"))
+                {
+                    var metallic = material.GetFloat("_Metallic");
+                    bMat.reflectivityColor = new float[] { metallic, metallic, metallic };
+                }
+
+                bMat.bumpTexture = DumpTextureFromMaterial(material, "_BumpMap");
+
+                materialsDictionary.Add(bMat.name, bMat);
+                return bMat;
+            }
+
+            return materialsDictionary[material.name];
+        }
+
         private BabylonTexture DumpTextureFromMaterial(Material material, string name)
         {
             if (!material.HasProperty(name))

+ 140 - 160
Exporters/Unity 5/Unity3D2Babylon/SceneBuilder.Meshes.cs

@@ -60,205 +60,185 @@ namespace Unity3D2Babylon
 
             babylonMesh.scaling = transform.localScale.ToFloat();
 
-            babylonMesh.positions = new float[mesh.vertexCount * 3];
-
-            for (int i = 0; i < mesh.vertices.Length; i++)
+            if (mesh != null)
             {
-                babylonMesh.positions[i * 3] = mesh.vertices[i].x;
-                babylonMesh.positions[(i * 3) + 1] = mesh.vertices[i].y;
-                babylonMesh.positions[(i * 3) + 2] = mesh.vertices[i].z;
-
-                // Computing world extends
-                var worldPosition = transform.TransformPoint(mesh.vertices[i]);
+                babylonMesh.positions = new float[mesh.vertexCount * 3];
 
-                if (worldPosition.x > babylonScene.MaxVector.X)
-                {
-                    babylonScene.MaxVector.X = worldPosition.x;
-                }
-                if (worldPosition.y > babylonScene.MaxVector.Y)
+                for (int i = 0; i < mesh.vertices.Length; i++)
                 {
-                    babylonScene.MaxVector.Y = worldPosition.y;
-                }
-                if (worldPosition.z > babylonScene.MaxVector.Z)
-                {
-                    babylonScene.MaxVector.Z = worldPosition.z;
-                }
+                    babylonMesh.positions[i * 3] = mesh.vertices[i].x;
+                    babylonMesh.positions[(i * 3) + 1] = mesh.vertices[i].y;
+                    babylonMesh.positions[(i * 3) + 2] = mesh.vertices[i].z;
 
-                if (worldPosition.x < babylonScene.MinVector.X)
-                {
-                    babylonScene.MinVector.X = worldPosition.x;
-                }
-                if (worldPosition.y < babylonScene.MinVector.Y)
-                {
-                    babylonScene.MinVector.Y = worldPosition.y;
-                }
-                if (worldPosition.z < babylonScene.MinVector.Z)
-                {
-                    babylonScene.MinVector.Z = worldPosition.z;
+                    // Computing world extends
+                    var worldPosition = transform.TransformPoint(mesh.vertices[i]);
+
+                    if (worldPosition.x > babylonScene.MaxVector.X)
+                    {
+                        babylonScene.MaxVector.X = worldPosition.x;
+                    }
+                    if (worldPosition.y > babylonScene.MaxVector.Y)
+                    {
+                        babylonScene.MaxVector.Y = worldPosition.y;
+                    }
+                    if (worldPosition.z > babylonScene.MaxVector.Z)
+                    {
+                        babylonScene.MaxVector.Z = worldPosition.z;
+                    }
+
+                    if (worldPosition.x < babylonScene.MinVector.X)
+                    {
+                        babylonScene.MinVector.X = worldPosition.x;
+                    }
+                    if (worldPosition.y < babylonScene.MinVector.Y)
+                    {
+                        babylonScene.MinVector.Y = worldPosition.y;
+                    }
+                    if (worldPosition.z < babylonScene.MinVector.Z)
+                    {
+                        babylonScene.MinVector.Z = worldPosition.z;
+                    }
                 }
-            }
 
-            babylonMesh.normals = new float[mesh.vertexCount * 3];
+                babylonMesh.normals = new float[mesh.vertexCount * 3];
 
-            for (int i = 0; i < mesh.normals.Length; i++)
-            {
-                babylonMesh.normals[i * 3] = mesh.normals[i].x;
-                babylonMesh.normals[(i * 3) + 1] = mesh.normals[i].y;
-                babylonMesh.normals[(i * 3) + 2] = mesh.normals[i].z;
-            }
+                for (int i = 0; i < mesh.normals.Length; i++)
+                {
+                    babylonMesh.normals[i * 3] = mesh.normals[i].x;
+                    babylonMesh.normals[(i * 3) + 1] = mesh.normals[i].y;
+                    babylonMesh.normals[(i * 3) + 2] = mesh.normals[i].z;
+                }
 
-            babylonMesh.uvs = new float[mesh.vertexCount * 2];
+                babylonMesh.uvs = new float[mesh.vertexCount * 2];
 
-            for (int i = 0; i < mesh.uv.Length; i++)
-            {
-                babylonMesh.uvs[i * 2] = mesh.uv[i].x;
-                babylonMesh.uvs[(i * 2) + 1] = mesh.uv[i].y;
-            }
+                for (int i = 0; i < mesh.uv.Length; i++)
+                {
+                    babylonMesh.uvs[i * 2] = mesh.uv[i].x;
+                    babylonMesh.uvs[(i * 2) + 1] = mesh.uv[i].y;
+                }
 
-            babylonMesh.uvs2 = new float[mesh.vertexCount * 2];
+                babylonMesh.uvs2 = new float[mesh.vertexCount * 2];
 
-            if (mesh.uv2 != null && mesh.uv2.Length > 0)
-            {
-                for (int i = 0; i < mesh.uv2.Length; i++)
+                if (mesh.uv2 != null && mesh.uv2.Length > 0)
                 {
-                    babylonMesh.uvs2[i * 2] = mesh.uv2[i].x;
-                    babylonMesh.uvs2[(i * 2) + 1] = mesh.uv2[i].y;
+                    for (int i = 0; i < mesh.uv2.Length; i++)
+                    {
+                        babylonMesh.uvs2[i * 2] = mesh.uv2[i].x;
+                        babylonMesh.uvs2[(i * 2) + 1] = mesh.uv2[i].y;
+                    }
                 }
-            }
-            else
-            {
-                for (int i = 0; i < mesh.uv.Length; i++)
+                else
                 {
-                    babylonMesh.uvs2[i * 2] = mesh.uv[i].x;
-                    babylonMesh.uvs2[(i * 2) + 1] = mesh.uv[i].y;
+                    for (int i = 0; i < mesh.uv.Length; i++)
+                    {
+                        babylonMesh.uvs2[i * 2] = mesh.uv[i].x;
+                        babylonMesh.uvs2[(i * 2) + 1] = mesh.uv[i].y;
+                    }
                 }
-            }
 
-            babylonMesh.indices = new int[mesh.triangles.Length];
+                babylonMesh.indices = new int[mesh.triangles.Length];
 
-            for (int i = 0; i < mesh.triangles.Length; i += 3)
-            {
-                babylonMesh.indices[i] = mesh.triangles[i + 2];
-                babylonMesh.indices[i + 1] = mesh.triangles[i + 1];
-                babylonMesh.indices[i + 2] = mesh.triangles[i];
-            }
+                for (int i = 0; i < mesh.triangles.Length; i += 3)
+                {
+                    babylonMesh.indices[i] = mesh.triangles[i + 2];
+                    babylonMesh.indices[i + 1] = mesh.triangles[i + 1];
+                    babylonMesh.indices[i + 2] = mesh.triangles[i];
+                }
 
-            if (renderer != null)
-            {
-                if (mesh.subMeshCount > 1) // Multimaterials
+                if (renderer != null && renderer.sharedMaterial != null)
                 {
-                    BabylonMultiMaterial bMultiMat;
-                    if (!multiMatDictionary.ContainsKey(renderer.sharedMaterial.name))
+                    if (mesh.subMeshCount > 1) // Multimaterials
                     {
-                        bMultiMat = new BabylonMultiMaterial
+                        BabylonMultiMaterial bMultiMat;
+                        if (!multiMatDictionary.ContainsKey(renderer.sharedMaterial.name))
                         {
-                            materials = new string[mesh.subMeshCount],
-                            id = Guid.NewGuid().ToString(),
-                            name = renderer.sharedMaterial.name
-                        };
-
-                        for (int i = 0; i < renderer.sharedMaterials.Length; i++)
+                            bMultiMat = new BabylonMultiMaterial
+                            {
+                                materials = new string[mesh.subMeshCount],
+                                id = Guid.NewGuid().ToString(),
+                                name = renderer.sharedMaterial.name
+                            };
+
+                            for (int i = 0; i < renderer.sharedMaterials.Length; i++)
+                            {
+                                var sharedMaterial = renderer.sharedMaterials[i];
+                                BabylonMaterial babylonMaterial;
+
+                                if (sharedMaterial.HasProperty("_Metallic"))
+                                {
+                                    babylonMaterial = DumpPBRMaterial(sharedMaterial, renderer);
+                                }
+                                else
+                                {
+                                    babylonMaterial = DumpMaterial(sharedMaterial, renderer);
+                                }
+
+                                bMultiMat.materials[i] = babylonMaterial.id;
+                            }
+                            if (mesh.subMeshCount > 1)
+                            {
+                                multiMatDictionary.Add(bMultiMat.name, bMultiMat);
+                            }
+                        }
+                        else
                         {
-                            var bMat = DumpMaterial(renderer.sharedMaterials[i], renderer);
-                            bMultiMat.materials[i] = bMat.id;
+                            bMultiMat = multiMatDictionary[renderer.sharedMaterial.name];
                         }
-                        if (mesh.subMeshCount > 1)
+
+                        babylonMesh.materialId = bMultiMat.id;
+                        babylonMesh.subMeshes = new BabylonSubMesh[mesh.subMeshCount];
+
+                        var offset = 0;
+                        for (int materialIndex = 0; materialIndex < mesh.subMeshCount; materialIndex++)
                         {
-                            multiMatDictionary.Add(bMultiMat.name, bMultiMat);
+                            var unityTriangles = mesh.GetTriangles(materialIndex);
+
+                            babylonMesh.subMeshes[materialIndex] = new BabylonSubMesh
+                            {
+                                verticesStart = 0,
+                                verticesCount = mesh.vertexCount,
+                                materialIndex = materialIndex,
+                                indexStart = offset,
+                                indexCount = unityTriangles.Length
+                            };
+
+                            offset += unityTriangles.Length;
                         }
                     }
                     else
                     {
-                        bMultiMat = multiMatDictionary[renderer.sharedMaterial.name];
-                    }
-
-                    babylonMesh.materialId = bMultiMat.id;
-                    babylonMesh.subMeshes = new BabylonSubMesh[mesh.subMeshCount];
-
-                    var offset = 0;
-                    for (int materialIndex = 0; materialIndex < mesh.subMeshCount; materialIndex++)
-                    {
-                        var unityTriangles = mesh.GetTriangles(materialIndex);
-
-                        babylonMesh.subMeshes[materialIndex] = new BabylonSubMesh
+                        if (renderer.sharedMaterial.HasProperty("_Metallic"))
                         {
-                            verticesStart = 0,
-                            verticesCount = mesh.vertexCount,
-                            materialIndex = materialIndex,
-                            indexStart = offset,
-                            indexCount = unityTriangles.Length
-                        };
-
-                        offset += unityTriangles.Length;
+                            babylonMesh.materialId = DumpPBRMaterial(renderer.sharedMaterial, renderer).id;
+                        }
+                        else
+                        {
+                            babylonMesh.materialId = DumpMaterial(renderer.sharedMaterial, renderer).id;
+                        }
                     }
                 }
-                else
-                {
-                    babylonMesh.materialId = DumpMaterial(renderer.sharedMaterial, renderer).id;
-                }
-            }
 
-            babylonScene.MeshesList.Add(babylonMesh);
+                babylonScene.MeshesList.Add(babylonMesh);
 
-            // Animations
-            ExportAnimations(transform, babylonMesh);
+                // Animations
+                ExportAnimations(transform, babylonMesh);
 
-            if (IsRotationQuaternionAnimated(babylonMesh))
-            {
-                babylonMesh.rotationQuaternion = transform.localRotation.ToFloat();
-            }
-
-            // Collisions
-            if (exportationOptions.ExportCollisions)
-            {
-                var collider = gameObject.GetComponent<Collider>();
-
-                if (collider != null)
+                if (IsRotationQuaternionAnimated(babylonMesh))
                 {
-                    babylonMesh.checkCollisions = true;
+                    babylonMesh.rotationQuaternion = transform.localRotation.ToFloat();
                 }
-            }
 
-            // Physics
-            if (exportationOptions.ExportPhysics)
-            {
-                DumpPhysics(gameObject, babylonMesh);
-            }
-        }
-
-        void DumpPhysics(GameObject gameObject, BabylonMesh babylonMesh)
-        {
-            var impostor = gameObject.GetComponent<Collider>();
-
-            if (impostor == null)
-            {
-                return;
-            }
-
-            babylonScene.physicsEnabled = true;
-            babylonScene.physicsGravity = Physics.gravity.ToFloat();
-
-            if (impostor is SphereCollider)
-            {
-                babylonMesh.physicsImpostor = 1;
-            }
-            else if (impostor is BoxCollider)
-            {
-                babylonMesh.physicsImpostor = 2;
-            }
-            else if (impostor is MeshCollider)
-            {
-                babylonMesh.physicsImpostor = 4;
-            }
-
-            var rigidBody = gameObject.GetComponent<Rigidbody>();
+                // Collisions
+                if (exportationOptions.ExportCollisions)
+                {
+                    var collider = gameObject.GetComponent<Collider>();
 
-            if (rigidBody == null)
-            {
-                return;
+                    if (collider != null)
+                    {
+                        babylonMesh.checkCollisions = true;
+                    }
+                }
             }
-
-            babylonMesh.physicsMass = rigidBody.mass;
         }
     }
 }