SceneBuilder.Meshes.cs 9.5 KB

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