SEA3DLegacy.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /**
  2. * SEA3D Legacy for Three.JS
  3. * @author Sunag / http://www.sunag.com.br/
  4. */
  5. 'use strict';
  6. //
  7. // Header
  8. //
  9. Object.assign( THREE.SEA3D.prototype, {
  10. _onHead: THREE.SEA3D.prototype.onHead,
  11. _updateTransform: THREE.SEA3D.prototype.updateTransform,
  12. _readVertexAnimation: THREE.SEA3D.prototype.readVertexAnimation,
  13. _readGeometryBuffer: THREE.SEA3D.prototype.readGeometryBuffer,
  14. _readLine: THREE.SEA3D.prototype.readLine,
  15. _getSkeletonAnimation: THREE.SEA3D.prototype.getSkeletonAnimation,
  16. _applyDefaultAnimation: THREE.SEA3D.prototype.applyDefaultAnimation
  17. } );
  18. //
  19. // Utils
  20. //
  21. THREE.SEA3D.prototype.isLegacy = function ( sea ) {
  22. var sea3d = sea.sea3d;
  23. if ( sea3d.sign == 'S3D' && ! sea._legacy ) {
  24. sea._legacy = sea3d.typeUnique[ sea.type ] == true;
  25. return sea3d.config.legacy;
  26. }
  27. return false;
  28. };
  29. THREE.SEA3D.prototype.flipZVec3 = function ( v ) {
  30. if ( ! v ) return;
  31. var i = 2; // z
  32. while ( i < v.length ) {
  33. v[ i ] = - v[ i ];
  34. i += 3;
  35. }
  36. return v;
  37. };
  38. THREE.SEA3D.prototype.expandJoints = function ( sea ) {
  39. var numJoints = sea.numVertex * 4;
  40. var joint = sea.isBig ? new Uint32Array( numJoints ) : new Uint16Array( numJoints );
  41. var weight = new Float32Array( numJoints );
  42. var w = 0, jpv = sea.jointPerVertex;
  43. for ( var i = 0; i < sea.numVertex; i ++ ) {
  44. var tjsIndex = i * 4;
  45. var seaIndex = i * jpv;
  46. joint[ tjsIndex ] = sea.joint[ seaIndex ];
  47. if ( jpv > 1 ) joint[ tjsIndex + 1 ] = sea.joint[ seaIndex + 1 ];
  48. if ( jpv > 2 ) joint[ tjsIndex + 2 ] = sea.joint[ seaIndex + 2 ];
  49. if ( jpv > 3 ) joint[ tjsIndex + 3 ] = sea.joint[ seaIndex + 3 ];
  50. weight[ tjsIndex ] = sea.weight[ seaIndex ];
  51. if ( jpv > 1 ) weight[ tjsIndex + 1 ] = sea.weight[ seaIndex + 1 ];
  52. if ( jpv > 2 ) weight[ tjsIndex + 2 ] = sea.weight[ seaIndex + 2 ];
  53. if ( jpv > 3 ) weight[ tjsIndex + 3 ] = sea.weight[ seaIndex + 3 ];
  54. w = weight[ tjsIndex ] + weight[ tjsIndex + 1 ] + weight[ tjsIndex + 2 ] + weight[ tjsIndex + 3 ];
  55. weight[ tjsIndex ] += 1 - w;
  56. }
  57. sea.joint = joint;
  58. sea.weight = weight;
  59. sea.jointPerVertex = 4;
  60. };
  61. THREE.SEA3D.prototype.compressJoints = function ( sea ) {
  62. var numJoints = sea.numVertex * 4;
  63. var joint = sea.isBig ? new Uint32Array( numJoints ) : new Uint16Array( numJoints );
  64. var weight = new Float32Array( numJoints );
  65. var w = 0, jpv = sea.jointPerVertex;
  66. for ( var i = 0; i < sea.numVertex; i ++ ) {
  67. var tjsIndex = i * 4;
  68. var seaIndex = i * jpv;
  69. joint[ tjsIndex ] = sea.joint[ seaIndex ];
  70. joint[ tjsIndex + 1 ] = sea.joint[ seaIndex + 1 ];
  71. joint[ tjsIndex + 2 ] = sea.joint[ seaIndex + 2 ];
  72. joint[ tjsIndex + 3 ] = sea.joint[ seaIndex + 3 ];
  73. weight[ tjsIndex ] = sea.weight[ seaIndex ];
  74. weight[ tjsIndex + 1 ] = sea.weight[ seaIndex + 1 ];
  75. weight[ tjsIndex + 2 ] = sea.weight[ seaIndex + 2 ];
  76. weight[ tjsIndex + 3 ] = sea.weight[ seaIndex + 3 ];
  77. w = weight[ tjsIndex ] + weight[ tjsIndex + 1 ] + weight[ tjsIndex + 2 ] + weight[ tjsIndex + 3 ];
  78. weight[ tjsIndex ] += 1 - w;
  79. }
  80. sea.joint = joint;
  81. sea.weight = weight;
  82. sea.jointPerVertex = 4;
  83. };
  84. THREE.SEA3D.prototype.flipZIndex = function ( v ) {
  85. var i = 1; // y >-< z
  86. while ( i < v.length ) {
  87. var idx = v[ i + 1 ];
  88. v[ i + 1 ] = v[ i ];
  89. v[ i ] = idx;
  90. i += 3;
  91. }
  92. return v;
  93. };
  94. THREE.SEA3D.prototype.flipMatrixBone = function ( mtx ) {
  95. var zero = new THREE.Vector3();
  96. var buf1 = new THREE.Matrix4();
  97. return function ( mtx ) {
  98. buf1.copy( mtx );
  99. mtx.setPosition( zero );
  100. mtx.multiplyMatrices( THREE.SEA3D.MTXBUF.makeRotationZ( THREE.Math.degToRad( 180 ) ), mtx );
  101. mtx.makeRotationFromQuaternion( THREE.SEA3D.QUABUF.setFromRotationMatrix( mtx ) );
  102. var pos = THREE.SEA3D.VECBUF.setFromMatrixPosition( buf1 );
  103. pos.z = - pos.z;
  104. mtx.setPosition( pos );
  105. return mtx;
  106. };
  107. }();
  108. THREE.SEA3D.prototype.flipMatrixScale = function ( local, global, parent, parentGlobal ) {
  109. var pos = new THREE.Vector3();
  110. var qua = new THREE.Quaternion();
  111. var slc = new THREE.Vector3();
  112. return function ( local, global, parent, parentGlobal ) {
  113. if ( parent ) local.multiplyMatrices( parent, local );
  114. local.decompose( pos, qua, slc );
  115. slc.z = - slc.z;
  116. if ( global ) {
  117. slc.y = - slc.y;
  118. slc.x = - slc.x;
  119. }
  120. local.compose( pos, qua, slc );
  121. if ( parent ) {
  122. parent = parent.clone();
  123. this.flipMatrixScale( parent, parentGlobal );
  124. local.multiplyMatrices( parent.getInverse( parent ), local );
  125. }
  126. return local;
  127. };
  128. }();
  129. //
  130. // Legacy
  131. //
  132. THREE.SEA3D.prototype.updateAnimationSet = function ( obj3d ) {
  133. var buf1 = new THREE.Matrix4();
  134. var buf2 = new THREE.Matrix4();
  135. var pos = new THREE.Vector3();
  136. var qua = new THREE.Quaternion();
  137. var slc = new THREE.Vector3();
  138. var to_pos = new THREE.Vector3();
  139. var to_qua = new THREE.Quaternion();
  140. var to_slc = new THREE.Vector3();
  141. return function ( obj3d ) {
  142. var anmSet = obj3d.animation.animationSet;
  143. var relative = obj3d.animation.relative;
  144. var anms = anmSet.animations;
  145. if ( anmSet.flip && ! anms.length )
  146. return;
  147. var dataList = anms[ 0 ].dataList,
  148. t_anm = [];
  149. for ( var i = 0; i < dataList.length; i ++ ) {
  150. var data = dataList[ i ];
  151. var raw = dataList[ i ].data;
  152. var kind = data.kind;
  153. var numFrames = raw.length / data.blockLength;
  154. switch ( kind ) {
  155. case SEA3D.Animation.POSITION:
  156. case SEA3D.Animation.ROTATION:
  157. case SEA3D.Animation.SCALE:
  158. t_anm.push( {
  159. kind: kind,
  160. numFrames: numFrames,
  161. raw: raw
  162. } );
  163. break;
  164. }
  165. }
  166. if ( t_anm.length > 0 ) {
  167. var numFrames = t_anm[ 0 ].numFrames,
  168. parent = undefined;
  169. if ( obj3d.animation.relative ) {
  170. buf1.identity();
  171. parent = this.flipMatrixScale( buf2.copy( obj3d.matrixWorld ) );
  172. } else {
  173. if ( obj3d.parent ) {
  174. parent = this.flipMatrixScale( buf2.copy( obj3d.parent.matrixWorld ) );
  175. }
  176. this.flipMatrixScale( buf1.copy( obj3d.matrix ), false, parent );
  177. }
  178. buf1.decompose( pos, qua, slc );
  179. for ( var f = 0, t, c; f < numFrames; f ++ ) {
  180. for ( t = 0; t < t_anm.length; t ++ ) {
  181. var raw = t_anm[ t ].raw,
  182. kind = t_anm[ t ].kind;
  183. switch ( kind ) {
  184. case SEA3D.Animation.POSITION:
  185. c = f * 3;
  186. pos.set(
  187. raw[ c ],
  188. raw[ c + 1 ],
  189. raw[ c + 2 ]
  190. );
  191. break;
  192. case SEA3D.Animation.ROTATION:
  193. c = f * 4;
  194. qua.set(
  195. raw[ c ],
  196. raw[ c + 1 ],
  197. raw[ c + 2 ],
  198. raw[ c + 3 ]
  199. );
  200. break;
  201. case SEA3D.Animation.SCALE:
  202. c = f * 4;
  203. slc.set(
  204. raw[ c ],
  205. raw[ c + 1 ],
  206. raw[ c + 2 ]
  207. );
  208. break;
  209. }
  210. }
  211. buf1.compose( pos, qua, slc );
  212. this.flipMatrixScale( buf1, false, buf2 );
  213. buf1.decompose( to_pos, to_qua, to_slc );
  214. for ( t = 0; t < t_anm.length; t ++ ) {
  215. var raw = t_anm[ t ].raw,
  216. kind = t_anm[ t ].kind;
  217. switch ( kind ) {
  218. case SEA3D.Animation.POSITION:
  219. c = f * 3;
  220. raw[ c ] = to_pos.x;
  221. raw[ c + 1 ] = to_pos.y;
  222. raw[ c + 2 ] = to_pos.z;
  223. break;
  224. case SEA3D.Animation.ROTATION:
  225. c = f * 4;
  226. raw[ c ] = to_qua.x;
  227. raw[ c + 1 ] = to_qua.y;
  228. raw[ c + 2 ] = to_qua.z;
  229. raw[ c + 3 ] = to_qua.w;
  230. break;
  231. case SEA3D.Animation.SCALE:
  232. c = f * 3;
  233. raw[ c ] = to_slc.x;
  234. raw[ c + 1 ] = to_slc.y;
  235. raw[ c + 2 ] = to_slc.z;
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. anmSet.flip = true;
  242. };
  243. }();
  244. THREE.SEA3D.prototype.applyDefaultAnimation = function ( sea, animatorClass ) {
  245. this._applyDefaultAnimation( sea, animatorClass );
  246. if ( this.isLegacy( sea ) && sea.tag.animation ) {
  247. this.updateAnimationSet( sea.tag );
  248. }
  249. };
  250. THREE.SEA3D.prototype.updateTransform = function ( obj3d, sea ) {
  251. var buf1 = new THREE.Matrix4();
  252. var identity = new THREE.Matrix4();
  253. return function ( obj3d, sea ) {
  254. if ( this.isLegacy( sea ) ) {
  255. if ( sea.transform ) buf1.elements.set( sea.transform );
  256. else buf1.makeTranslation( sea.position.x, sea.position.y, sea.position.z );
  257. this.flipMatrixScale(
  258. buf1, false,
  259. obj3d.parent ? obj3d.parent.matrixWorld : identity,
  260. obj3d.parent instanceof THREE.Bone
  261. );
  262. obj3d.position.setFromMatrixPosition( buf1 );
  263. obj3d.scale.setFromMatrixScale( buf1 );
  264. // ignore rotation scale
  265. buf1.scale( THREE.SEA3D.VECBUF.set( 1 / obj3d.scale.x, 1 / obj3d.scale.y, 1 / obj3d.scale.z ) );
  266. obj3d.rotation.setFromRotationMatrix( buf1 );
  267. obj3d.updateMatrixWorld();
  268. } else {
  269. this._updateTransform( obj3d, sea );
  270. }
  271. };
  272. }();
  273. THREE.SEA3D.prototype.readSkeleton = function ( sea ) {
  274. var mtx_tmp_inv = new THREE.Matrix4(),
  275. mtx_local = new THREE.Matrix4(),
  276. mtx_parent = new THREE.Matrix4(),
  277. pos = new THREE.Vector3(),
  278. qua = new THREE.Quaternion();
  279. return function ( sea ) {
  280. var bones = [],
  281. isLegacy = sea.sea3d.config.legacy;
  282. for ( var i = 0; i < sea.joint.length; i ++ ) {
  283. var bone = sea.joint[ i ];
  284. // get world inverse matrix
  285. mtx_tmp_inv.elements = bone.inverseBindMatrix;
  286. // convert to world matrix
  287. mtx_local.getInverse( mtx_tmp_inv );
  288. // convert to three.js order
  289. if ( isLegacy ) this.flipMatrixBone( mtx_local );
  290. if ( bone.parentIndex > - 1 ) {
  291. // to world
  292. mtx_tmp_inv.elements = sea.joint[ bone.parentIndex ].inverseBindMatrix;
  293. mtx_parent.getInverse( mtx_tmp_inv );
  294. // convert parent to three.js order
  295. if ( isLegacy ) this.flipMatrixBone( mtx_parent );
  296. // to local
  297. mtx_parent.getInverse( mtx_parent );
  298. mtx_local.multiplyMatrices( mtx_parent, mtx_local );
  299. }
  300. // apply matrix
  301. pos.setFromMatrixPosition( mtx_local );
  302. qua.setFromRotationMatrix( mtx_local );
  303. bones[ i ] = {
  304. name: bone.name,
  305. pos: [ pos.x, pos.y, pos.z ],
  306. rotq: [ qua.x, qua.y, qua.z, qua.w ],
  307. parent: bone.parentIndex
  308. };
  309. }
  310. return sea.tag = bones;
  311. };
  312. }();
  313. THREE.SEA3D.prototype.getSkeletonAnimation = function ( sea, skl ) {
  314. if ( this.isLegacy( sea ) ) return this.getSkeletonAnimationLegacy( sea, skl );
  315. else return this._getSkeletonAnimation( sea, skl );
  316. };
  317. THREE.SEA3D.prototype.getSkeletonAnimationLegacy = function ( sea, skl ) {
  318. var mtx_tmp_inv = new THREE.Matrix4(),
  319. mtx_local = new THREE.Matrix4(),
  320. mtx_global = new THREE.Matrix4(),
  321. mtx_parent = new THREE.Matrix4();
  322. return function ( sea, skl ) {
  323. if ( sea.tag ) return sea.tag;
  324. var animations = [],
  325. delta = ( 1000 / sea.frameRate ) / 1000,
  326. scale = [ 1, 1, 1 ];
  327. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  328. var seq = sea.sequence[ i ];
  329. var start = seq.start;
  330. var end = start + seq.count;
  331. var animation = {
  332. name: seq.name,
  333. repeat: seq.repeat,
  334. fps: sea.frameRate,
  335. JIT: 0,
  336. length: delta * seq.count,
  337. hierarchy: []
  338. };
  339. var numJoints = sea.numJoints,
  340. raw = sea.raw;
  341. for ( var j = 0; j < numJoints; j ++ ) {
  342. var bone = skl.joint[ j ],
  343. node = { parent: bone.parentIndex, keys: [] },
  344. keys = node.keys,
  345. time = 0;
  346. for ( var frame = start; frame < end; frame ++ ) {
  347. var idx = ( frame * numJoints * 7 ) + ( j * 7 );
  348. mtx_local.makeRotationFromQuaternion( THREE.SEA3D.QUABUF.set( raw[ idx + 3 ], raw[ idx + 4 ], raw[ idx + 5 ], raw[ idx + 6 ] ) );
  349. mtx_local.setPosition( THREE.SEA3D.VECBUF.set( raw[ idx ], raw[ idx + 1 ], raw[ idx + 2 ] ) );
  350. if ( bone.parentIndex > - 1 ) {
  351. // to global
  352. mtx_tmp_inv.elements = skl.joint[ bone.parentIndex ].inverseBindMatrix;
  353. mtx_parent.getInverse( mtx_tmp_inv );
  354. mtx_global.multiplyMatrices( mtx_parent, mtx_local );
  355. // convert to three.js matrix
  356. this.flipMatrixBone( mtx_global );
  357. // flip parent inverse
  358. this.flipMatrixBone( mtx_parent );
  359. // to local
  360. mtx_parent.getInverse( mtx_parent );
  361. mtx_local.multiplyMatrices( mtx_parent, mtx_global );
  362. } else {
  363. this.flipMatrixBone( mtx_local );
  364. }
  365. var posQ = THREE.SEA3D.VECBUF.setFromMatrixPosition( mtx_local );
  366. var newQ = THREE.SEA3D.QUABUF.setFromRotationMatrix( mtx_local );
  367. keys.push( {
  368. time: time,
  369. pos: [ posQ.x, posQ.y, posQ.z ],
  370. rot: [ newQ.x, newQ.y, newQ.z, newQ.w ],
  371. scl: scale
  372. } );
  373. time += delta;
  374. }
  375. animation.hierarchy[ j ] = node;
  376. }
  377. var anm = THREE.AnimationClip.parseAnimation( animation, skl.tag );
  378. anm.loop = seq.repeat;
  379. anm.timeScale = 1;
  380. animations.push( anm );
  381. }
  382. return sea.tag = animations;
  383. };
  384. }();
  385. THREE.SEA3D.prototype.readVertexAnimation = function ( sea ) {
  386. if ( this.isLegacy( sea ) ) {
  387. for ( var i = 0, l = sea.frame.length; i < l; i ++ ) {
  388. var frame = sea.frame[ i ];
  389. this.flipZVec3( frame.vertex );
  390. this.flipZVec3( frame.normal );
  391. }
  392. }
  393. this._readVertexAnimation( sea );
  394. };
  395. THREE.SEA3D.prototype.readGeometryBuffer = function ( sea ) {
  396. if ( this.isLegacy( sea ) ) {
  397. this.flipZVec3( sea.vertex );
  398. this.flipZVec3( sea.normal );
  399. this.flipZIndex( sea.indexes );
  400. if ( sea.jointPerVertex > 4 ) this.compressJoints( sea );
  401. else if ( sea.jointPerVertex < 4 ) this.expandJoints( sea );
  402. }
  403. this._readGeometryBuffer( sea );
  404. };
  405. THREE.SEA3D.prototype.readLines = function ( sea ) {
  406. if ( this.isLegacy( sea ) ) {
  407. this.flipZVec3( sea.vertex );
  408. }
  409. this._readLines( sea );
  410. };
  411. THREE.SEA3D.prototype.onHead = function ( args ) {
  412. // TODO: Ignore sign
  413. };
  414. THREE.SEA3D.EXTENSIONS_LOADER.push( { setTypeRead: function () {
  415. // CONFIG
  416. this.config.legacy = this.config.legacy == undefined ? true : this.config.legacy;
  417. this.file.typeRead[ SEA3D.Skeleton.prototype.type ] = this.readSkeleton;
  418. } } );