XSequence.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import {http2} from "./Http2.js"
  2. import XSubSequence from "./XSubSequence.js"
  3. const DefaultUrlTransformer = async i=>typeof i != "string" ? (console.warn("url transformer error", i),
  4. i) : i.startsWith("blob:") ? i : http2.get({
  5. url: i,
  6. useIndexedDb: !0,
  7. key: "url"
  8. });
  9. export default class XSequence {
  10. constructor(e, t, r="test", n=DefaultUrlTransformer) {
  11. E(this, "_scene");
  12. E(this, "_name");
  13. E(this, "_subSeqs");
  14. E(this, "_animGroup");
  15. E(this, "_targetSubSeqs");
  16. E(this, "_abosoluteUrl");
  17. E(this, "_rootDir");
  18. E(this, "urlTransformer");
  19. E(this, "init", async()=>new Promise(e=>{
  20. this.urlTransformer(this._abosoluteUrl).then(t=>{
  21. const r = new XMLHttpRequest;
  22. r.open("get", t),
  23. r.send(null),
  24. r.onload = ()=>{
  25. if (r.status == 200) {
  26. const n = JSON.parse(r.responseText);
  27. this.load(n).then(()=>{
  28. e()
  29. }
  30. )
  31. }
  32. }
  33. }
  34. )
  35. }
  36. ));
  37. E(this, "getRootOfSubSeqs", ()=>{
  38. const e = new Array;
  39. return this._subSeqs.forEach(t=>{
  40. e.push(t.root)
  41. }
  42. ),
  43. e
  44. }
  45. );
  46. E(this, "play", async(e=!0)=>new Promise(t=>{
  47. this._animGroup.play(e),
  48. e ? this._animGroup.onAnimationGroupLoopObservable.addOnce(()=>{
  49. t()
  50. }
  51. ) : this._animGroup.onAnimationGroupEndObservable.addOnce(()=>{
  52. t()
  53. }
  54. )
  55. }
  56. ));
  57. E(this, "goToFrame", e=>{
  58. this._animGroup.goToFrame(e)
  59. }
  60. );
  61. E(this, "hide", ()=>{
  62. this._subSeqs.forEach(e=>{
  63. e.hide()
  64. }
  65. )
  66. }
  67. );
  68. E(this, "show", ()=>{
  69. this._subSeqs.forEach(e=>{
  70. e.show()
  71. }
  72. )
  73. }
  74. );
  75. E(this, "pause", ()=>{
  76. this._animGroup.pause()
  77. }
  78. );
  79. E(this, "reset", ()=>{
  80. this._animGroup.reset()
  81. }
  82. );
  83. this._scene = e,
  84. this._abosoluteUrl = t,
  85. this._rootDir = t.split("/").slice(0, -1).join("/") + "/",
  86. this._name = r,
  87. this._subSeqs = new Map,
  88. this._animGroup = new BABYLON.AnimationGroup("Seq_" + r,e),
  89. this._targetSubSeqs = new Map,
  90. this._animGroup.onAnimationGroupPlayObservable.add(()=>{
  91. this._subSeqs.forEach(o=>{
  92. o.show()
  93. }
  94. )
  95. }
  96. ),
  97. this._animGroup.onAnimationGroupEndObservable.add(()=>{
  98. this._subSeqs.forEach(o=>{
  99. o.hide()
  100. }
  101. )
  102. }
  103. ),
  104. this.urlTransformer = n
  105. }
  106. get animGroup() {
  107. return this._animGroup
  108. }
  109. serialize() {
  110. const e = {};
  111. return e.SubSequence = new Array,
  112. e.TimeLine = new Array,
  113. this._subSeqs.forEach(t=>{
  114. const r = {
  115. name: t.name,
  116. uri: t.path
  117. };
  118. e.SubSequence.push(r);
  119. const n = this._targetSubSeqs.get(t);
  120. n && e.TimeLine.push({
  121. frame: n == null ? void 0 : n.frame,
  122. position: n.position,
  123. rotation: n.rotation,
  124. scaling: n.scaling,
  125. name: t.name
  126. })
  127. }
  128. ),
  129. e
  130. }
  131. get isPlaying() {
  132. return this._animGroup.isPlaying
  133. }
  134. get isStarted() {
  135. return this._animGroup.isStarted
  136. }
  137. get loaded() {
  138. let e = !0;
  139. return this._subSeqs.forEach(t=>{
  140. e = e && t.loaded
  141. }
  142. ),
  143. e
  144. }
  145. dispose() {
  146. this._subSeqs.forEach(e=>{
  147. e.dispose()
  148. }
  149. ),
  150. this.animGroup.dispose()
  151. }
  152. setFrame(e, t) {
  153. const r = this._subSeqs.get(e);
  154. if (r) {
  155. const n = this._targetSubSeqs.get(r);
  156. n && (n.frame = t),
  157. n && this.update(r, n)
  158. }
  159. }
  160. get name() {
  161. return this._name
  162. }
  163. update(e, t) {
  164. if (t) {
  165. const r = {
  166. frame: t.frame,
  167. scaling: new BABYLON.Vector3(t.scaling[0],t.scaling[1],t.scaling[2]),
  168. position: new BABYLON.Vector3(t.position[0],t.position[1],t.position[2]),
  169. rotation: new BABYLON.Vector3(t.rotation[0] / 180 * Math.PI,t.rotation[1] / 180 * Math.PI,t.rotation[2] / 180 * Math.PI),
  170. name: t.name
  171. }
  172. , n = this._subSeqs.get(r.name);
  173. n && (n.setPositionVector(r.position),
  174. n.setRotationVector(r.rotation),
  175. n.setScalingVector(r.scaling),
  176. n.setStartFrame(r.frame),
  177. this._targetSubSeqs.set(n, t),
  178. n.onSubSequenceTransformationChangeObservable.add(()=>{
  179. const o = this._targetSubSeqs.get(n);
  180. o && (o.position = [n.pos.x, n.pos.y, n.pos.z]),
  181. o && (o.rotation = [n.rot.x, n.rot.y, n.rot.z]),
  182. o && (o.scaling = [n.scal.x, n.scal.y, n.scal.z])
  183. }
  184. ))
  185. }
  186. }
  187. load(e) {
  188. return new Promise((t,r)=>{
  189. const n = new Array
  190. , o = e.SubSequence
  191. , a = e.TimeLine;
  192. for (const s of o) {
  193. s.uri.indexOf("./") == 0 && (s.uri = s.uri.slice(2));
  194. const l = new XSubSequence(this._scene,this._rootDir + s.uri,this.urlTransformer);
  195. this._subSeqs.set(s.name, l),
  196. n.push(l.init())
  197. }
  198. Promise.all(n).then(()=>{
  199. a.forEach(s=>{
  200. const l = this._subSeqs.get(s.name);
  201. l && this.update(l, s)
  202. }
  203. ),
  204. this._subSeqs.forEach(s=>{
  205. s.animGroup.targetedAnimations.forEach(l=>{
  206. this._animGroup.addTargetedAnimation(l.animation, l.target)
  207. }
  208. )
  209. }
  210. ),
  211. t()
  212. }
  213. , ()=>{
  214. r()
  215. }
  216. )
  217. }
  218. )
  219. }
  220. }