2
0

SEA3D.js 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779
  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. 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. else continue;
  822. }
  823. this.groups.push( {
  824. start : start,
  825. count : vec.length - start,
  826. } );
  827. }
  828. } else {
  829. // TRIANGLE
  830. j = 0;
  831. for ( i = 0; i < count; i ++ ) {
  832. len = data.readUInteger() * 3;
  833. this.groups.push( {
  834. start : j,
  835. count : len,
  836. } );
  837. len += j;
  838. while ( j < len ) {
  839. vec[ j ++ ] = data.readUInteger();
  840. }
  841. }
  842. }
  843. };
  844. SEA3D.GeometryDeltaBase.prototype = Object.create( SEA3D.GeometryDeltaBase.prototype );
  845. SEA3D.GeometryDeltaBase.prototype.constructor = SEA3D.GeometryDelta;
  846. SEA3D.GeometryDelta.prototype.type = "geDL";
  847. //
  848. // Object3D
  849. //
  850. SEA3D.Object3D = function( name, data, sea3d ) {
  851. this.name = name;
  852. this.data = data;
  853. this.sea3d = sea3d;
  854. this.isStatic = false;
  855. this.visible = true;
  856. this.attrib = data.readUShort();
  857. if ( this.attrib & 1 ) this.parent = sea3d.getObject( data.readUInt() );
  858. if ( this.attrib & 2 ) this.animations = data.readAnimationList( sea3d );
  859. if ( this.attrib & 4 ) this.scripts = data.readScriptList( sea3d );
  860. if ( this.attrib & 16 ) this.properties = sea3d.getObject( data.readUInt() );
  861. if ( this.attrib & 32 ) {
  862. var objectType = data.readUByte();
  863. this.isStatic = ( objectType & 1 ) != 0;
  864. this.visible = ( objectType & 2 ) != 0;
  865. }
  866. };
  867. SEA3D.Object3D.prototype.readTag = function( kind, data, size ) {
  868. };
  869. //
  870. // Entity3D
  871. //
  872. SEA3D.Entity3D = function( name, data, sea3d ) {
  873. SEA3D.Object3D.call( this, name, data, sea3d );
  874. this.castShadows = true;
  875. if ( this.attrib & 64 ) {
  876. var lightType = data.readUByte();
  877. this.castShadows = ( lightType & 1 ) == 0;
  878. }
  879. };
  880. SEA3D.Entity3D.prototype = Object.create( SEA3D.Object3D.prototype );
  881. SEA3D.Entity3D.prototype.constructor = SEA3D.Entity3D;
  882. //
  883. // Sound3D
  884. //
  885. SEA3D.Sound3D = function( name, data, sea3d ) {
  886. SEA3D.Object3D.call( this, name, data, sea3d );
  887. this.autoPlay = ( this.attrib & 64 ) != 0;
  888. if ( this.attrib & 128 ) this.mixer = sea3d.getObject( data.readUInt() );
  889. this.sound = sea3d.getObject( data.readUInt() );
  890. this.volume = data.readFloat();
  891. };
  892. SEA3D.Sound3D.prototype = Object.create( SEA3D.Object3D.prototype );
  893. SEA3D.Sound3D.prototype.constructor = SEA3D.Sound3D;
  894. //
  895. // Sound Point
  896. //
  897. SEA3D.SoundPoint = function( name, data, sea3d ) {
  898. SEA3D.Sound3D.call( this, name, data, sea3d );
  899. this.position = data.readVector3();
  900. this.distance = data.readFloat();
  901. data.readTags( this.readTag.bind( this ) );
  902. };
  903. SEA3D.SoundPoint.prototype = Object.create( SEA3D.Sound3D.prototype );
  904. SEA3D.SoundPoint.prototype.constructor = SEA3D.SoundPoint;
  905. SEA3D.SoundPoint.prototype.type = "sp";
  906. //
  907. // Container3D
  908. //
  909. SEA3D.Container3D = function( name, data, sea3d ) {
  910. SEA3D.Object3D.call( this, name, data, sea3d );
  911. this.transform = data.readMatrix();
  912. data.readTags( this.readTag.bind( this ) );
  913. };
  914. SEA3D.Container3D.prototype = Object.create( SEA3D.Object3D.prototype );
  915. SEA3D.Container3D.prototype.constructor = SEA3D.Container3D;
  916. SEA3D.Container3D.prototype.type = "c3d";
  917. //
  918. // Texture URL
  919. //
  920. SEA3D.TextureURL = function( name, data, sea3d ) {
  921. this.name = name;
  922. this.data = data;
  923. this.sea3d = sea3d;
  924. this.url = data.readUTF( data.length );
  925. };
  926. SEA3D.TextureURL.prototype.type = "urlT";
  927. //
  928. // Actions
  929. //
  930. SEA3D.Actions = function( name, data, sea3d ) {
  931. this.name = name;
  932. this.data = data;
  933. this.sea3d = sea3d;
  934. this.count = data.readUInt();
  935. this.actions = [];
  936. for ( var i = 0; i < this.count; i ++ ) {
  937. var flag = data.readUByte();
  938. var kind = data.readUShort();
  939. var size = data.readUShort();
  940. var position = data.position;
  941. var act = this.actions[ i ] = { kind: kind };
  942. // range of animation
  943. if ( flag & 1 ) {
  944. // start and count in frames
  945. act.range = [ data.readUInt(), data.readUInt() ];
  946. }
  947. // time
  948. if ( flag & 2 ) {
  949. act.time = data.readUInt();
  950. }
  951. // easing
  952. if ( flag & 4 ) {
  953. act.intrpl = data.readInterpolation();
  954. if ( act.intrpl.indexOf( 'back.' ) == 0 ) {
  955. act.intrplParam0 = data.readFloat();
  956. }
  957. else if ( act.intrpl.indexOf( 'elastic.' ) == 0 ) {
  958. act.intrplParam0 = data.readFloat();
  959. act.intrplParam1 = data.readFloat();
  960. }
  961. }
  962. switch ( kind ) {
  963. case SEA3D.Actions.RTT_TARGET:
  964. act.source = sea3d.getObject( data.readUInt() );
  965. act.target = sea3d.getObject( data.readUInt() );
  966. break;
  967. case SEA3D.Actions.LOOK_AT:
  968. act.source = sea3d.getObject( data.readUInt() );
  969. act.target = sea3d.getObject( data.readUInt() );
  970. break;
  971. case SEA3D.Actions.PLAY_SOUND:
  972. act.sound = sea3d.getObject( data.readUInt() );
  973. act.offset = data.readUInt();
  974. break;
  975. case SEA3D.Actions.PLAY_ANIMATION:
  976. act.object = sea3d.getObject( data.readUInt() );
  977. act.name = data.readUTF8();
  978. break;
  979. case SEA3D.Actions.FOG:
  980. act.color = data.readUInt24();
  981. act.min = data.readFloat();
  982. act.max = data.readFloat();
  983. break;
  984. case SEA3D.Actions.ENVIRONMENT:
  985. act.texture = sea3d.getObject( data.readUInt() );
  986. break;
  987. case SEA3D.Actions.ENVIRONMENT_COLOR:
  988. act.color = data.readUInt24F();
  989. break;
  990. case SEA3D.Actions.CAMERA:
  991. act.camera = sea3d.getObject( data.readUInt() );
  992. break;
  993. case SEA3D.Actions.SCRIPTS:
  994. act.scripts = data.readScriptList( sea3d );
  995. break;
  996. case SEA3D.Actions.CLASS_OF:
  997. act.classof = sea3d.getObject( data.readUInt() );
  998. break;
  999. default:
  1000. console.log( "Action \"" + kind + "\" not found." );
  1001. break;
  1002. }
  1003. data.position = position + size;
  1004. }
  1005. };
  1006. SEA3D.Actions.SCENE = 0;
  1007. SEA3D.Actions.ENVIRONMENT_COLOR = 1;
  1008. SEA3D.Actions.ENVIRONMENT = 2;
  1009. SEA3D.Actions.FOG = 3;
  1010. SEA3D.Actions.PLAY_ANIMATION = 4;
  1011. SEA3D.Actions.PLAY_SOUND = 5;
  1012. SEA3D.Actions.ANIMATION_AUDIO_SYNC = 6;
  1013. SEA3D.Actions.LOOK_AT = 7;
  1014. SEA3D.Actions.RTT_TARGET = 8;
  1015. SEA3D.Actions.CAMERA = 9;
  1016. SEA3D.Actions.SCRIPTS = 10;
  1017. SEA3D.Actions.CLASS_OF = 11;
  1018. SEA3D.Actions.prototype.type = "act";
  1019. //
  1020. // Properties
  1021. //
  1022. SEA3D.Properties = function( name, data, sea3d ) {
  1023. this.name = name;
  1024. this.data = data;
  1025. this.sea3d = sea3d;
  1026. this.tag = data.readProperties( sea3d );
  1027. this.tag.__name = name;
  1028. };
  1029. SEA3D.Properties.prototype.type = "prop";
  1030. //
  1031. // File Info
  1032. //
  1033. SEA3D.FileInfo = function( name, data, sea3d ) {
  1034. this.name = name;
  1035. this.data = data;
  1036. this.sea3d = sea3d;
  1037. this.tag = data.readProperties( sea3d );
  1038. this.tag.__name = name;
  1039. sea3d.info = this.tag;
  1040. };
  1041. SEA3D.FileInfo.prototype.type = "info";
  1042. //
  1043. // Java Script
  1044. //
  1045. SEA3D.JavaScript = function( name, data, sea3d ) {
  1046. this.name = name;
  1047. this.data = data;
  1048. this.sea3d = sea3d;
  1049. this.src = data.readUTF( data.length );
  1050. };
  1051. SEA3D.JavaScript.prototype.type = "js";
  1052. //
  1053. // Java Script Method
  1054. //
  1055. SEA3D.JavaScriptMethod = function( name, data, sea3d ) {
  1056. this.name = name;
  1057. this.data = data;
  1058. this.sea3d = sea3d;
  1059. var count = data.readUShort();
  1060. this.methods = {};
  1061. for ( var i = 0; i < count; i ++ ) {
  1062. var flag = data.readUByte();
  1063. var method = data.readUTF8();
  1064. this.methods[ method ] = {
  1065. src : data.readUTF8Long()
  1066. }
  1067. }
  1068. };
  1069. SEA3D.JavaScriptMethod.prototype.type = "jsm";
  1070. //
  1071. // GLSL
  1072. //
  1073. SEA3D.GLSL = function( name, data, sea3d ) {
  1074. this.name = name;
  1075. this.data = data;
  1076. this.sea3d = sea3d;
  1077. this.src = data.readUTF( data.length );
  1078. };
  1079. SEA3D.GLSL.prototype.type = "glsl";
  1080. //
  1081. // Dummy
  1082. //
  1083. SEA3D.Dummy = function( name, data, sea3d ) {
  1084. SEA3D.Object3D.call( this, name, data, sea3d );
  1085. this.transform = data.readMatrix();
  1086. this.width = data.readFloat();
  1087. this.height = data.readFloat();
  1088. this.depth = data.readFloat();
  1089. data.readTags( this.readTag.bind( this ) );
  1090. };
  1091. SEA3D.Dummy.prototype = Object.create( SEA3D.Object3D.prototype );
  1092. SEA3D.Dummy.prototype.constructor = SEA3D.Dummy;
  1093. SEA3D.Dummy.prototype.type = "dmy";
  1094. //
  1095. // Line
  1096. //
  1097. SEA3D.Line = function( name, data, sea3d ) {
  1098. SEA3D.Object3D.call( this, name, data, sea3d );
  1099. this.count = ( this.attrib & 64 ? data.readUInt() : data.readUShort() ) * 3;
  1100. this.closed = ( this.attrib & 128 ) != 0;
  1101. this.transform = data.readMatrix();
  1102. this.vertex = [];
  1103. var i = 0;
  1104. while ( i < this.count ) {
  1105. this.vertex[ i ++ ] = data.readFloat();
  1106. }
  1107. data.readTags( this.readTag.bind( this ) );
  1108. };
  1109. SEA3D.Line.prototype = Object.create( SEA3D.Object3D.prototype );
  1110. SEA3D.Line.prototype.constructor = SEA3D.Line;
  1111. SEA3D.Line.prototype.type = "line";
  1112. //
  1113. // Sprite
  1114. //
  1115. SEA3D.Sprite = function( name, data, sea3d ) {
  1116. SEA3D.Object3D.call( this, name, data, sea3d );
  1117. if ( this.attrib & 256 ) {
  1118. this.material = sea3d.getObject( data.readUInt() );
  1119. }
  1120. this.position = data.readVector3();
  1121. this.width = data.readFloat();
  1122. this.height = data.readFloat();
  1123. data.readTags( this.readTag.bind( this ) );
  1124. };
  1125. SEA3D.Sprite.prototype = Object.create( SEA3D.Object3D.prototype );
  1126. SEA3D.Sprite.prototype.constructor = SEA3D.Sprite;
  1127. SEA3D.Sprite.prototype.type = "m2d";
  1128. //
  1129. // Mesh
  1130. //
  1131. SEA3D.Mesh = function( name, data, sea3d ) {
  1132. SEA3D.Entity3D.call( this, name, data, sea3d );
  1133. // MATERIAL
  1134. if ( this.attrib & 256 ) {
  1135. this.material = [];
  1136. var len = data.readUByte();
  1137. if ( len == 1 ) this.material[ 0 ] = sea3d.getObject( data.readUInt() );
  1138. else {
  1139. var i = 0;
  1140. while ( i < len ) {
  1141. var matIndex = data.readUInt();
  1142. if ( matIndex > 0 ) this.material[ i ++ ] = sea3d.getObject( matIndex - 1 );
  1143. else this.material[ i ++ ] = undefined;
  1144. }
  1145. }
  1146. }
  1147. if ( this.attrib & 512 ) {
  1148. this.modifiers = [];
  1149. var len = data.readUByte();
  1150. for ( var i = 0; i < len; i ++ ) {
  1151. this.modifiers[ i ] = sea3d.getObject( data.readUInt() );
  1152. }
  1153. }
  1154. if ( this.attrib & 1024 ) {
  1155. this.reference = {
  1156. type : data.readUByte(),
  1157. ref : sea3d.getObject( data.readUInt() )
  1158. };
  1159. }
  1160. this.transform = data.readMatrix();
  1161. this.geometry = sea3d.getObject( data.readUInt() );
  1162. data.readTags( this.readTag.bind( this ) );
  1163. };
  1164. SEA3D.Mesh.prototype = Object.create( SEA3D.Entity3D.prototype );
  1165. SEA3D.Mesh.prototype.constructor = SEA3D.Mesh;
  1166. SEA3D.Mesh.prototype.type = "m3d";
  1167. //
  1168. // Skeleton
  1169. //
  1170. SEA3D.Skeleton = function( name, data, sea3d ) {
  1171. this.name = name;
  1172. this.data = data;
  1173. this.sea3d = sea3d;
  1174. var length = data.readUShort();
  1175. this.joint = [];
  1176. for ( var i = 0; i < length; i ++ ) {
  1177. this.joint[ i ] = {
  1178. name: data.readUTF8(),
  1179. parentIndex: data.readUShort() - 1,
  1180. inverseBindMatrix: data.readMatrix()
  1181. };
  1182. }
  1183. };
  1184. SEA3D.Skeleton.prototype.type = "skl";
  1185. //
  1186. // Skeleton Local
  1187. //
  1188. SEA3D.SkeletonLocal = function( name, data, sea3d ) {
  1189. this.name = name;
  1190. this.data = data;
  1191. this.sea3d = sea3d;
  1192. var length = data.readUShort();
  1193. this.joint = [];
  1194. for ( var i = 0; i < length; i ++ ) {
  1195. this.joint[ i ] = {
  1196. name: data.readUTF8(),
  1197. parentIndex: data.readUShort() - 1,
  1198. // POSITION XYZ
  1199. x: data.readFloat(),
  1200. y: data.readFloat(),
  1201. z: data.readFloat(),
  1202. // QUATERNION XYZW
  1203. qx: data.readFloat(),
  1204. qy: data.readFloat(),
  1205. qz: data.readFloat(),
  1206. qw: data.readFloat()
  1207. };
  1208. }
  1209. };
  1210. SEA3D.SkeletonLocal.prototype.type = "sklq";
  1211. //
  1212. // Animation Base
  1213. //
  1214. SEA3D.AnimationBase = function( name, data, sea3d ) {
  1215. this.name = name;
  1216. this.data = data;
  1217. this.sea3d = sea3d;
  1218. var flag = data.readUByte();
  1219. this.sequence = [];
  1220. if ( flag & 1 ) {
  1221. var count = data.readUShort();
  1222. for ( var i = 0; i < count; i ++ ) {
  1223. flag = data.readUByte();
  1224. this.sequence[ i ] = {
  1225. name: data.readUTF8(),
  1226. start: data.readUInt(),
  1227. count: data.readUInt(),
  1228. repeat: ( flag & 1 ) != 0,
  1229. intrpl: ( flag & 2 ) == 0
  1230. };
  1231. }
  1232. }
  1233. this.frameRate = data.readUByte();
  1234. this.numFrames = data.readUInt();
  1235. // no contains sequence
  1236. if ( this.sequence.length == 0 ) {
  1237. this.sequence[ 0 ] = { name: "root", start: 0, count: this.numFrames, repeat: true, intrpl: true };
  1238. }
  1239. };
  1240. //
  1241. // Animation
  1242. //
  1243. SEA3D.Animation = function( name, data, sea3d ) {
  1244. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1245. this.dataList = [];
  1246. for ( var i = 0, l = data.readUByte(); i < l; i ++ ) {
  1247. var kind = data.readUShort(),
  1248. type = data.readUByte();
  1249. var anmRaw = data.readVector( type, this.numFrames, 0 );
  1250. this.dataList.push( {
  1251. kind: kind,
  1252. type: type,
  1253. blockSize: SEA3D.Stream.sizeOf( type ),
  1254. data: anmRaw
  1255. } );
  1256. }
  1257. };
  1258. SEA3D.Animation.POSITION = 0;
  1259. SEA3D.Animation.ROTATION = 1;
  1260. SEA3D.Animation.SCALE = 2;
  1261. SEA3D.Animation.COLOR = 3;
  1262. SEA3D.Animation.MULTIPLIER = 4;
  1263. SEA3D.Animation.ATTENUATION_START = 5;
  1264. SEA3D.Animation.ATTENUATION_END = 6;
  1265. SEA3D.Animation.FOV = 7;
  1266. SEA3D.Animation.OFFSET_U = 8;
  1267. SEA3D.Animation.OFFSET_V = 9;
  1268. SEA3D.Animation.SCALE_U = 10;
  1269. SEA3D.Animation.SCALE_V = 11;
  1270. SEA3D.Animation.ANGLE = 12;
  1271. SEA3D.Animation.ALPHA = 13;
  1272. SEA3D.Animation.VOLUME = 14;
  1273. SEA3D.Animation.DefaultLerpFuncs = [
  1274. SEA3D.Math.lerp3x, // POSITION
  1275. SEA3D.Math.lerpQuat4x, // ROTATION
  1276. SEA3D.Math.lerp3x, // SCALE
  1277. SEA3D.Math.lerpColor1x, // COLOR
  1278. SEA3D.Math.lerp1x, // MULTIPLIER
  1279. SEA3D.Math.lerp1x, // ATTENUATION_START
  1280. SEA3D.Math.lerp1x, // ATTENUATION_END
  1281. SEA3D.Math.lerp1x, // FOV
  1282. SEA3D.Math.lerp1x, // OFFSET_U
  1283. SEA3D.Math.lerp1x, // OFFSET_V
  1284. SEA3D.Math.lerp1x, // SCALE_U
  1285. SEA3D.Math.lerp1x, // SCALE_V
  1286. SEA3D.Math.lerpAng1x, // ANGLE
  1287. SEA3D.Math.lerp1x, // ALPHA
  1288. SEA3D.Math.lerp1x // VOLUME
  1289. ];
  1290. SEA3D.Animation.prototype = Object.create( SEA3D.AnimationBase.prototype );
  1291. SEA3D.Animation.prototype.constructor = SEA3D.Animation;
  1292. SEA3D.Animation.prototype.type = "anm";
  1293. //
  1294. // Skeleton Animation
  1295. //
  1296. SEA3D.SkeletonAnimation = function( name, data, sea3d ) {
  1297. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1298. this.name = name;
  1299. this.data = data;
  1300. this.sea3d = sea3d;
  1301. this.numJoints = data.readUShort();
  1302. this.raw = data.readFloatArray( this.numFrames * this.numJoints * 7 );
  1303. };
  1304. SEA3D.SkeletonAnimation.prototype.type = "skla";
  1305. //
  1306. // Morph
  1307. //
  1308. SEA3D.Morph = function( name, data, sea3d ) {
  1309. SEA3D.GeometryBase.call( this, name, data, sea3d );
  1310. var useVertex = ( this.attrib & 2 ) != 0;
  1311. var useNormal = ( this.attrib & 4 ) != 0;
  1312. var nodeCount = data.readUShort();
  1313. this.node = [];
  1314. for ( var i = 0; i < nodeCount; i ++ ) {
  1315. var nodeName = data.readUTF8(),
  1316. verts, norms;
  1317. if ( useVertex ) verts = data.readFloatArray( this.length );
  1318. if ( useNormal ) norms = data.readFloatArray( this.length );
  1319. this.node[ i ] = { vertex: verts, normal: norms, name: nodeName }
  1320. }
  1321. };
  1322. SEA3D.Morph.prototype = Object.create( SEA3D.GeometryBase.prototype );
  1323. SEA3D.Morph.prototype.constructor = SEA3D.Morph;
  1324. SEA3D.Morph.prototype.type = "mph";
  1325. //
  1326. // Vertex Animation
  1327. //
  1328. SEA3D.VertexAnimation = function( name, data, sea3d ) {
  1329. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1330. var flags = data.readUByte();
  1331. this.isBig = ( flags & 1 ) != 0;
  1332. data.readVInt = this.isBig ? data.readUInt : data.readUShort;
  1333. this.numVertex = data.readVInt();
  1334. this.length = this.numVertex * 3;
  1335. var useVertex = ( flags & 2 ) != 0;
  1336. var useNormal = ( flags & 4 ) != 0;
  1337. this.frame = [];
  1338. var i, verts, norms;
  1339. for ( i = 0; i < this.numFrames; i ++ ) {
  1340. if ( useVertex ) verts = data.readFloatArray( this.length );
  1341. if ( useNormal ) norms = data.readFloatArray( this.length );
  1342. this.frame[ i ] = { vertex: verts, normal: norms }
  1343. }
  1344. };
  1345. SEA3D.VertexAnimation.prototype = Object.create( SEA3D.AnimationBase.prototype );
  1346. SEA3D.VertexAnimation.prototype.constructor = SEA3D.VertexAnimation;
  1347. SEA3D.VertexAnimation.prototype.type = "vtxa";
  1348. //
  1349. // Camera
  1350. //
  1351. SEA3D.Camera = function( name, data, sea3d ) {
  1352. SEA3D.Object3D.call( this, name, data, sea3d );
  1353. if ( this.attrib & 64 ) {
  1354. this.dof = {
  1355. distance: data.readFloat(),
  1356. range: data.readFloat()
  1357. };
  1358. }
  1359. this.transform = data.readMatrix();
  1360. this.fov = data.readFloat();
  1361. data.readTags( this.readTag.bind( this ) );
  1362. };
  1363. SEA3D.Camera.prototype = Object.create( SEA3D.Object3D.prototype );
  1364. SEA3D.Camera.prototype.constructor = SEA3D.Camera;
  1365. SEA3D.Camera.prototype.type = "cam";
  1366. //
  1367. // Orthographic Camera
  1368. //
  1369. SEA3D.OrthographicCamera = function( name, data, sea3d ) {
  1370. SEA3D.Object3D.call( this, name, data, sea3d );
  1371. this.transform = data.readMatrix();
  1372. this.height = data.readFloat();
  1373. data.readTags( this.readTag.bind( this ) );
  1374. };
  1375. SEA3D.OrthographicCamera.prototype = Object.create( SEA3D.Object3D.prototype );
  1376. SEA3D.OrthographicCamera.prototype.constructor = SEA3D.OrthographicCamera;
  1377. SEA3D.OrthographicCamera.prototype.type = "camo";
  1378. //
  1379. // Joint Object
  1380. //
  1381. SEA3D.JointObject = function( name, data, sea3d ) {
  1382. SEA3D.Object3D.call( this, name, data, sea3d );
  1383. this.target = sea3d.getObject( data.readUInt() );
  1384. this.joint = data.readUShort();
  1385. data.readTags( this.readTag.bind( this ) );
  1386. };
  1387. SEA3D.JointObject.prototype = Object.create( SEA3D.Object3D.prototype );
  1388. SEA3D.JointObject.prototype.constructor = SEA3D.JointObject;
  1389. SEA3D.JointObject.prototype.type = "jnt";
  1390. //
  1391. // Light
  1392. //
  1393. SEA3D.Light = function( name, data, sea3d ) {
  1394. SEA3D.Object3D.call( this, name, data, sea3d );
  1395. this.attenStart = Number.MAX_VALUE;
  1396. this.attenEnd = Number.MAX_VALUE;
  1397. if ( this.attrib & 64 ) {
  1398. var shadowHeader = data.readUByte();
  1399. this.shadow = {}
  1400. this.shadow.opacity = shadowHeader & 1 ? data.readFloat() : 1;
  1401. this.shadow.color = shadowHeader & 2 ? data.readUInt24() : 0x000000;
  1402. }
  1403. if ( this.attrib & 512 ) {
  1404. this.attenStart = data.readFloat();
  1405. this.attenEnd = data.readFloat();
  1406. }
  1407. this.color = data.readUInt24();
  1408. this.multiplier = data.readFloat();
  1409. };
  1410. SEA3D.Light.prototype = Object.create( SEA3D.Object3D.prototype );
  1411. SEA3D.Light.prototype.constructor = SEA3D.Light;
  1412. //
  1413. // Point Light
  1414. //
  1415. SEA3D.PointLight = function( name, data, sea3d ) {
  1416. SEA3D.Light.call( this, name, data, sea3d );
  1417. if ( this.attrib & 128 ) {
  1418. this.attenuation = {
  1419. start: data.readFloat(),
  1420. end: data.readFloat()
  1421. }
  1422. }
  1423. this.position = data.readVector3();
  1424. data.readTags( this.readTag.bind( this ) );
  1425. };
  1426. SEA3D.PointLight.prototype = Object.create( SEA3D.Light.prototype );
  1427. SEA3D.PointLight.prototype.constructor = SEA3D.PointLight;
  1428. SEA3D.PointLight.prototype.type = "plht";
  1429. //
  1430. // Hemisphere Light
  1431. //
  1432. SEA3D.HemisphereLight = function( name, data, sea3d ) {
  1433. SEA3D.Light.call( this, name, data, sea3d );
  1434. if ( this.attrib & 128 ) {
  1435. this.attenuation = {
  1436. start: data.readFloat(),
  1437. end: data.readFloat()
  1438. }
  1439. }
  1440. this.secondColor = data.readUInt24();
  1441. data.readTags( this.readTag.bind( this ) );
  1442. };
  1443. SEA3D.HemisphereLight.prototype = Object.create( SEA3D.Light.prototype );
  1444. SEA3D.HemisphereLight.prototype.constructor = SEA3D.HemisphereLight;
  1445. SEA3D.HemisphereLight.prototype.type = "hlht";
  1446. //
  1447. // Ambient Light
  1448. //
  1449. SEA3D.AmbientLight = function( name, data, sea3d ) {
  1450. SEA3D.Light.call( this, name, data, sea3d );
  1451. data.readTags( this.readTag.bind( this ) );
  1452. };
  1453. SEA3D.AmbientLight.prototype = Object.create( SEA3D.Light.prototype );
  1454. SEA3D.AmbientLight.prototype.constructor = SEA3D.AmbientLight;
  1455. SEA3D.AmbientLight.prototype.type = "alht";
  1456. //
  1457. // Directional Light
  1458. //
  1459. SEA3D.DirectionalLight = function( name, data, sea3d ) {
  1460. SEA3D.Light.call( this, name, data, sea3d );
  1461. this.transform = data.readMatrix();
  1462. data.readTags( this.readTag.bind( this ) );
  1463. };
  1464. SEA3D.DirectionalLight.prototype = Object.create( SEA3D.Light.prototype );
  1465. SEA3D.DirectionalLight.prototype.constructor = SEA3D.DirectionalLight;
  1466. SEA3D.DirectionalLight.prototype.type = "dlht";
  1467. //
  1468. // Material
  1469. //
  1470. SEA3D.Material = function( name, data, sea3d ) {
  1471. this.name = name;
  1472. this.data = data;
  1473. this.sea3d = sea3d;
  1474. this.technique = [];
  1475. this.attrib = data.readUShort();
  1476. this.alpha = 1;
  1477. this.blendMode = "normal";
  1478. this.alphaThreshold = .5;
  1479. this.physical = false;
  1480. this.anisotropy = false;
  1481. this.bothSides = ( this.attrib & 1 ) != 0;
  1482. this.receiveLights = ( this.attrib & 2 ) == 0;
  1483. this.receiveShadows = ( this.attrib & 4 ) == 0;
  1484. this.receiveFog = ( this.attrib & 8 ) == 0;
  1485. this.smooth = ( this.attrib & 16 ) == 0;
  1486. if ( this.attrib & 32 )
  1487. this.alpha = data.readFloat();
  1488. if ( this.attrib & 64 )
  1489. this.blendMode = data.readBlendMode();
  1490. if ( this.attrib & 128 )
  1491. this.animations = data.readAnimationList( sea3d );
  1492. this.depthMask = ( this.attrib & 256 ) == 0;
  1493. this.depthTest = ( this.attrib & 512 ) == 0;
  1494. var count = data.readUByte();
  1495. for ( var i = 0; i < count; ++ i ) {
  1496. var kind = data.readUShort();
  1497. var size = data.readUShort();
  1498. var pos = data.position;
  1499. var tech, methodAttrib;
  1500. switch ( kind ) {
  1501. case SEA3D.Material.PHONG:
  1502. tech = {
  1503. ambientColor: data.readUInt24(),
  1504. diffuseColor: data.readUInt24(),
  1505. specularColor: data.readUInt24(),
  1506. specular: data.readFloat(),
  1507. gloss: data.readFloat()
  1508. };
  1509. break;
  1510. case SEA3D.Material.PHYSICAL:
  1511. this.physical = true;
  1512. tech = {
  1513. color: data.readUInt24(),
  1514. roughness: data.readFloat(),
  1515. metalness: data.readFloat()
  1516. };
  1517. break;
  1518. case SEA3D.Material.ANISOTROPIC:
  1519. this.anisotropy = true;
  1520. break;
  1521. case SEA3D.Material.COMPOSITE_TEXTURE:
  1522. tech = {
  1523. composite: sea3d.getObject( data.readUInt() )
  1524. };
  1525. break;
  1526. case SEA3D.Material.DIFFUSE_MAP:
  1527. tech = {
  1528. texture: sea3d.getObject( data.readUInt() )
  1529. };
  1530. break;
  1531. case SEA3D.Material.SPECULAR_MAP:
  1532. tech = {
  1533. texture: sea3d.getObject( data.readUInt() )
  1534. };
  1535. break;
  1536. case SEA3D.Material.NORMAL_MAP:
  1537. tech = {
  1538. texture: sea3d.getObject( data.readUInt() )
  1539. };
  1540. break;
  1541. case SEA3D.Material.REFLECTION:
  1542. case SEA3D.Material.FRESNEL_REFLECTION:
  1543. tech = {
  1544. texture: sea3d.getObject( data.readUInt() ),
  1545. alpha: data.readFloat()
  1546. };
  1547. if ( kind == SEA3D.Material.FRESNEL_REFLECTION ) {
  1548. tech.power = data.readFloat();
  1549. tech.normal = data.readFloat();
  1550. }
  1551. break;
  1552. case SEA3D.Material.REFRACTION:
  1553. tech = {
  1554. texture: sea3d.getObject( data.readUInt() ),
  1555. alpha: data.readFloat(),
  1556. ior: data.readFloat()
  1557. };
  1558. break;
  1559. case SEA3D.Material.RIM:
  1560. tech = {
  1561. color: data.readUInt24(),
  1562. strength: data.readFloat(),
  1563. power: data.readFloat(),
  1564. blendMode: data.readBlendMode()
  1565. };
  1566. break;
  1567. case SEA3D.Material.LIGHT_MAP:
  1568. tech = {
  1569. texture: sea3d.getObject( data.readUInt() ),
  1570. channel: data.readUByte(),
  1571. blendMode: data.readBlendMode()
  1572. };
  1573. break;
  1574. case SEA3D.Material.DETAIL_MAP:
  1575. tech = {
  1576. texture: sea3d.getObject( data.readUInt() ),
  1577. scale: data.readFloat(),
  1578. blendMode: data.readBlendMode()
  1579. };
  1580. break;
  1581. case SEA3D.Material.CEL:
  1582. tech = {
  1583. color: data.readUInt24(),
  1584. levels: data.readUByte(),
  1585. size: data.readFloat(),
  1586. specularCutOff: data.readFloat(),
  1587. smoothness: data.readFloat()
  1588. };
  1589. break;
  1590. case SEA3D.Material.TRANSLUCENT:
  1591. tech = {
  1592. translucency: data.readFloat(),
  1593. scattering: data.readFloat()
  1594. };
  1595. break;
  1596. case SEA3D.Material.BLEND_NORMAL_MAP:
  1597. methodAttrib = data.readUByte();
  1598. tech = {
  1599. texture: sea3d.getObject( data.readUInt() ),
  1600. secondaryTexture: sea3d.getObject( data.readUInt() )
  1601. };
  1602. if ( methodAttrib & 1 ) {
  1603. tech.offsetX0 = data.readFloat();
  1604. tech.offsetY0 = data.readFloat();
  1605. tech.offsetX1 = data.readFloat();
  1606. tech.offsetY1 = data.readFloat();
  1607. }
  1608. else {
  1609. tech.offsetX0 = tech.offsetY0 =
  1610. tech.offsetX1 = tech.offsetY1 = 0
  1611. }
  1612. tech.animate = methodAttrib & 2;
  1613. break;
  1614. case SEA3D.Material.MIRROR_REFLECTION:
  1615. tech = {
  1616. texture: sea3d.getObject( data.readUInt() ),
  1617. alpha: data.readFloat()
  1618. };
  1619. break;
  1620. case SEA3D.Material.AMBIENT_MAP:
  1621. tech = {
  1622. texture: sea3d.getObject( data.readUInt() )
  1623. }
  1624. break;
  1625. case SEA3D.Material.ALPHA_MAP:
  1626. tech = {
  1627. texture: sea3d.getObject( data.readUInt() )
  1628. };
  1629. break;
  1630. case SEA3D.Material.EMISSIVE:
  1631. tech = {
  1632. color: data.readUInt24()
  1633. };
  1634. break;
  1635. case SEA3D.Material.EMISSIVE_MAP:
  1636. tech = {
  1637. texture: sea3d.getObject( data.readUInt() )
  1638. };
  1639. break;
  1640. case SEA3D.Material.ROUGHNESS_MAP:
  1641. case SEA3D.Material.METALNESS_MAP:
  1642. tech = {
  1643. texture: sea3d.getObject( data.readUInt() )
  1644. };
  1645. break;
  1646. case SEA3D.Material.VERTEX_COLOR:
  1647. tech = {
  1648. blendMode: data.readBlendMode()
  1649. };
  1650. break;
  1651. case SEA3D.Material.WRAP_LIGHTING:
  1652. tech = {
  1653. color: data.readUInt24(),
  1654. strength: data.readFloat()
  1655. };
  1656. break;
  1657. case SEA3D.Material.COLOR_REPLACE:
  1658. methodAttrib = data.readUByte();
  1659. tech = {
  1660. red: data.readUInt24(),
  1661. green: data.readUInt24(),
  1662. blue: data.readUInt24F()
  1663. };
  1664. if ( methodAttrib & 1 ) tech.mask = sea3d.getObject( data.readUInt() );
  1665. if ( methodAttrib & 2 ) tech.alpha = data.readFloat();
  1666. break;
  1667. case SEA3D.Material.REFLECTION_SPHERICAL:
  1668. tech = {
  1669. texture: sea3d.getObject( data.readUInt() ),
  1670. alpha: data.readFloat()
  1671. };
  1672. break;
  1673. default:
  1674. console.warn( "SEA3D: MaterialTechnique not found:", kind.toString( 16 ) );
  1675. data.position = pos += size;
  1676. continue;
  1677. }
  1678. tech.kind = kind;
  1679. this.technique.push( tech );
  1680. data.position = pos += size;
  1681. }
  1682. };
  1683. SEA3D.Material.PHONG = 0;
  1684. SEA3D.Material.COMPOSITE_TEXTURE = 1;
  1685. SEA3D.Material.DIFFUSE_MAP = 2;
  1686. SEA3D.Material.SPECULAR_MAP = 3;
  1687. SEA3D.Material.REFLECTION = 4;
  1688. SEA3D.Material.REFRACTION = 5;
  1689. SEA3D.Material.NORMAL_MAP = 6;
  1690. SEA3D.Material.FRESNEL_REFLECTION = 7;
  1691. SEA3D.Material.RIM = 8;
  1692. SEA3D.Material.LIGHT_MAP = 9;
  1693. SEA3D.Material.DETAIL_MAP = 10;
  1694. SEA3D.Material.CEL = 11;
  1695. SEA3D.Material.TRANSLUCENT = 12;
  1696. SEA3D.Material.BLEND_NORMAL_MAP = 13;
  1697. SEA3D.Material.MIRROR_REFLECTION = 14;
  1698. SEA3D.Material.AMBIENT_MAP = 15;
  1699. SEA3D.Material.ALPHA_MAP = 16;
  1700. SEA3D.Material.EMISSIVE_MAP = 17;
  1701. SEA3D.Material.VERTEX_COLOR = 18;
  1702. SEA3D.Material.WRAP_LIGHTING = 19;
  1703. SEA3D.Material.COLOR_REPLACE = 20;
  1704. SEA3D.Material.REFLECTION_SPHERICAL = 21;
  1705. SEA3D.Material.ANISOTROPIC = 22;
  1706. SEA3D.Material.EMISSIVE = 23;
  1707. SEA3D.Material.PHYSICAL = 24;
  1708. SEA3D.Material.ROUGHNESS_MAP = 25;
  1709. SEA3D.Material.METALNESS_MAP = 26;
  1710. SEA3D.Material.prototype.type = "mat";
  1711. //
  1712. // Composite
  1713. //
  1714. SEA3D.Composite = function( name, data, sea3d ) {
  1715. this.name = name;
  1716. this.data = data;
  1717. this.sea3d = sea3d;
  1718. var layerCount = data.readUByte();
  1719. this.layer = [];
  1720. for ( var i = 0; i < layerCount; i ++ ) {
  1721. this.layer[ i ] = new SEA3D.Composite.prototype.Layer( data, sea3d );
  1722. }
  1723. };
  1724. SEA3D.Composite.prototype.getLayerByName = function( name ) {
  1725. for ( var i = 0; i < this.layer.length; i ++ ) {
  1726. if ( this.layer[ i ].name == name ) {
  1727. return this.layer[ i ];
  1728. }
  1729. }
  1730. };
  1731. SEA3D.Composite.prototype.Layer = function( data, sea3d ) {
  1732. var attrib = data.readUShort();
  1733. if ( attrib & 1 ) this.texture = new SEA3D.Composite.LayerBitmap( data, sea3d );
  1734. else this.color = data.readUInt24();
  1735. if ( attrib & 2 ) {
  1736. this.mask = new SEA3D.Composite.LayerBitmap( data, sea3d );
  1737. }
  1738. if ( attrib & 4 ) {
  1739. this.name = data.readUTF8();
  1740. }
  1741. this.blendMode = attrib & 8 ? data.readBlendMode() : "normal";
  1742. this.opacity = attrib & 16 ? data.readFloat() : 1;
  1743. };
  1744. SEA3D.Composite.LayerBitmap = function( data, sea3d ) {
  1745. this.map = sea3d.getObject( data.readUInt() );
  1746. var attrib = data.readUShort();
  1747. this.channel = attrib & 1 ? data.readUByte() : 0;
  1748. this.repeat = attrib & 2 == 0;
  1749. this.offsetU = attrib & 4 ? data.readFloat() : 0;
  1750. this.offsetV = attrib & 8 ? data.readFloat() : 0;
  1751. this.scaleU = attrib & 16 ? data.readFloat() : 1;
  1752. this.scaleV = attrib & 32 ? data.readFloat() : 1;
  1753. this.rotation = attrib & 64 ? data.readFloat() : 0;
  1754. if ( attrib & 128 ) this.animation = data.readAnimationList( sea3d );
  1755. };
  1756. SEA3D.Composite.prototype.type = "ctex";
  1757. //
  1758. // Sphere
  1759. //
  1760. SEA3D.Sphere = function( name, data, sea3d ) {
  1761. this.name = name;
  1762. this.data = data;
  1763. this.sea3d = sea3d;
  1764. this.radius = data.readFloat();
  1765. };
  1766. SEA3D.Sphere.prototype.type = "sph";
  1767. //
  1768. // Box
  1769. //
  1770. SEA3D.Box = function( name, data, sea3d ) {
  1771. this.name = name;
  1772. this.data = data;
  1773. this.sea3d = sea3d;
  1774. this.width = data.readFloat();
  1775. this.height = data.readFloat();
  1776. this.depth = data.readFloat();
  1777. };
  1778. SEA3D.Box.prototype.type = "box";
  1779. //
  1780. // Cone
  1781. //
  1782. SEA3D.Cone = function( name, data, sea3d ) {
  1783. this.name = name;
  1784. this.data = data;
  1785. this.sea3d = sea3d;
  1786. this.radius = data.readFloat();
  1787. this.height = data.readFloat();
  1788. };
  1789. SEA3D.Cone.prototype.type = "cone";
  1790. //
  1791. // Capsule
  1792. //
  1793. SEA3D.Capsule = function( name, data, sea3d ) {
  1794. this.name = name;
  1795. this.data = data;
  1796. this.sea3d = sea3d;
  1797. this.radius = data.readFloat();
  1798. this.height = data.readFloat();
  1799. };
  1800. SEA3D.Capsule.prototype.type = "cap";
  1801. //
  1802. // Cylinder
  1803. //
  1804. SEA3D.Cylinder = function( name, data, sea3d ) {
  1805. this.name = name;
  1806. this.data = data;
  1807. this.sea3d = sea3d;
  1808. this.radius = data.readFloat();
  1809. this.height = data.readFloat();
  1810. };
  1811. SEA3D.Cylinder.prototype.type = "cyl";
  1812. //
  1813. // Convex Geometry
  1814. //
  1815. SEA3D.ConvexGeometry = function( name, data, sea3d ) {
  1816. this.name = name;
  1817. this.data = data;
  1818. this.sea3d = sea3d;
  1819. this.geometry = sea3d.getObject( data.readUInt() );
  1820. this.subGeometryIndex = data.readUByte();
  1821. };
  1822. SEA3D.ConvexGeometry.prototype.type = "gs";
  1823. //
  1824. // Triangle Geometry
  1825. //
  1826. SEA3D.TriangleGeometry = function( name, data, sea3d ) {
  1827. this.name = name;
  1828. this.data = data;
  1829. this.sea3d = sea3d;
  1830. this.geometry = sea3d.getObject( data.readUInt() );
  1831. this.subGeometryIndex = data.readUByte();
  1832. };
  1833. SEA3D.TriangleGeometry.prototype.type = "sgs";
  1834. //
  1835. // Compound
  1836. //
  1837. SEA3D.Compound = function( name, data, sea3d ) {
  1838. this.name = name;
  1839. this.data = data;
  1840. this.sea3d = sea3d;
  1841. this.compounds = [];
  1842. var count = data.readUByte();
  1843. for ( var i = 0; i < count; i ++ ) {
  1844. this.compounds.push( {
  1845. shape : sea3d.getObject( data.readUInt() ),
  1846. transform : data.readMatrix()
  1847. } );
  1848. }
  1849. };
  1850. SEA3D.Compound.prototype.type = "cmps";
  1851. //
  1852. // Physics
  1853. //
  1854. SEA3D.Physics = function( name, data, sea3d ) {
  1855. this.name = name;
  1856. this.data = data;
  1857. this.sea3d = sea3d;
  1858. this.attrib = data.readUShort();
  1859. this.shape = sea3d.getObject( data.readUInt() );
  1860. if ( this.attrib & 1 ) this.target = sea3d.getObject( data.readUInt() );
  1861. else this.transform = data.readMatrix();
  1862. };
  1863. SEA3D.Physics.prototype.readTag = function( kind, data, size ) {
  1864. };
  1865. //
  1866. // Rigidy Body Base
  1867. //
  1868. SEA3D.RigidBodyBase = function( name, data, sea3d ) {
  1869. SEA3D.Physics.call( this, name, data, sea3d );
  1870. if ( this.attrib & 32 ) {
  1871. this.linearDamping = data.readFloat();
  1872. this.angularDamping = data.readFloat();
  1873. } else {
  1874. this.linearDamping = 0;
  1875. this.angularDamping = 0;
  1876. }
  1877. this.mass = data.readFloat();
  1878. this.friction = data.readFloat();
  1879. this.restitution = data.readFloat();
  1880. };
  1881. SEA3D.RigidBodyBase.prototype = Object.create( SEA3D.Physics.prototype );
  1882. SEA3D.RigidBodyBase.prototype.constructor = SEA3D.RigidBodyBase;
  1883. //
  1884. // Rigidy Body
  1885. //
  1886. SEA3D.RigidBody = function( name, data, sea3d ) {
  1887. SEA3D.RigidBodyBase.call( this, name, data, sea3d );
  1888. data.readTags( this.readTag.bind( this ) );
  1889. };
  1890. SEA3D.RigidBody.prototype = Object.create( SEA3D.RigidBodyBase.prototype );
  1891. SEA3D.RigidBody.prototype.constructor = SEA3D.RigidBody;
  1892. SEA3D.RigidBody.prototype.type = "rb";
  1893. //
  1894. // Car Controller
  1895. //
  1896. SEA3D.CarController = function( name, data, sea3d ) {
  1897. SEA3D.RigidBodyBase.call( this, name, data, sea3d );
  1898. this.suspensionStiffness = data.readFloat();
  1899. this.suspensionCompression = data.readFloat();
  1900. this.suspensionDamping = data.readFloat();
  1901. this.maxSuspensionTravelCm = data.readFloat();
  1902. this.frictionSlip = data.readFloat();
  1903. this.maxSuspensionForce = data.readFloat();
  1904. this.dampingCompression = data.readFloat();
  1905. this.dampingRelaxation = data.readFloat();
  1906. var count = data.readUByte();
  1907. this.wheel = [];
  1908. for ( var i = 0; i < count; i ++ ) {
  1909. this.wheel[ i ] = new SEA3D.CarController.Wheel( data, sea3d );
  1910. }
  1911. data.readTags( this.readTag.bind( this ) );
  1912. };
  1913. SEA3D.CarController.Wheel = function( data, sea3d ) {
  1914. this.data = data;
  1915. this.sea3d = sea3d;
  1916. var attrib = data.readUShort();
  1917. this.isFront = ( attrib & 1 ) != 0,
  1918. this.target = sea3d.getObject( data.readUInt() );
  1919. this.pos = data.readVector3();
  1920. this.dir = data.readVector3();
  1921. this.axle = data.readVector3();
  1922. this.radius = data.readFloat();
  1923. this.suspensionRestLength = data.readFloat();
  1924. };
  1925. SEA3D.CarController.prototype = Object.create( SEA3D.RigidBodyBase.prototype );
  1926. SEA3D.CarController.prototype.constructor = SEA3D.CarController;
  1927. SEA3D.CarController.prototype.type = "carc";
  1928. //
  1929. // Constraints
  1930. //
  1931. SEA3D.Constraints = function( name, data, sea3d ) {
  1932. this.name = name;
  1933. this.data = data;
  1934. this.sea3d = sea3d;
  1935. this.attrib = data.readUShort();
  1936. this.disableCollisionsBetweenBodies = this.attrib & 1 != 0;
  1937. this.targetA = sea3d.getObject( data.readUInt() );
  1938. this.pointA = data.readVector3();
  1939. if ( this.attrib & 2 ) {
  1940. this.targetB = sea3d.getObject( data.readUInt() );
  1941. this.pointB = data.readVector3();
  1942. }
  1943. };
  1944. //
  1945. // P2P Constraint
  1946. //
  1947. SEA3D.P2PConstraint = function( name, data, sea3d ) {
  1948. this.name = name;
  1949. this.data = data;
  1950. this.sea3d = sea3d;
  1951. SEA3D.Constraints.call( this, name, data, sea3d );
  1952. };
  1953. SEA3D.P2PConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  1954. SEA3D.P2PConstraint.prototype.constructor = SEA3D.P2PConstraint;
  1955. SEA3D.P2PConstraint.prototype.type = "p2pc";
  1956. //
  1957. // Hinge Constraint
  1958. //
  1959. SEA3D.HingeConstraint = function( name, data, sea3d ) {
  1960. SEA3D.Constraints.call( this, name, data, sea3d );
  1961. this.axisA = data.readVector3();
  1962. if ( this.attrib & 1 ) {
  1963. this.axisB = data.readVector3();
  1964. }
  1965. if ( this.attrib & 4 ) {
  1966. this.limit = {
  1967. low : data.readFloat(),
  1968. high : data.readFloat(),
  1969. softness : data.readFloat(),
  1970. biasFactor : data.readFloat(),
  1971. relaxationFactor : data.readFloat()
  1972. }
  1973. }
  1974. if ( this.attrib & 8 ) {
  1975. this.angularMotor = {
  1976. velocity : data.readFloat(),
  1977. impulse : data.readFloat()
  1978. }
  1979. }
  1980. };
  1981. SEA3D.HingeConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  1982. SEA3D.HingeConstraint.prototype.constructor = SEA3D.HingeConstraint;
  1983. SEA3D.HingeConstraint.prototype.type = "hnec";
  1984. //
  1985. // Cone Twist Constraint
  1986. //
  1987. SEA3D.ConeTwistConstraint = function( name, data, sea3d ) {
  1988. SEA3D.Constraints.call( this, name, data, sea3d );
  1989. this.axisA = data.readVector3();
  1990. if ( this.attrib & 1 ) {
  1991. this.axisB = data.readVector3();
  1992. }
  1993. if ( this.attrib & 4 ) {
  1994. this.limit = {
  1995. swingSpan1 : data.readFloat(),
  1996. swingSpan2 : data.readFloat(),
  1997. twistSpan : data.readFloat(),
  1998. softness : data.readFloat(),
  1999. biasFactor : data.readFloat(),
  2000. relaxationFactor : data.readFloat()
  2001. };
  2002. }
  2003. };
  2004. SEA3D.ConeTwistConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  2005. SEA3D.ConeTwistConstraint.prototype.constructor = SEA3D.ConeTwistConstraint;
  2006. SEA3D.ConeTwistConstraint.prototype.type = "ctwc";
  2007. //
  2008. // Planar Render
  2009. //
  2010. SEA3D.PlanarRender = function( name, data, sea3d ) {
  2011. this.name = name;
  2012. this.data = data;
  2013. this.sea3d = sea3d;
  2014. this.attrib = data.readUByte();
  2015. this.quality = ( this.attrib & 1 ) | ( this.attrib & 2 );
  2016. this.transform = data.readMatrix();
  2017. };
  2018. SEA3D.PlanarRender.prototype.type = "rttp";
  2019. //
  2020. // Cube Render
  2021. //
  2022. SEA3D.CubeRender = function( name, data, sea3d ) {
  2023. this.name = name;
  2024. this.data = data;
  2025. this.sea3d = sea3d;
  2026. this.attrib = data.readUByte();
  2027. this.quality = ( this.attrib & 1 ) | ( this.attrib & 2 );
  2028. this.position = data.readVector3();
  2029. };
  2030. SEA3D.CubeRender.prototype.type = "rttc";
  2031. //
  2032. // Cube Maps
  2033. //
  2034. SEA3D.CubeMap = function( name, data, sea3d ) {
  2035. this.name = name;
  2036. this.data = data;
  2037. this.sea3d = sea3d;
  2038. this.transparent = false;
  2039. var ext = data.readExt();
  2040. this.faces = [];
  2041. for ( var i = 0; i < 6; i ++ ) {
  2042. var size = data.readUInt();
  2043. this.faces[ i ] = data.concat( data.position, size );
  2044. data.position += size;
  2045. }
  2046. };
  2047. SEA3D.CubeMap.prototype.type = "cmap";
  2048. //
  2049. // JPEG
  2050. //
  2051. SEA3D.JPEG = function( name, data, sea3d ) {
  2052. this.name = name;
  2053. this.data = data;
  2054. this.sea3d = sea3d;
  2055. this.transparent = false;
  2056. };
  2057. SEA3D.JPEG.prototype.type = "jpg";
  2058. //
  2059. // JPEG_XR
  2060. //
  2061. SEA3D.JPEG_XR = function( name, data, sea3d ) {
  2062. this.name = name;
  2063. this.data = data;
  2064. this.sea3d = sea3d;
  2065. this.transparent = true;
  2066. };
  2067. SEA3D.JPEG_XR.prototype.type = "wdp";
  2068. //
  2069. // PNG
  2070. //
  2071. SEA3D.PNG = function( name, data, sea3d ) {
  2072. this.name = name;
  2073. this.data = data;
  2074. this.sea3d = sea3d;
  2075. this.transparent = data.getByte( 25 ) == 0x06;
  2076. };
  2077. SEA3D.PNG.prototype.type = "png";
  2078. //
  2079. // GIF
  2080. //
  2081. SEA3D.GIF = function( name, data, sea3d ) {
  2082. this.name = name;
  2083. this.data = data;
  2084. this.sea3d = sea3d;
  2085. this.transparent = data.getByte( 11 ) > 0;
  2086. };
  2087. SEA3D.GIF.prototype.type = "gif";
  2088. //
  2089. // OGG
  2090. //
  2091. SEA3D.OGG = function( name, data, sea3d ) {
  2092. this.name = name;
  2093. this.data = data;
  2094. this.sea3d = sea3d;
  2095. };
  2096. SEA3D.OGG.prototype.type = "ogg";
  2097. //
  2098. // MP3
  2099. //
  2100. SEA3D.MP3 = function( name, data, sea3d ) {
  2101. this.name = name;
  2102. this.data = data;
  2103. this.sea3d = sea3d;
  2104. };
  2105. SEA3D.MP3.prototype.type = "mp3";
  2106. //
  2107. // FILE FORMAT
  2108. //
  2109. SEA3D.File = function( config ) {
  2110. this.config = {
  2111. streaming: true,
  2112. timeLimit: 60
  2113. };
  2114. if ( config ) {
  2115. if ( config.streaming !== undefined ) this.config.streaming = config.streaming;
  2116. if ( config.timeLimit !== undefined ) this.config.timeLimit = config.timeLimit;
  2117. }
  2118. this.version = SEA3D.VERSION;
  2119. this.objects = [];
  2120. this.typeClass = {};
  2121. this.typeRead = {};
  2122. this.typeUnique = {};
  2123. this.position =
  2124. this.dataPosition = 0;
  2125. this.scope = this;
  2126. // SEA3D
  2127. this.addClass( SEA3D.FileInfo, true );
  2128. this.addClass( SEA3D.Geometry, true );
  2129. this.addClass( SEA3D.GeometryDelta, true );
  2130. this.addClass( SEA3D.Mesh );
  2131. this.addClass( SEA3D.Sprite );
  2132. this.addClass( SEA3D.Material );
  2133. this.addClass( SEA3D.Composite );
  2134. this.addClass( SEA3D.PointLight );
  2135. this.addClass( SEA3D.DirectionalLight );
  2136. this.addClass( SEA3D.HemisphereLight );
  2137. this.addClass( SEA3D.AmbientLight );
  2138. this.addClass( SEA3D.Skeleton, true );
  2139. this.addClass( SEA3D.SkeletonLocal, true );
  2140. this.addClass( SEA3D.SkeletonAnimation, true );
  2141. this.addClass( SEA3D.JointObject );
  2142. this.addClass( SEA3D.Camera );
  2143. this.addClass( SEA3D.OrthographicCamera );
  2144. this.addClass( SEA3D.Morph, true );
  2145. this.addClass( SEA3D.VertexAnimation, true );
  2146. this.addClass( SEA3D.CubeMap, true );
  2147. this.addClass( SEA3D.Animation );
  2148. this.addClass( SEA3D.Dummy );
  2149. this.addClass( SEA3D.Line );
  2150. this.addClass( SEA3D.SoundPoint );
  2151. this.addClass( SEA3D.PlanarRender );
  2152. this.addClass( SEA3D.CubeRender );
  2153. this.addClass( SEA3D.Actions );
  2154. this.addClass( SEA3D.Container3D );
  2155. this.addClass( SEA3D.Properties );
  2156. // URL
  2157. this.addClass( SEA3D.TextureURL, true );
  2158. // PHYSICS
  2159. this.addClass( SEA3D.Sphere );
  2160. this.addClass( SEA3D.Box );
  2161. this.addClass( SEA3D.Cone );
  2162. this.addClass( SEA3D.Capsule );
  2163. this.addClass( SEA3D.Cylinder );
  2164. this.addClass( SEA3D.ConvexGeometry );
  2165. this.addClass( SEA3D.TriangleGeometry );
  2166. this.addClass( SEA3D.Compound );
  2167. this.addClass( SEA3D.RigidBody );
  2168. this.addClass( SEA3D.P2PConstraint );
  2169. this.addClass( SEA3D.HingeConstraint );
  2170. this.addClass( SEA3D.ConeTwistConstraint );
  2171. this.addClass( SEA3D.CarController );
  2172. // UNIVERSAL
  2173. this.addClass( SEA3D.JPEG, true );
  2174. this.addClass( SEA3D.JPEG_XR, true );
  2175. this.addClass( SEA3D.PNG, true );
  2176. this.addClass( SEA3D.GIF, true );
  2177. this.addClass( SEA3D.OGG, true );
  2178. this.addClass( SEA3D.MP3, true );
  2179. this.addClass( SEA3D.JavaScript, true );
  2180. this.addClass( SEA3D.JavaScriptMethod, true );
  2181. this.addClass( SEA3D.GLSL, true );
  2182. };
  2183. SEA3D.File.CompressionLibs = {};
  2184. SEA3D.File.DecompressionMethod = {}
  2185. SEA3D.File.setDecompressionEngine = function( id, name, method ) {
  2186. SEA3D.File.CompressionLibs[ id ] = name;
  2187. SEA3D.File.DecompressionMethod[ id ] = method;
  2188. };
  2189. SEA3D.File.prototype.addClass = function( clazz, unique ) {
  2190. this.typeClass[ clazz.prototype.type ] = clazz;
  2191. this.typeUnique[ clazz.prototype.type ] = unique === true;
  2192. };
  2193. SEA3D.File.prototype.readHead = function() {
  2194. if ( this.stream.bytesAvailable < 16 )
  2195. return false;
  2196. if ( this.stream.readUTF( 3 ) != "SEA" )
  2197. throw new Error( "Invalid SEA3D format." );
  2198. this.sign = this.stream.readUTF( 3 );
  2199. this.version = this.stream.readUInt24();
  2200. if ( this.stream.readUByte() != 0 ) {
  2201. throw new Error( "Protection algorithm not compatible." );
  2202. }
  2203. this.compressionID = this.stream.readUByte();
  2204. this.compressionAlgorithm = SEA3D.File.CompressionLibs[ this.compressionID ];
  2205. this.decompressionMethod = SEA3D.File.DecompressionMethod[ this.compressionID ];
  2206. if ( this.compressionID > 0 && ! this.decompressionMethod ) {
  2207. throw new Error( "Compression algorithm not compatible." );
  2208. }
  2209. this.length = this.stream.readUInt();
  2210. this.dataPosition = this.stream.position;
  2211. this.objects.length = 0;
  2212. this.state = this.readBody;
  2213. if ( this.onHead )
  2214. this.onHead( {
  2215. file: this,
  2216. sign: this.sign
  2217. } );
  2218. return true;
  2219. };
  2220. SEA3D.File.prototype.getObject = function( index ) {
  2221. return this.objects[ index ];
  2222. };
  2223. SEA3D.File.prototype.readSEAObject = function() {
  2224. if ( this.stream.bytesAvailable < 4 )
  2225. return null;
  2226. var size = this.stream.readUInt();
  2227. var position = this.stream.position;
  2228. if ( this.stream.bytesAvailable < size )
  2229. return null;
  2230. var flag = this.stream.readUByte();
  2231. var type = this.stream.readExt();
  2232. var meta = null;
  2233. var name = flag & 1 ? this.stream.readUTF8() : "",
  2234. compressed = ( flag & 2 ) != 0,
  2235. streaming = ( flag & 4 ) != 0;
  2236. if ( flag & 8 ) {
  2237. var metalen = this.stream.readUShort();
  2238. var metabytes = this.stream.concat( this.stream.position, metalen );
  2239. this.stream.position += metalen;
  2240. if ( compressed && this.decompressionMethod ) {
  2241. metabytes.buffer = this.decompressionMethod( metabytes.buffer );
  2242. }
  2243. meta = metabytes.readProperties( this );
  2244. }
  2245. size -= this.stream.position - position;
  2246. position = this.stream.position;
  2247. var data = this.stream.concat( position, size ),
  2248. obj;
  2249. if ( this.typeClass[ type ] ) {
  2250. if ( compressed && this.decompressionMethod ) {
  2251. data.buffer = this.decompressionMethod( data.buffer );
  2252. }
  2253. obj = new this.typeClass[ type ]( name, data, this );
  2254. if ( this.config.streaming && streaming && this.typeRead[ type ] ) {
  2255. this.typeRead[ type ].call( this.scope, obj );
  2256. }
  2257. }
  2258. else {
  2259. obj = new SEA3D.Object( name, data, type, this );
  2260. console.warn( "SEA3D: Unknown format \"" + type + "\" of file \"" + name + "\". Add a module referring for this format." );
  2261. }
  2262. obj.streaming = streaming;
  2263. obj.metadata = meta;
  2264. this.objects.push( this.objects[ obj.type + "/" + obj.name ] = obj );
  2265. this.dataPosition = position + size;
  2266. ++ this.position;
  2267. return obj;
  2268. };
  2269. SEA3D.File.prototype.isDone = function() {
  2270. return this.position == this.length;
  2271. };
  2272. SEA3D.File.prototype.readBody = function() {
  2273. this.timer.update();
  2274. while ( this.position < this.length ) {
  2275. if ( this.timer.deltaTime < this.config.timeLimit ) {
  2276. this.stream.position = this.dataPosition;
  2277. var sea = this.readSEAObject();
  2278. if ( sea ) this.dispatchCompleteObject( sea );
  2279. else return false;
  2280. }
  2281. else return false;
  2282. }
  2283. this.state = this.readComplete;
  2284. return true;
  2285. };
  2286. SEA3D.File.prototype.parse = function() {
  2287. this.timer = new SEA3D.Timer();
  2288. this.position = 0;
  2289. if ( isFinite( this.config.timeLimit ) ) setTimeout( this.parseObject.bind( this ), 10 );
  2290. else this.parseObject();
  2291. };
  2292. SEA3D.File.prototype.parseObject = function() {
  2293. this.timer.update();
  2294. while ( this.position < this.length && this.timer.deltaTime < this.config.timeLimit ) {
  2295. var obj = this.objects[ this.position ++ ],
  2296. type = obj.type;
  2297. if ( ! this.typeUnique[ type ] ) delete obj.tag;
  2298. if ( obj.streaming && this.typeRead[ type ] ) {
  2299. if ( obj.tag == undefined ) {
  2300. this.typeRead[ type ].call( this.scope, obj );
  2301. }
  2302. }
  2303. }
  2304. if ( this.position == this.length ) {
  2305. var elapsedTime = this.timer.elapsedTime;
  2306. var message = elapsedTime + "ms, " + this.objects.length + " objects";
  2307. if ( this.onParseComplete ) {
  2308. this.onParseComplete( {
  2309. file: this,
  2310. timeTotal: elapsedTime,
  2311. message: message
  2312. } );
  2313. } else console.log( "SEA3D Parse Complete:", message );
  2314. } else {
  2315. if ( this.onParseProgress ) {
  2316. this.onParseProgress( {
  2317. file: this,
  2318. loaded: this.position,
  2319. total: this.length,
  2320. progress: this.position / this.length
  2321. } );
  2322. }
  2323. setTimeout( this.parseObject.bind( this ), 10 );
  2324. }
  2325. };
  2326. SEA3D.File.prototype.readComplete = function() {
  2327. this.stream.position = this.dataPosition;
  2328. if ( this.stream.readUInt24F() != 0x5EA3D1 )
  2329. console.warn( "SEA3D file is corrupted." );
  2330. delete this.state;
  2331. this.dispatchComplete();
  2332. };
  2333. SEA3D.File.prototype.readState = function() {
  2334. while ( this.state && this.state() );
  2335. if ( this.state ) {
  2336. setTimeout( this.readState.bind( this ), 10 );
  2337. this.dispatchProgress();
  2338. }
  2339. };
  2340. SEA3D.File.prototype.read = function( data ) {
  2341. this.stream = new SEA3D.Stream( data );
  2342. this.timer = new SEA3D.Timer();
  2343. this.state = this.readHead;
  2344. this.readState();
  2345. };
  2346. SEA3D.File.prototype.dispatchCompleteObject = function( obj ) {
  2347. if ( ! this.onCompleteObject ) return;
  2348. this.onCompleteObject( {
  2349. file: this,
  2350. object: obj
  2351. } );
  2352. };
  2353. SEA3D.File.prototype.dispatchProgress = function() {
  2354. if ( ! this.onProgress ) return;
  2355. this.onProgress( {
  2356. file: this,
  2357. loaded: this.position,
  2358. total: this.length,
  2359. progress: this.position / this.length
  2360. } );
  2361. };
  2362. SEA3D.File.prototype.dispatchDownloadProgress = function( position, length ) {
  2363. if ( ! this.onDownloadProgress ) return;
  2364. this.onDownloadProgress( {
  2365. file: this,
  2366. loaded: position,
  2367. total: length,
  2368. progress: position / length
  2369. } );
  2370. };
  2371. SEA3D.File.prototype.dispatchComplete = function() {
  2372. var elapsedTime = this.timer.elapsedTime;
  2373. var message = elapsedTime + "ms, " + this.objects.length + " objects";
  2374. if ( this.onComplete ) this.onComplete( {
  2375. file: this,
  2376. timeTotal: elapsedTime,
  2377. message: message
  2378. } );
  2379. else console.log( "SEA3D:", message );
  2380. };
  2381. SEA3D.File.prototype.dispatchError = function( id, message ) {
  2382. if ( this.onError ) this.onError( { file: this, id: id, message: message } );
  2383. else console.error( "SEA3D: #" + id, message );
  2384. };
  2385. SEA3D.File.prototype.load = function( url ) {
  2386. var file = this,
  2387. xhr = new XMLHttpRequest();
  2388. xhr.open( "GET", url, true );
  2389. xhr.responseType = 'arraybuffer';
  2390. xhr.onprogress = function( e ) {
  2391. if ( e.lengthComputable ) {
  2392. file.dispatchDownloadProgress( e.loaded, e.total );
  2393. }
  2394. }
  2395. xhr.onreadystatechange = function() {
  2396. if ( xhr.readyState === 2 ) {
  2397. //xhr.getResponseHeader("Content-Length");
  2398. } else if ( xhr.readyState === 3 ) {
  2399. // progress
  2400. } else if ( xhr.readyState === 4 ) {
  2401. if ( xhr.status === 200 || xhr.status === 0 ) {
  2402. // complete
  2403. file.read( this.response );
  2404. } else {
  2405. this.dispatchError( 1001, "Couldn't load [" + url + "] [" + xhr.status + "]" );
  2406. }
  2407. }
  2408. }
  2409. xhr.send();
  2410. };