SEA3D.js 64 KB

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