SEA3DLegacy.js 13 KB

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