XSceneManager.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. const log$t = new Logger$1("XSceneManager");
  2. var ECurrentShaderMode = (i=>(i[i.default = 0] = "default",
  3. i[i.video = 1] = "video",
  4. i[i.pano = 2] = "pano",
  5. i))(ECurrentShaderMode || {})
  6. , EImageQuality = (i=>(i[i.low = 0] = "low",
  7. i[i.mid = 1] = "mid",
  8. i[i.high = 2] = "high",
  9. i))(EImageQuality || {});
  10. class XSceneManager {
  11. constructor(e, t) {
  12. E(this, "scene");
  13. E(this, "engine");
  14. E(this, "canvas");
  15. E(this, "gl");
  16. E(this, "_yuvInfo");
  17. E(this, "cameraParam");
  18. E(this, "shaderMode");
  19. E(this, "panoInfo");
  20. E(this, "_initEngineScaleNumber");
  21. E(this, "_forceKeepVertical", !1);
  22. E(this, "_currentShader");
  23. E(this, "_currentPanoId");
  24. E(this, "_renderStatusCheckCount", 0);
  25. E(this, "_renderStatusNotChecktCount", 0);
  26. E(this, "_nonlinearCanvasResize", !1);
  27. E(this, "_bChangeEngineSize", !0);
  28. E(this, "_cameraManager");
  29. E(this, "_lowpolyManager");
  30. E(this, "_materialManager");
  31. E(this, "_statisticManager");
  32. E(this, "_breathPointManager");
  33. E(this, "_skytv");
  34. E(this, "_mv", []);
  35. E(this, "_decalManager");
  36. E(this, "_lightManager");
  37. E(this, "_avatarManager");
  38. E(this, "urlTransformer");
  39. E(this, "_billboardManager");
  40. E(this, "_backgroundImg");
  41. E(this, "engineRunTimeStats");
  42. E(this, "uploadHardwareSystemInfo", ()=>{
  43. const e = this.statisticComponent.getHardwareRenderInfo()
  44. , t = this.statisticComponent.getSystemInfo()
  45. , r = {
  46. driver: t.driver,
  47. vender: t.vender,
  48. webgl: t.version,
  49. os: t.os
  50. };
  51. log$t.warn(JSON.stringify(e)),
  52. log$t.warn(JSON.stringify(r))
  53. }
  54. );
  55. E(this, "addNewLowPolyMesh", async(e,t)=>(this._currentShader == null && await this.initSceneManager(),
  56. this._lowpolyManager.addNewLowPolyMesh(e, t, this._currentShader)));
  57. E(this, "initSceneManager", async()=>(await this._materialManager.initMaterial(),
  58. this.applyShader()));
  59. E(this, "registerAfterRender", ()=>{
  60. var e;
  61. if (this._forceKeepVertical) {
  62. const t = this.canvas.width
  63. , r = this.canvas.height;
  64. let n = 0
  65. , o = [[0, 0, 0, 0], [0, 0, 0, 0]];
  66. if (((e = this._cameraManager.MainCamera) == null ? void 0 : e.fovMode) === BABYLON.Camera.FOVMODE_HORIZONTAL_FIXED ? (n = Math.ceil((r - this._yuvInfo.height * t / this._yuvInfo.width) / 2),
  67. o = [[0, 0, t, n], [0, r - n, t, n]]) : (n = Math.ceil((t - this._yuvInfo.width * r / this._yuvInfo.height) / 2),
  68. o = [[0, 0, n, r], [t - n, 0, n, r]]),
  69. n > 0) {
  70. this.gl.enable(this.gl.SCISSOR_TEST);
  71. for (let a = 0; a < o.length; ++a)
  72. this.gl.scissor(o[a][0], o[a][1], o[a][2], o[a][3]),
  73. this.gl.clearColor(0, 0, 0, 1),
  74. this.gl.clear(this.gl.COLOR_BUFFER_BIT);
  75. this.gl.disable(this.gl.SCISSOR_TEST)
  76. }
  77. }
  78. }
  79. );
  80. E(this, "resetRender", ()=>{
  81. this.scene.environmentTexture && (this.scene.environmentTexture._texture ? this.lightComponent.setIBL(this.scene.environmentTexture._texture.url) : this.scene.environmentTexture.url && this.lightComponent.setIBL(this.scene.environmentTexture.url))
  82. }
  83. );
  84. const r = /iphone|ipad/gi.test(window.navigator.userAgent) || t.disableWebGL2
  85. , n = new Engine(e,!0,{
  86. preserveDrawingBuffer: !0,
  87. stencil: !0,
  88. disableWebGL2Support: r
  89. },!0)
  90. , o = new Scene(n);
  91. this.scene = o,
  92. this.engine = n,
  93. this.canvas = e,
  94. this.scene.clearColor = new Color4(.7,.7,.7,1),
  95. this.engine.getCaps().parallelShaderCompile = void 0,
  96. this._initEngineScaleNumber = this.engine.getHardwareScalingLevel(),
  97. this.engine.enableOfflineSupport = !1,
  98. this.engine.doNotHandleContextLost = !0,
  99. this.scene.clearCachedVertexData(),
  100. this.scene.cleanCachedTextureBuffer(),
  101. this.urlTransformer = t.urlTransformer || (s=>Promise.resolve(s)),
  102. t.logger && Logger$1.setLogger(t.logger),
  103. this.gl = e.getContext("webgl2", {
  104. preserveDrawingBuffer: !0
  105. }) || e.getContext("webgl", {
  106. preserveDrawingBuffer: !0
  107. }) || e.getContext("experimental-webgl", {
  108. preserveDrawingBuffer: !0
  109. }),
  110. this.gl.pixelStorei(this.gl.UNPACK_ALIGNMENT, 1),
  111. this._currentPanoId = 0,
  112. t.forceKeepVertical != null && (this._forceKeepVertical = t.forceKeepVertical),
  113. t.panoInfo != null && (this.panoInfo = t.panoInfo),
  114. t.shaderMode != null && (this.shaderMode = t.shaderMode),
  115. t.yuvInfo != null ? this._yuvInfo = t.yuvInfo : this._yuvInfo = {
  116. width: t.videoResOriArray[0].width,
  117. height: t.videoResOriArray[0].height,
  118. fov: 50
  119. },
  120. t.cameraParam != null && (this.cameraParam = t.cameraParam),
  121. t.nonlinearCanvasResize != null && (this._nonlinearCanvasResize = t.nonlinearCanvasResize),
  122. this._cameraManager = new XCameraComponent(this.canvas,this.scene,{
  123. cameraParam: this.cameraParam,
  124. yuvInfo: this._yuvInfo,
  125. forceKeepVertical: this._forceKeepVertical
  126. }),
  127. this._lowpolyManager = new XStaticMeshComponent(this),
  128. this._materialManager = new XMaterialComponent(this,{
  129. videoResOriArray: t.videoResOriArray,
  130. yuvInfo: this._yuvInfo,
  131. panoInfo: this.panoInfo,
  132. shaderMode: this.shaderMode
  133. }),
  134. this._statisticManager = new XStats(this),
  135. this._breathPointManager = new XBreathPointManager(this),
  136. this._decalManager = new XDecalManager(this),
  137. this._avatarManager = new XAvatarManager(this),
  138. this._billboardManager = new XBillboardManager(this),
  139. this.billboardComponent.loadBackGroundTexToIDB(),
  140. this._lightManager = new XLightManager(this),
  141. this.postprocessing(),
  142. this.initSceneManager(),
  143. this.engineRunTimeStats = new XEngineRunTimeStats,
  144. /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 375 && window.screen.height === 812 ? this.engine.setHardwareScalingLevel(this._initEngineScaleNumber * 2) : this.engine.setHardwareScalingLevel(this._initEngineScaleNumber * 1.8),
  145. this.scene.registerBeforeRender(()=>{
  146. this._nonlinearCanvasResize && this._bChangeEngineSize && (this.setEngineSize(this._yuvInfo),
  147. this._bChangeEngineSize = !1)
  148. }
  149. ),
  150. this.scene.registerAfterRender(()=>{
  151. this._nonlinearCanvasResize || this.registerAfterRender()
  152. }
  153. ),
  154. window.addEventListener("resize", ()=>{
  155. this._nonlinearCanvasResize ? this._bChangeEngineSize = !0 : this.engine.resize()
  156. }
  157. ),
  158. XBillboardManager.alphaWidthMap = getAlphaWidthMap("Arial", this.scene),
  159. this.uploadHardwareSystemInfo()
  160. }
  161. get yuvInfo() {
  162. return this.getCurrentShaderMode() == 1 ? this._yuvInfo : {
  163. width: -1,
  164. height: -1,
  165. fov: -1
  166. }
  167. }
  168. set yuvInfo(e) {
  169. this.getCurrentShaderMode() == 1 && (this._yuvInfo = e,
  170. this._cameraManager.cameraFovChange(e))
  171. }
  172. get mainScene() {
  173. return this.scene
  174. }
  175. get cameraComponent() {
  176. return this._cameraManager
  177. }
  178. get staticmeshComponent() {
  179. return this._lowpolyManager
  180. }
  181. get materialComponent() {
  182. return this._materialManager
  183. }
  184. get statisticComponent() {
  185. return this._statisticManager
  186. }
  187. get avatarComponent() {
  188. return this._avatarManager
  189. }
  190. get lightComponent() {
  191. return this._lightManager
  192. }
  193. get Engine() {
  194. return this.engine
  195. }
  196. get Scene() {
  197. return this.scene
  198. }
  199. get billboardComponent() {
  200. return this._billboardManager
  201. }
  202. get breathPointComponent() {
  203. return this._breathPointManager
  204. }
  205. get skytvComponent() {
  206. return this._skytv
  207. }
  208. get mvComponent() {
  209. return this._mv
  210. }
  211. get decalComponent() {
  212. return this._decalManager
  213. }
  214. get currentShader() {
  215. return this._currentShader
  216. }
  217. get initEngineScaleNumber() {
  218. return this._initEngineScaleNumber
  219. }
  220. setImageQuality(e) {
  221. e == 0 ? (this.engine.setHardwareScalingLevel(this._initEngineScaleNumber * 1.8),
  222. log$t.info("[Engine] change image quality to low, [" + this._initEngineScaleNumber * 1.8 + "]")) : e == 1 ? (this.engine.setHardwareScalingLevel(this._initEngineScaleNumber * 1.5),
  223. log$t.info("[Engine] change image quality to mid, [" + this._initEngineScaleNumber * 1.5 + "]")) : e == 2 && (this.engine.setHardwareScalingLevel(this._initEngineScaleNumber * 1),
  224. log$t.info("[Engine] change image quality to high, [" + this._initEngineScaleNumber * 1 + "]"))
  225. }
  226. setNonlinearCanvasResize(e) {
  227. this._nonlinearCanvasResize = e,
  228. this._bChangeEngineSize = e,
  229. e || this.engine.resize()
  230. }
  231. setBackgroundColor(e) {
  232. this.scene.clearColor = new Color4(e.r,e.g,e.b,e.a)
  233. }
  234. setBackgroundImg(e) {
  235. return this._backgroundImg != null && this._backgroundImg.url == e ? Promise.resolve(!0) : new Promise((t,r)=>{
  236. this.urlTransformer(e).then(n=>{
  237. this._backgroundImg == null ? this._backgroundImg = {
  238. layer: new Layer("tex_background_" + Date.now(),n,this.Scene,!0),
  239. url: e
  240. } : this._backgroundImg.url != e && this._backgroundImg.layer != null && this._backgroundImg.layer.texture != null && (this._backgroundImg.layer.texture.updateURL(n),
  241. this._backgroundImg.layer.name = "tex_background_" + Date.now(),
  242. this._backgroundImg.url = e),
  243. t(!0)
  244. }
  245. ).catch(n=>{
  246. log$t.error(`[Engine] set background image Error: ${n}`),
  247. r(`[Engine] set background image Error: ${n}`)
  248. }
  249. )
  250. }
  251. )
  252. }
  253. cleanTheWholeScene() {
  254. const e = this.scene.getFrameId();
  255. this.scene.onBeforeRenderObservable.clear(),
  256. this.scene.onAfterRenderObservable.clear(),
  257. this.scene.clearCachedVertexData(),
  258. this.scene.cleanCachedTextureBuffer(),
  259. this.scene.registerBeforeRender(()=>{
  260. this.scene.getFrameId() - e > 5 && this.scene.dispose()
  261. }
  262. )
  263. }
  264. getAreaAvatar(e, t) {
  265. const r = [];
  266. return this._avatarManager.getAvatarList().forEach(n=>{
  267. const o = e
  268. , a = n.position;
  269. a && o && calcDistance3D(o, a) < t && r.push(n.id)
  270. }
  271. ),
  272. r
  273. }
  274. setEngineSize(e) {
  275. const t = e.width
  276. , r = e.height
  277. , n = this.canvas.width;
  278. this.canvas.height,
  279. this.engine.setSize(Math.round(n), Math.round(n * (r / t)))
  280. }
  281. getCurrentShaderMode() {
  282. return this._currentShader === this._materialManager.getDefaultShader() ? 0 : this._currentShader === this._materialManager.getPureVideoShader() ? 1 : 2
  283. }
  284. addSkyTV(e, t) {
  285. return this._skytv = new XTelevision(this.scene,e,this,t),
  286. this._skytv
  287. }
  288. addMv(e, t) {
  289. this._mv.push(new XTelevision(this.scene,e,this,t))
  290. }
  291. addMeshInfo(e) {
  292. this._lowpolyManager.setMeshInfo(e)
  293. }
  294. applyShader() {
  295. return new Promise((e,t)=>{
  296. this.shaderMode == EShaderMode.videoAndPano || this.shaderMode == EShaderMode.video ? this.changeVideoShaderForLowModel() : this.shaderMode == EShaderMode.default && this.changeDefaultShaderForLowModel(),
  297. e(!0)
  298. }
  299. )
  300. }
  301. changeHardwareScaling(e) {
  302. e < 1 ? e = 1 : e > 2.5 && (e = 2.5),
  303. this._bChangeEngineSize = !0,
  304. this.engine.setHardwareScalingLevel(this._initEngineScaleNumber * e)
  305. }
  306. getCurrentUsedPanoId() {
  307. return this._currentPanoId
  308. }
  309. render() {
  310. try {
  311. this.scene.render()
  312. } catch (e) {
  313. throw log$t.error(`[Engine] Render Error: ${e}`),
  314. e
  315. }
  316. }
  317. isReadyToRender(e) {
  318. const {checkMesh: t=!0, checkEffect: r=!1, checkPostProgress: n=!1, checkParticle: o=!1, checkAnimation: a=!1, materialNameWhiteLists: s=[]} = e;
  319. if (this.scene._isDisposed)
  320. return log$t.error("[Engine] this.scene._isDisposed== false "),
  321. !1;
  322. let l;
  323. const u = this.scene.getEngine();
  324. if (r && !u.areAllEffectsReady())
  325. return log$t.error("[Engine] engine.areAllEffectsReady == false"),
  326. !1;
  327. if (a && this.scene._pendingData.length > 0)
  328. return log$t.error("[Engine] scene._pendingData.length > 0 && animation error"),
  329. !1;
  330. if (t) {
  331. for (l = 0; l < this.scene.meshes.length; l++) {
  332. const c = this.scene.meshes[l];
  333. if (!c.isEnabled() || !c.subMeshes || c.subMeshes.length === 0 || c != null && c.material != null && !(c.material.name.startsWith("Pure") || c.material.name.startsWith("Pano")))
  334. continue;
  335. if (!c.isReady(!0))
  336. return log$t.error(`[Engine] scene. mesh isReady == false, mesh name:${c.name}, mesh xtype: ${c == null ? void 0 : c.xtype}, mesh xgroup: ${c == null ? void 0 : c.xgroup}, mesh xskinInfo: ${c == null ? void 0 : c.xskinInfo}`),
  337. !1;
  338. const h = c.hasThinInstances || c.getClassName() === "InstancedMesh" || c.getClassName() === "InstancedLinesMesh" || u.getCaps().instancedArrays && c.instances.length > 0;
  339. for (const f of this.scene._isReadyForMeshStage)
  340. if (!f.action(c, h))
  341. return log$t.error(`[Engine] scene._isReadyForMeshStage == false, mesh name:${c.name}, mesh xtype: ${c == null ? void 0 : c.xtype}, mesh xgroup: ${c == null ? void 0 : c.xgroup}, mesh xskinInfo: ${c == null ? void 0 : c.xskinInfo}`),
  342. !1
  343. }
  344. for (l = 0; l < this.scene.geometries.length; l++)
  345. if (this.scene.geometries[l].delayLoadState === 2)
  346. return log$t.error("[Engine] geometry.delayLoadState === 2"),
  347. !1
  348. }
  349. if (n) {
  350. if (this.scene.activeCameras && this.scene.activeCameras.length > 0) {
  351. for (const c of this.scene.activeCameras)
  352. if (!c.isReady(!0))
  353. return log$t.error("[Engine] camera not ready === false, ", c.name),
  354. !1
  355. } else if (this.scene.activeCamera && !this.scene.activeCamera.isReady(!0))
  356. return log$t.error("[Engine] activeCamera ready === false, ", this.scene.activeCamera.name),
  357. !1
  358. }
  359. if (o) {
  360. for (const c of this.scene.particleSystems)
  361. if (!c.isReady())
  362. return log$t.error("[Engine] particleSystem ready === false, ", c.name),
  363. !1
  364. }
  365. return !0
  366. }
  367. changePanoShaderForLowModel(e) {
  368. return log$t.info(`[Engine] changePanoShaderForLowModel: ${e}`),
  369. this._materialManager.allowYUVUpdate(),
  370. new Promise((t,r)=>{
  371. this._materialManager._isInDynamicRange(e) == !1 && r(!1),
  372. this._currentPanoId = e,
  373. this._currentShader = this._materialManager.getDynamicShader(e),
  374. this.changeShaderForLowModel().then(()=>{
  375. t(!0)
  376. }
  377. )
  378. }
  379. )
  380. }
  381. changeVideoShaderForLowModel() {
  382. return log$t.info("[Engine] changeVideoShaderForLowModel"),
  383. this._currentShader = this._materialManager.getPureVideoShader(),
  384. this._materialManager.allowYUVUpdate(),
  385. this.changeShaderForLowModel()
  386. }
  387. changeDefaultShaderForLowModel() {
  388. return log$t.info("[Engine] changeDefaultShaderForLowModel"),
  389. this._currentShader = this._materialManager.getDefaultShader(),
  390. this._materialManager.stopYUVUpdate(),
  391. this.changeShaderForLowModel()
  392. }
  393. changeShaderForLowModel() {
  394. return new Promise((e,t)=>{
  395. this._lowpolyManager.getMeshes().forEach(r=>{
  396. r.setMaterial(this._currentShader)
  397. }
  398. ),
  399. this._lowpolyManager.getCgMesh().mesh.material = this._currentShader,
  400. e(!0)
  401. }
  402. )
  403. }
  404. setIBL(e) {
  405. this._lightManager.setIBL(e)
  406. }
  407. postprocessing() {
  408. const e = new DefaultRenderingPipeline("default",!0,this.scene);
  409. e.imageProcessingEnabled = !1,
  410. e.bloomEnabled = !0,
  411. e.bloomThreshold = 1,
  412. e.bloomWeight = 1,
  413. e.bloomKernel = 64,
  414. e.bloomScale = .1
  415. }
  416. getGround(e) {
  417. const t = this._lowpolyManager.getMeshes()
  418. , r = [];
  419. return t.forEach(n=>{
  420. n.mesh.name.indexOf("SM_Stage") >= 0 && r.push(n.mesh)
  421. }
  422. ),
  423. this.Scene.meshes.forEach(n=>{
  424. n.name.split("_")[0] === "ground" && r.push(n)
  425. }
  426. ),
  427. r
  428. }
  429. }