XBreathPointManager.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. class XBreathPointManager {
  2. constructor(e) {
  3. E(this, "_scene");
  4. E(this, "materialMap", new Map);
  5. E(this, "breathPoints", new Map);
  6. E(this, "_sceneManager");
  7. E(this, "_allIds", new Set);
  8. E(this, "_loopBPKeys", []);
  9. E(this, "addBreathPoint", async e=>{
  10. const t = [{
  11. url: "https://static.xverse.cn/qqktv/texture.png"
  12. }];
  13. if (t.length <= 0) {
  14. log$w.warn("[Engine] BreathPoint get texture list error: textureList.length <= 0"),
  15. new XBreathPointError("[Engine] BreathPoint get texture list error!");
  16. return
  17. }
  18. const r = t[0]
  19. , {id: n, spriteSheet: o=r.url, spriteWidthNumber: a=20, spriteHeightNumber: s=1, position: l, rotation: u={
  20. pitch: -90,
  21. yaw: 270,
  22. roll: 0
  23. }, size: c=.6, width: h=-1, height: f=-1, fps: d=30, billboardMode: _=!1, forceLeaveGround: g=!1, type: m="default", lifeTime: v=-1, backfaceculling: y=!0, maxVisibleRegion: b=-1, skinInfo: T="default"} = e;
  24. if (this.breathPoints.get(n)) {
  25. log$w.warn("[Engine] Cannot add breathPoint with an existing id: [" + n + "]"),
  26. new XBreathPointError("[Engine] Cannot add breathPoint with an existing id: [" + n + "]");
  27. return
  28. }
  29. if (g) {
  30. const I = this.castRay(new Vector3(l.x,l.y,l.z)) * scaleFromUE4toXverse;
  31. I != 0 ? l.z = l.z - I + 1 : l.z = l.z + 1
  32. }
  33. let C;
  34. if (this.materialMap.get(m)) {
  35. const I = this.materialMap.get(m);
  36. I.count = I.count + 1,
  37. C = I.mat
  38. } else {
  39. const I = new Texture(o,this._scene,!0,!0,Texture.BILINEAR_SAMPLINGMODE,null,()=>{
  40. log$w.error("[Engine] Breathpoint create texture error."),
  41. new XBreathPointError("[Engine] Breathpoint create texture error.")
  42. }
  43. ,null,!0);
  44. I.name = "TexBreathPoint_" + n,
  45. C = new StandardMaterial(`MaterialBreathPoint_${n}`,this._scene),
  46. C.alpha = 1,
  47. C.emissiveTexture = I,
  48. C.backFaceCulling = y,
  49. C.diffuseTexture = I,
  50. C.diffuseTexture.hasAlpha = !0,
  51. C.useAlphaFromDiffuseTexture = !0,
  52. this.materialMap.set(m, {
  53. mat: C,
  54. count: 1,
  55. lastRenderTime: Date.now(),
  56. fps: d,
  57. spriteWidthNumber: a,
  58. spriteHeightNumber: s,
  59. spriteSheet: o,
  60. texture: I
  61. })
  62. }
  63. const A = new Array(6);
  64. for (let I = 0; I < 6; I++)
  65. A[I] = new Vector4(0,0,0,0);
  66. A[0] = new Vector4(0,0,1 / a,1 / s),
  67. A[1] = new Vector4(0,0,1 / a,1 / s);
  68. let S = {};
  69. h > 0 && f > 0 ? S = {
  70. width: h,
  71. height: f,
  72. depth: .01,
  73. faceUV: A
  74. } : S = {
  75. size: c,
  76. depth: .01,
  77. faceUV: A
  78. };
  79. const P = MeshBuilder.CreateBox(n, S, this._scene);
  80. P.material = C;
  81. const R = new XStaticMesh({
  82. id: n,
  83. mesh: P,
  84. xtype: EMeshType.XBreathPoint,
  85. skinInfo: T
  86. });
  87. let M = u;
  88. _ && (P.billboardMode = Mesh.BILLBOARDMODE_ALL,
  89. R.allowMove(),
  90. M = {
  91. pitch: 0,
  92. yaw: 270,
  93. roll: 0
  94. });
  95. const x = new BreathPoint({
  96. type: m,
  97. mesh: R,
  98. id: n,
  99. position: l,
  100. rotation: M,
  101. mat: C,
  102. maxVisibleRegion: b,
  103. scene: this._scene,
  104. skinInfo: T
  105. });
  106. this.breathPoints.set(n, x),
  107. this._allIds.add(n),
  108. v > 0 && setTimeout(()=>{
  109. this.clearBreathPoints(n)
  110. }
  111. , v * 1e3)
  112. }
  113. );
  114. E(this, "reg_breathpoint_update", ()=>{
  115. const e = new Date().getTime();
  116. if (this.materialMap != null)
  117. for (const [t,r] of this.materialMap)
  118. e - r.lastRenderTime > 1e3 / r.fps && (r.lastRenderTime = e,
  119. Math.abs(r.mat.diffuseTexture.uOffset - (1 - 1 / r.spriteWidthNumber)) < 1e-6 ? (r.mat.diffuseTexture.uOffset = 0,
  120. Math.abs(r.mat.diffuseTexture.vOffset - (1 - 1 / r.spriteHeightNumber)) < 1e-6 ? r.mat.diffuseTexture.vOffset = 0 : r.mat.diffuseTexture.vOffset += 1 / r.spriteHeightNumber) : r.mat.diffuseTexture.uOffset += 1 / r.spriteWidthNumber)
  121. }
  122. );
  123. E(this, "reg_breathpoint_autovisible", ()=>{
  124. if (this._scene.getFrameId() % 2 == 0)
  125. if (this._loopBPKeys.length == 0)
  126. this._loopBPKeys = Array.from(this._allIds);
  127. else {
  128. const e = this._getMainPlayerPosition();
  129. for (let t = 0; t < 5 && this._loopBPKeys.length > 0; ++t) {
  130. const r = this._loopBPKeys.pop();
  131. if (r != null) {
  132. const n = this.getBreathPoint(r);
  133. if (n != null && n.maxvisibleregion >= 0 && n.mesh.visibility == 1) {
  134. const o = n.mesh.position;
  135. calcDistance3DVector(e, o) >= n.maxvisibleregion ? n == null || n.removeFromScene() : n == null || n.addToScene()
  136. }
  137. }
  138. }
  139. }
  140. }
  141. );
  142. this._sceneManager = e,
  143. this._scene = e.Scene,
  144. this._scene.registerBeforeRender(this.reg_breathpoint_update),
  145. this._scene.registerBeforeRender(this.reg_breathpoint_autovisible)
  146. }
  147. setAllBreathPointVisibility(e) {
  148. for (const [t,r] of this.breathPoints.entries())
  149. r.toggleVisibility(e)
  150. }
  151. toggleBPVisibilityBySkinInfo(e, t) {
  152. for (const [r,n] of this.breathPoints.entries())
  153. n.skinInfo == e && n.toggleVisibility(t)
  154. }
  155. toggleBPVisibilityById(e, t) {
  156. const r = this.getBreathPoint(e);
  157. r != null && r.toggleVisibility(t)
  158. }
  159. getBreathPointBySkinInfo(e) {
  160. const t = [];
  161. for (const [r,n] of this.breathPoints.entries())
  162. n.skinInfo == e && t.push(n);
  163. return t
  164. }
  165. getAllBreathPoint() {
  166. return this.breathPoints
  167. }
  168. getBreathPoint(e) {
  169. return this.breathPoints.get(e)
  170. }
  171. delete(e) {
  172. const t = this.breathPoints.get(e);
  173. if (t != null) {
  174. t.dispose(),
  175. this._allIds.delete(e);
  176. const r = this.materialMap.get(t._type);
  177. r != null && (r.count = r.count - 1,
  178. r.count <= 0 && (r.count = 0,
  179. r.texture.dispose(),
  180. r.mat.dispose(!0, !0),
  181. this.materialMap.delete(t._type))),
  182. this.breathPoints.delete(e)
  183. }
  184. }
  185. castRay(e) {
  186. var s;
  187. e = ue4Position2Xverse({
  188. x: e.x,
  189. y: e.y,
  190. z: e.z
  191. });
  192. const t = new Vector3(0,-1,0)
  193. , r = new Ray(e,t,length)
  194. , n = []
  195. , o = (s = this._sceneManager) == null ? void 0 : s.getGround({
  196. x: e.x,
  197. y: e.y,
  198. z: e.z
  199. });
  200. let a = r.intersectsMeshes(o);
  201. if (a.length > 0) {
  202. const l = a[0];
  203. if (l && l.pickedMesh) {
  204. const u = l.distance;
  205. t.y = 1;
  206. const c = r.intersectsMeshes(n);
  207. let h = 1e8;
  208. if (c.length > 0) {
  209. const f = c[0];
  210. return f && f.pickedMesh && (h = -f.distance),
  211. h == 1e8 ? u : Math.abs(h) < Math.abs(u) ? h : u
  212. }
  213. }
  214. } else if (t.y = 1,
  215. a = r.intersectsMeshes(n),
  216. a.length > 0) {
  217. const l = a[0];
  218. if (l && l.pickedMesh)
  219. return l.distance
  220. }
  221. return 0
  222. }
  223. changePickable(e) {
  224. for (const [t,r] of this.breathPoints.entries())
  225. r.changePickable(e)
  226. }
  227. clearBreathPoints(e) {
  228. log$w.info(`[Engine] clearBreathPoints: ${e}`);
  229. for (const [t,r] of this.breathPoints.entries())
  230. (r._type == e || r._id == e) && this.delete(r._id)
  231. }
  232. clearBreathPointsBySkinInfo(e) {
  233. log$w.info(`[Engine] clearBreathPointsBySkinInfo: ${e}`);
  234. for (const [t,r] of this.breathPoints.entries())
  235. r.skinInfo == e && this.delete(r._id)
  236. }
  237. clearAllBreathPoints() {
  238. log$w.info("[Engine] ClearAllBreathPoints");
  239. for (const [e,t] of this.breathPoints.entries())
  240. this.delete(t._id)
  241. }
  242. _getMainPlayerPosition() {
  243. var r;
  244. const e = this._sceneManager.cameraComponent.MainCamera.position
  245. , t = this._sceneManager.avatarComponent.getMainAvatar();
  246. if (t != null && t != null) {
  247. const n = (r = t == null ? void 0 : t.rootNode) == null ? void 0 : r.position;
  248. if (n != null)
  249. return n
  250. }
  251. return e
  252. }
  253. changeBreathPointPose(e, t, r) {
  254. const n = new Vector3(e.position.x,e.position.y,e.position.z);
  255. if (this.breathPoints.get(r) != null) {
  256. log$w.info(`[Engine] changeBreathPointPose, id:${r}`);
  257. const o = this.breathPoints.get(r)
  258. , a = o.mesh.position;
  259. let s = a.subtract(n);
  260. s = Vector3.Normalize(s);
  261. const l = Vector3.Distance(a, n)
  262. , u = new Ray(n,s,l)
  263. , c = this._scene.multiPickWithRay(u);
  264. if (c) {
  265. for (let h = 0; h < c.length; h++)
  266. if (c[h].pickedMesh != null && t.mesh.name.indexOf(c[h].pickedMesh.name) >= 0) {
  267. const f = c[h].pickedPoint;
  268. o.mesh.position = n.add(f.subtract(n).scale(.99)),
  269. this.breathPoints.set(r, o)
  270. }
  271. }
  272. } else
  273. log$w.warn(`[Engine] changeBreathPointPose, id:${r} is not existing!`)
  274. }
  275. }