SEA3D.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259
  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 nodeCount = data.readUShort();
  1783. this.node = [];
  1784. var i, j;
  1785. for(i = 0; i < nodeCount; i++) {
  1786. var nodeName = data.readUTF8();
  1787. if (useVertex) {
  1788. var verts = [];
  1789. j = 0;
  1790. while(j < this.length)
  1791. verts[j++] = data.readFloat();
  1792. }
  1793. if (useNormal) {
  1794. var norms = [];
  1795. j = 0;
  1796. while(j < this.length)
  1797. norms[j++] = data.readFloat();
  1798. }
  1799. this.node[i] = {vertex:verts, normal:norms, name:nodeName}
  1800. }
  1801. }
  1802. SEA3D.Morph.prototype.type = "mph";
  1803. //
  1804. // Vertex Animation
  1805. //
  1806. SEA3D.VertexAnimation = function(name, data, sea) {
  1807. this.name = name;
  1808. this.data = data;
  1809. this.sea = sea;
  1810. SEA3D.AnimationBase.read(this);
  1811. var flags = data.readUByte();
  1812. this.isBigMesh = (flags & 1) != 0;
  1813. data.readVInt = this.isBigMesh ? data.readUInt : data.readUShort;
  1814. this.numVertex = data.readVInt();
  1815. this.length = this.numVertex * 3;
  1816. var useVertex = (flags & 2) != 0;
  1817. var useNormal = (flags & 4) != 0;
  1818. this.frame = [];
  1819. var i, j;
  1820. for(i = 0; i < this.numFrames; i++) {
  1821. if (useVertex) {
  1822. var verts = [];
  1823. j = 0;
  1824. while(j < this.length)
  1825. verts[j++] = data.readFloat();
  1826. }
  1827. if (useNormal) {
  1828. var norms = [];
  1829. j = 0;
  1830. while(j < this.length)
  1831. norms[j++] = data.readFloat();
  1832. }
  1833. this.frame[i] = {vertex:verts, normal:norms}
  1834. }
  1835. }
  1836. SEA3D.VertexAnimation.prototype.type = "vtxa";
  1837. //
  1838. // Camera
  1839. //
  1840. SEA3D.Camera = function(name, data, sea) {
  1841. this.name = name;
  1842. this.data = data;
  1843. this.sea = sea;
  1844. SEA3D.Object3D.read(this);
  1845. if (this.attrib & 64) {
  1846. this.dof = {
  1847. distance:data.readFloat(),
  1848. range:data.readFloat()
  1849. }
  1850. }
  1851. this.transform = data.readMatrix();
  1852. this.fov = data.readFloat();
  1853. SEA3D.Object3D.readTags(this);
  1854. }
  1855. SEA3D.Camera.prototype.type = "cam";
  1856. //
  1857. // Joint Object
  1858. //
  1859. SEA3D.JointObject = function(name, data, sea) {
  1860. this.name = name;
  1861. this.data = data;
  1862. this.sea = sea;
  1863. SEA3D.Object3D.read(this);
  1864. this.target = sea.getObject(data.readUInt());
  1865. this.joint = data.readUShort();
  1866. this.transform = data.readMatrix();
  1867. this.fov = data.readFloat();
  1868. SEA3D.Object3D.readTags(this);
  1869. }
  1870. SEA3D.JointObject.prototype.type = "jnt";
  1871. //
  1872. // Light
  1873. //
  1874. SEA3D.Light = {
  1875. read : function(scope) {
  1876. SEA3D.Object3D.read(scope);
  1877. var data = scope.data;
  1878. scope.attenStart = Number.MAX_VALUE;
  1879. scope.attenEnd = Number.MAX_VALUE;
  1880. if (scope.attrib & 64) {
  1881. var shadowHeader = data.readUByte();
  1882. scope.shadow = {}
  1883. scope.shadow.opacity = shadowHeader & 1 ? data.readFloat() : 1;
  1884. scope.shadow.color = shadowHeader & 2 ? data.readUInt24() : 0x000000;
  1885. }
  1886. if (scope.attrib & 512) {
  1887. scope.attenStart = data.readFloat();
  1888. scope.attenEnd = data.readFloat();
  1889. }
  1890. scope.color = data.readUInt24();
  1891. scope.multiplier = data.readFloat();
  1892. }
  1893. }
  1894. //
  1895. // Point Light
  1896. //
  1897. SEA3D.PointLight = function(name, data, sea) {
  1898. this.name = name;
  1899. this.data = data;
  1900. this.sea = sea;
  1901. SEA3D.Light.read(this);
  1902. if (this.attrib & 128) {
  1903. this.attenuation = {
  1904. start:data.readFloat(),
  1905. end:data.readFloat()
  1906. }
  1907. }
  1908. this.position = data.readVector3();
  1909. SEA3D.Object3D.readTags(this);
  1910. }
  1911. SEA3D.PointLight.prototype.type = "plht";
  1912. //
  1913. // Hemisphere Light
  1914. //
  1915. SEA3D.HemisphereLight = function(name, data, sea) {
  1916. this.name = name;
  1917. this.data = data;
  1918. this.sea = sea;
  1919. SEA3D.Light.read(this);
  1920. if (this.attrib & 128) {
  1921. this.attenuation = {
  1922. start:data.readFloat(),
  1923. end:data.readFloat()
  1924. }
  1925. }
  1926. this.secondColor = data.readUInt24();
  1927. SEA3D.Object3D.readTags(this);
  1928. }
  1929. SEA3D.HemisphereLight.prototype.type = "hlht";
  1930. //
  1931. // Directional Light
  1932. //
  1933. SEA3D.DirectionalLight = function(name, data, sea) {
  1934. this.name = name;
  1935. this.data = data;
  1936. this.sea = sea;
  1937. SEA3D.Light.read(this);
  1938. this.transform = data.readMatrix();
  1939. SEA3D.Object3D.readTags(this);
  1940. }
  1941. SEA3D.DirectionalLight.prototype.type = "dlht";
  1942. //
  1943. // Material
  1944. //
  1945. SEA3D.Material = function(name, data, sea) {
  1946. this.name = name;
  1947. this.data = data;
  1948. this.sea = sea;
  1949. this.technique = [];
  1950. this.attrib = data.readUShort();
  1951. this.alpha = 1;
  1952. this.blendMode = "normal";
  1953. this.alphaThreshold = .5;
  1954. this.bothSides = (this.attrib & 1) != 0;
  1955. this.receiveLights = (this.attrib & 2) == 0;
  1956. this.receiveShadows = (this.attrib & 4) == 0;
  1957. this.receiveFog = (this.attrib & 8) == 0;
  1958. this.smooth = (this.attrib & 16) == 0;
  1959. if (this.attrib & 32)
  1960. this.alpha = data.readFloat();
  1961. if (this.attrib & 64)
  1962. this.blendMode = data.readBlendMode();
  1963. if (this.attrib & 128)
  1964. this.animations = SEA3D.Animation.readAnimationList(data, sea);
  1965. this.depthMask = (this.attrib & 256) == 0;
  1966. var count = data.readUByte();
  1967. for (var i = 0; i < count; ++i) {
  1968. var kind = data.readUShort();
  1969. var size = data.readUShort();
  1970. var pos = data.position;
  1971. var tech, methodAttrib;
  1972. switch(kind) {
  1973. case SEA3D.Material.DEFAULT:
  1974. tech = {
  1975. ambientColor:data.readUInt24(),
  1976. diffuseColor:data.readUInt24(),
  1977. specularColor:data.readUInt24(),
  1978. specular:data.readFloat(),
  1979. gloss:data.readFloat()
  1980. }
  1981. break;
  1982. case SEA3D.Material.COMPOSITE_TEXTURE:
  1983. tech = {
  1984. composite:sea.getObject(data.readUInt())
  1985. }
  1986. break;
  1987. case SEA3D.Material.DIFFUSE_MAP:
  1988. tech = {
  1989. texture:sea.getObject(data.readUInt())
  1990. }
  1991. break;
  1992. case SEA3D.Material.SPECULAR_MAP:
  1993. tech = {
  1994. texture:sea.getObject(data.readUInt())
  1995. }
  1996. break;
  1997. case SEA3D.Material.NORMAL_MAP:
  1998. tech = {
  1999. texture:sea.getObject(data.readUInt())
  2000. }
  2001. break;
  2002. case SEA3D.Material.REFLECTION:
  2003. case SEA3D.Material.FRESNEL_REFLECTION:
  2004. tech = {
  2005. texture:sea.getObject(data.readUInt()),
  2006. alpha:data.readFloat()
  2007. }
  2008. if (kind == SEA3D.Material.FRESNEL_REFLECTION) {
  2009. tech.power = data.readFloat();
  2010. tech.normal = data.readFloat();
  2011. }
  2012. break;
  2013. case SEA3D.Material.REFRACTION:
  2014. tech = {
  2015. texture:sea.getObject(data.readUInt()),
  2016. alpha:data.readFloat(),
  2017. ior:data.readFloat()
  2018. }
  2019. break;
  2020. case SEA3D.Material.RIM:
  2021. tech = {
  2022. color:data.readUInt24(),
  2023. strength:data.readFloat(),
  2024. power:data.readFloat(),
  2025. blendMode:data.readBlendMode()
  2026. }
  2027. break;
  2028. case SEA3D.Material.LIGHT_MAP:
  2029. tech = {
  2030. texture:sea.getObject(data.readUInt()),
  2031. channel:data.readUByte(),
  2032. blendMode:data.readBlendMode()
  2033. }
  2034. break;
  2035. case SEA3D.Material.DETAIL_MAP:
  2036. tech = {
  2037. texture:sea.getObject(data.readUInt()),
  2038. scale:data.readFloat(),
  2039. blendMode:data.readBlendMode()
  2040. }
  2041. break;
  2042. case SEA3D.Material.CEL:
  2043. tech = {
  2044. color:data.readUInt24(),
  2045. levels:data.readUByte(),
  2046. size:data.readFloat(),
  2047. specularCutOff:data.readFloat(),
  2048. smoothness:data.readFloat()
  2049. }
  2050. break;
  2051. case SEA3D.Material.TRANSLUCENT:
  2052. tech = {
  2053. color:data.readUInt24(),
  2054. translucency:data.readFloat(),
  2055. scattering:data.readFloat()
  2056. }
  2057. break;
  2058. case SEA3D.Material.BLEND_NORMAL_MAP:
  2059. methodAttrib = data.readUByte();
  2060. tech = {
  2061. texture:sea.getObject(data.readUInt()),
  2062. secondaryTexture:sea.getObject(data.readUInt())
  2063. }
  2064. if (methodAttrib & 1)
  2065. {
  2066. tech.offsetX0 = data.readFloat();
  2067. tech.offsetY0 = data.readFloat();
  2068. tech.offsetX1 = data.readFloat();
  2069. tech.offsetY1 = data.readFloat();
  2070. }
  2071. else
  2072. {
  2073. tech.offsetX0 = tech.offsetY0 =
  2074. tech.offsetX1 = tech.offsetY1 = 0
  2075. }
  2076. tech.animate = methodAttrib & 2;
  2077. break;
  2078. case SEA3D.Material.MIRROR_REFLECTION:
  2079. tech = {
  2080. texture:sea.getObject(data.readUInt()),
  2081. alpha:data.readFloat()
  2082. }
  2083. break;
  2084. case SEA3D.Material.AMBIENT_MAP:
  2085. tech = {
  2086. texture:sea.getObject(data.readUInt())
  2087. }
  2088. break;
  2089. case SEA3D.Material.ALPHA_MAP:
  2090. tech = {
  2091. texture:sea.getObject(data.readUInt())
  2092. }
  2093. break;
  2094. case SEA3D.Material.EMISSIVE_MAP:
  2095. tech = {
  2096. texture:sea.getObject(data.readUInt())
  2097. }
  2098. break;
  2099. case SEA3D.Material.VERTEX_COLOR:
  2100. tech = {
  2101. blendMode:data.readBlendMode()
  2102. }
  2103. break;
  2104. case SEA3D.Material.WRAP_LIGHTING:
  2105. tech = {
  2106. color:data.readUInt24(),
  2107. strength:data.readFloat()
  2108. }
  2109. break;
  2110. case SEA3D.Material.COLOR_REPLACE:
  2111. methodAttrib = data.readUByte();
  2112. tech = {
  2113. red:data.readUInt24(),
  2114. green:data.readUInt24(),
  2115. blue:data.readUInt24()
  2116. }
  2117. if (methodAttrib & 1)
  2118. tech.mask = sea.getObject(data.readUInt());
  2119. if (methodAttrib & 2)
  2120. tech.alpha = data.readFloat();
  2121. break;
  2122. case SEA3D.Material.REFLECTION_SPHERICAL:
  2123. tech = {
  2124. texture:sea.getObject(data.readUInt()),
  2125. alpha:data.readFloat()
  2126. }
  2127. break;
  2128. default:
  2129. console.warn("SEA3D: MaterialTechnique not found:", kind.toString(16));
  2130. data.position = pos += size;
  2131. continue;
  2132. }
  2133. tech.kind = kind;
  2134. this.technique.push(tech);
  2135. data.position = pos += size;
  2136. }
  2137. }
  2138. SEA3D.Material.DEFAULT = 0;
  2139. SEA3D.Material.COMPOSITE_TEXTURE = 1;
  2140. SEA3D.Material.DIFFUSE_MAP = 2;
  2141. SEA3D.Material.SPECULAR_MAP = 3;
  2142. SEA3D.Material.REFLECTION = 4;
  2143. SEA3D.Material.REFRACTION = 5;
  2144. SEA3D.Material.NORMAL_MAP = 6;
  2145. SEA3D.Material.FRESNEL_REFLECTION = 7;
  2146. SEA3D.Material.RIM = 8;
  2147. SEA3D.Material.LIGHT_MAP = 9;
  2148. SEA3D.Material.DETAIL_MAP = 10;
  2149. SEA3D.Material.CEL = 11;
  2150. SEA3D.Material.TRANSLUCENT = 12;
  2151. SEA3D.Material.BLEND_NORMAL_MAP = 13;
  2152. SEA3D.Material.MIRROR_REFLECTION = 14;
  2153. SEA3D.Material.AMBIENT_MAP = 15;
  2154. SEA3D.Material.ALPHA_MAP = 16;
  2155. SEA3D.Material.EMISSIVE_MAP = 17;
  2156. SEA3D.Material.VERTEX_COLOR = 18;
  2157. SEA3D.Material.WRAP_LIGHTING = 19;
  2158. SEA3D.Material.COLOR_REPLACE = 20;
  2159. SEA3D.Material.REFLECTION_SPHERICAL = 21;
  2160. SEA3D.Material.prototype.type = "mat";
  2161. //
  2162. // Composite
  2163. //
  2164. SEA3D.Composite = function(name, data, sea) {
  2165. this.name = name;
  2166. this.data = data;
  2167. this.sea = sea;
  2168. var layerCount = data.readUByte();
  2169. this.layer = [];
  2170. for(var i = 0; i < layerCount; i++)
  2171. this.layer[i] = this.readLayer(data, this);
  2172. }
  2173. SEA3D.Composite.prototype.getLayerByName = function(name) {
  2174. for(var i = 0; i < this.layer.length; i++) {
  2175. if (this.layer[i].name == name)
  2176. return this.layer[i];
  2177. }
  2178. }
  2179. SEA3D.Composite.prototype.readLayer = function(data, scope) {
  2180. this.scope = scope;
  2181. var out = {
  2182. blendMode:"normal",
  2183. opacity:1
  2184. }
  2185. var attrib = data.readUShort();
  2186. if (attrib & 1) out.texture = this.readLayerBitmap(data, scope);
  2187. else out.color = data.readUInt24();
  2188. if (attrib & 2)
  2189. out.mask = this.readLayerBitmap(data, scope);
  2190. if (attrib & 4)
  2191. out.name = data.readUTF8();
  2192. if (attrib & 8)
  2193. out.blendMode = data.readBlendMode();
  2194. if (attrib & 16)
  2195. out.opacity = data.readFloat();
  2196. return out;
  2197. }
  2198. SEA3D.Composite.prototype.readLayerBitmap = function(data, scope) {
  2199. this.scope = scope;
  2200. var out = {
  2201. channel:0,
  2202. repeat:true,
  2203. offsetU:0,
  2204. offsetV:0,
  2205. scaleU:0,
  2206. scaleV:0,
  2207. rotation:0
  2208. }
  2209. out.map = scope.sea.getObject(data.readUInt());
  2210. var attrib = data.readUShort();
  2211. if (attrib > 0) {
  2212. if (attrib & 1)
  2213. out.channel = data.readUByte();
  2214. if (attrib & 2)
  2215. out.repeat = false;
  2216. if (attrib & 4)
  2217. out.offsetU = data.readFloat();
  2218. if (attrib & 8)
  2219. out.offsetV = data.readFloat();
  2220. if (attrib & 16)
  2221. out.scaleU = data.readFloat();
  2222. if (attrib & 32)
  2223. out.scaleV = data.readFloat();
  2224. if (attrib & 64)
  2225. out.rotation = data.readFloat();
  2226. if (attrib & 128)
  2227. out.animation = SEA3D.Animation.readAnimationList(data, scope.sea);
  2228. }
  2229. return out;
  2230. }
  2231. SEA3D.Composite.prototype.type = "ctex";
  2232. //
  2233. // Cube Maps
  2234. //
  2235. SEA3D.CubeMap = function(name, data, sea) {
  2236. this.name = name;
  2237. this.data = data;
  2238. this.sea = sea;
  2239. this.transparent = false;
  2240. var ext = data.readExt();
  2241. this.faces = [];
  2242. for(var i = 0; i < 6; i++) {
  2243. var size = data.readUInt();
  2244. this.faces[i] = data.concat(data.position, size);
  2245. data.position += size;
  2246. }
  2247. }
  2248. SEA3D.CubeMap.prototype.type = "cmap";
  2249. //
  2250. // JPEG
  2251. //
  2252. SEA3D.JPEG = function(name, data, sea) {
  2253. this.name = name;
  2254. this.data = data;
  2255. this.sea = sea;
  2256. this.transparent = false;
  2257. }
  2258. SEA3D.JPEG.prototype.type = "jpg";
  2259. //
  2260. // JPEG_XR
  2261. //
  2262. SEA3D.JPEG_XR = function(name, data, sea) {
  2263. this.name = name;
  2264. this.data = data;
  2265. this.sea = sea;
  2266. this.transparent = true;
  2267. }
  2268. SEA3D.JPEG_XR.prototype.type = "wdp";
  2269. //
  2270. // PNG
  2271. //
  2272. SEA3D.PNG = function(name, data, sea) {
  2273. this.name = name;
  2274. this.data = data;
  2275. this.sea = sea;
  2276. this.transparent = data.buffer[25] == 0x06;
  2277. }
  2278. SEA3D.PNG.prototype.type = "png";
  2279. //
  2280. // GIF
  2281. //
  2282. SEA3D.GIF = function(name, data, sea) {
  2283. this.name = name;
  2284. this.data = data;
  2285. this.sea = sea;
  2286. this.transparent = data.buffer[11] > 0;
  2287. }
  2288. SEA3D.GIF.prototype.type = "gif";
  2289. //
  2290. // MP3
  2291. //
  2292. SEA3D.MP3 = function(name, data, sea) {
  2293. this.name = name;
  2294. this.data = data;
  2295. this.sea = sea;
  2296. }
  2297. SEA3D.MP3.prototype.type = "mp3";
  2298. //
  2299. // FILE FORMAT
  2300. //
  2301. SEA3D.File = function(data) {
  2302. this.stream = new SEA3D.Stream(data);
  2303. this.version = SEA3D.VERSION;
  2304. this.objects = [];
  2305. this.typeClass = {}
  2306. this.typeRead = {}
  2307. this.position =
  2308. this.dataPosition = 0;
  2309. this.scope = this;
  2310. this.timeLimit = 60;
  2311. // SEA3D
  2312. this.addClass(SEA3D.Geometry);
  2313. this.addClass(SEA3D.GeometryDelta);
  2314. this.addClass(SEA3D.Mesh);
  2315. this.addClass(SEA3D.Mesh2D);
  2316. this.addClass(SEA3D.Material);
  2317. this.addClass(SEA3D.Composite);
  2318. this.addClass(SEA3D.PointLight);
  2319. this.addClass(SEA3D.DirectionalLight);
  2320. this.addClass(SEA3D.HemisphereLight);
  2321. this.addClass(SEA3D.Skeleton);
  2322. this.addClass(SEA3D.SkeletonLocal);
  2323. this.addClass(SEA3D.SkeletonAnimation);
  2324. this.addClass(SEA3D.JointObject);
  2325. this.addClass(SEA3D.Camera);
  2326. this.addClass(SEA3D.Morph);
  2327. this.addClass(SEA3D.VertexAnimation);
  2328. this.addClass(SEA3D.CubeMap);
  2329. this.addClass(SEA3D.Animation);
  2330. this.addClass(SEA3D.Dummy);
  2331. this.addClass(SEA3D.Line);
  2332. this.addClass(SEA3D.SoundPoint);
  2333. this.addClass(SEA3D.PlanarRender);
  2334. this.addClass(SEA3D.CubeRender);
  2335. this.addClass(SEA3D.Actions);
  2336. this.addClass(SEA3D.TextureURL);
  2337. this.addClass(SEA3D.Container3D);
  2338. this.addClass(SEA3D.Properties);
  2339. this.addClass(SEA3D.FileInfo);
  2340. // UNIVERSAL
  2341. this.addClass(SEA3D.JPEG);
  2342. this.addClass(SEA3D.JPEG_XR);
  2343. this.addClass(SEA3D.PNG);
  2344. this.addClass(SEA3D.GIF);
  2345. this.addClass(SEA3D.MP3);
  2346. this.addClass(SEA3D.JavaScript);
  2347. this.addClass(SEA3D.GLSL);
  2348. }
  2349. SEA3D.File.CompressionLibs = {};
  2350. SEA3D.File.DecompressionMethod = {}
  2351. SEA3D.File.setDecompressionEngine = function(id, name, method) {
  2352. SEA3D.File.CompressionLibs[id] = name;
  2353. SEA3D.File.DecompressionMethod[id] = method;
  2354. }
  2355. SEA3D.File.prototype.addClass = function(clazz) {
  2356. this.typeClass[clazz.prototype.type] = clazz;
  2357. }
  2358. SEA3D.File.prototype.readHead = function() {
  2359. if (this.stream.bytesAvailable() < 16)
  2360. return false;
  2361. if (this.stream.readUTF(3) != "SEA")
  2362. console.error("Invalid SEA3D format.");
  2363. var sign = this.stream.readUTF(3);
  2364. if (sign != "S3D")
  2365. console.warn("Signature \"" + sign + "\" not recognized.");
  2366. this.version = this.stream.readUInt24();
  2367. if (this.stream.readUByte() != 0) {
  2368. throw new Error("Protection algorithm not is compatible.");
  2369. }
  2370. this.compressionID = this.stream.readUByte();
  2371. this.compressionAlgorithm = SEA3D.File.CompressionLibs[this.compressionID];
  2372. this.decompressionMethod = SEA3D.File.DecompressionMethod[this.compressionID];
  2373. if (this.compressionID > 0 && !this.decompressionMethod) {
  2374. throw new Error("Compression algorithm not is compatible.");
  2375. }
  2376. this.length = this.stream.readUInt();
  2377. this.dataPosition = this.stream.position;
  2378. this.objects.length = 0;
  2379. this.stage = this.readBody;
  2380. return true;
  2381. }
  2382. SEA3D.File.prototype.getObject = function(index) {
  2383. return this.objects[index];
  2384. }
  2385. SEA3D.File.prototype.readSEAObject = function() {
  2386. if (this.stream.bytesAvailable() < 4)
  2387. return null;
  2388. var size = this.stream.readUInt();
  2389. var position = this.stream.position;
  2390. if (this.stream.bytesAvailable() < size)
  2391. return null;
  2392. var flag = this.stream.readUByte();
  2393. var type = this.stream.readExt();
  2394. var meta = null;
  2395. var name = flag & 1 ? this.stream.readUTF8() : "",
  2396. compressed = (flag & 2) != 0,
  2397. streaming = (flag & 4) != 0;
  2398. if (flag & 8)
  2399. {
  2400. var metalen = this.stream.readUShort();
  2401. var metabytes = this.stream.concat(this.stream.position, metalen);
  2402. this.stream.position += metalen;
  2403. if (compressed && this.decompressionMethod)
  2404. metabytes.set(this.decompressionMethod(metabytes.buffer));
  2405. meta = SEA3D.Properties.readProperties(metabytes, this.objects);
  2406. }
  2407. size -= this.stream.position - position;
  2408. position = this.stream.position;
  2409. var data = this.stream.concat(position, size),
  2410. obj;
  2411. if (this.typeClass[type])
  2412. {
  2413. if (compressed && this.decompressionMethod)
  2414. data.set(this.decompressionMethod(data.buffer));
  2415. obj = new this.typeClass[type](name, data, this);
  2416. if (streaming && this.typeRead[type])
  2417. this.typeRead[type].call(this.scope, obj);
  2418. }
  2419. else
  2420. {
  2421. obj = new SEA3D.Object(name, data, type, this);
  2422. console.warn("SEA3D: Unknown format \"" + type + "\" of file \"" + name + "\". Add a module referring for this format.");
  2423. }
  2424. obj.metadata = meta;
  2425. this.objects.push(this.objects[obj.type + "/" + obj.name] = obj);
  2426. this.dataPosition = position + size;
  2427. ++this.position;
  2428. return obj;
  2429. }
  2430. SEA3D.File.prototype.readBody = function() {
  2431. this.timer.update();
  2432. while (this.position < this.length) {
  2433. if (this.timer.getDeltaTime() < this.timeLimit) {
  2434. this.stream.position = this.dataPosition;
  2435. var sea = this.readSEAObject();
  2436. if (sea) this.dispatchCompleteObject(sea);
  2437. else return false;
  2438. }
  2439. else return false;
  2440. }
  2441. this.stage = this.readComplete;
  2442. return true;
  2443. }
  2444. SEA3D.File.prototype.readComplete = function() {
  2445. this.stream.position = this.dataPosition;
  2446. if (this.stream.readUInt24() != 0x5EA3D1)
  2447. console.warn("SEA3D file is corrupted.");
  2448. this.stage = null;
  2449. this.dispatchComplete();
  2450. }
  2451. SEA3D.File.prototype.readStage = function(scope) {
  2452. var t;
  2453. while (scope.stage && scope.stage()) t=0;
  2454. if (scope.stage) {
  2455. window.setInterval(scope.readStage, 10, scope);
  2456. scope.dispatchProgress();
  2457. }
  2458. }
  2459. SEA3D.File.prototype.read = function() {
  2460. this.timer = new SEA3D.Timer();
  2461. this.stage = this.readHead;
  2462. this.readStage(this);
  2463. }
  2464. SEA3D.File.prototype.dispatchCompleteObject = function(obj) {
  2465. if (!this.onCompleteObject) return;
  2466. this.onCompleteObject({
  2467. file:this,
  2468. object:obj
  2469. });
  2470. }
  2471. SEA3D.File.prototype.dispatchProgress = function() {
  2472. if (!this.onProgress) return;
  2473. this.onProgress({
  2474. file:this,
  2475. loaded:this.position,
  2476. total:this.length,
  2477. progress:this.position / this.length
  2478. });
  2479. }
  2480. SEA3D.File.prototype.dispatchDownloadProgress = function(position, length) {
  2481. if (!this.onDownloadProgress) return;
  2482. this.onDownloadProgress({
  2483. file:this,
  2484. loaded:position,
  2485. total:length,
  2486. progress:position / length
  2487. });
  2488. }
  2489. SEA3D.File.prototype.dispatchComplete = function() {
  2490. var elapsedTime = this.timer.getElapsedTime();
  2491. var message = elapsedTime + "ms, " + this.objects.length + " objects";
  2492. if (this.onComplete) this.onComplete({
  2493. file:this,
  2494. timeTotal:elapsedTime,
  2495. message:message
  2496. });
  2497. else console.log("SEA3D:", message);
  2498. }
  2499. SEA3D.File.prototype.dispatchError = function(id, message) {
  2500. if (this.onError) this.onError({file:this,id:id,message:message});
  2501. else console.error("SEA3D: #" + id, message);
  2502. }
  2503. SEA3D.File.prototype.load = function( url ) {
  2504. var scope = this,
  2505. xhr = new XMLHttpRequest();
  2506. xhr.open( "GET", url, true );
  2507. xhr.responseType = 'arraybuffer';
  2508. xhr.onprogress = function(e) {
  2509. if (e.lengthComputable)
  2510. scope.dispatchDownloadProgress( e.loaded, e.total );
  2511. }
  2512. xhr.onreadystatechange = function() {
  2513. if ( xhr.readyState === 2 ){
  2514. //xhr.getResponseHeader("Content-Length");
  2515. } else if ( xhr.readyState === 3 ) {
  2516. // progress
  2517. } else if ( xhr.readyState === 4 ) {
  2518. if ( xhr.status === 200 || xhr.status === 0 ) {
  2519. // complete
  2520. scope.stream.set(new Uint8Array(this.response));
  2521. scope.read();
  2522. } else {
  2523. this.dispatchError(1001, "Couldn't load [" + url + "] [" + xhr.status + "]");
  2524. }
  2525. }
  2526. }
  2527. xhr.send();
  2528. }