XLightManager.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const log$C = new Logger$1("XLightManager");
  2. class XLightManager {
  3. constructor(e) {
  4. E(this, "_scene");
  5. E(this, "_envTexture");
  6. E(this, "_shadowLight");
  7. E(this, "_shadowGenerator");
  8. E(this, "_avatarShadowMeshMap");
  9. E(this, "_cullingShadowObservers");
  10. E(this, "sceneManager");
  11. this.sceneManager = e,
  12. this._scene = this.sceneManager.Scene,
  13. this._envTexture = null,
  14. this.shadowLean = .1;
  15. const t = new Vector3(this.shadowLean,-1,0)
  16. , r = 1024;
  17. this._shadowLight = new DirectionalLight("AvatarLight",t,this._scene),
  18. this._shadowLight.shadowMaxZ = 5e3,
  19. this._shadowLight.intensity = 0,
  20. this.attachLightToCamera(this._shadowLight),
  21. this._shadowGenerator = new ShadowGenerator(r,this._shadowLight,!0),
  22. this._avatarShadowMeshMap = new Map,
  23. this._cullingShadowObservers = new Map
  24. }
  25. set shadowLean(e) {
  26. e = Math.min(e, 1),
  27. e = Math.max(e, -1),
  28. this._shadowLight && (this._shadowLight.direction = new Vector3(e,-1,0))
  29. }
  30. setIBL(e) {
  31. return new Promise((t,r)=>{
  32. this.sceneManager.urlTransformer(e).then(n=>{
  33. var o;
  34. if (n == ((o = this._envTexture) == null ? void 0 : o.url))
  35. return t("env set success");
  36. this._envTexture != null && this.disposeIBL(),
  37. this._envTexture = CubeTexture.CreateFromPrefilteredData(n, this._scene, ".env"),
  38. this._scene.environmentTexture = this._envTexture,
  39. this._envTexture.onLoadObservable.addOnce(()=>{
  40. t("env set success"),
  41. log$C.info("env set success")
  42. }
  43. )
  44. }
  45. ).catch(()=>{
  46. r("env set fail")
  47. }
  48. )
  49. }
  50. )
  51. }
  52. disposeIBL() {
  53. this._envTexture == null ? log$C.info("env not exist") : (this._envTexture.dispose(),
  54. this._envTexture = null,
  55. this._scene.environmentTexture = null,
  56. log$C.info("env dispose success"))
  57. }
  58. removeShadow(e) {
  59. var t;
  60. if (this._avatarShadowMeshMap.has(e)) {
  61. this._avatarShadowMeshMap.delete(e),
  62. this._cullingShadowObservers.get(e) && (this._scene.onBeforeRenderObservable.remove(this._cullingShadowObservers.get(e)),
  63. this._cullingShadowObservers.delete(e));
  64. const r = e.rootNode;
  65. r && ((t = this._shadowGenerator) == null || t.removeShadowCaster(r))
  66. } else
  67. return
  68. }
  69. setShadow(e) {
  70. if (this._avatarShadowMeshMap.has(e))
  71. return;
  72. e.rootNode && this._avatarShadowMeshMap.set(e, e.rootNode.getChildMeshes());
  73. const t = 20
  74. , r = 10
  75. , n = this.cullingShadow(t, r, e);
  76. this._cullingShadowObservers.set(e, n)
  77. }
  78. cullingShadow(e, t, r) {
  79. let n = 0;
  80. const o = ()=>{
  81. var s, l;
  82. if (n == t) {
  83. const u = this._avatarShadowMeshMap.get(r)
  84. , c = (s = r.rootNode) == null ? void 0 : s.getChildMeshes()
  85. , h = this._scene.activeCamera;
  86. u == null || u.forEach(f=>{
  87. var d;
  88. (d = this._shadowGenerator) == null || d.removeShadowCaster(f, !1)
  89. }
  90. ),
  91. c == null || c.forEach(f=>{
  92. var d;
  93. (d = this._shadowGenerator) == null || d.addShadowCaster(f, !1)
  94. }
  95. ),
  96. h && r.rootNode && ((l = r.rootNode.position) == null ? void 0 : l.subtract(h.position).length()) > e && (c == null || c.forEach(f=>{
  97. var d;
  98. (d = this._shadowGenerator) == null || d.removeShadowCaster(f, !1)
  99. }
  100. )),
  101. c && this._avatarShadowMeshMap.set(r, c),
  102. n = 0
  103. } else
  104. n += 1
  105. }
  106. ;
  107. return this._scene.onBeforeRenderObservable.add(o)
  108. }
  109. attachLightToCamera(e) {
  110. const t = e
  111. , r = 15
  112. , n = ()=>{
  113. const o = this._scene.activeCamera;
  114. if (o) {
  115. const a = t.direction
  116. , s = new Vector3(r * a.x,r * a.y,r * a.z)
  117. , l = o.position;
  118. t.position = l.subtract(s)
  119. }
  120. }
  121. ;
  122. return t && this._scene.registerBeforeRender(n),
  123. n
  124. }
  125. }