XStaticMeshFromOneGltf.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(scene, options) {
  9. this._meshes = [],
  10. this._scene = scene,
  11. this._url = options.url,
  12. options.group != null ? this._group = options.group : this._group = "default",
  13. options.pick != null ? this._pickable = options.pick : this._pickable = !1,
  14. options.id != null ? this._id = options.id : this._id = "default",
  15. options.lod != null ? this._lod = options.lod : this._lod = -1,
  16. options.skinInfo != null ? this._skinInfo = options.skinInfo : this._skinInfo = "default",
  17. this._groupUuid = util.uuid(),
  18. this._isInScene = !1
  19. }
  20. loadMesh(pureVideoShader, t) {
  21. const meshLength0 = this._meshes.length
  22. , isVisi = t ? 1 : 0
  23. , url = this._url;
  24. return BABYLON.SceneLoader.LoadAssetContainerAsync("", url, this._scene, ()=>{
  25. this._scene.blockMaterialDirtyMechanism = !0
  26. }, ".glb").then(modelInfo => {
  27. for (let s = modelInfo.materials.length - 1; s >= 0; --s)
  28. modelInfo.materials[s].dispose();
  29. this._scene.blockMaterialDirtyMechanism = !0;
  30. for (let i = 0; i < modelInfo.meshes.length; ++i) {
  31. const mesh = modelInfo.meshes[i];
  32. if ("instances"in mesh) {
  33. "visibility"in mesh && (mesh.visibility = 0);
  34. "isPickable"in mesh && (mesh.isPickable = this._pickable);
  35. pureVideoShader && (mesh.material = pureVideoShader);
  36. "hasVertexAlpha"in mesh && (mesh.hasVertexAlpha = !1);
  37. const xMesh = new XStaticMesh({
  38. id: this._groupUuid + "-" + Math.random().toString(36).substr(2, 5),
  39. mesh,
  40. lod: this._lod,
  41. group: this._group,
  42. url: this._url,
  43. xtype: EMeshType.XStaticMesh,
  44. skinInfo: this._skinInfo
  45. });
  46. // 房间模型初始180度矫正
  47. xMesh.setRotation({pitch: 0, roll:0, yaw:180})
  48. this._meshes.push(xMesh);
  49. }
  50. this._scene.addMesh(mesh);
  51. }
  52. return !0
  53. }).then(() => {
  54. this._isInScene = !0;
  55. for (let i = meshLength0; i < this._meshes.length; ++i)
  56. this._meshes[i].mesh.visibility = isVisi;
  57. return Promise.resolve(!0)
  58. }).catch(e => {
  59. logger.error("[Engine] input gltf mesh uri error! " + e),
  60. Promise.reject(new XLowpolyModelError("[Engine] input gltf mesh uri error! " + e))
  61. })
  62. }
  63. get isinscene() {
  64. return this._isInScene
  65. }
  66. get groupUuid() {
  67. return this._groupUuid
  68. }
  69. get skinInfo() {
  70. return this._skinInfo
  71. }
  72. get group() {
  73. return this._group
  74. }
  75. get meshes() {
  76. return this._meshes
  77. }
  78. get url() {
  79. return this._url
  80. }
  81. get id() {
  82. return this._id
  83. }
  84. get lod() {
  85. return this._lod
  86. }
  87. removeFromScene() {
  88. if (this._isInScene) {
  89. this._isInScene = !1;
  90. for (let e = 0, t = this._meshes.length; e < t; ++e)
  91. this._meshes[e].mesh != null && this._scene.removeMesh(this._meshes[e].mesh)
  92. }
  93. }
  94. addToScene() {
  95. if (this._isInScene == !1) {
  96. this._isInScene = !0;
  97. for (let e = 0, t = this._meshes.length; e < t; ++e)
  98. this._meshes[e].mesh != null && this._scene.addMesh(this._meshes[e].mesh)
  99. }
  100. }
  101. toggleVisibility(e) {
  102. const t = e ? 1 : 0;
  103. for (let r = 0, n = this._meshes.length; r < n; ++r)
  104. "visibility"in this._meshes[r].mesh && (this._meshes[r].mesh.visibility = t)
  105. }
  106. togglePickable(e) {
  107. for (let t = 0, r = this._meshes.length; t < r; ++t)
  108. "isPickable"in this._meshes[t].mesh && (this._meshes[t].mesh.isPickable = e)
  109. }
  110. setMaterial(e) {
  111. for (let t = 0, r = this._meshes.length; t < r; ++t)
  112. "material"in this._meshes[t].mesh && (this._meshes[t].mesh.material = e)
  113. }
  114. dispose() {
  115. for (let e = 0, t = this._meshes.length; e < t; ++e)
  116. this._meshes[e].mesh.dispose(!1, !1)
  117. }
  118. }