SEA3DLegacy.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. _readMorph: THREE.SEA3D.prototype.readMorph,
  13. _readVertexAnimation: THREE.SEA3D.prototype.readVertexAnimation,
  14. _readGeometryBuffer: THREE.SEA3D.prototype.readGeometryBuffer,
  15. _readLine: THREE.SEA3D.prototype.readLine,
  16. _getModifier: THREE.SEA3D.prototype.getModifier,
  17. _readAnimation: THREE.SEA3D.prototype.readAnimation
  18. } );
  19. //
  20. // Utils
  21. //
  22. THREE.SEA3D.prototype.isLegacy = function ( sea ) {
  23. var sea3d = sea.sea3d;
  24. if ( sea3d.sign === "S3D" ) {
  25. return sea3d.config.legacy;
  26. }
  27. return false;
  28. };
  29. THREE.SEA3D.prototype.flipVec3 = function ( v ) {
  30. if ( ! v ) return;
  31. var i = 2;
  32. while ( i < v.length ) {
  33. v[ i ] = - v[ i ];
  34. i += 3;
  35. }
  36. return v;
  37. };
  38. THREE.SEA3D.prototype.addVector = function ( v, t ) {
  39. if ( ! v ) return;
  40. for ( var i = 0; i < v.length; i ++ ) {
  41. v[ i ] += t[ i ];
  42. }
  43. return v;
  44. };
  45. THREE.SEA3D.prototype.expandJoints = function ( sea ) {
  46. var numJoints = sea.numVertex * 4;
  47. var joint = sea.isBig ? new Uint32Array( numJoints ) : new Uint16Array( numJoints );
  48. var weight = new Float32Array( numJoints );
  49. var w = 0, jpv = sea.jointPerVertex;
  50. for ( var i = 0; i < sea.numVertex; i ++ ) {
  51. var tjsIndex = i * 4;
  52. var seaIndex = i * jpv;
  53. joint[ tjsIndex ] = sea.joint[ seaIndex ];
  54. if ( jpv > 1 ) joint[ tjsIndex + 1 ] = sea.joint[ seaIndex + 1 ];
  55. if ( jpv > 2 ) joint[ tjsIndex + 2 ] = sea.joint[ seaIndex + 2 ];
  56. if ( jpv > 3 ) joint[ tjsIndex + 3 ] = sea.joint[ seaIndex + 3 ];
  57. weight[ tjsIndex ] = sea.weight[ seaIndex ];
  58. if ( jpv > 1 ) weight[ tjsIndex + 1 ] = sea.weight[ seaIndex + 1 ];
  59. if ( jpv > 2 ) weight[ tjsIndex + 2 ] = sea.weight[ seaIndex + 2 ];
  60. if ( jpv > 3 ) weight[ tjsIndex + 3 ] = sea.weight[ seaIndex + 3 ];
  61. w = weight[ tjsIndex ] + weight[ tjsIndex + 1 ] + weight[ tjsIndex + 2 ] + weight[ tjsIndex + 3 ];
  62. weight[ tjsIndex ] += 1 - w;
  63. }
  64. sea.joint = joint;
  65. sea.weight = weight;
  66. sea.jointPerVertex = 4;
  67. };
  68. THREE.SEA3D.prototype.compressJoints = function ( sea ) {
  69. var numJoints = sea.numVertex * 4;
  70. var joint = sea.isBig ? new Uint32Array( numJoints ) : new Uint16Array( numJoints );
  71. var weight = new Float32Array( numJoints );
  72. var w = 0, jpv = sea.jointPerVertex;
  73. for ( var i = 0; i < sea.numVertex; i ++ ) {
  74. var tjsIndex = i * 4;
  75. var seaIndex = i * jpv;
  76. joint[ tjsIndex ] = sea.joint[ seaIndex ];
  77. joint[ tjsIndex + 1 ] = sea.joint[ seaIndex + 1 ];
  78. joint[ tjsIndex + 2 ] = sea.joint[ seaIndex + 2 ];
  79. joint[ tjsIndex + 3 ] = sea.joint[ seaIndex + 3 ];
  80. weight[ tjsIndex ] = sea.weight[ seaIndex ];
  81. weight[ tjsIndex + 1 ] = sea.weight[ seaIndex + 1 ];
  82. weight[ tjsIndex + 2 ] = sea.weight[ seaIndex + 2 ];
  83. weight[ tjsIndex + 3 ] = sea.weight[ seaIndex + 3 ];
  84. w = weight[ tjsIndex ] + weight[ tjsIndex + 1 ] + weight[ tjsIndex + 2 ] + weight[ tjsIndex + 3 ];
  85. weight[ tjsIndex ] += 1 - w;
  86. }
  87. sea.joint = joint;
  88. sea.weight = weight;
  89. sea.jointPerVertex = 4;
  90. };
  91. THREE.SEA3D.prototype.flipIndexes = function ( v ) {
  92. var i = 1; // y >-< z
  93. while ( i < v.length ) {
  94. var idx = v[ i + 1 ];
  95. v[ i + 1 ] = v[ i ];
  96. v[ i ] = idx;
  97. i += 3;
  98. }
  99. return v;
  100. };
  101. THREE.SEA3D.prototype.flipBoneMatrix = function () {
  102. var zero = new THREE.Vector3();
  103. return function ( mtx ) {
  104. var pos = THREE.SEA3D.VECBUF.setFromMatrixPosition( mtx );
  105. pos.z = - pos.z;
  106. mtx.setPosition( zero );
  107. mtx.multiplyMatrices( THREE.SEA3D.MTXBUF.makeRotationZ( THREE.Math.degToRad( 180 ) ), mtx );
  108. mtx.setPosition( pos );
  109. return mtx;
  110. };
  111. }();
  112. THREE.SEA3D.prototype.flipScaleMatrix = function () {
  113. var pos = new THREE.Vector3();
  114. var qua = new THREE.Quaternion();
  115. var slc = new THREE.Vector3();
  116. return function ( local, rotate, parent, parentRotate ) {
  117. if ( parent ) local.multiplyMatrices( parent, local );
  118. local.decompose( pos, qua, slc );
  119. slc.z = - slc.z;
  120. local.compose( pos, qua, slc );
  121. if ( rotate ) {
  122. local.multiplyMatrices( local, THREE.SEA3D.MTXBUF.makeRotationZ( THREE.Math.degToRad( 180 ) ) );
  123. }
  124. if ( parent ) {
  125. parent = parent.clone();
  126. this.flipScaleMatrix( parent, parentRotate );
  127. local.multiplyMatrices( parent.getInverse( parent ), local );
  128. }
  129. return local;
  130. };
  131. }();
  132. //
  133. // Legacy
  134. //
  135. THREE.SEA3D.prototype.flipDefaultAnimation = function () {
  136. var buf1 = new THREE.Matrix4();
  137. var buf2 = new THREE.Matrix4();
  138. var pos = new THREE.Vector3();
  139. var qua = new THREE.Quaternion();
  140. var slc = new THREE.Vector3();
  141. var to_pos = new THREE.Vector3();
  142. var to_qua = new THREE.Quaternion();
  143. var to_slc = new THREE.Vector3();
  144. return function ( animation, obj3d, relative ) {
  145. if ( animation.isFliped ) return;
  146. var dataList = animation.dataList,
  147. t_anm = [];
  148. for ( var i = 0; i < dataList.length; i ++ ) {
  149. var data = dataList[ i ],
  150. raw = data.data,
  151. kind = data.kind,
  152. numFrames = raw.length / data.blockSize;
  153. switch ( kind ) {
  154. case SEA3D.Animation.POSITION:
  155. case SEA3D.Animation.ROTATION:
  156. case SEA3D.Animation.SCALE:
  157. t_anm.push( {
  158. kind: kind,
  159. numFrames: numFrames,
  160. raw: raw
  161. } );
  162. break;
  163. }
  164. }
  165. if ( t_anm.length > 0 ) {
  166. var numFrames = t_anm[ 0 ].numFrames,
  167. parent = undefined;
  168. if ( relative ) {
  169. buf1.identity();
  170. parent = this.flipScaleMatrix( buf2.copy( obj3d.matrixWorld ) );
  171. } else {
  172. if ( obj3d.parent ) {
  173. parent = this.flipScaleMatrix( buf2.copy( obj3d.parent.matrixWorld ) );
  174. }
  175. this.flipScaleMatrix( 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.flipScaleMatrix( 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. animation.isFliped = true;
  241. };
  242. }();
  243. THREE.SEA3D.prototype.readAnimation = function ( sea ) {
  244. if ( ! this.isLegacy( sea ) ) {
  245. this._readAnimation( sea );
  246. }
  247. };
  248. THREE.SEA3D.prototype.getModifier = function ( req ) {
  249. var sea = req.sea;
  250. if ( this.isLegacy( sea ) && ! sea.done ) {
  251. sea.done = true;
  252. switch ( sea.type ) {
  253. case SEA3D.SkeletonAnimation.prototype.type:
  254. this.readSkeletonAnimationLegacy( sea, req.skeleton );
  255. return sea.tag;
  256. break;
  257. case SEA3D.Animation.prototype.type:
  258. case SEA3D.MorphAnimation.prototype.type:
  259. case SEA3D.UVWAnimation.prototype.type:
  260. if ( req.scope instanceof THREE.Object3D ) {
  261. this.flipDefaultAnimation( sea, req.scope, req.relative );
  262. }
  263. this._readAnimation( sea );
  264. return sea.tag;
  265. break;
  266. case SEA3D.Morph.prototype.type:
  267. this.readMorphLegacy( sea, req.geometry );
  268. break;
  269. }
  270. }
  271. return this._getModifier( req );
  272. };
  273. THREE.SEA3D.prototype.updateTransform = function () {
  274. var buf1 = new THREE.Matrix4();
  275. var identity = new THREE.Matrix4();
  276. return function ( obj3d, sea ) {
  277. if ( this.isLegacy( sea ) ) {
  278. if ( sea.transform ) buf1.fromArray( sea.transform );
  279. else buf1.makeTranslation( sea.position.x, sea.position.y, sea.position.z );
  280. this.flipScaleMatrix(
  281. buf1, false,
  282. obj3d.parent ? obj3d.parent.matrixWorld : identity,
  283. obj3d.parent instanceof THREE.Bone
  284. );
  285. obj3d.position.setFromMatrixPosition( buf1 );
  286. obj3d.scale.setFromMatrixScale( buf1 );
  287. // ignore rotation scale
  288. buf1.scale( THREE.SEA3D.VECBUF.set( 1 / obj3d.scale.x, 1 / obj3d.scale.y, 1 / obj3d.scale.z ) );
  289. obj3d.rotation.setFromRotationMatrix( buf1 );
  290. obj3d.updateMatrixWorld();
  291. } else {
  292. this._updateTransform( obj3d, sea );
  293. }
  294. };
  295. }();
  296. THREE.SEA3D.prototype.readSkeleton = function () {
  297. var mtx_tmp_inv = new THREE.Matrix4(),
  298. mtx_local = new THREE.Matrix4(),
  299. mtx_parent = new THREE.Matrix4(),
  300. pos = new THREE.Vector3(),
  301. qua = new THREE.Quaternion();
  302. return function ( sea ) {
  303. var bones = [],
  304. isLegacy = sea.sea3d.config.legacy;
  305. for ( var i = 0; i < sea.joint.length; i ++ ) {
  306. var bone = sea.joint[ i ];
  307. // get world inverse matrix
  308. mtx_tmp_inv.fromArray( bone.inverseBindMatrix );
  309. // convert to world matrix
  310. mtx_local.getInverse( mtx_tmp_inv );
  311. // convert to three.js order
  312. if ( isLegacy ) this.flipBoneMatrix( mtx_local );
  313. if ( bone.parentIndex > - 1 ) {
  314. // to world
  315. mtx_tmp_inv.fromArray( sea.joint[ bone.parentIndex ].inverseBindMatrix );
  316. mtx_parent.getInverse( mtx_tmp_inv );
  317. // convert parent to three.js order
  318. if ( isLegacy ) this.flipBoneMatrix( mtx_parent );
  319. // to local
  320. mtx_parent.getInverse( mtx_parent );
  321. mtx_local.multiplyMatrices( mtx_parent, mtx_local );
  322. }
  323. // apply matrix
  324. pos.setFromMatrixPosition( mtx_local );
  325. qua.setFromRotationMatrix( mtx_local );
  326. bones[ i ] = {
  327. name: bone.name,
  328. pos: [ pos.x, pos.y, pos.z ],
  329. rotq: [ qua.x, qua.y, qua.z, qua.w ],
  330. parent: bone.parentIndex
  331. };
  332. }
  333. this.domain.bones = this.bones = this.bones || [];
  334. this.bones.push( this.objects[ sea.name + '.sklq' ] = sea.tag = bones );
  335. return bones;
  336. };
  337. }();
  338. THREE.SEA3D.prototype.readSkeletonAnimationLegacy = function () {
  339. var mtx_tmp_inv = new THREE.Matrix4(),
  340. mtx_local = new THREE.Matrix4(),
  341. mtx_global = new THREE.Matrix4(),
  342. mtx_parent = new THREE.Matrix4();
  343. return function ( sea, skl ) {
  344. if ( sea.tag ) return sea.tag;
  345. var animations = [],
  346. delta = ( 1000 / sea.frameRate ) / 1000,
  347. scale = [ 1, 1, 1 ];
  348. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  349. var seq = sea.sequence[ i ];
  350. var start = seq.start;
  351. var end = start + seq.count;
  352. var animation = {
  353. name: seq.name,
  354. repeat: seq.repeat,
  355. fps: sea.frameRate,
  356. JIT: 0,
  357. length: delta * seq.count,
  358. hierarchy: []
  359. };
  360. var numJoints = sea.numJoints,
  361. raw = sea.raw;
  362. for ( var j = 0; j < numJoints; j ++ ) {
  363. var bone = skl.joint[ j ],
  364. node = { parent: bone.parentIndex, keys: [] },
  365. keys = node.keys,
  366. time = 0;
  367. for ( var frame = start; frame < end; frame ++ ) {
  368. var idx = ( frame * numJoints * 7 ) + ( j * 7 );
  369. mtx_local.makeRotationFromQuaternion( THREE.SEA3D.QUABUF.set( raw[ idx + 3 ], raw[ idx + 4 ], raw[ idx + 5 ], raw[ idx + 6 ] ) );
  370. mtx_local.setPosition( THREE.SEA3D.VECBUF.set( raw[ idx ], raw[ idx + 1 ], raw[ idx + 2 ] ) );
  371. if ( bone.parentIndex > - 1 ) {
  372. // to global
  373. mtx_tmp_inv.fromArray( skl.joint[ bone.parentIndex ].inverseBindMatrix );
  374. mtx_parent.getInverse( mtx_tmp_inv );
  375. mtx_global.multiplyMatrices( mtx_parent, mtx_local );
  376. // convert to three.js matrix
  377. this.flipBoneMatrix( mtx_global );
  378. // flip parent inverse
  379. this.flipBoneMatrix( mtx_parent );
  380. // to local
  381. mtx_parent.getInverse( mtx_parent );
  382. mtx_local.multiplyMatrices( mtx_parent, mtx_global );
  383. } else {
  384. this.flipBoneMatrix( mtx_local );
  385. }
  386. var posQ = THREE.SEA3D.VECBUF.setFromMatrixPosition( mtx_local );
  387. var newQ = THREE.SEA3D.QUABUF.setFromRotationMatrix( mtx_local );
  388. keys.push( {
  389. time: time,
  390. pos: [ posQ.x, posQ.y, posQ.z ],
  391. rot: [ newQ.x, newQ.y, newQ.z, newQ.w ],
  392. scl: scale
  393. } );
  394. time += delta;
  395. }
  396. animation.hierarchy[ j ] = node;
  397. }
  398. animations.push( THREE.SEA3D.AnimationClip.fromClip( THREE.AnimationClip.parseAnimation( animation, skl.tag ), seq.repeat ) );
  399. }
  400. this.domain.clips = this.clips = this.clips || [];
  401. this.clips.push( this.objects[ sea.name + '.anm' ] = sea.tag = animations );
  402. };
  403. }();
  404. THREE.SEA3D.prototype.readMorphLegacy = function ( sea, geo ) {
  405. for ( var i = 0; i < sea.node.length; i ++ ) {
  406. var node = sea.node[ i ];
  407. this.flipVec3( node.vertex );
  408. this.flipVec3( node.normal );
  409. this.addVector( node.vertex, geo.vertex );
  410. this.addVector( node.normal, geo.normal );
  411. }
  412. this._readMorph( sea );
  413. };
  414. THREE.SEA3D.prototype.readMorph = function ( sea ) {
  415. if ( ! this.isLegacy( sea ) ) {
  416. this._readMorph( sea );
  417. }
  418. };
  419. THREE.SEA3D.prototype.readVertexAnimation = function ( sea ) {
  420. if ( this.isLegacy( sea ) ) {
  421. for ( var i = 0, l = sea.frame.length; i < l; i ++ ) {
  422. var frame = sea.frame[ i ];
  423. this.flipVec3( frame.vertex );
  424. this.flipVec3( frame.normal );
  425. }
  426. }
  427. this._readVertexAnimation( sea );
  428. };
  429. THREE.SEA3D.prototype.readGeometryBuffer = function ( sea ) {
  430. if ( this.isLegacy( sea ) ) {
  431. this.flipVec3( sea.vertex );
  432. this.flipVec3( sea.normal );
  433. this.flipIndexes( sea.indexes );
  434. if ( sea.jointPerVertex > 4 ) this.compressJoints( sea );
  435. else if ( sea.jointPerVertex < 4 ) this.expandJoints( sea );
  436. }
  437. this._readGeometryBuffer( sea );
  438. };
  439. THREE.SEA3D.prototype.readLines = function ( sea ) {
  440. if ( this.isLegacy( sea ) ) {
  441. this.flipVec3( sea.vertex );
  442. }
  443. this._readLines( sea );
  444. };
  445. THREE.SEA3D.prototype.onHead = function ( args ) {
  446. if ( args.sign != "S3D" && args.sign != "TJS" ) {
  447. throw new Error( "Sign '" + args.sign + "' unknown." );
  448. }
  449. };
  450. THREE.SEA3D.EXTENSIONS_LOADER.push( { setTypeRead: function () {
  451. // CONFIG
  452. this.config.legacy = this.config.legacy == undefined ? true : this.config.legacy;
  453. this.file.typeRead[ SEA3D.Skeleton.prototype.type ] = this.readSkeleton;
  454. } } );