SEA3D.js 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245
  1. /**
  2. * SEA3D SDK
  3. * @author Sunag / http://www.sunag.com.br/
  4. */
  5. var SEA3D = { VERSION : 17000 }
  6. SEA3D.getVersion = function() {
  7. // Max = 16777215 - VVSSBB | V = Version | S = Subversion | B = Buildversion
  8. var v = SEA3D.VERSION.toString(), l = v.length;
  9. return v.substring( 0, l - 4 ) + "." + v.substring( l - 4, l - 3 ) + "." + v.substring( l - 3, l - 2 ) + "." + parseFloat( v.substring( l - 2, l ) ).toString();
  10. }
  11. console.log( 'SEA3D ' + SEA3D.getVersion() );
  12. //
  13. // Timer
  14. //
  15. SEA3D.Timer = function() {
  16. this.time = this.start = Date.now();
  17. }
  18. SEA3D.Timer.prototype = {
  19. constructor: SEA3D.Timer,
  20. get now () {
  21. return Date.now();
  22. },
  23. get deltaTime () {
  24. return Date.now() - this.time;
  25. },
  26. get elapsedTime () {
  27. return Date.now() - this.start;
  28. },
  29. update: function () {
  30. this.time = Date.now();
  31. }
  32. }
  33. //
  34. // STREAM : STANDARD DATA-IO ( LITTLE-ENDIAN )
  35. //
  36. SEA3D.Stream = function( buffer ) {
  37. this.position = 0;
  38. this.buffer = buffer || new ArrayBuffer();
  39. }
  40. SEA3D.Stream.prototype = {
  41. constructor: SEA3D.Stream,
  42. set buffer ( val ) {
  43. this.buf = val;
  44. this.length = val.byteLength;
  45. this.data = new DataView( val );
  46. },
  47. get buffer () {
  48. return this.buf;
  49. },
  50. get bytesAvailable () {
  51. return this.length - this.position;
  52. }
  53. }
  54. SEA3D.Stream.prototype.readBytes = function( len ) {
  55. var buf = this.buf.slice( this.position, this.position + len );
  56. this.position += len;
  57. return buf;
  58. }
  59. SEA3D.Stream.bufferToBase64 = function( buffer ) {
  60. buffer = new Uint8Array( buffer );
  61. var i = 0,
  62. count = buffer.length,
  63. binary = "";
  64. while ( i < count ) {
  65. binary += String.fromCharCode( buffer[ i ++ ] );
  66. }
  67. return window.btoa( binary );
  68. }
  69. SEA3D.Stream.prototype.readByte = function() {
  70. return this.data.getInt8( this.position ++ );
  71. }
  72. SEA3D.Stream.prototype.readUByte = function() {
  73. return this.data.getUint8( this.position ++ );
  74. }
  75. SEA3D.Stream.prototype.readBool = function() {
  76. return this.data.getInt8( this.position ++ ) != 0;
  77. }
  78. SEA3D.Stream.prototype.readShort = function() {
  79. var v = this.data.getInt16( this.position, true );
  80. this.position += 2;
  81. return v;
  82. }
  83. SEA3D.Stream.prototype.readUShort = function() {
  84. var v = this.data.getUint16( this.position, true );
  85. this.position += 2;
  86. return v;
  87. }
  88. SEA3D.Stream.prototype.readUInt24 = function() {
  89. return this.readUShort() | ( this.readUByte() << 16 );
  90. }
  91. SEA3D.Stream.prototype.readInt = function() {
  92. var v = this.data.getInt32( this.position, true );
  93. this.position += 4;
  94. return v;
  95. }
  96. SEA3D.Stream.prototype.readUInt = function() {
  97. var v = this.data.getUint32( this.position, true );
  98. this.position += 4;
  99. return v;
  100. }
  101. SEA3D.Stream.prototype.readFloat = function() {
  102. var v = this.data.getFloat32( this.position, true );
  103. this.position += 4;
  104. return v;
  105. }
  106. SEA3D.Stream.prototype.readUInteger = function() {
  107. var v = this.readUByte(),
  108. r = v & 0x7F;
  109. if ( ( v & 0x80 ) != 0 ) {
  110. v = this.readUByte();
  111. r |= ( v & 0x7F ) << 7;
  112. if ( ( v & 0x80 ) != 0 ) {
  113. v = this.readUByte();
  114. r |= ( v & 0x7F ) << 13;
  115. }
  116. }
  117. return r;
  118. }
  119. SEA3D.Stream.prototype.readVector2 = function() {
  120. return { x: this.readFloat(), y: this.readFloat() }
  121. }
  122. SEA3D.Stream.prototype.readVector3 = function() {
  123. return { x: this.readFloat(), y: this.readFloat(), z: this.readFloat() }
  124. }
  125. SEA3D.Stream.prototype.readVector4 = function() {
  126. return { x: this.readFloat(), y: this.readFloat(), z: this.readFloat(), w: this.readFloat() }
  127. }
  128. SEA3D.Stream.prototype.readMatrix = function() {
  129. var mtx = new Float32Array( 16 );
  130. mtx[ 0 ] = this.readFloat();
  131. mtx[ 1 ] = this.readFloat();
  132. mtx[ 2 ] = this.readFloat();
  133. mtx[ 3 ] = 0.0;
  134. mtx[ 4 ] = this.readFloat();
  135. mtx[ 5 ] = this.readFloat();
  136. mtx[ 6 ] = this.readFloat();
  137. mtx[ 7 ] = 0.0;
  138. mtx[ 8 ] = this.readFloat();
  139. mtx[ 9 ] = this.readFloat();
  140. mtx[ 10 ] = this.readFloat();
  141. mtx[ 11 ] = 0.0;
  142. mtx[ 12 ] = this.readFloat();
  143. mtx[ 13 ] = this.readFloat();
  144. mtx[ 14 ] = this.readFloat();
  145. mtx[ 15 ] = 1.0;
  146. return mtx;
  147. }
  148. SEA3D.Stream.prototype.readUTF = function( len ) {
  149. return String.fromCharCode.apply( null, new Uint16Array( new Uint8Array( this.readBytes( len ) ) ) );
  150. }
  151. SEA3D.Stream.prototype.readExt = function() {
  152. return this.readUTF( 4 ).replace( /\0/g, "" );
  153. }
  154. SEA3D.Stream.prototype.readUTF8 = function() {
  155. return this.readUTF( this.readUByte() );
  156. }
  157. SEA3D.Stream.prototype.readUTF8Short = function() {
  158. return this.readUTF( this.readUShort() );
  159. }
  160. SEA3D.Stream.prototype.readUTF8Long = function() {
  161. return this.readUTF( this.readUInt() );
  162. }
  163. SEA3D.Stream.prototype.readBlendMode = function() {
  164. return SEA3D.DataTable.BLEND_MODE[ this.readUByte() ];
  165. }
  166. SEA3D.Stream.prototype.toBase64 = function() {
  167. return SEA3D.Stream.bufferToBase64( this.data );
  168. }
  169. SEA3D.Stream.prototype.append = function( data ) {
  170. var tmp = new ArrayBuffer( this.data.byteLength + data.byteLength );
  171. tmp.set( new ArrayBuffer( this.data ), 0 );
  172. tmp.set( new ArrayBuffer( data ), this.data.byteLength );
  173. this.data = tmp;
  174. }
  175. SEA3D.Stream.prototype.concat = function( position, length ) {
  176. return new SEA3D.Stream( this.buffer.slice( position, position + length ) );
  177. }
  178. //
  179. // Data Table
  180. //
  181. SEA3D.DataTable = {
  182. NONE : 0,
  183. // 1D = 0 at 31
  184. BOOLEAN : 1,
  185. BYTE : 2,
  186. UBYTE : 3,
  187. SHORT : 4,
  188. USHORT : 5,
  189. INT24 : 6,
  190. UINT24 : 7,
  191. INT : 8,
  192. UINT : 9,
  193. FLOAT : 10,
  194. DOUBLE : 11,
  195. DECIMAL : 12,
  196. // 2D = 32 at 63
  197. // 3D = 64 at 95
  198. VECTOR3D : 74,
  199. // 4D = 96 at 127
  200. VECTOR4D : 106,
  201. // Undefined Size = 128 at 255
  202. STRING_TINY : 128,
  203. STRING_SHORT : 129,
  204. STRING_LONG : 130,
  205. ASSET : 200,
  206. GROUP : 255
  207. }
  208. SEA3D.DataTable.BLEND_MODE = [
  209. "normal", "add", "subtract", "multiply", "dividing", "alpha", "screen", "darken",
  210. "overlay", "colorburn", "linearburn", "lighten", "colordodge", "lineardodge",
  211. "softlight", "hardlight", "pinlight", "spotlight", "spotlightblend", "hardmix",
  212. "average", "difference", "exclusion", "hue", "saturation", "color", "value"
  213. ]
  214. SEA3D.DataTable.INTERPOLATION_TABLE = [
  215. "normal", "linear",
  216. "sine.in", "sine.out", "sine.inout",
  217. "cubic.in", "cubic.out", "cubic.inout",
  218. "quint.in", "quint.out", "quint.inout",
  219. "circ.in", "circ.out", "circ.inout",
  220. "back.in", "back.out", "back.inout",
  221. "quad.in", "quad.out", "quad.inout",
  222. "quart.in", "quart.out", "quart.inout",
  223. "expo.in", "expo.out", "expo.inout",
  224. "elastic.in", "elastic.out", "elastic.inout",
  225. "bounce.in", "bounce.out", "bounce.inout"
  226. ]
  227. SEA3D.DataTable.readObject = function( data, sea ) {
  228. return SEA3D.DataTable.readToken( data.readUByte(), data, sea );
  229. }
  230. SEA3D.DataTable.readToken = function( type, data, sea ) {
  231. switch ( type )
  232. {
  233. // 1D
  234. case SEA3D.DataTable.BOOLEAN:
  235. return data.readBool();
  236. break;
  237. case SEA3D.DataTable.UBYTE:
  238. return data.readUByte();
  239. break;
  240. case SEA3D.DataTable.USHORT:
  241. return data.readUShort();
  242. break;
  243. case SEA3D.DataTable.UINT24:
  244. return data.readUInt24();
  245. break;
  246. case SEA3D.DataTable.INT:
  247. return data.readInt();
  248. break;
  249. case SEA3D.DataTable.UINT:
  250. return data.readUInt();
  251. break;
  252. case SEA3D.DataTable.FLOAT:
  253. return data.readFloat();
  254. break;
  255. // 3D
  256. case SEA3D.DataTable.VECTOR3D:
  257. return data.readVector3();
  258. break;
  259. // 4D
  260. case SEA3D.DataTable.VECTOR4D:
  261. return data.readVector4();
  262. break;
  263. // Undefined Values
  264. case SEA3D.DataTable.STRING_TINY:
  265. return data.readUTF8();
  266. break;
  267. case SEA3D.DataTable.STRING_SHORT:
  268. return data.readUTF8Short();
  269. break;
  270. case SEA3D.DataTable.STRING_LONG:
  271. return data.readUTF8Long();
  272. break
  273. case SEA3D.DataTable.ASSET:
  274. var asset = data.readUInt();
  275. return asset > 0 ? sea.getObject( asset - 1 ).tag : null;
  276. break;
  277. default:
  278. console.error( "DataType not found!" );
  279. break;
  280. }
  281. return null;
  282. }
  283. SEA3D.DataTable.readVector = function( type, data, out, length, offset ) {
  284. var size = SEA3D.DataTable.sizeOf( type ),
  285. i = offset * size,
  286. count = i + ( length * size );
  287. switch ( type )
  288. {
  289. // 1D
  290. case SEA3D.DataTable.BOOLEAN:
  291. while ( i < count ) {
  292. out[ i ++ ] = data.readBool() ? 1 : 0;
  293. }
  294. break;
  295. case SEA3D.DataTable.UBYTE:
  296. while ( i < count ) {
  297. out[ i ++ ] = data.readUByte();
  298. }
  299. break;
  300. case SEA3D.DataTable.USHORT:
  301. while ( i < count ) {
  302. out[ i ++ ] = data.readUShort();
  303. }
  304. break;
  305. case SEA3D.DataTable.UINT24:
  306. while ( i < count ) {
  307. out[ i ++ ] = data.readUInt24();
  308. }
  309. break;
  310. case SEA3D.DataTable.UINT:
  311. while ( i < count ) {
  312. out[ i ++ ] = data.readUInt();
  313. }
  314. break;
  315. case SEA3D.DataTable.FLOAT:
  316. while ( i < count ) {
  317. out[ i ++ ] = data.readFloat();
  318. }
  319. break;
  320. // 3D
  321. case SEA3D.DataTable.VECTOR3D:
  322. while ( i < count )
  323. {
  324. out[ i ++ ] = data.readFloat();
  325. out[ i ++ ] = data.readFloat();
  326. out[ i ++ ] = data.readFloat();
  327. }
  328. break;
  329. // 4D
  330. case SEA3D.DataTable.VECTOR4D:
  331. while ( i < count )
  332. {
  333. out[ i ++ ] = data.readFloat();
  334. out[ i ++ ] = data.readFloat();
  335. out[ i ++ ] = data.readFloat();
  336. out[ i ++ ] = data.readFloat();
  337. }
  338. break;
  339. }
  340. }
  341. SEA3D.DataTable.sizeOf = function( kind ) {
  342. if ( kind == 0 ) return 0;
  343. else if ( kind >= 1 && kind <= 31 ) return 1;
  344. else if ( kind >= 32 && kind <= 63 ) return 2;
  345. else if ( kind >= 64 && kind <= 95 ) return 3;
  346. else if ( kind >= 96 && kind <= 125 ) return 4;
  347. return - 1;
  348. }
  349. //
  350. // Math
  351. //
  352. SEA3D.Math = {
  353. DEGREES : 180 / Math.PI,
  354. RADIANS : Math.PI / 180
  355. }
  356. SEA3D.Math.angle = function( val ) {
  357. var ang = 180,
  358. inv = val < 0;
  359. val = ( inv ? - val : val ) % 360;
  360. if ( val > ang )
  361. {
  362. val = - ang + ( val - ang );
  363. }
  364. return ( inv ? - val : val );
  365. }
  366. SEA3D.Math.lerpAngle = function( val, tar, t ) {
  367. if ( Math.abs( val - tar ) > 180 )
  368. {
  369. if ( val > tar )
  370. {
  371. tar += 360;
  372. }
  373. else
  374. {
  375. tar -= 360;
  376. }
  377. }
  378. val += ( tar - val ) * t;
  379. return SEA3D.Math.angle( val );
  380. }
  381. SEA3D.Math.lerpColor = function( val, tar, t ) {
  382. var a0 = val >> 24 & 0xff,
  383. r0 = val >> 16 & 0xff,
  384. g0 = val >> 8 & 0xff,
  385. b0 = val & 0xff;
  386. var a1 = tar >> 24 & 0xff,
  387. r1 = tar >> 16 & 0xff,
  388. g1 = tar >> 8 & 0xff,
  389. b1 = tar & 0xff;
  390. a0 += ( a1 - a0 ) * t;
  391. r0 += ( r1 - r0 ) * t;
  392. g0 += ( g1 - g0 ) * t;
  393. b0 += ( b1 - b0 ) * t;
  394. return a0 << 24 | r0 << 16 | g0 << 8 | b0;
  395. }
  396. SEA3D.Math.lerp = function( val, tar, t ) {
  397. return val + ( ( tar - val ) * t );
  398. }
  399. SEA3D.Math.lerp1x = function( val, tar, t ) {
  400. val[ 0 ] += ( tar[ 0 ] - val[ 0 ] ) * t;
  401. }
  402. SEA3D.Math.lerp3x = function( val, tar, t ) {
  403. val[ 0 ] += ( tar[ 0 ] - val[ 0 ] ) * t;
  404. val[ 1 ] += ( tar[ 1 ] - val[ 1 ] ) * t;
  405. val[ 2 ] += ( tar[ 2 ] - val[ 2 ] ) * t;
  406. }
  407. SEA3D.Math.lerpAng1x = function( val, tar, t ) {
  408. val[ 0 ] = SEA3D.Math.lerpAngle( val[ 0 ], tar[ 0 ], t );
  409. }
  410. SEA3D.Math.lerpColor1x = function( val, tar, t ) {
  411. val[ 0 ] = SEA3D.Math.lerpColor( val[ 0 ], tar[ 0 ], t );
  412. }
  413. SEA3D.Math.lerpQuat4x = function( val, tar, t ) {
  414. var x1 = val[ 0 ],
  415. y1 = val[ 1 ],
  416. z1 = val[ 2 ],
  417. w1 = val[ 3 ];
  418. var x2 = tar[ 0 ],
  419. y2 = tar[ 1 ],
  420. z2 = tar[ 2 ],
  421. w2 = tar[ 3 ];
  422. var x, y, z, w, l;
  423. // shortest direction
  424. if ( x1 * x2 + y1 * y2 + z1 * z2 + w1 * w2 < 0 ) {
  425. x2 = - x2;
  426. y2 = - y2;
  427. z2 = - z2;
  428. w2 = - w2;
  429. }
  430. x = x1 + t * ( x2 - x1 );
  431. y = y1 + t * ( y2 - y1 );
  432. z = z1 + t * ( z2 - z1 );
  433. w = w1 + t * ( w2 - w1 );
  434. l = 1.0 / Math.sqrt( w * w + x * x + y * y + z * z );
  435. val[ 0 ] = x * l;
  436. val[ 1 ] = y * l;
  437. val[ 2 ] = z * l;
  438. val[ 3 ] = w * l;
  439. }
  440. //
  441. // BlendMethod
  442. //
  443. SEA3D.AnimationBlendMethod = {
  444. LINEAR : 'linear',
  445. EASING : 'easing'
  446. }
  447. //
  448. // AnimationFrame
  449. //
  450. SEA3D.AnimationFrame = function() {
  451. this.data = [ 0, 0, 0, 0 ];
  452. }
  453. SEA3D.AnimationFrame.prototype.toVector = function() {
  454. return { x: this.data[ 0 ], y: this.data[ 1 ], z: this.data[ 2 ], w: this.data[ 3 ] };
  455. }
  456. SEA3D.AnimationFrame.prototype.toAngles = function( d ) {
  457. var x = this.data[ 0 ],
  458. y = this.data[ 1 ],
  459. z = this.data[ 2 ],
  460. w = this.data[ 3 ];
  461. var a = 2 * ( w * y - z * x );
  462. if ( a < - 1 ) a = - 1;
  463. else if ( a > 1 ) a = 1;
  464. return {
  465. x : Math.atan2( 2 * ( w * x + y * z ), 1 - 2 * ( x * x + y * y ) ) * d,
  466. y : Math.asin( a ) * d,
  467. z : Math.atan2( 2 * ( w * z + x * y ), 1 - 2 * ( y * y + z * z ) ) * d
  468. }
  469. }
  470. SEA3D.AnimationFrame.prototype.toEuler = function() {
  471. return this.toAngles( SEA3D.Math.DEGREES );
  472. }
  473. SEA3D.AnimationFrame.prototype.toRadians = function() {
  474. return this.toAngles( 1 );
  475. }
  476. SEA3D.AnimationFrame.prototype.setX = function( val ) {
  477. this.data[ 0 ] = val;
  478. }
  479. SEA3D.AnimationFrame.prototype.getX = function() {
  480. return this.data[ 0 ];
  481. }
  482. SEA3D.AnimationFrame.prototype.setY = function( val ) {
  483. this.data[ 1 ] = val;
  484. }
  485. SEA3D.AnimationFrame.prototype.getY = function() {
  486. return this.data[ 1 ];
  487. }
  488. SEA3D.AnimationFrame.prototype.setZ = function( val ) {
  489. this.data[ 2 ] = val;
  490. }
  491. SEA3D.AnimationFrame.prototype.getZ = function() {
  492. return this.data[ 2 ];
  493. }
  494. SEA3D.AnimationFrame.prototype.setW = function( val ) {
  495. this.data[ 3 ] = val;
  496. }
  497. SEA3D.AnimationFrame.prototype.getW = function() {
  498. return this.data[ 3 ];
  499. }
  500. //
  501. // AnimationData
  502. //
  503. SEA3D.AnimationData = function( kind, dataType, data, offset ) {
  504. this.kind = kind;
  505. this.type = dataType;
  506. this.blockLength = SEA3D.DataTable.sizeOf( dataType );
  507. this.data = data;
  508. this.offset = offset == undefined ? 0 : offset;
  509. switch ( this.blockLength )
  510. {
  511. case 1: this.getData = this.getData1x; break;
  512. case 2: this.getData = this.getData2x; break;
  513. case 3: this.getData = this.getData3x; break;
  514. case 4: this.getData = this.getData4x; break;
  515. }
  516. }
  517. SEA3D.AnimationData.prototype.getData1x = function( frame, data ) {
  518. frame = this.offset + frame * this.blockLength;
  519. data[ 0 ] = this.data[ frame ];
  520. }
  521. SEA3D.AnimationData.prototype.getData2x = function( frame, data ) {
  522. frame = this.offset + frame * this.blockLength;
  523. data[ 0 ] = this.data[ frame ];
  524. data[ 1 ] = this.data[ frame + 1 ];
  525. }
  526. SEA3D.AnimationData.prototype.getData3x = function( frame, data ) {
  527. frame = this.offset + frame * this.blockLength;
  528. data[ 0 ] = this.data[ frame ];
  529. data[ 1 ] = this.data[ frame + 1 ];
  530. data[ 2 ] = this.data[ frame + 2 ];
  531. }
  532. SEA3D.AnimationData.prototype.getData4x = function( frame, data ) {
  533. frame = this.offset + frame * this.blockLength;
  534. data[ 0 ] = this.data[ frame ];
  535. data[ 1 ] = this.data[ frame + 1 ];
  536. data[ 2 ] = this.data[ frame + 2 ];
  537. data[ 3 ] = this.data[ frame + 3 ];
  538. }
  539. //
  540. // AnimationNode
  541. //
  542. SEA3D.AnimationNode = function( name, frameRate, numFrames, repeat, intrpl ) {
  543. this.name = name;
  544. this.frameRate = frameRate;
  545. this.frameMill = 1000 / frameRate;
  546. this.numFrames = numFrames;
  547. this.length = numFrames - 1;
  548. this.time = 0;
  549. this.duration = this.length * this.frameMill;
  550. this.repeat = repeat;
  551. this.intrpl = intrpl;
  552. this.invalidState = true;
  553. this.dataList = [];
  554. this.dataListId = {};
  555. this.buffer = new SEA3D.AnimationFrame();
  556. this.percent = 0;
  557. this.prevFrame = 0;
  558. this.nextFrame = 0;
  559. this.frame = 0;
  560. }
  561. SEA3D.AnimationNode.prototype.setTime = function( value ) {
  562. this.frame = this.validFrame( value / this.frameMill );
  563. this.time = this.frame * this.frameRate;
  564. this.invalidState = true;
  565. }
  566. SEA3D.AnimationNode.prototype.getTime = function() {
  567. return this.time;
  568. }
  569. SEA3D.AnimationNode.prototype.setFrame = function( value ) {
  570. this.setTime( value * this.frameMill );
  571. }
  572. SEA3D.AnimationNode.prototype.getRealFrame = function() {
  573. return Math.floor( this.frame );
  574. }
  575. SEA3D.AnimationNode.prototype.getFrame = function() {
  576. return this.frame;
  577. }
  578. SEA3D.AnimationNode.prototype.setPosition = function( value ) {
  579. this.setFrame( value * ( this.numFrames - 1 ) );
  580. }
  581. SEA3D.AnimationNode.prototype.getPosition = function() {
  582. return this.frame / ( this.numFrames - 1 );
  583. }
  584. SEA3D.AnimationNode.prototype.validFrame = function( value ) {
  585. var inverse = value < 0;
  586. if ( inverse ) value = - value;
  587. if ( value > this.length )
  588. value = this.repeat ? value % this.length : this.length;
  589. if ( inverse ) value = this.length - value;
  590. return value;
  591. }
  592. SEA3D.AnimationNode.prototype.addData = function( animationData ) {
  593. this.dataListId[ animationData.kind ] = animationData;
  594. this.dataList[ this.dataList.length ] = animationData;
  595. }
  596. SEA3D.AnimationNode.prototype.removeData = function( animationData ) {
  597. delete this.dataListId[ animationData.kind ];
  598. this.dataList.splice( this.dataList.indexOf( animationData ), 1 );
  599. }
  600. SEA3D.AnimationNode.prototype.getDataByKind = function( kind ) {
  601. return this.dataListId[ kind ];
  602. }
  603. SEA3D.AnimationNode.prototype.getFrameAt = function( frame, id ) {
  604. this.dataListId[ id ].getFrameData( frame, this.buffer.data );
  605. return this.buffer;
  606. }
  607. SEA3D.AnimationNode.prototype.getFrame = function( id ) {
  608. this.dataListId[ id ].getFrameData( this.getRealFrame(), this.buffer.data );
  609. return this.buffer;
  610. }
  611. SEA3D.AnimationNode.prototype.getInterpolationFrame = function( animationData, iFunc ) {
  612. if ( this.numFrames == 0 )
  613. return this.buffer;
  614. if ( this.invalidState )
  615. {
  616. this.prevFrame = this.getRealFrame();
  617. this.nextFrame = this.validFrame( this.prevFrame + 1 );
  618. this.percent = this.frame - this.prevFrame;
  619. this.invalidState = false;
  620. }
  621. animationData.getData( this.prevFrame, this.buffer.data );
  622. if ( this.percent > 0 )
  623. {
  624. animationData.getData( this.nextFrame, SEA3D.AnimationNode.FRAME_BUFFER );
  625. // interpolation function
  626. iFunc( this.buffer.data, SEA3D.AnimationNode.FRAME_BUFFER, this.percent );
  627. }
  628. return this.buffer;
  629. }
  630. SEA3D.AnimationNode.FRAME_BUFFER = [ 0, 0, 0, 0 ];
  631. //
  632. // AnimationSet
  633. //
  634. SEA3D.AnimationSet = function() {
  635. this.animations = [];
  636. this.dataCount = - 1;
  637. }
  638. SEA3D.AnimationSet.prototype.addAnimation = function( node ) {
  639. if ( this.dataCount == - 1 )
  640. this.dataCount = node.dataList.length;
  641. this.animations[ node.name ] = node;
  642. this.animations.push( node );
  643. }
  644. SEA3D.AnimationSet.prototype.getAnimationByName = function( name ) {
  645. return this.animations[ name ];
  646. }
  647. //
  648. // AnimationState
  649. //
  650. SEA3D.AnimationState = function( node ) {
  651. this.node = node;
  652. this.offset = 0;
  653. this.weight = 0;
  654. this.time = 0;
  655. }
  656. SEA3D.AnimationState.prototype.setTime = function( val ) {
  657. this.node.time = this.time = val;
  658. }
  659. SEA3D.AnimationState.prototype.getTime = function() {
  660. return this.time;
  661. }
  662. SEA3D.AnimationState.prototype.setFrame = function( val ) {
  663. this.node.setFrame( val );
  664. this.time = this.node.time;
  665. }
  666. SEA3D.AnimationState.prototype.getFrame = function() {
  667. this.update();
  668. return this.node.getFrame();
  669. }
  670. SEA3D.AnimationState.prototype.setPosition = function( val ) {
  671. this.node.setPosition( val );
  672. this.time = this.node.time;
  673. }
  674. SEA3D.AnimationState.prototype.getPosition = function() {
  675. this.update();
  676. return this.node.getPosition();
  677. }
  678. SEA3D.AnimationState.prototype.update = function() {
  679. if ( this.node.time != this.time )
  680. this.node.setTime( this.time );
  681. }
  682. //
  683. // Animation Handler
  684. //
  685. SEA3D.AnimationHandler = function( animationSet ) {
  686. this.animationSet = animationSet;
  687. this.states = SEA3D.AnimationHandler.stateFromAnimations( animationSet.animations );
  688. this.timeScale = 1;
  689. this.time = 0;
  690. this.numAnimation = animationSet.animations.length;
  691. this.relative = false;
  692. this.playing = false;
  693. this.delta = 0;
  694. this.easeSpeed = 2;
  695. this.crossfade = 0;
  696. this.updateAllStates = false;
  697. this.blendMethod = SEA3D.AnimationBlendMethod.LINEAR;
  698. }
  699. SEA3D.AnimationHandler.prototype.update = function( delta ) {
  700. this.delta = delta;
  701. this.time += delta * this.timeScale;
  702. this.updateState();
  703. this.updateAnimation();
  704. }
  705. SEA3D.AnimationHandler.prototype.updateState = function() {
  706. var i, state;
  707. this.currentState.node.setTime( this.time - this.currentState.offset );
  708. if ( this.currentState.weight < 1 && this.crossfade > 0 )
  709. {
  710. var delta = Math.abs( this.delta ) / ( 1000.0 * this.crossfade );
  711. var weight = 1;
  712. if ( this.blendMethod === SEA3D.AnimationBlendMethod.EASING )
  713. delta *= this.easeSpeed;
  714. for ( i = 0; i < this.states.length; ++ i ) {
  715. state = this.states[ i ];
  716. if ( state !== this.currentState ) {
  717. if ( this.blendMethod === SEA3D.AnimationBlendMethod.LINEAR )
  718. state.weight -= delta;
  719. else if ( this.blendMethod === SEA3D.AnimationBlendMethod.EASING )
  720. state.weight -= state.weight * delta;
  721. if ( state.weight < 0 )
  722. state.weight = 0;
  723. weight -= state.weight;
  724. if ( this.updateAllStates ) {
  725. state.node.setTime( this.time - state.offset );
  726. }
  727. }
  728. }
  729. if ( weight < 0 )
  730. weight = 0;
  731. this.currentState.weight = weight;
  732. } else {
  733. for ( i = 0; i < this.states.length; ++ i ) {
  734. state = this.states[ i ];
  735. if ( state === this.currentState )
  736. state.weight = 1;
  737. else {
  738. state.weight = 0;
  739. if ( this.updateAllStates ) {
  740. state.node.setTime( this.time );
  741. }
  742. }
  743. }
  744. }
  745. }
  746. SEA3D.AnimationHandler.prototype.updateAnimation = function() {
  747. var dataCount = this.animationSet.dataCount;
  748. var nodes = this.animationSet.animations;
  749. var currentNode = this.currentState.node;
  750. for ( var i = 0; i < dataCount; i ++ ) {
  751. for ( var n = 0; n < nodes.length; n ++ ) {
  752. var node = nodes[ n ],
  753. state = this.states[ n ],
  754. data = node.dataList[ i ],
  755. iFunc = SEA3D.Animation.DefaultLerpFuncs[ data.kind ],
  756. frame;
  757. if ( n == 0 ) {
  758. frame = currentNode.getInterpolationFrame( currentNode.dataList[ i ], iFunc );
  759. if ( ! currentNode.repeat && currentNode.frame == currentNode.numFrames - 1 ) {
  760. if ( this.onComplete )
  761. this.onComplete( this );
  762. }
  763. }
  764. if ( node != currentNode ) {
  765. if ( state.weight > 0 )
  766. {
  767. iFunc(
  768. frame.data,
  769. node.getInterpolationFrame( data, iFunc ).data,
  770. state.weight
  771. );
  772. }
  773. }
  774. if ( this.updateAnimationFrame )
  775. this.updateAnimationFrame( frame, data.kind );
  776. }
  777. }
  778. }
  779. SEA3D.AnimationHandler.prototype.getStateByName = function( name ) {
  780. return this.states[ name ];
  781. }
  782. SEA3D.AnimationHandler.prototype.getStateNameByIndex = function( index ) {
  783. return this.animationSet.animations[ index ].name;
  784. }
  785. SEA3D.AnimationHandler.prototype.play = function( name, crossfade, offset ) {
  786. this.currentState = this.getStateByName( name );
  787. if ( ! this.currentState )
  788. throw new Error( 'Animation "' + name + '" not found.' );
  789. this.crossfade = crossfade;
  790. this.currentState.offset = this.time;
  791. if ( offset !== undefined )
  792. this.currentState.time = offset;
  793. if ( ! this.playing ) {
  794. // add in animation collector
  795. SEA3D.AnimationHandler.add( this );
  796. this.playing = true;
  797. }
  798. }
  799. SEA3D.AnimationHandler.prototype.resume = function() {
  800. if ( ! this.playing ) {
  801. SEA3D.AnimationHandler.add( this );
  802. this.playing = true;
  803. }
  804. }
  805. SEA3D.AnimationHandler.prototype.pause = function() {
  806. if ( this.playing ) {
  807. SEA3D.AnimationHandler.remove( this );
  808. this.playing = false;
  809. }
  810. }
  811. SEA3D.AnimationHandler.prototype.stop = function() {
  812. this.time = 0;
  813. this.pause();
  814. }
  815. SEA3D.AnimationHandler.prototype.setRelative = function( val ) {
  816. this.relative = val;
  817. }
  818. SEA3D.AnimationHandler.prototype.getRelative = function() {
  819. return this.relative;
  820. }
  821. //
  822. // Manager
  823. //
  824. SEA3D.AnimationHandler.add = function( animation ) {
  825. SEA3D.AnimationHandler.animations.push( animation );
  826. }
  827. SEA3D.AnimationHandler.remove = function( animation ) {
  828. SEA3D.AnimationHandler.animations.splice( SEA3D.AnimationHandler.animations.indexOf( animation ), 1 );
  829. }
  830. SEA3D.AnimationHandler.stateFromAnimations = function( anms ) {
  831. var states = [];
  832. for ( var i = 0; i < anms.length; i ++ ) {
  833. states[ anms[ i ].name ] = states[ i ] = new SEA3D.AnimationState( anms[ i ] );
  834. }
  835. return states;
  836. }
  837. SEA3D.AnimationHandler.update = function( delta ) {
  838. for ( var i = 0, len = SEA3D.AnimationHandler.animations.length; i < len; i ++ ) {
  839. SEA3D.AnimationHandler.animations[ i ].update( delta * 1000 );
  840. }
  841. }
  842. SEA3D.AnimationHandler.setTime = function( time ) {
  843. for ( var i = 0, len = SEA3D.AnimationHandler.animations.length; i < len; i ++ ) {
  844. SEA3D.AnimationHandler.animations[ i ].time = time;
  845. }
  846. }
  847. SEA3D.AnimationHandler.stop = function() {
  848. while ( SEA3D.AnimationHandler.animations.length ) {
  849. SEA3D.AnimationHandler.animations[ 0 ].stop();
  850. }
  851. }
  852. SEA3D.AnimationHandler.animations = [];
  853. //
  854. // Object
  855. //
  856. SEA3D.Object = function( name, data, type, sea ) {
  857. this.name = name;
  858. this.data = data;
  859. this.type = type;
  860. this.sea = sea;
  861. }
  862. //
  863. // Geometry Base
  864. //
  865. SEA3D.GeometryBase = function( scope ) {
  866. var data = scope.data;
  867. scope.attrib = data.readUShort();
  868. scope.isBigMesh = ( scope.attrib & 1 ) != 0;
  869. // variable uint
  870. data.readVInt = scope.isBigMesh ? data.readUInt : data.readUShort;
  871. scope.numVertex = data.readVInt();
  872. scope.length = scope.numVertex * 3;
  873. }
  874. //
  875. // Geometry
  876. //
  877. SEA3D.Geometry = function( name, data, sea ) {
  878. var i, j, vec, len;
  879. this.name = name;
  880. this.data = data;
  881. this.sea = sea;
  882. SEA3D.GeometryBase( this );
  883. // NORMAL
  884. if ( this.attrib & 4 ) {
  885. this.normal = [];
  886. i = 0;
  887. while ( i < this.length ) {
  888. this.normal[ i ++ ] = data.readFloat();
  889. }
  890. }
  891. // TANGENT
  892. if ( this.attrib & 8 ) {
  893. this.tangent = [];
  894. i = 0;
  895. while ( i < this.length ) {
  896. this.tangent[ i ++ ] = data.readFloat();
  897. }
  898. }
  899. // UV
  900. if ( this.attrib & 32 ) {
  901. this.uv = [];
  902. this.uv.length = data.readUByte();
  903. len = this.numVertex * 2;
  904. i = 0;
  905. while ( i < this.uv.length ) {
  906. // UV VERTEX DATA
  907. this.uv[ i ++ ] = vec = [];
  908. j = 0;
  909. while ( j < len ) {
  910. vec[ j ++ ] = data.readFloat();
  911. }
  912. }
  913. }
  914. // JOINT-INDEXES / WEIGHTS
  915. if ( this.attrib & 64 ) {
  916. this.jointPerVertex = data.readUByte();
  917. var jntLen = this.numVertex * this.jointPerVertex;
  918. this.joint = [];
  919. this.weight = [];
  920. i = 0;
  921. while ( i < jntLen ) {
  922. this.joint[ i ++ ] = data.readUShort();
  923. }
  924. i = 0;
  925. while ( i < jntLen ) {
  926. this.weight[ i ++ ] = data.readFloat();
  927. }
  928. }
  929. // VERTEX_COLOR
  930. if ( this.attrib & 128 ) {
  931. var colorAttrib = data.readUByte(),
  932. numColor = ( ( ( colorAttrib & 64 ) >> 6 ) | ( ( colorAttrib & 128 ) >> 6 ) ) + 1,
  933. colorCount = this.numVertex * 4;
  934. this.color = [];
  935. this.color.length = colorAttrib & 15;
  936. for ( i = 0; i < this.color.length; i ++ )
  937. {
  938. var vColor = [];
  939. switch ( numColor )
  940. {
  941. case 1:
  942. j = 0;
  943. while ( j < colorCount )
  944. {
  945. vColor[ j ++ ] = data.readUByte() / 0xFF;
  946. vColor[ j ++ ] = 0;
  947. vColor[ j ++ ] = 0;
  948. vColor[ j ++ ] = 1;
  949. }
  950. break;
  951. case 2:
  952. j = 0;
  953. while ( j < colorCount )
  954. {
  955. vColor[ j ++ ] = data.readUByte() / 0xFF;
  956. vColor[ j ++ ] = data.readUByte() / 0xFF;
  957. vColor[ j ++ ] = 0;
  958. vColor[ j ++ ] = 1;
  959. }
  960. break;
  961. case 3:
  962. j = 0;
  963. while ( j < colorCount )
  964. {
  965. vColor[ j ++ ] = data.readUByte() / 0xFF;
  966. vColor[ j ++ ] = data.readUByte() / 0xFF;
  967. vColor[ j ++ ] = data.readUByte() / 0xFF;
  968. vColor[ j ++ ] = 1;
  969. }
  970. break;
  971. case 4:
  972. j = 0;
  973. while ( j < colorCount )
  974. {
  975. vColor[ j ++ ] = data.readUByte() / 0xFF;
  976. vColor[ j ++ ] = data.readUByte() / 0xFF;
  977. vColor[ j ++ ] = data.readUByte() / 0xFF;
  978. vColor[ j ++ ] = data.readUByte() / 0xFF;
  979. }
  980. break;
  981. }
  982. this.color[ i ] = vColor;
  983. }
  984. }
  985. // VERTEX
  986. this.vertex = [];
  987. i = 0;
  988. while ( i < this.length ) {
  989. this.vertex[ i ++ ] = data.readFloat();
  990. }
  991. // SUB-MESHES
  992. var count = data.readUByte();
  993. this.indexes = vec = [];
  994. this.groups = [];
  995. // INDEXES
  996. j = 0;
  997. for ( i = 0; i < count; i ++ )
  998. {
  999. len = data.readVInt() * 3;
  1000. this.groups.push( {
  1001. start : j,
  1002. count : len,
  1003. } );
  1004. len += j;
  1005. while ( j < len ) {
  1006. vec[ j ++ ] = data.readVInt();
  1007. }
  1008. }
  1009. }
  1010. SEA3D.Geometry.prototype.type = "geo";
  1011. //
  1012. // Geometry Delta
  1013. //
  1014. SEA3D.GeometryDelta = function( name, data, sea ) {
  1015. var i, j, start, delta, len, readNumber, numDiv, vecUint, vec;
  1016. this.name = name;
  1017. this.data = data;
  1018. this.sea = sea;
  1019. this.attrib = data.readUShort();
  1020. this.numVertex = data.readUInteger();
  1021. this.length = this.numVertex * 3;
  1022. if ( this.attrib & 1 )
  1023. {
  1024. readNumber = function() {
  1025. return data.readByte();
  1026. }
  1027. numDiv = 0xFF / 2;
  1028. }
  1029. else
  1030. {
  1031. readNumber = function() {
  1032. return data.readShort();
  1033. }
  1034. numDiv = 0xFFFF / 2;
  1035. }
  1036. // NORMAL
  1037. if ( this.attrib & 4 )
  1038. {
  1039. delta = data.readFloat();
  1040. this.normal = [];
  1041. i = 0;
  1042. while ( i < this.length ) {
  1043. this.normal[ i ++ ] = ( readNumber() / numDiv ) * delta;
  1044. }
  1045. }
  1046. // TANGENT
  1047. if ( this.attrib & 8 )
  1048. {
  1049. delta = data.readFloat();
  1050. this.tangent = [];
  1051. i = 0;
  1052. while ( i < this.length ) {
  1053. this.tangent[ i ++ ] = ( readNumber() / numDiv ) * delta;
  1054. }
  1055. }
  1056. // UV
  1057. if ( this.attrib & 32 ) {
  1058. this.uv = [];
  1059. this.uv.length = data.readUByte();
  1060. var uvLen = this.numVertex * 2;
  1061. i = 0;
  1062. while ( i < this.uv.length ) {
  1063. // UV VERTEX DATA
  1064. delta = data.readFloat();
  1065. this.uv[ i ++ ] = vec = [];
  1066. j = 0;
  1067. while ( j < uvLen ) {
  1068. vec[ j ++ ] = ( readNumber() / numDiv ) * delta;
  1069. }
  1070. }
  1071. }
  1072. // JOINT-INDEXES / WEIGHTS
  1073. if ( this.attrib & 64 ) {
  1074. this.jointPerVertex = data.readUByte();
  1075. var jntLen = this.numVertex * this.jointPerVertex;
  1076. this.joint = [];
  1077. this.weight = [];
  1078. i = 0;
  1079. while ( i < jntLen ) {
  1080. this.joint[ i ++ ] = data.readUInteger();
  1081. }
  1082. i = 0;
  1083. while ( i < jntLen ) {
  1084. this.weight[ i ++ ] = ( readNumber() / numDiv ) * 1;
  1085. }
  1086. }
  1087. // VERTEX_COLOR
  1088. if ( this.attrib & 128 ) {
  1089. var colorAttrib = data.readUByte(),
  1090. numColor = ( ( ( colorAttrib & 64 ) >> 6 ) | ( ( colorAttrib & 128 ) >> 6 ) ) + 1,
  1091. colorCount = this.numVertex * 4;
  1092. this.color = [];
  1093. this.color.length = colorAttrib & 15;
  1094. for ( i = 0; i < this.color.length; i ++ )
  1095. {
  1096. var vColor = [];
  1097. switch ( numColor )
  1098. {
  1099. case 1:
  1100. j = 0;
  1101. while ( j < colorCount )
  1102. {
  1103. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1104. vColor[ j ++ ] = 0;
  1105. vColor[ j ++ ] = 0;
  1106. vColor[ j ++ ] = 1;
  1107. }
  1108. break;
  1109. case 2:
  1110. j = 0;
  1111. while ( j < colorCount )
  1112. {
  1113. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1114. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1115. vColor[ j ++ ] = 0;
  1116. vColor[ j ++ ] = 1;
  1117. }
  1118. break;
  1119. case 3:
  1120. j = 0;
  1121. while ( j < colorCount )
  1122. {
  1123. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1124. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1125. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1126. vColor[ j ++ ] = 1;
  1127. }
  1128. break;
  1129. case 4:
  1130. j = 0;
  1131. while ( j < colorCount )
  1132. {
  1133. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1134. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1135. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1136. vColor[ j ++ ] = data.readUByte() / 0xFF;
  1137. }
  1138. break;
  1139. }
  1140. this.color[ i ] = vColor;
  1141. }
  1142. }
  1143. // VERTEX
  1144. delta = data.readFloat();
  1145. this.vertex = [];
  1146. i = 0;
  1147. while ( i < this.length ) {
  1148. this.vertex[ i ++ ] = ( readNumber() / numDiv ) * delta;
  1149. }
  1150. // SUB-MESHES
  1151. var count = data.readUByte();
  1152. this.indexes = vec = [];
  1153. this.groups = [];
  1154. // INDEXES
  1155. j = 0;
  1156. for ( i = 0; i < count; i ++ )
  1157. {
  1158. len = data.readVInt() * 3;
  1159. this.groups.push( {
  1160. start : j,
  1161. count : len,
  1162. } );
  1163. len += j;
  1164. while ( j < len ) {
  1165. vec[ j ++ ] = data.readVInt();
  1166. }
  1167. }
  1168. // SUB-MESHES
  1169. var count = data.readUByte();
  1170. this.indexes = vec = [];
  1171. this.groups = [];
  1172. // INDEXES
  1173. if ( this.attrib & 2 ) {
  1174. // POLYGON
  1175. for ( i = 0; i < count; i ++ ) {
  1176. len = data.readUInteger();
  1177. start = vec.length;
  1178. for ( j = 0; j < len; j ++ )
  1179. {
  1180. var a = data.readUInteger(),
  1181. b = data.readUInteger(),
  1182. c = data.readUInteger(),
  1183. d = data.readUInteger();
  1184. vec.push( a );
  1185. vec.push( b );
  1186. vec.push( c );
  1187. if ( d > 0 )
  1188. {
  1189. vec.push( c );
  1190. vec.push( d + 1 );
  1191. vec.push( a );
  1192. }
  1193. else continue;
  1194. }
  1195. this.groups.push( {
  1196. start : start,
  1197. count : vec.length - start,
  1198. } );
  1199. }
  1200. } else {
  1201. // TRIANGLE
  1202. j = 0;
  1203. for ( i = 0; i < count; i ++ )
  1204. {
  1205. len = data.readUInteger() * 3;
  1206. this.groups.push( {
  1207. start : j,
  1208. count : len,
  1209. } );
  1210. len += j;
  1211. while ( j < len ) {
  1212. vec[ j ++ ] = data.readUInteger();
  1213. }
  1214. }
  1215. }
  1216. }
  1217. SEA3D.GeometryDelta.prototype.type = "geDL";
  1218. //
  1219. // Object3D
  1220. //
  1221. SEA3D.Object3D =
  1222. {
  1223. read : function( scope ) {
  1224. var data = scope.data;
  1225. scope.isStatic = false;
  1226. scope.attrib = data.readUShort();
  1227. scope.tags = [];
  1228. if ( scope.attrib & 1 )
  1229. scope.parent = scope.sea.getObject( data.readUInt() );
  1230. if ( scope.attrib & 2 )
  1231. scope.animations = SEA3D.Animation.readAnimationList( data, scope.sea );
  1232. if ( scope.attrib & 4 )
  1233. scope.scripts = SEA3D.Script.readScriptList( data, scope.sea );
  1234. if ( scope.attrib & 16 )
  1235. scope.properties = scope.sea.getObject( data.readUInt() );
  1236. if ( scope.attrib & 32 ) {
  1237. var objectType = data.readUByte();
  1238. scope.isStatic = objectType & 1;
  1239. }
  1240. }
  1241. , readTags : function( scope, callback ) {
  1242. var data = scope.data,
  1243. numTag = data.readUByte();
  1244. for ( var i = 0; i < numTag; ++ i ) {
  1245. var kind = data.readUShort();
  1246. var size = data.readUInt();
  1247. var pos = data.position;
  1248. //not implemented
  1249. //readTag(kind, size)
  1250. data.position = pos += size;
  1251. }
  1252. }
  1253. }
  1254. //
  1255. // Entity3D
  1256. //
  1257. SEA3D.Entity3D =
  1258. {
  1259. read : function( scope ) {
  1260. SEA3D.Object3D.read( scope );
  1261. var data = scope.data;
  1262. scope.castShadows = true;
  1263. if ( scope.attrib & 64 ) {
  1264. var lightType = data.readUByte();
  1265. scope.castShadows = ( lightType & 1 ) == 0;
  1266. }
  1267. }
  1268. , readTags : function( scope, callback ) {
  1269. SEA3D.Object3D.readTags( scope, callback );
  1270. }
  1271. }
  1272. //
  1273. // Sound3D
  1274. //
  1275. SEA3D.Sound3D =
  1276. {
  1277. read : function( scope ) {
  1278. SEA3D.Object3D.read( scope );
  1279. var data = scope.data,
  1280. sea = scope.sea;
  1281. scope.autoPlay = ( scope.attrib & 64 ) != 0;
  1282. if ( scope.attrib & 128 )
  1283. scope.mixer = sea.getObject( data.readUInt() );
  1284. scope.sound = sea.getObject( data.readUInt() );
  1285. scope.volume = data.readFloat();
  1286. }
  1287. , readTags : function( scope, callback ) {
  1288. SEA3D.Object3D.readTags( scope, callback );
  1289. }
  1290. }
  1291. //
  1292. // Sound Point
  1293. //
  1294. SEA3D.SoundPoint = function( name, data, sea ) {
  1295. this.name = name;
  1296. this.data = data;
  1297. this.sea = sea;
  1298. SEA3D.Sound3D.read( this );
  1299. this.position = data.readVector3();
  1300. this.distance = data.readFloat();
  1301. SEA3D.Sound3D.readTags( this );
  1302. }
  1303. SEA3D.SoundPoint.prototype.type = "sp";
  1304. //
  1305. // Container3D
  1306. //
  1307. SEA3D.Container3D = function( name, data, sea ) {
  1308. this.name = name;
  1309. this.data = data;
  1310. this.sea = sea;
  1311. SEA3D.Object3D.read( this );
  1312. this.transform = data.readMatrix();
  1313. SEA3D.Object3D.readTags( this );
  1314. }
  1315. SEA3D.Container3D.prototype.type = "c3d";
  1316. //
  1317. // Texture URL
  1318. //
  1319. SEA3D.TextureURL = function( name, data, sea ) {
  1320. this.name = name;
  1321. this.data = data;
  1322. this.sea = sea;
  1323. this.url = data.readUTF( data.length );
  1324. }
  1325. SEA3D.TextureURL.prototype.type = "urlT";
  1326. //
  1327. // Actions
  1328. //
  1329. SEA3D.Actions = function( name, data, sea ) {
  1330. this.name = name;
  1331. this.data = data;
  1332. this.sea = sea;
  1333. this.count = data.readUInt();
  1334. this.action = [];
  1335. for ( var i = 0; i < this.count; i ++ ) {
  1336. var flag = data.readUByte();
  1337. var kind = data.readUShort();
  1338. var size = data.readUShort();
  1339. var position = data.position;
  1340. var act = this.action[ i ] = { kind: kind };
  1341. // range of animation
  1342. if ( flag & 1 )
  1343. {
  1344. // start and count in frames
  1345. act.range = [ data.readUInt(), data.readUInt() ];
  1346. }
  1347. // time
  1348. if ( flag & 2 )
  1349. {
  1350. act.time = data.readUInt();
  1351. }
  1352. // easing
  1353. if ( flag & 4 )
  1354. {
  1355. act.intrpl = SEA3D.DataTable.INTERPOLATION_TABLE[ data.readUByte() ];
  1356. if ( act.intrpl.indexOf( 'back.' ) == 0 )
  1357. {
  1358. act.intrplParam0 = data.readFloat();
  1359. }
  1360. else if ( act.intrpl.indexOf( 'elastic.' ) == 0 )
  1361. {
  1362. act.intrplParam0 = data.readFloat();
  1363. act.intrplParam1 = data.readFloat();
  1364. }
  1365. }
  1366. switch ( kind ) {
  1367. case SEA3D.Actions.RTT_TARGET:
  1368. act.source = sea.getObject( data.readUInt() );
  1369. act.target = sea.getObject( data.readUInt() );
  1370. break;
  1371. case SEA3D.Actions.LOOK_AT:
  1372. act.source = sea.getObject( data.readUInt() );
  1373. act.target = sea.getObject( data.readUInt() );
  1374. break;
  1375. case SEA3D.Actions.PLAY_SOUND:
  1376. act.sound = sea.getObject( data.readUInt() );
  1377. act.offset = data.readUInt();
  1378. break;
  1379. case SEA3D.Actions.PLAY_ANIMATION:
  1380. act.object = sea.getObject( data.readUInt() );
  1381. act.name = data.readUTF8();
  1382. break;
  1383. case SEA3D.Actions.FOG:
  1384. act.color = data.readUInt24();
  1385. act.min = data.readFloat();
  1386. act.max = data.readFloat();
  1387. break;
  1388. case SEA3D.Actions.ENVIRONMENT:
  1389. act.texture = sea.getObject( data.readUInt() );
  1390. break;
  1391. case SEA3D.Actions.ENVIRONMENT_COLOR:
  1392. act.color = data.readUInt24();
  1393. break;
  1394. case SEA3D.Actions.CAMERA:
  1395. act.camera = sea.getObject( data.readUInt() );
  1396. break;
  1397. default:
  1398. console.log( "Action \"" + kind + "\" not found." );
  1399. break;
  1400. }
  1401. data.position = position + size;
  1402. }
  1403. }
  1404. SEA3D.Actions.SCENE = 0;
  1405. SEA3D.Actions.ENVIRONMENT_COLOR = 1;
  1406. SEA3D.Actions.ENVIRONMENT = 2;
  1407. SEA3D.Actions.FOG = 3;
  1408. SEA3D.Actions.PLAY_ANIMATION = 4;
  1409. SEA3D.Actions.PLAY_SOUND = 5;
  1410. SEA3D.Actions.ANIMATION_AUDIO_SYNC = 6;
  1411. SEA3D.Actions.LOOK_AT = 7;
  1412. SEA3D.Actions.RTT_TARGET = 8;
  1413. SEA3D.Actions.CAMERA = 9;
  1414. SEA3D.Actions.prototype.type = "act";
  1415. //
  1416. // Script
  1417. //
  1418. SEA3D.Script =
  1419. {
  1420. DETAILED : false
  1421. , readScriptList : function( data, sea ) {
  1422. var list = [],
  1423. count = data.readUByte();
  1424. var i = 0;
  1425. while ( i < count )
  1426. {
  1427. var attrib = data.readUByte(),
  1428. numParams,
  1429. script = {};
  1430. script.priority = ( attrib & 1 ) | ( attrib & 2 );
  1431. if ( attrib & 4 )
  1432. {
  1433. var j, name;
  1434. numParams = data.readUByte();
  1435. if ( SEA3D.Script.DETAILED )
  1436. {
  1437. script.params = [];
  1438. for ( j = 0; j < numParams; j ++ )
  1439. {
  1440. name = data.readUTF8();
  1441. var type = data.readUByte();
  1442. script.params[ j ] =
  1443. {
  1444. name : name,
  1445. type : type,
  1446. data : SEA3D.DataTable.readToken( type, data, sea )
  1447. };
  1448. }
  1449. }
  1450. else
  1451. {
  1452. script.params = {};
  1453. for ( j = 0; j < numParams; j ++ )
  1454. {
  1455. name = data.readUTF8();
  1456. script.params[ name ] = SEA3D.DataTable.readObject( data, sea );
  1457. }
  1458. }
  1459. }
  1460. if ( attrib & 8 )
  1461. {
  1462. script.method = data.readUTF8();
  1463. }
  1464. script.tag = sea.getObject( data.readUInt() );
  1465. list[ i ++ ] = script;
  1466. }
  1467. return list;
  1468. }
  1469. }
  1470. //
  1471. // Properties
  1472. //
  1473. SEA3D.Properties = function( name, data, sea ) {
  1474. this.name = name;
  1475. this.data = data;
  1476. this.sea = sea;
  1477. this.tag = SEA3D.Properties.readProperties( data, sea.objects, name );
  1478. }
  1479. SEA3D.Properties.readProperties = function( data, sea, pname ) {
  1480. var count, i, type, name, attribs;
  1481. if ( SEA3D.Properties.DETAILED )
  1482. {
  1483. count = data.readUByte();
  1484. props = [];
  1485. for ( i = 0; i < count; i ++ )
  1486. {
  1487. name = data.readUTF8();
  1488. type = data.readUByte();
  1489. props[ i ] =
  1490. {
  1491. name : name,
  1492. type : type,
  1493. value : SEA3D.DataTable.readToken( type, data, sea )
  1494. };
  1495. }
  1496. }
  1497. else
  1498. {
  1499. count = data.readUByte();
  1500. props = {};
  1501. if ( pname )
  1502. props.__name__ = pname;
  1503. for ( i = 0; i < count; i ++ )
  1504. {
  1505. name = data.readUTF8();
  1506. props[ name ] = SEA3D.DataTable.readObject( data, sea );
  1507. }
  1508. }
  1509. return props;
  1510. }
  1511. SEA3D.Properties.DETAILED = false;
  1512. SEA3D.Properties.prototype.type = "prop";
  1513. //
  1514. // File Info
  1515. //
  1516. SEA3D.FileInfo = function( name, data, sea ) {
  1517. this.name = name;
  1518. this.data = data;
  1519. this.sea = sea;
  1520. sea.info = this.tag = SEA3D.Properties.readProperties( data, sea.objects, name );
  1521. }
  1522. SEA3D.FileInfo.prototype.type = "info";
  1523. //
  1524. // Java Script
  1525. //
  1526. SEA3D.JavaScript = function( name, data, sea ) {
  1527. this.name = name;
  1528. this.data = data;
  1529. this.sea = sea;
  1530. this.src = data.readUTF( data.length );
  1531. }
  1532. SEA3D.JavaScript.prototype.type = "js";
  1533. //
  1534. // Java Script Method
  1535. //
  1536. SEA3D.JavaScriptMethod = function( name, data, sea ) {
  1537. this.name = name;
  1538. this.data = data;
  1539. this.sea = sea;
  1540. this.count = data.readUShort();
  1541. this.methods = {};
  1542. for ( var i = 0; i < this.count; i ++ )
  1543. {
  1544. var flag = data.readUByte();
  1545. var method = data.readUTF8();
  1546. this.methods[ method ] = {
  1547. src : data.readUTF8Long()
  1548. }
  1549. }
  1550. }
  1551. SEA3D.JavaScriptMethod.prototype.type = "jsm";
  1552. //
  1553. // GLSL
  1554. //
  1555. SEA3D.GLSL = function( name, data, sea ) {
  1556. this.name = name;
  1557. this.data = data;
  1558. this.sea = sea;
  1559. this.src = data.readUTF( data.length );
  1560. }
  1561. SEA3D.GLSL.prototype.type = "glsl";
  1562. //
  1563. // Dummy
  1564. //
  1565. SEA3D.Dummy = function( name, data, sea ) {
  1566. this.name = name;
  1567. this.data = data;
  1568. this.sea = sea;
  1569. SEA3D.Object3D.read( this );
  1570. this.transform = data.readMatrix();
  1571. this.width = data.readFloat();
  1572. this.height = data.readFloat();
  1573. this.depth = data.readFloat();
  1574. SEA3D.Object3D.readTags( this );
  1575. }
  1576. SEA3D.Dummy.prototype.type = "dmy";
  1577. //
  1578. // Line
  1579. //
  1580. SEA3D.Line = function( name, data, sea ) {
  1581. this.name = name;
  1582. this.data = data;
  1583. this.sea = sea;
  1584. SEA3D.Object3D.read( this );
  1585. this.count = ( this.attrib & 64 ? data.readUInt() : data.readUShort() ) * 3;
  1586. this.closed = ( this.attrib & 128 ) != 0;
  1587. this.transform = data.readMatrix();
  1588. this.vertex = [];
  1589. var i = 0;
  1590. while ( i < this.count ) {
  1591. this.vertex[ i ++ ] = data.readFloat();
  1592. }
  1593. SEA3D.Object3D.readTags( this );
  1594. }
  1595. SEA3D.Line.prototype.type = "line";
  1596. //
  1597. // Planar Render
  1598. //
  1599. SEA3D.PlanarRender = function( name, data, sea ) {
  1600. this.name = name;
  1601. this.data = data;
  1602. this.sea = sea;
  1603. this.attrib = data.readUByte();
  1604. this.quality = ( this.attrib & 1 ) | ( this.attrib & 2 );
  1605. this.transform = data.readMatrix();
  1606. }
  1607. SEA3D.PlanarRender.prototype.type = "rttp";
  1608. //
  1609. // Cube Render
  1610. //
  1611. SEA3D.CubeRender = function( name, data, sea ) {
  1612. this.name = name;
  1613. this.data = data;
  1614. this.sea = sea;
  1615. this.attrib = data.readUByte();
  1616. this.quality = ( this.attrib & 1 ) | ( this.attrib & 2 );
  1617. this.position = data.readVector3();
  1618. }
  1619. SEA3D.CubeRender.prototype.type = "rttc";
  1620. //
  1621. // Mesh2D
  1622. //
  1623. SEA3D.Mesh2D = function( name, data, sea ) {
  1624. this.name = name;
  1625. this.data = data;
  1626. this.sea = sea;
  1627. SEA3D.Object3D.read( this );
  1628. if ( this.attrib & 256 )
  1629. this.material = sea.getObject( data.readUInt() );
  1630. this.position = data.readVector3();
  1631. this.width = data.readFloat();
  1632. this.height = data.readFloat();
  1633. SEA3D.Object3D.readTags( this );
  1634. }
  1635. SEA3D.Mesh2D.prototype.type = "m2d";
  1636. //
  1637. // Mesh
  1638. //
  1639. SEA3D.Mesh = function( name, data, sea ) {
  1640. this.name = name;
  1641. this.data = data;
  1642. this.sea = sea;
  1643. SEA3D.Entity3D.read( this );
  1644. // MATERIAL
  1645. if ( this.attrib & 256 ) {
  1646. this.material = [];
  1647. var len = data.readUByte();
  1648. if ( len == 1 ) this.material[ 0 ] = sea.getObject( data.readUInt() );
  1649. else
  1650. {
  1651. var i = 0;
  1652. while ( i < len )
  1653. {
  1654. var matIndex = data.readUInt();
  1655. if ( matIndex > 0 ) this.material[ i ++ ] = sea.getObject( matIndex - 1 );
  1656. else this.material[ i ++ ] = undefined;
  1657. }
  1658. }
  1659. }
  1660. if ( this.attrib & 512 ) {
  1661. this.modifiers = [];
  1662. var len = data.readUByte();
  1663. for ( var i = 0; i < len; i ++ )
  1664. this.modifiers[ i ] = sea.getObject( data.readUInt() );
  1665. }
  1666. this.transform = data.readMatrix();
  1667. this.geometry = sea.getObject( data.readUInt() );
  1668. SEA3D.Entity3D.readTags( this );
  1669. }
  1670. SEA3D.Mesh.prototype.type = "m3d";
  1671. //
  1672. // Skeleton
  1673. //
  1674. SEA3D.Skeleton = function( name, data, sea ) {
  1675. this.name = name;
  1676. this.data = data;
  1677. this.sea = sea;
  1678. var length = data.readUShort();
  1679. this.joint = [];
  1680. for ( var i = 0; i < length; i ++ ) {
  1681. this.joint[ i ] = {
  1682. name: data.readUTF8(),
  1683. parentIndex: data.readUShort() - 1,
  1684. inverseBindMatrix: data.readMatrix()
  1685. }
  1686. }
  1687. }
  1688. SEA3D.Skeleton.prototype.type = "skl";
  1689. //
  1690. // Skeleton Local
  1691. //
  1692. SEA3D.SkeletonLocal = function( name, data, sea ) {
  1693. this.name = name;
  1694. this.data = data;
  1695. this.sea = sea;
  1696. var length = data.readUShort();
  1697. this.joint = [];
  1698. for ( var i = 0; i < length; i ++ ) {
  1699. this.joint[ i ] = {
  1700. name: data.readUTF8(),
  1701. parentIndex: data.readUShort() - 1,
  1702. // POSITION XYZ
  1703. x: data.readFloat(),
  1704. y: data.readFloat(),
  1705. z: data.readFloat(),
  1706. // QUATERNION XYZW
  1707. qx: data.readFloat(),
  1708. qy: data.readFloat(),
  1709. qz: data.readFloat(),
  1710. qw: data.readFloat()
  1711. }
  1712. }
  1713. }
  1714. SEA3D.SkeletonLocal.prototype.type = "sklq";
  1715. //
  1716. // Animation Base
  1717. //
  1718. SEA3D.AnimationBase =
  1719. {
  1720. read : function( scope ) {
  1721. var data = scope.data, flag = data.readUByte();
  1722. scope.sequence = [];
  1723. if ( flag & 1 ) {
  1724. var count = data.readUShort();
  1725. for ( var i = 0; i < count; i ++ ) {
  1726. flag = data.readUByte();
  1727. scope.sequence[ i ] = {
  1728. name: data.readUTF8(),
  1729. start: data.readUInt(),
  1730. count: data.readUInt(),
  1731. repeat: ( flag & 1 ) != 0,
  1732. intrpl: ( flag & 2 ) != 0
  1733. }
  1734. }
  1735. }
  1736. scope.frameRate = data.readUByte();
  1737. scope.numFrames = data.readUInt();
  1738. // no contains sequence
  1739. if ( scope.sequence.length == 0 )
  1740. scope.sequence[ 0 ] = { name: "root", start: 0, count: scope.numFrames, repeat: true, intrpl: true };
  1741. }
  1742. }
  1743. //
  1744. // Animation
  1745. //
  1746. SEA3D.Animation = function( name, data, sea ) {
  1747. this.name = name;
  1748. this.data = data;
  1749. this.sea = sea;
  1750. SEA3D.AnimationBase.read( this );
  1751. this.dataList = [];
  1752. this.dataList.length = data.readUByte();
  1753. for ( var i = 0; i < this.dataList.length; i ++ ) {
  1754. var kind = data.readUShort(),
  1755. type = data.readUByte(),
  1756. anmRaw = [];
  1757. SEA3D.DataTable.readVector( type, data, anmRaw, this.numFrames, 0 );
  1758. this.dataList[ i ] = {
  1759. kind: kind,
  1760. type: type,
  1761. blockSize: SEA3D.DataTable.sizeOf( type ),
  1762. data: anmRaw
  1763. }
  1764. }
  1765. }
  1766. SEA3D.Animation.POSITION = 0;
  1767. SEA3D.Animation.ROTATION = 1;
  1768. SEA3D.Animation.SCALE = 2;
  1769. SEA3D.Animation.COLOR = 3;
  1770. SEA3D.Animation.MULTIPLIER = 4;
  1771. SEA3D.Animation.ATTENUATION_START = 5;
  1772. SEA3D.Animation.ATTENUATION_END = 6;
  1773. SEA3D.Animation.FOV = 7;
  1774. SEA3D.Animation.OFFSET_U = 8;
  1775. SEA3D.Animation.OFFSET_V = 9;
  1776. SEA3D.Animation.SCALE_U = 10;
  1777. SEA3D.Animation.SCALE_V = 11;
  1778. SEA3D.Animation.ANGLE = 12;
  1779. SEA3D.Animation.ALPHA = 13;
  1780. SEA3D.Animation.VOLUME = 14;
  1781. SEA3D.Animation.DefaultLerpFuncs = [
  1782. SEA3D.Math.lerp3x, // POSITION
  1783. SEA3D.Math.lerpQuat4x, // ROTATION
  1784. SEA3D.Math.lerp3x, // SCALE
  1785. SEA3D.Math.lerpColor1x, // COLOR
  1786. SEA3D.Math.lerp1x, // MULTIPLIER
  1787. SEA3D.Math.lerp1x, // ATTENUATION_START
  1788. SEA3D.Math.lerp1x, // ATTENUATION_END
  1789. SEA3D.Math.lerp1x, // FOV
  1790. SEA3D.Math.lerp1x, // OFFSET_U
  1791. SEA3D.Math.lerp1x, // OFFSET_V
  1792. SEA3D.Math.lerp1x, // SCALE_U
  1793. SEA3D.Math.lerp1x, // SCALE_V
  1794. SEA3D.Math.lerpAng1x, // ANGLE
  1795. SEA3D.Math.lerp1x, // ALPHA
  1796. SEA3D.Math.lerp1x // VOLUME
  1797. ]
  1798. SEA3D.Animation.readAnimationList = function( data, sea ) {
  1799. var list = [],
  1800. count = data.readUByte();
  1801. var i = 0;
  1802. while ( i < count ) {
  1803. var attrib = data.readUByte(),
  1804. anm = {};
  1805. anm.relative = ( attrib & 1 ) != 0;
  1806. if ( attrib & 2 )
  1807. anm.timeScale = data.readFloat();
  1808. anm.tag = sea.getObject( data.readUInt() );
  1809. list[ i ++ ] = anm;
  1810. }
  1811. return list;
  1812. }
  1813. SEA3D.Animation.prototype.type = "anm";
  1814. //
  1815. // Skeleton Animation
  1816. //
  1817. SEA3D.SkeletonAnimation = function( name, data, sea ) {
  1818. this.name = name;
  1819. this.data = data;
  1820. this.sea = sea;
  1821. var i, j, count, joint;
  1822. SEA3D.AnimationBase.read( this );
  1823. count = data.readUShort()
  1824. this.pose = [];
  1825. for ( i = 0; i < this.numFrames; i ++ ) {
  1826. joint = [];
  1827. joint.length = count;
  1828. for ( j = 0; j < count; j ++ ) {
  1829. joint[ j ] = {
  1830. // POSITION XYZ
  1831. x: data.readFloat(),
  1832. y: data.readFloat(),
  1833. z: data.readFloat(),
  1834. // QUATERNION XYZW
  1835. qx: data.readFloat(),
  1836. qy: data.readFloat(),
  1837. qz: data.readFloat(),
  1838. qw: data.readFloat()
  1839. }
  1840. }
  1841. this.pose[ i ] = joint;
  1842. }
  1843. }
  1844. SEA3D.SkeletonAnimation.prototype.type = "skla";
  1845. //
  1846. // Morph
  1847. //
  1848. SEA3D.Morph = function( name, data, sea ) {
  1849. this.name = name;
  1850. this.data = data;
  1851. this.sea = sea;
  1852. SEA3D.GeometryBase( this );
  1853. var useVertex = ( this.attrib & 2 ) != 0;
  1854. var useNormal = ( this.attrib & 4 ) != 0;
  1855. var nodeCount = data.readUShort();
  1856. this.node = [];
  1857. var i, j;
  1858. for ( i = 0; i < nodeCount; i ++ ) {
  1859. var nodeName = data.readUTF8();
  1860. if ( useVertex ) {
  1861. var verts = [];
  1862. j = 0;
  1863. while ( j < this.length ) {
  1864. verts[ j ++ ] = data.readFloat();
  1865. }
  1866. }
  1867. if ( useNormal ) {
  1868. var norms = [];
  1869. j = 0;
  1870. while ( j < this.length ) {
  1871. norms[ j ++ ] = data.readFloat();
  1872. }
  1873. }
  1874. this.node[ i ] = { vertex: verts, normal: norms, name: nodeName }
  1875. }
  1876. }
  1877. SEA3D.Morph.prototype.type = "mph";
  1878. //
  1879. // Vertex Animation
  1880. //
  1881. SEA3D.VertexAnimation = function( name, data, sea ) {
  1882. this.name = name;
  1883. this.data = data;
  1884. this.sea = sea;
  1885. SEA3D.AnimationBase.read( this );
  1886. var flags = data.readUByte();
  1887. this.isBigMesh = ( flags & 1 ) != 0;
  1888. data.readVInt = this.isBigMesh ? data.readUInt : data.readUShort;
  1889. this.numVertex = data.readVInt();
  1890. this.length = this.numVertex * 3;
  1891. var useVertex = ( flags & 2 ) != 0;
  1892. var useNormal = ( flags & 4 ) != 0;
  1893. this.frame = [];
  1894. var i, j;
  1895. for ( i = 0; i < this.numFrames; i ++ ) {
  1896. if ( useVertex ) {
  1897. var verts = [];
  1898. j = 0;
  1899. while ( j < this.length ) {
  1900. verts[ j ++ ] = data.readFloat();
  1901. }
  1902. }
  1903. if ( useNormal ) {
  1904. var norms = [];
  1905. j = 0;
  1906. while ( j < this.length ) {
  1907. norms[ j ++ ] = data.readFloat();
  1908. }
  1909. }
  1910. this.frame[ i ] = { vertex: verts, normal: norms }
  1911. }
  1912. }
  1913. SEA3D.VertexAnimation.prototype.type = "vtxa";
  1914. //
  1915. // Camera
  1916. //
  1917. SEA3D.Camera = function( name, data, sea ) {
  1918. this.name = name;
  1919. this.data = data;
  1920. this.sea = sea;
  1921. SEA3D.Object3D.read( this );
  1922. if ( this.attrib & 64 ) {
  1923. this.dof = {
  1924. distance: data.readFloat(),
  1925. range: data.readFloat()
  1926. }
  1927. }
  1928. this.transform = data.readMatrix();
  1929. this.fov = data.readFloat();
  1930. SEA3D.Object3D.readTags( this );
  1931. }
  1932. SEA3D.Camera.prototype.type = "cam";
  1933. //
  1934. // Joint Object
  1935. //
  1936. SEA3D.JointObject = function( name, data, sea ) {
  1937. this.name = name;
  1938. this.data = data;
  1939. this.sea = sea;
  1940. SEA3D.Object3D.read( this );
  1941. this.target = sea.getObject( data.readUInt() );
  1942. this.joint = data.readUShort();
  1943. SEA3D.Object3D.readTags( this );
  1944. }
  1945. SEA3D.JointObject.prototype.type = "jnt";
  1946. //
  1947. // Light
  1948. //
  1949. SEA3D.Light = {
  1950. read : function( scope ) {
  1951. SEA3D.Object3D.read( scope );
  1952. var data = scope.data;
  1953. scope.attenStart = Number.MAX_VALUE;
  1954. scope.attenEnd = Number.MAX_VALUE;
  1955. if ( scope.attrib & 64 ) {
  1956. var shadowHeader = data.readUByte();
  1957. scope.shadow = {}
  1958. scope.shadow.opacity = shadowHeader & 1 ? data.readFloat() : 1;
  1959. scope.shadow.color = shadowHeader & 2 ? data.readUInt24() : 0x000000;
  1960. }
  1961. if ( scope.attrib & 512 ) {
  1962. scope.attenStart = data.readFloat();
  1963. scope.attenEnd = data.readFloat();
  1964. }
  1965. scope.color = data.readUInt24();
  1966. scope.multiplier = data.readFloat();
  1967. }
  1968. }
  1969. //
  1970. // Point Light
  1971. //
  1972. SEA3D.PointLight = function( name, data, sea ) {
  1973. this.name = name;
  1974. this.data = data;
  1975. this.sea = sea;
  1976. SEA3D.Light.read( this );
  1977. if ( this.attrib & 128 ) {
  1978. this.attenuation = {
  1979. start: data.readFloat(),
  1980. end: data.readFloat()
  1981. }
  1982. }
  1983. this.position = data.readVector3();
  1984. SEA3D.Object3D.readTags( this );
  1985. }
  1986. SEA3D.PointLight.prototype.type = "plht";
  1987. //
  1988. // Hemisphere Light
  1989. //
  1990. SEA3D.HemisphereLight = function( name, data, sea ) {
  1991. this.name = name;
  1992. this.data = data;
  1993. this.sea = sea;
  1994. SEA3D.Light.read( this );
  1995. if ( this.attrib & 128 ) {
  1996. this.attenuation = {
  1997. start: data.readFloat(),
  1998. end: data.readFloat()
  1999. }
  2000. }
  2001. this.secondColor = data.readUInt24();
  2002. SEA3D.Object3D.readTags( this );
  2003. }
  2004. SEA3D.HemisphereLight.prototype.type = "hlht";
  2005. //
  2006. // Directional Light
  2007. //
  2008. SEA3D.DirectionalLight = function( name, data, sea ) {
  2009. this.name = name;
  2010. this.data = data;
  2011. this.sea = sea;
  2012. SEA3D.Light.read( this );
  2013. this.transform = data.readMatrix();
  2014. SEA3D.Object3D.readTags( this );
  2015. }
  2016. SEA3D.DirectionalLight.prototype.type = "dlht";
  2017. //
  2018. // Material
  2019. //
  2020. SEA3D.Material = function( name, data, sea ) {
  2021. this.name = name;
  2022. this.data = data;
  2023. this.sea = sea;
  2024. this.technique = [];
  2025. this.attrib = data.readUShort();
  2026. this.alpha = 1;
  2027. this.blendMode = "normal";
  2028. this.alphaThreshold = .5;
  2029. this.bothSides = ( this.attrib & 1 ) != 0;
  2030. this.receiveLights = ( this.attrib & 2 ) == 0;
  2031. this.receiveShadows = ( this.attrib & 4 ) == 0;
  2032. this.receiveFog = ( this.attrib & 8 ) == 0;
  2033. this.smooth = ( this.attrib & 16 ) == 0;
  2034. if ( this.attrib & 32 )
  2035. this.alpha = data.readFloat();
  2036. if ( this.attrib & 64 )
  2037. this.blendMode = data.readBlendMode();
  2038. if ( this.attrib & 128 )
  2039. this.animations = SEA3D.Animation.readAnimationList( data, sea );
  2040. this.depthMask = ( this.attrib & 256 ) == 0;
  2041. var count = data.readUByte();
  2042. for ( var i = 0; i < count; ++ i ) {
  2043. var kind = data.readUShort();
  2044. var size = data.readUShort();
  2045. var pos = data.position;
  2046. var tech, methodAttrib;
  2047. switch ( kind ) {
  2048. case SEA3D.Material.DEFAULT:
  2049. tech = {
  2050. ambientColor: data.readUInt24(),
  2051. diffuseColor: data.readUInt24(),
  2052. specularColor: data.readUInt24(),
  2053. specular: data.readFloat(),
  2054. gloss: data.readFloat()
  2055. }
  2056. break;
  2057. case SEA3D.Material.COMPOSITE_TEXTURE:
  2058. tech = {
  2059. composite: sea.getObject( data.readUInt() )
  2060. }
  2061. break;
  2062. case SEA3D.Material.DIFFUSE_MAP:
  2063. tech = {
  2064. texture: sea.getObject( data.readUInt() )
  2065. }
  2066. break;
  2067. case SEA3D.Material.SPECULAR_MAP:
  2068. tech = {
  2069. texture: sea.getObject( data.readUInt() )
  2070. }
  2071. break;
  2072. case SEA3D.Material.NORMAL_MAP:
  2073. tech = {
  2074. texture: sea.getObject( data.readUInt() )
  2075. }
  2076. break;
  2077. case SEA3D.Material.REFLECTION:
  2078. case SEA3D.Material.FRESNEL_REFLECTION:
  2079. tech = {
  2080. texture: sea.getObject( data.readUInt() ),
  2081. alpha: data.readFloat()
  2082. }
  2083. if ( kind == SEA3D.Material.FRESNEL_REFLECTION ) {
  2084. tech.power = data.readFloat();
  2085. tech.normal = data.readFloat();
  2086. }
  2087. break;
  2088. case SEA3D.Material.REFRACTION:
  2089. tech = {
  2090. texture: sea.getObject( data.readUInt() ),
  2091. alpha: data.readFloat(),
  2092. ior: data.readFloat()
  2093. }
  2094. break;
  2095. case SEA3D.Material.RIM:
  2096. tech = {
  2097. color: data.readUInt24(),
  2098. strength: data.readFloat(),
  2099. power: data.readFloat(),
  2100. blendMode: data.readBlendMode()
  2101. }
  2102. break;
  2103. case SEA3D.Material.LIGHT_MAP:
  2104. tech = {
  2105. texture: sea.getObject( data.readUInt() ),
  2106. channel: data.readUByte(),
  2107. blendMode: data.readBlendMode()
  2108. }
  2109. break;
  2110. case SEA3D.Material.DETAIL_MAP:
  2111. tech = {
  2112. texture: sea.getObject( data.readUInt() ),
  2113. scale: data.readFloat(),
  2114. blendMode: data.readBlendMode()
  2115. }
  2116. break;
  2117. case SEA3D.Material.CEL:
  2118. tech = {
  2119. color: data.readUInt24(),
  2120. levels: data.readUByte(),
  2121. size: data.readFloat(),
  2122. specularCutOff: data.readFloat(),
  2123. smoothness: data.readFloat()
  2124. }
  2125. break;
  2126. case SEA3D.Material.TRANSLUCENT:
  2127. tech = {
  2128. color: data.readUInt24(),
  2129. translucency: data.readFloat(),
  2130. scattering: data.readFloat()
  2131. }
  2132. break;
  2133. case SEA3D.Material.BLEND_NORMAL_MAP:
  2134. methodAttrib = data.readUByte();
  2135. tech = {
  2136. texture: sea.getObject( data.readUInt() ),
  2137. secondaryTexture: sea.getObject( data.readUInt() )
  2138. }
  2139. if ( methodAttrib & 1 )
  2140. {
  2141. tech.offsetX0 = data.readFloat();
  2142. tech.offsetY0 = data.readFloat();
  2143. tech.offsetX1 = data.readFloat();
  2144. tech.offsetY1 = data.readFloat();
  2145. }
  2146. else
  2147. {
  2148. tech.offsetX0 = tech.offsetY0 =
  2149. tech.offsetX1 = tech.offsetY1 = 0
  2150. }
  2151. tech.animate = methodAttrib & 2;
  2152. break;
  2153. case SEA3D.Material.MIRROR_REFLECTION:
  2154. tech = {
  2155. texture: sea.getObject( data.readUInt() ),
  2156. alpha: data.readFloat()
  2157. }
  2158. break;
  2159. case SEA3D.Material.AMBIENT_MAP:
  2160. tech = {
  2161. texture: sea.getObject( data.readUInt() )
  2162. }
  2163. break;
  2164. case SEA3D.Material.ALPHA_MAP:
  2165. tech = {
  2166. texture: sea.getObject( data.readUInt() )
  2167. }
  2168. break;
  2169. case SEA3D.Material.EMISSIVE_MAP:
  2170. tech = {
  2171. texture: sea.getObject( data.readUInt() )
  2172. }
  2173. break;
  2174. case SEA3D.Material.VERTEX_COLOR:
  2175. tech = {
  2176. blendMode: data.readBlendMode()
  2177. }
  2178. break;
  2179. case SEA3D.Material.WRAP_LIGHTING:
  2180. tech = {
  2181. color: data.readUInt24(),
  2182. strength: data.readFloat()
  2183. }
  2184. break;
  2185. case SEA3D.Material.COLOR_REPLACE:
  2186. methodAttrib = data.readUByte();
  2187. tech = {
  2188. red: data.readUInt24(),
  2189. green: data.readUInt24(),
  2190. blue: data.readUInt24()
  2191. }
  2192. if ( methodAttrib & 1 )
  2193. tech.mask = sea.getObject( data.readUInt() );
  2194. if ( methodAttrib & 2 )
  2195. tech.alpha = data.readFloat();
  2196. break;
  2197. case SEA3D.Material.REFLECTION_SPHERICAL:
  2198. tech = {
  2199. texture: sea.getObject( data.readUInt() ),
  2200. alpha: data.readFloat()
  2201. }
  2202. break;
  2203. default:
  2204. console.warn( "SEA3D: MaterialTechnique not found:", kind.toString( 16 ) );
  2205. data.position = pos += size;
  2206. continue;
  2207. }
  2208. tech.kind = kind;
  2209. this.technique.push( tech );
  2210. data.position = pos += size;
  2211. }
  2212. }
  2213. SEA3D.Material.DEFAULT = 0;
  2214. SEA3D.Material.COMPOSITE_TEXTURE = 1;
  2215. SEA3D.Material.DIFFUSE_MAP = 2;
  2216. SEA3D.Material.SPECULAR_MAP = 3;
  2217. SEA3D.Material.REFLECTION = 4;
  2218. SEA3D.Material.REFRACTION = 5;
  2219. SEA3D.Material.NORMAL_MAP = 6;
  2220. SEA3D.Material.FRESNEL_REFLECTION = 7;
  2221. SEA3D.Material.RIM = 8;
  2222. SEA3D.Material.LIGHT_MAP = 9;
  2223. SEA3D.Material.DETAIL_MAP = 10;
  2224. SEA3D.Material.CEL = 11;
  2225. SEA3D.Material.TRANSLUCENT = 12;
  2226. SEA3D.Material.BLEND_NORMAL_MAP = 13;
  2227. SEA3D.Material.MIRROR_REFLECTION = 14;
  2228. SEA3D.Material.AMBIENT_MAP = 15;
  2229. SEA3D.Material.ALPHA_MAP = 16;
  2230. SEA3D.Material.EMISSIVE_MAP = 17;
  2231. SEA3D.Material.VERTEX_COLOR = 18;
  2232. SEA3D.Material.WRAP_LIGHTING = 19;
  2233. SEA3D.Material.COLOR_REPLACE = 20;
  2234. SEA3D.Material.REFLECTION_SPHERICAL = 21;
  2235. SEA3D.Material.prototype.type = "mat";
  2236. //
  2237. // Composite
  2238. //
  2239. SEA3D.Composite = function( name, data, sea ) {
  2240. this.name = name;
  2241. this.data = data;
  2242. this.sea = sea;
  2243. var layerCount = data.readUByte();
  2244. this.layer = [];
  2245. for ( var i = 0; i < layerCount; i ++ )
  2246. this.layer[ i ] = this.readLayer( data, this );
  2247. }
  2248. SEA3D.Composite.prototype.getLayerByName = function( name ) {
  2249. for ( var i = 0; i < this.layer.length; i ++ ) {
  2250. if ( this.layer[ i ].name == name )
  2251. return this.layer[ i ];
  2252. }
  2253. }
  2254. SEA3D.Composite.prototype.readLayer = function( data, scope ) {
  2255. this.scope = scope;
  2256. var out = {
  2257. blendMode: "normal",
  2258. opacity: 1
  2259. }
  2260. var attrib = data.readUShort();
  2261. if ( attrib & 1 ) out.texture = this.readLayerBitmap( data, scope );
  2262. else out.color = data.readUInt24();
  2263. if ( attrib & 2 )
  2264. out.mask = this.readLayerBitmap( data, scope );
  2265. if ( attrib & 4 )
  2266. out.name = data.readUTF8();
  2267. if ( attrib & 8 )
  2268. out.blendMode = data.readBlendMode();
  2269. if ( attrib & 16 )
  2270. out.opacity = data.readFloat();
  2271. return out;
  2272. }
  2273. SEA3D.Composite.prototype.readLayerBitmap = function( data, scope ) {
  2274. this.scope = scope;
  2275. var out = {
  2276. channel: 0,
  2277. repeat: true,
  2278. offsetU: 0,
  2279. offsetV: 0,
  2280. scaleU: 0,
  2281. scaleV: 0,
  2282. rotation: 0
  2283. }
  2284. out.map = scope.sea.getObject( data.readUInt() );
  2285. var attrib = data.readUShort();
  2286. if ( attrib > 0 ) {
  2287. if ( attrib & 1 )
  2288. out.channel = data.readUByte();
  2289. if ( attrib & 2 )
  2290. out.repeat = false;
  2291. if ( attrib & 4 )
  2292. out.offsetU = data.readFloat();
  2293. if ( attrib & 8 )
  2294. out.offsetV = data.readFloat();
  2295. if ( attrib & 16 )
  2296. out.scaleU = data.readFloat();
  2297. if ( attrib & 32 )
  2298. out.scaleV = data.readFloat();
  2299. if ( attrib & 64 )
  2300. out.rotation = data.readFloat();
  2301. if ( attrib & 128 )
  2302. out.animation = SEA3D.Animation.readAnimationList( data, scope.sea );
  2303. }
  2304. return out;
  2305. }
  2306. SEA3D.Composite.prototype.type = "ctex";
  2307. //
  2308. // Sphere
  2309. //
  2310. SEA3D.Sphere = function( name, data, sea ) {
  2311. this.name = name;
  2312. this.data = data;
  2313. this.sea = sea;
  2314. this.radius = data.readFloat();
  2315. }
  2316. SEA3D.Sphere.prototype.type = "sph";
  2317. //
  2318. // Box
  2319. //
  2320. SEA3D.Box = function( name, data, sea ) {
  2321. this.name = name;
  2322. this.data = data;
  2323. this.sea = sea;
  2324. this.width = data.readFloat();
  2325. this.height = data.readFloat();
  2326. this.depth = data.readFloat();
  2327. }
  2328. SEA3D.Box.prototype.type = "box";
  2329. //
  2330. // Cone
  2331. //
  2332. SEA3D.Cone = function( name, data, sea ) {
  2333. this.name = name;
  2334. this.data = data;
  2335. this.sea = sea;
  2336. this.radius = data.readFloat();
  2337. this.height = data.readFloat();
  2338. }
  2339. SEA3D.Cone.prototype.type = "cone";
  2340. //
  2341. // Capsule
  2342. //
  2343. SEA3D.Capsule = function( name, data, sea ) {
  2344. this.name = name;
  2345. this.data = data;
  2346. this.sea = sea;
  2347. this.radius = data.readFloat();
  2348. this.height = data.readFloat();
  2349. }
  2350. SEA3D.Capsule.prototype.type = "cap";
  2351. //
  2352. // Cylinder
  2353. //
  2354. SEA3D.Cylinder = function( name, data, sea ) {
  2355. this.name = name;
  2356. this.data = data;
  2357. this.sea = sea;
  2358. this.radius = data.readFloat();
  2359. this.height = data.readFloat();
  2360. }
  2361. SEA3D.Cylinder.prototype.type = "cyl";
  2362. //
  2363. // Geometry Shape
  2364. //
  2365. SEA3D.GeometryShape = function( name, data, sea ) {
  2366. this.name = name;
  2367. this.data = data;
  2368. this.sea = sea;
  2369. this.geometry = sea.getObject( data.readUInt() );
  2370. this.subGeometryIndex = data.readUByte();
  2371. }
  2372. SEA3D.GeometryShape.prototype.type = "gs";
  2373. //
  2374. // Static Geometry Shape
  2375. //
  2376. SEA3D.StaticGeometryShape = function( name, data, sea ) {
  2377. this.name = name;
  2378. this.data = data;
  2379. this.sea = sea;
  2380. this.geometry = sea.getObject( data.readUInt() );
  2381. this.subGeometryIndex = data.readUByte();
  2382. }
  2383. SEA3D.StaticGeometryShape.prototype.type = "sgs";
  2384. //
  2385. // Physics
  2386. //
  2387. SEA3D.Physics =
  2388. {
  2389. read : function( scope ) {
  2390. var data = scope.data,
  2391. sea = scope.sea;
  2392. scope.attrib = data.readUShort();
  2393. scope.shape = sea.getObject( data.readUInt() );
  2394. if ( scope.attrib & 1 )
  2395. scope.target = sea.getObject( data.readUInt() );
  2396. else
  2397. scope.transform = data.readMatrix();
  2398. }
  2399. , readTags : function( scope, readTag ) {
  2400. var data = scope.data,
  2401. numTag = data.readUByte();
  2402. for ( var i = 0; i < numTag; ++ i ) {
  2403. var kind = data.readUShort();
  2404. var size = data.readUInt();
  2405. var pos = data.position;
  2406. if ( readTag !== undefined ) {
  2407. readTag( kind, data, size )
  2408. }
  2409. data.position = pos += size;
  2410. }
  2411. }
  2412. }
  2413. //
  2414. // Rigidy Body
  2415. //
  2416. SEA3D.RigidBody = function( name, data, sea ) {
  2417. this.name = name;
  2418. this.data = data;
  2419. this.sea = sea;
  2420. SEA3D.Physics.read( this );
  2421. if ( this.attrib & 32 ) {
  2422. this.linearDamping = data.readFloat();
  2423. this.angularDamping = data.readFloat();
  2424. } else {
  2425. this.linearDamping = 0;
  2426. this.angularDamping = 0;
  2427. }
  2428. this.mass = data.readFloat();
  2429. this.friction = data.readFloat();
  2430. this.restitution = data.readFloat();
  2431. }
  2432. SEA3D.RigidBody.prototype.type = "rb";
  2433. //
  2434. // Cube Maps
  2435. //
  2436. SEA3D.CubeMap = function( name, data, sea ) {
  2437. this.name = name;
  2438. this.data = data;
  2439. this.sea = sea;
  2440. this.transparent = false;
  2441. var ext = data.readExt();
  2442. this.faces = [];
  2443. for ( var i = 0; i < 6; i ++ ) {
  2444. var size = data.readUInt();
  2445. this.faces[ i ] = data.concat( data.position, size );
  2446. data.position += size;
  2447. }
  2448. }
  2449. SEA3D.CubeMap.prototype.type = "cmap";
  2450. //
  2451. // JPEG
  2452. //
  2453. SEA3D.JPEG = function( name, data, sea ) {
  2454. this.name = name;
  2455. this.data = data;
  2456. this.sea = sea;
  2457. this.transparent = false;
  2458. }
  2459. SEA3D.JPEG.prototype.type = "jpg";
  2460. //
  2461. // JPEG_XR
  2462. //
  2463. SEA3D.JPEG_XR = function( name, data, sea ) {
  2464. this.name = name;
  2465. this.data = data;
  2466. this.sea = sea;
  2467. this.transparent = true;
  2468. }
  2469. SEA3D.JPEG_XR.prototype.type = "wdp";
  2470. //
  2471. // PNG
  2472. //
  2473. SEA3D.PNG = function( name, data, sea ) {
  2474. this.name = name;
  2475. this.data = data;
  2476. this.sea = sea;
  2477. this.transparent = data.buffer[ 25 ] == 0x06;
  2478. }
  2479. SEA3D.PNG.prototype.type = "png";
  2480. //
  2481. // GIF
  2482. //
  2483. SEA3D.GIF = function( name, data, sea ) {
  2484. this.name = name;
  2485. this.data = data;
  2486. this.sea = sea;
  2487. this.transparent = data.buffer[ 11 ] > 0;
  2488. }
  2489. SEA3D.GIF.prototype.type = "gif";
  2490. //
  2491. // OGG
  2492. //
  2493. SEA3D.OGG = function( name, data, sea ) {
  2494. this.name = name;
  2495. this.data = data;
  2496. this.sea = sea;
  2497. }
  2498. SEA3D.OGG.prototype.type = "ogg";
  2499. //
  2500. // MP3
  2501. //
  2502. SEA3D.MP3 = function( name, data, sea ) {
  2503. this.name = name;
  2504. this.data = data;
  2505. this.sea = sea;
  2506. }
  2507. SEA3D.MP3.prototype.type = "mp3";
  2508. //
  2509. // FILE FORMAT
  2510. //
  2511. SEA3D.File = function( data ) {
  2512. this.version = SEA3D.VERSION;
  2513. this.objects = [];
  2514. this.typeClass = {}
  2515. this.typeRead = {}
  2516. this.position =
  2517. this.dataPosition = 0;
  2518. this.scope = this;
  2519. this.timeLimit = 60;
  2520. // SEA3D
  2521. this.addClass( SEA3D.Geometry );
  2522. this.addClass( SEA3D.GeometryDelta );
  2523. this.addClass( SEA3D.Mesh );
  2524. this.addClass( SEA3D.Mesh2D );
  2525. this.addClass( SEA3D.Material );
  2526. this.addClass( SEA3D.Composite );
  2527. this.addClass( SEA3D.PointLight );
  2528. this.addClass( SEA3D.DirectionalLight );
  2529. this.addClass( SEA3D.HemisphereLight );
  2530. this.addClass( SEA3D.Skeleton );
  2531. this.addClass( SEA3D.SkeletonLocal );
  2532. this.addClass( SEA3D.SkeletonAnimation );
  2533. this.addClass( SEA3D.JointObject );
  2534. this.addClass( SEA3D.Camera );
  2535. this.addClass( SEA3D.Morph );
  2536. this.addClass( SEA3D.VertexAnimation );
  2537. this.addClass( SEA3D.CubeMap );
  2538. this.addClass( SEA3D.Animation );
  2539. this.addClass( SEA3D.Dummy );
  2540. this.addClass( SEA3D.Line );
  2541. this.addClass( SEA3D.SoundPoint );
  2542. this.addClass( SEA3D.PlanarRender );
  2543. this.addClass( SEA3D.CubeRender );
  2544. this.addClass( SEA3D.Actions );
  2545. this.addClass( SEA3D.TextureURL );
  2546. this.addClass( SEA3D.Container3D );
  2547. this.addClass( SEA3D.Properties );
  2548. this.addClass( SEA3D.FileInfo );
  2549. // PHYSICS
  2550. this.addClass( SEA3D.Sphere );
  2551. this.addClass( SEA3D.Box );
  2552. this.addClass( SEA3D.Cone );
  2553. this.addClass( SEA3D.Capsule );
  2554. this.addClass( SEA3D.Cylinder );
  2555. this.addClass( SEA3D.GeometryShape );
  2556. this.addClass( SEA3D.StaticGeometryShape );
  2557. this.addClass( SEA3D.RigidBody );
  2558. // UNIVERSAL
  2559. this.addClass( SEA3D.JPEG );
  2560. this.addClass( SEA3D.JPEG_XR );
  2561. this.addClass( SEA3D.PNG );
  2562. this.addClass( SEA3D.GIF );
  2563. this.addClass( SEA3D.OGG );
  2564. this.addClass( SEA3D.MP3 );
  2565. this.addClass( SEA3D.JavaScript );
  2566. this.addClass( SEA3D.JavaScriptMethod );
  2567. this.addClass( SEA3D.GLSL );
  2568. }
  2569. SEA3D.File.CompressionLibs = {};
  2570. SEA3D.File.DecompressionMethod = {}
  2571. SEA3D.File.setDecompressionEngine = function( id, name, method ) {
  2572. SEA3D.File.CompressionLibs[ id ] = name;
  2573. SEA3D.File.DecompressionMethod[ id ] = method;
  2574. }
  2575. SEA3D.File.prototype.addClass = function( clazz ) {
  2576. this.typeClass[ clazz.prototype.type ] = clazz;
  2577. }
  2578. SEA3D.File.prototype.readHead = function() {
  2579. if ( this.stream.bytesAvailable < 16 )
  2580. return false;
  2581. if ( this.stream.readUTF( 3 ) != "SEA" )
  2582. console.error( "Invalid SEA3D format." );
  2583. this.sign = this.stream.readUTF( 3 );
  2584. this.version = this.stream.readUInt24();
  2585. if ( this.stream.readUByte() != 0 ) {
  2586. throw new Error( "Protection algorithm not is compatible." );
  2587. }
  2588. this.compressionID = this.stream.readUByte();
  2589. this.compressionAlgorithm = SEA3D.File.CompressionLibs[ this.compressionID ];
  2590. this.decompressionMethod = SEA3D.File.DecompressionMethod[ this.compressionID ];
  2591. if ( this.compressionID > 0 && ! this.decompressionMethod ) {
  2592. throw new Error( "Compression algorithm not is compatible." );
  2593. }
  2594. this.length = this.stream.readUInt();
  2595. this.dataPosition = this.stream.position;
  2596. this.objects.length = 0;
  2597. this.state = this.readBody;
  2598. if ( this.onHead )
  2599. this.onHead( {
  2600. file: this,
  2601. sign: this.sign
  2602. } );
  2603. return true;
  2604. }
  2605. SEA3D.File.prototype.getObject = function( index ) {
  2606. return this.objects[ index ];
  2607. }
  2608. SEA3D.File.prototype.readSEAObject = function() {
  2609. if ( this.stream.bytesAvailable < 4 )
  2610. return null;
  2611. var size = this.stream.readUInt();
  2612. var position = this.stream.position;
  2613. if ( this.stream.bytesAvailable < size )
  2614. return null;
  2615. var flag = this.stream.readUByte();
  2616. var type = this.stream.readExt();
  2617. var meta = null;
  2618. var name = flag & 1 ? this.stream.readUTF8() : "",
  2619. compressed = ( flag & 2 ) != 0,
  2620. streaming = ( flag & 4 ) != 0;
  2621. if ( flag & 8 )
  2622. {
  2623. var metalen = this.stream.readUShort();
  2624. var metabytes = this.stream.concat( this.stream.position, metalen );
  2625. this.stream.position += metalen;
  2626. if ( compressed && this.decompressionMethod )
  2627. metabytes.set( this.decompressionMethod( metabytes.buffer ) );
  2628. meta = SEA3D.Properties.readProperties( metabytes, this.objects );
  2629. }
  2630. size -= this.stream.position - position;
  2631. position = this.stream.position;
  2632. var data = this.stream.concat( position, size ),
  2633. obj;
  2634. if ( this.typeClass[ type ] )
  2635. {
  2636. if ( compressed && this.decompressionMethod )
  2637. data.buffer = this.decompressionMethod( data.buffer );
  2638. obj = new this.typeClass[ type ]( name, data, this );
  2639. if ( streaming && this.typeRead[ type ] )
  2640. this.typeRead[ type ].call( this.scope, obj );
  2641. }
  2642. else
  2643. {
  2644. obj = new SEA3D.Object( name, data, type, this );
  2645. console.warn( "SEA3D: Unknown format \"" + type + "\" of file \"" + name + "\". Add a module referring for this format." );
  2646. }
  2647. obj.metadata = meta;
  2648. this.objects.push( this.objects[ obj.type + "/" + obj.name ] = obj );
  2649. this.dataPosition = position + size;
  2650. ++ this.position;
  2651. return obj;
  2652. }
  2653. SEA3D.File.prototype.readBody = function() {
  2654. this.timer.update();
  2655. while ( this.position < this.length ) {
  2656. if ( this.timer.deltaTime < this.timeLimit ) {
  2657. this.stream.position = this.dataPosition;
  2658. var sea = this.readSEAObject();
  2659. if ( sea ) this.dispatchCompleteObject( sea );
  2660. else return false;
  2661. }
  2662. else return false;
  2663. }
  2664. this.state = this.readComplete;
  2665. return true;
  2666. }
  2667. SEA3D.File.prototype.readComplete = function() {
  2668. this.stream.position = this.dataPosition;
  2669. if ( this.stream.readUInt24() != 0x5EA3D1 )
  2670. console.warn( "SEA3D file is corrupted." );
  2671. delete this.state;
  2672. this.dispatchComplete();
  2673. }
  2674. SEA3D.File.prototype.readState = function() {
  2675. while ( this.state && this.state() );
  2676. if ( this.state ) {
  2677. window.setTimeout( this.readState.bind( this ), 10 );
  2678. this.dispatchProgress();
  2679. }
  2680. }
  2681. SEA3D.File.prototype.read = function( data ) {
  2682. this.stream = new SEA3D.Stream( data );
  2683. this.timer = new SEA3D.Timer();
  2684. this.state = this.readHead;
  2685. this.readState();
  2686. }
  2687. SEA3D.File.prototype.dispatchCompleteObject = function( obj ) {
  2688. if ( ! this.onCompleteObject ) return;
  2689. this.onCompleteObject( {
  2690. file: this,
  2691. object: obj
  2692. } );
  2693. }
  2694. SEA3D.File.prototype.dispatchProgress = function() {
  2695. if ( ! this.onProgress ) return;
  2696. this.onProgress( {
  2697. file: this,
  2698. loaded: this.position,
  2699. total: this.length,
  2700. progress: this.position / this.length
  2701. } );
  2702. }
  2703. SEA3D.File.prototype.dispatchDownloadProgress = function( position, length ) {
  2704. if ( ! this.onDownloadProgress ) return;
  2705. this.onDownloadProgress( {
  2706. file: this,
  2707. loaded: position,
  2708. total: length,
  2709. progress: position / length
  2710. } );
  2711. }
  2712. SEA3D.File.prototype.dispatchComplete = function() {
  2713. var elapsedTime = this.timer.elapsedTime;
  2714. var message = elapsedTime + "ms, " + this.objects.length + " objects";
  2715. if ( this.onComplete ) this.onComplete( {
  2716. file: this,
  2717. timeTotal: elapsedTime,
  2718. message: message
  2719. } );
  2720. else console.log( "SEA3D:", message );
  2721. }
  2722. SEA3D.File.prototype.dispatchError = function( id, message ) {
  2723. if ( this.onError ) this.onError( { file: this, id: id, message: message } );
  2724. else console.error( "SEA3D: #" + id, message );
  2725. }
  2726. SEA3D.File.prototype.load = function( url ) {
  2727. var file = this,
  2728. xhr = new XMLHttpRequest();
  2729. xhr.open( "GET", url, true );
  2730. xhr.responseType = 'arraybuffer';
  2731. xhr.onprogress = function( e ) {
  2732. if ( e.lengthComputable )
  2733. file.dispatchDownloadProgress( e.loaded, e.total );
  2734. }
  2735. xhr.onreadystatechange = function() {
  2736. if ( xhr.readyState === 2 ) {
  2737. //xhr.getResponseHeader("Content-Length");
  2738. } else if ( xhr.readyState === 3 ) {
  2739. // progress
  2740. } else if ( xhr.readyState === 4 ) {
  2741. if ( xhr.status === 200 || xhr.status === 0 ) {
  2742. // complete
  2743. file.read( this.response );
  2744. } else {
  2745. this.dispatchError( 1001, "Couldn't load [" + url + "] [" + xhr.status + "]" );
  2746. }
  2747. }
  2748. }
  2749. xhr.send();
  2750. }