XStaticMeshFromOneGltf.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import XStaticMesh from "./XStaticMesh.js"
  2. import XLowpolyModelError from "./error/XLowpolyModelError.js"
  3. import util from "./util.js"
  4. import EMeshType from "./enum/EMeshType.js"
  5. import Logger from "./Logger.js"
  6. const logger = new Logger('XStaticMeshComponent')
  7. export default class XStaticMeshFromOneGltf {
  8. constructor(e, t) {
  9. E(this, "_scene");
  10. E(this, "_url");
  11. E(this, "_group");
  12. E(this, "_pickable");
  13. E(this, "_meshes");
  14. E(this, "_id");
  15. E(this, "_lod");
  16. E(this, "_groupUuid");
  17. E(this, "_isInScene");
  18. E(this, "_skinInfo");
  19. E(this, "loadMesh", (e,t)=>{
  20. const r = this._meshes.length
  21. , n = t ? 1 : 0
  22. , o = this._url;
  23. return BABYLON.SceneLoader.LoadAssetContainerAsync("", o, this._scene, ()=>{
  24. this._scene.blockMaterialDirtyMechanism = !0
  25. }
  26. , ".glb").then(a=>{
  27. for (let s = a.materials.length - 1; s >= 0; --s)
  28. a.materials[s].dispose();
  29. this._scene.blockMaterialDirtyMechanism = !0;
  30. for (let s = 0; s < a.meshes.length; ++s) {
  31. const l = a.meshes[s];
  32. if ("instances"in l) {
  33. "visibility"in l && (l.visibility = 0),
  34. "isPickable"in l && (l.isPickable = this._pickable),
  35. e != null && (l.material = e),
  36. "hasVertexAlpha"in l && (l.hasVertexAlpha = !1);
  37. const u = new XStaticMesh({
  38. id: this._groupUuid + "-" + Math.random().toString(36).substr(2, 5),
  39. mesh: l,
  40. lod: this._lod,
  41. group: this._group,
  42. url: this._url,
  43. xtype: EMeshType.XStaticMesh,
  44. skinInfo: this._skinInfo
  45. });
  46. this._meshes.push(u)
  47. }
  48. this._scene.addMesh(l)
  49. }
  50. return !0
  51. }
  52. ).then(()=>{
  53. this._isInScene = !0;
  54. for (let a = r; a < this._meshes.length; ++a)
  55. this._meshes[a].mesh.visibility = n;
  56. return Promise.resolve(!0)
  57. }
  58. ).catch(a=>{
  59. logger.error("[Engine] input gltf mesh uri error! " + a),
  60. Promise.reject(new XLowpolyModelError("[Engine] input gltf mesh uri error! " + a))
  61. }
  62. )
  63. }
  64. );
  65. this._meshes = [],
  66. this._scene = e,
  67. this._url = t.url,
  68. t.group != null ? this._group = t.group : this._group = "default",
  69. t.pick != null ? this._pickable = t.pick : this._pickable = !1,
  70. t.id != null ? this._id = t.id : this._id = "default",
  71. t.lod != null ? this._lod = t.lod : this._lod = -1,
  72. t.skinInfo != null ? this._skinInfo = t.skinInfo : this._skinInfo = "default",
  73. this._groupUuid = util.uuid(),
  74. this._isInScene = !1
  75. }
  76. get isinscene() {
  77. return this._isInScene
  78. }
  79. get groupUuid() {
  80. return this._groupUuid
  81. }
  82. get skinInfo() {
  83. return this._skinInfo
  84. }
  85. get group() {
  86. return this._group
  87. }
  88. get meshes() {
  89. return this._meshes
  90. }
  91. get url() {
  92. return this._url
  93. }
  94. get id() {
  95. return this._id
  96. }
  97. get lod() {
  98. return this._lod
  99. }
  100. removeFromScene() {
  101. if (this._isInScene) {
  102. this._isInScene = !1;
  103. for (let e = 0, t = this._meshes.length; e < t; ++e)
  104. this._meshes[e].mesh != null && this._scene.removeMesh(this._meshes[e].mesh)
  105. }
  106. }
  107. addToScene() {
  108. if (this._isInScene == !1) {
  109. this._isInScene = !0;
  110. for (let e = 0, t = this._meshes.length; e < t; ++e)
  111. this._meshes[e].mesh != null && this._scene.addMesh(this._meshes[e].mesh)
  112. }
  113. }
  114. toggleVisibility(e) {
  115. const t = e ? 1 : 0;
  116. for (let r = 0, n = this._meshes.length; r < n; ++r)
  117. "visibility"in this._meshes[r].mesh && (this._meshes[r].mesh.visibility = t)
  118. }
  119. togglePickable(e) {
  120. for (let t = 0, r = this._meshes.length; t < r; ++t)
  121. "isPickable"in this._meshes[t].mesh && (this._meshes[t].mesh.isPickable = e)
  122. }
  123. setMaterial(e) {
  124. for (let t = 0, r = this._meshes.length; t < r; ++t)
  125. "material"in this._meshes[t].mesh && (this._meshes[t].mesh.material = e)
  126. }
  127. dispose() {
  128. for (let e = 0, t = this._meshes.length; e < t; ++e)
  129. this._meshes[e].mesh.dispose(!1, !1)
  130. }
  131. }