XLightManager.js 4.7 KB

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