SEA3D.js 67 KB

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