SEA3DLegacyZ.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. _getAnimationType: THREE.SEA3D.prototype.getAnimationType,
  16. _readAnimation: THREE.SEA3D.prototype.readAnimation
  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 () {
  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 () {
  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.flipDefaultAnimation = function () {
  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 ( animation, obj3d, relative ) {
  142. if ( animation.isFliped ) return;
  143. var dataList = animation.dataList,
  144. t_anm = [];
  145. for ( var i = 0; i < dataList.length; i ++ ) {
  146. var data = dataList[ i ],
  147. raw = data.data,
  148. kind = data.kind,
  149. numFrames = raw.length / data.blockSize;
  150. switch ( kind ) {
  151. case SEA3D.Animation.POSITION:
  152. case SEA3D.Animation.ROTATION:
  153. case SEA3D.Animation.SCALE:
  154. t_anm.push( {
  155. kind: kind,
  156. numFrames: numFrames,
  157. raw: raw
  158. } );
  159. break;
  160. }
  161. }
  162. if ( t_anm.length > 0 ) {
  163. var numFrames = t_anm[ 0 ].numFrames,
  164. parent = undefined;
  165. if ( relative ) {
  166. buf1.identity();
  167. parent = this.flipMatrixScale( buf2.copy( obj3d.matrixWorld ) );
  168. } else {
  169. if ( obj3d.parent ) {
  170. parent = this.flipMatrixScale( buf2.copy( obj3d.parent.matrixWorld ) );
  171. }
  172. this.flipMatrixScale( buf1.copy( obj3d.matrix ), false, parent );
  173. }
  174. buf1.decompose( pos, qua, slc );
  175. for ( var f = 0, t, c; f < numFrames; f ++ ) {
  176. for ( t = 0; t < t_anm.length; t ++ ) {
  177. var raw = t_anm[ t ].raw,
  178. kind = t_anm[ t ].kind;
  179. switch ( kind ) {
  180. case SEA3D.Animation.POSITION:
  181. c = f * 3;
  182. pos.set(
  183. raw[ c ],
  184. raw[ c + 1 ],
  185. raw[ c + 2 ]
  186. );
  187. break;
  188. case SEA3D.Animation.ROTATION:
  189. c = f * 4;
  190. qua.set(
  191. raw[ c ],
  192. raw[ c + 1 ],
  193. raw[ c + 2 ],
  194. raw[ c + 3 ]
  195. );
  196. break;
  197. case SEA3D.Animation.SCALE:
  198. c = f * 4;
  199. slc.set(
  200. raw[ c ],
  201. raw[ c + 1 ],
  202. raw[ c + 2 ]
  203. );
  204. break;
  205. }
  206. }
  207. buf1.compose( pos, qua, slc );
  208. this.flipMatrixScale( buf1, false, buf2 );
  209. buf1.decompose( to_pos, to_qua, to_slc );
  210. for ( t = 0; t < t_anm.length; t ++ ) {
  211. var raw = t_anm[ t ].raw,
  212. kind = t_anm[ t ].kind;
  213. switch ( kind ) {
  214. case SEA3D.Animation.POSITION:
  215. c = f * 3;
  216. raw[ c ] = to_pos.x;
  217. raw[ c + 1 ] = to_pos.y;
  218. raw[ c + 2 ] = to_pos.z;
  219. break;
  220. case SEA3D.Animation.ROTATION:
  221. c = f * 4;
  222. raw[ c ] = to_qua.x;
  223. raw[ c + 1 ] = to_qua.y;
  224. raw[ c + 2 ] = to_qua.z;
  225. raw[ c + 3 ] = to_qua.w;
  226. break;
  227. case SEA3D.Animation.SCALE:
  228. c = f * 3;
  229. raw[ c ] = to_slc.x;
  230. raw[ c + 1 ] = to_slc.y;
  231. raw[ c + 2 ] = to_slc.z;
  232. break;
  233. }
  234. }
  235. }
  236. }
  237. animation.isFliped = true;
  238. };
  239. }();
  240. THREE.SEA3D.prototype.readAnimation = function ( sea ) {
  241. if ( ! this.isLegacy( sea ) ) {
  242. this._readAnimation( sea );
  243. }
  244. };
  245. THREE.SEA3D.prototype.getAnimationType = function ( req ) {
  246. var sea = req.sea;
  247. if ( this.isLegacy( sea ) ) {
  248. switch ( sea.type ) {
  249. case SEA3D.SkeletonAnimation.prototype.type:
  250. this.readSkeletonAnimationLegacy( sea, req.skeleton );
  251. return sea.tag;
  252. break;
  253. case SEA3D.Animation.prototype.type:
  254. if ( req.scope instanceof THREE.Object3D ) {
  255. this.flipDefaultAnimation( sea, req.scope, req.relative );
  256. }
  257. this._readAnimation( sea );
  258. return sea.tag;
  259. break;
  260. }
  261. }
  262. return this._getAnimationType( req );
  263. };
  264. THREE.SEA3D.prototype.updateTransform = function () {
  265. var buf1 = new THREE.Matrix4();
  266. var identity = new THREE.Matrix4();
  267. return function ( obj3d, sea ) {
  268. if ( this.isLegacy( sea ) ) {
  269. if ( sea.transform ) buf1.elements.set( sea.transform );
  270. else buf1.makeTranslation( sea.position.x, sea.position.y, sea.position.z );
  271. this.flipMatrixScale(
  272. buf1, false,
  273. obj3d.parent ? obj3d.parent.matrixWorld : identity,
  274. obj3d.parent instanceof THREE.Bone
  275. );
  276. obj3d.position.setFromMatrixPosition( buf1 );
  277. obj3d.scale.setFromMatrixScale( buf1 );
  278. // ignore rotation scale
  279. buf1.scale( THREE.SEA3D.VECBUF.set( 1 / obj3d.scale.x, 1 / obj3d.scale.y, 1 / obj3d.scale.z ) );
  280. obj3d.rotation.setFromRotationMatrix( buf1 );
  281. obj3d.updateMatrixWorld();
  282. } else {
  283. this._updateTransform( obj3d, sea );
  284. }
  285. };
  286. }();
  287. THREE.SEA3D.prototype.readSkeleton = function () {
  288. var mtx_tmp_inv = new THREE.Matrix4(),
  289. mtx_local = new THREE.Matrix4(),
  290. mtx_parent = new THREE.Matrix4(),
  291. pos = new THREE.Vector3(),
  292. qua = new THREE.Quaternion();
  293. return function ( sea ) {
  294. var bones = [],
  295. isLegacy = sea.sea3d.config.legacy;
  296. for ( var i = 0; i < sea.joint.length; i ++ ) {
  297. var bone = sea.joint[ i ];
  298. // get world inverse matrix
  299. mtx_tmp_inv.elements = bone.inverseBindMatrix;
  300. // convert to world matrix
  301. mtx_local.getInverse( mtx_tmp_inv );
  302. // convert to three.js order
  303. if ( isLegacy ) this.flipMatrixBone( mtx_local );
  304. if ( bone.parentIndex > - 1 ) {
  305. // to world
  306. mtx_tmp_inv.elements = sea.joint[ bone.parentIndex ].inverseBindMatrix;
  307. mtx_parent.getInverse( mtx_tmp_inv );
  308. // convert parent to three.js order
  309. if ( isLegacy ) this.flipMatrixBone( mtx_parent );
  310. // to local
  311. mtx_parent.getInverse( mtx_parent );
  312. mtx_local.multiplyMatrices( mtx_parent, mtx_local );
  313. }
  314. // apply matrix
  315. pos.setFromMatrixPosition( mtx_local );
  316. qua.setFromRotationMatrix( mtx_local );
  317. bones[ i ] = {
  318. name: bone.name,
  319. pos: [ pos.x, pos.y, pos.z ],
  320. rotq: [ qua.x, qua.y, qua.z, qua.w ],
  321. parent: bone.parentIndex
  322. };
  323. }
  324. return sea.tag = bones;
  325. };
  326. }();
  327. THREE.SEA3D.prototype.readSkeletonAnimationLegacy = function () {
  328. var mtx_tmp_inv = new THREE.Matrix4(),
  329. mtx_local = new THREE.Matrix4(),
  330. mtx_global = new THREE.Matrix4(),
  331. mtx_parent = new THREE.Matrix4();
  332. return function ( sea, skl ) {
  333. if ( sea.tag ) return sea.tag;
  334. var animations = [],
  335. delta = ( 1000 / sea.frameRate ) / 1000,
  336. scale = [ 1, 1, 1 ];
  337. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  338. var seq = sea.sequence[ i ];
  339. var start = seq.start;
  340. var end = start + seq.count;
  341. var animation = {
  342. name: seq.name,
  343. repeat: seq.repeat,
  344. fps: sea.frameRate,
  345. JIT: 0,
  346. length: delta * seq.count,
  347. hierarchy: []
  348. };
  349. var numJoints = sea.numJoints,
  350. raw = sea.raw;
  351. for ( var j = 0; j < numJoints; j ++ ) {
  352. var bone = skl.joint[ j ],
  353. node = { parent: bone.parentIndex, keys: [] },
  354. keys = node.keys,
  355. time = 0;
  356. for ( var frame = start; frame < end; frame ++ ) {
  357. var idx = ( frame * numJoints * 7 ) + ( j * 7 );
  358. mtx_local.makeRotationFromQuaternion( THREE.SEA3D.QUABUF.set( raw[ idx + 3 ], raw[ idx + 4 ], raw[ idx + 5 ], raw[ idx + 6 ] ) );
  359. mtx_local.setPosition( THREE.SEA3D.VECBUF.set( raw[ idx ], raw[ idx + 1 ], raw[ idx + 2 ] ) );
  360. if ( bone.parentIndex > - 1 ) {
  361. // to global
  362. mtx_tmp_inv.elements = skl.joint[ bone.parentIndex ].inverseBindMatrix;
  363. mtx_parent.getInverse( mtx_tmp_inv );
  364. mtx_global.multiplyMatrices( mtx_parent, mtx_local );
  365. // convert to three.js matrix
  366. this.flipMatrixBone( mtx_global );
  367. // flip parent inverse
  368. this.flipMatrixBone( mtx_parent );
  369. // to local
  370. mtx_parent.getInverse( mtx_parent );
  371. mtx_local.multiplyMatrices( mtx_parent, mtx_global );
  372. } else {
  373. this.flipMatrixBone( mtx_local );
  374. }
  375. var posQ = THREE.SEA3D.VECBUF.setFromMatrixPosition( mtx_local );
  376. var newQ = THREE.SEA3D.QUABUF.setFromRotationMatrix( mtx_local );
  377. keys.push( {
  378. time: time,
  379. pos: [ posQ.x, posQ.y, posQ.z ],
  380. rot: [ newQ.x, newQ.y, newQ.z, newQ.w ],
  381. scl: scale
  382. } );
  383. time += delta;
  384. }
  385. animation.hierarchy[ j ] = node;
  386. }
  387. animations.push( THREE.SEA3D.AnimationClip.fromClip( THREE.AnimationClip.parseAnimation( animation, skl.tag ), seq.repeat ) );
  388. }
  389. this.domain.clips = this.clips = this.clips || [];
  390. this.clips.push( this.objects[ sea.name + '.anm' ] = sea.tag = animations );
  391. };
  392. }();
  393. THREE.SEA3D.prototype.readVertexAnimation = function ( sea ) {
  394. if ( this.isLegacy( sea ) ) {
  395. for ( var i = 0, l = sea.frame.length; i < l; i ++ ) {
  396. var frame = sea.frame[ i ];
  397. this.flipZVec3( frame.vertex );
  398. this.flipZVec3( frame.normal );
  399. }
  400. }
  401. this._readVertexAnimation( sea );
  402. };
  403. THREE.SEA3D.prototype.readGeometryBuffer = function ( sea ) {
  404. if ( this.isLegacy( sea ) ) {
  405. this.flipZVec3( sea.vertex );
  406. this.flipZVec3( sea.normal );
  407. this.flipZIndex( sea.indexes );
  408. if ( sea.jointPerVertex > 4 ) this.compressJoints( sea );
  409. else if ( sea.jointPerVertex < 4 ) this.expandJoints( sea );
  410. }
  411. this._readGeometryBuffer( sea );
  412. };
  413. THREE.SEA3D.prototype.readLines = function ( sea ) {
  414. if ( this.isLegacy( sea ) ) {
  415. this.flipZVec3( sea.vertex );
  416. }
  417. this._readLines( sea );
  418. };
  419. THREE.SEA3D.prototype.onHead = function ( args ) {
  420. // TODO: Ignore sign
  421. };
  422. THREE.SEA3D.EXTENSIONS_LOADER.push( { setTypeRead: function () {
  423. // CONFIG
  424. this.config.legacy = this.config.legacy == undefined ? true : this.config.legacy;
  425. this.file.typeRead[ SEA3D.Skeleton.prototype.type ] = this.readSkeleton;
  426. } } );