SceneBuilder.Meshes.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using BabylonExport.Entities;
  3. using UnityEngine;
  4. namespace Unity3D2Babylon
  5. {
  6. partial class SceneBuilder
  7. {
  8. private BabylonAbstractMesh ConvertUnityMeshToInstance(GameObject gameObject)
  9. {
  10. BabylonAbstractMesh babylonMesh = new BabylonAbstractMesh();
  11. Transform transform = gameObject.transform;
  12. babylonMesh.name = gameObject.name;
  13. babylonMesh.position = new float[3];
  14. babylonMesh.position[0] = transform.position.x;
  15. babylonMesh.position[1] = transform.position.y;
  16. babylonMesh.position[2] = transform.position.z;
  17. babylonMesh.rotation = new float[3];
  18. babylonMesh.rotation[0] = transform.rotation.eulerAngles.x * (float)Math.PI / 180;
  19. babylonMesh.rotation[1] = transform.rotation.eulerAngles.y * (float)Math.PI / 180;
  20. babylonMesh.rotation[2] = transform.rotation.eulerAngles.z * (float)Math.PI / 180;
  21. babylonMesh.scaling = new float[3];
  22. babylonMesh.scaling[0] = transform.localScale.x;
  23. babylonMesh.scaling[1] = transform.localScale.y;
  24. babylonMesh.scaling[2] = transform.localScale.z;
  25. return babylonMesh;
  26. }
  27. private void ConvertUnityEmptyObjectToBabylon(GameObject gameObject)
  28. {
  29. BabylonMesh babylonMesh = new BabylonMesh { name = gameObject.name, id = GetID(gameObject) };
  30. var transform = gameObject.transform;
  31. babylonMesh.parentId = GetParentID(transform);
  32. babylonMesh.position = transform.localPosition.ToFloat();
  33. babylonMesh.rotation = new float[3];
  34. babylonMesh.rotation[0] = transform.localRotation.eulerAngles.x * (float)Math.PI / 180;
  35. babylonMesh.rotation[1] = transform.localRotation.eulerAngles.y * (float)Math.PI / 180;
  36. babylonMesh.rotation[2] = transform.localRotation.eulerAngles.z * (float)Math.PI / 180;
  37. babylonMesh.scaling = transform.localScale.ToFloat();
  38. babylonScene.MeshesList.Add(babylonMesh);
  39. // Animations
  40. ExportAnimations(transform, babylonMesh);
  41. if (IsRotationQuaternionAnimated(babylonMesh))
  42. {
  43. babylonMesh.rotationQuaternion = transform.localRotation.ToFloat();
  44. }
  45. }
  46. private void ConvertUnityMeshToBabylon(Mesh mesh, Transform transform, GameObject gameObject, float progress)
  47. {
  48. BabylonMesh babylonMesh = new BabylonMesh();
  49. var renderer = gameObject.GetComponent<Renderer>();
  50. ExporterWindow.ReportProgress(progress, "Exporting mesh: " + gameObject.name);
  51. babylonMesh.name = gameObject.name;
  52. babylonMesh.id = GetID(transform.gameObject);
  53. babylonMesh.receiveShadows = renderer.receiveShadows;
  54. babylonMesh.parentId = GetParentID(transform);
  55. babylonMesh.position = transform.localPosition.ToFloat();
  56. babylonMesh.rotation = new float[3];
  57. babylonMesh.rotation[0] = transform.localRotation.eulerAngles.x * (float)Math.PI / 180;
  58. babylonMesh.rotation[1] = transform.localRotation.eulerAngles.y * (float)Math.PI / 180;
  59. babylonMesh.rotation[2] = transform.localRotation.eulerAngles.z * (float)Math.PI / 180;
  60. babylonMesh.scaling = transform.localScale.ToFloat();
  61. babylonMesh.positions = new float[mesh.vertexCount * 3];
  62. for (int i = 0; i < mesh.vertices.Length; i++)
  63. {
  64. babylonMesh.positions[i * 3] = mesh.vertices[i].x;
  65. babylonMesh.positions[(i * 3) + 1] = mesh.vertices[i].y;
  66. babylonMesh.positions[(i * 3) + 2] = mesh.vertices[i].z;
  67. // Computing world extends
  68. var worldPosition = transform.TransformPoint(mesh.vertices[i]);
  69. if (worldPosition.x > babylonScene.MaxVector.X)
  70. {
  71. babylonScene.MaxVector.X = worldPosition.x;
  72. }
  73. if (worldPosition.y > babylonScene.MaxVector.Y)
  74. {
  75. babylonScene.MaxVector.Y = worldPosition.y;
  76. }
  77. if (worldPosition.z > babylonScene.MaxVector.Z)
  78. {
  79. babylonScene.MaxVector.Z = worldPosition.z;
  80. }
  81. if (worldPosition.x < babylonScene.MinVector.X)
  82. {
  83. babylonScene.MinVector.X = worldPosition.x;
  84. }
  85. if (worldPosition.y < babylonScene.MinVector.Y)
  86. {
  87. babylonScene.MinVector.Y = worldPosition.y;
  88. }
  89. if (worldPosition.z < babylonScene.MinVector.Z)
  90. {
  91. babylonScene.MinVector.Z = worldPosition.z;
  92. }
  93. }
  94. babylonMesh.normals = new float[mesh.vertexCount * 3];
  95. for (int i = 0; i < mesh.normals.Length; i++)
  96. {
  97. babylonMesh.normals[i * 3] = mesh.normals[i].x;
  98. babylonMesh.normals[(i * 3) + 1] = mesh.normals[i].y;
  99. babylonMesh.normals[(i * 3) + 2] = mesh.normals[i].z;
  100. }
  101. babylonMesh.uvs = new float[mesh.vertexCount * 2];
  102. for (int i = 0; i < mesh.uv.Length; i++)
  103. {
  104. babylonMesh.uvs[i * 2] = mesh.uv[i].x;
  105. babylonMesh.uvs[(i * 2) + 1] = mesh.uv[i].y;
  106. }
  107. if (mesh.uv2 != null)
  108. {
  109. babylonMesh.uvs2 = new float[mesh.vertexCount * 2];
  110. for (int i = 0; i < mesh.uv2.Length; i++)
  111. {
  112. babylonMesh.uvs2[i * 2] = mesh.uv2[i].x;
  113. babylonMesh.uvs2[(i * 2) + 1] = mesh.uv2[i].y;
  114. }
  115. }
  116. babylonMesh.indices = new int[mesh.triangles.Length];
  117. for (int i = 0; i < mesh.triangles.Length; i += 3)
  118. {
  119. babylonMesh.indices[i] = mesh.triangles[i + 2];
  120. babylonMesh.indices[i + 1] = mesh.triangles[i + 1];
  121. babylonMesh.indices[i + 2] = mesh.triangles[i];
  122. }
  123. if (mesh.subMeshCount > 1) // Multimaterials
  124. {
  125. BabylonMultiMaterial bMultiMat;
  126. if (!multiMatDictionary.ContainsKey(renderer.sharedMaterial.name))
  127. {
  128. bMultiMat = new BabylonMultiMaterial
  129. {
  130. materials = new string[mesh.subMeshCount],
  131. id = Guid.NewGuid().ToString(),
  132. name = renderer.sharedMaterial.name
  133. };
  134. for (int i = 0; i < renderer.sharedMaterials.Length; i++)
  135. {
  136. var bMat = DumpMaterial(renderer.sharedMaterials[i], renderer);
  137. bMultiMat.materials[i] = bMat.id;
  138. }
  139. if (mesh.subMeshCount > 1)
  140. {
  141. multiMatDictionary.Add(bMultiMat.name, bMultiMat);
  142. }
  143. }
  144. else
  145. {
  146. bMultiMat = multiMatDictionary[renderer.sharedMaterial.name];
  147. }
  148. babylonMesh.materialId = bMultiMat.id;
  149. babylonMesh.subMeshes = new BabylonSubMesh[mesh.subMeshCount];
  150. var offset = 0;
  151. for (int materialIndex = 0; materialIndex < mesh.subMeshCount; materialIndex++)
  152. {
  153. var unityTriangles = mesh.GetTriangles(materialIndex);
  154. babylonMesh.subMeshes[materialIndex] = new BabylonSubMesh
  155. {
  156. verticesStart = 0,
  157. verticesCount = mesh.vertexCount,
  158. materialIndex = materialIndex,
  159. indexStart = offset,
  160. indexCount = unityTriangles.Length
  161. };
  162. offset += unityTriangles.Length;
  163. }
  164. }
  165. else
  166. {
  167. babylonMesh.materialId = DumpMaterial(renderer.sharedMaterial, renderer).id;
  168. }
  169. babylonScene.MeshesList.Add(babylonMesh);
  170. // Animations
  171. ExportAnimations(transform, babylonMesh);
  172. if (IsRotationQuaternionAnimated(babylonMesh))
  173. {
  174. babylonMesh.rotationQuaternion = transform.localRotation.ToFloat();
  175. }
  176. // Collisions
  177. if (exportationOptions.ExportCollisions)
  178. {
  179. var collider = gameObject.GetComponent<Collider>();
  180. if (collider != null)
  181. {
  182. babylonMesh.checkCollisions = true;
  183. }
  184. }
  185. }
  186. }
  187. }