SEA3D.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309
  1. /**
  2. * SEA3D SDK
  3. * @author Sunag / http://www.sunag.com.br/
  4. */
  5. 'use strict';
  6. var SEA3D = { VERSION: 18110 };
  7. SEA3D.getVersion = function () {
  8. // Max = 16777215 - VV.S.S.BB | 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.data = new DataView( val );
  78. },
  79. get buffer() {
  80. return this.buf;
  81. },
  82. get bytesAvailable() {
  83. return this.length - this.position;
  84. },
  85. get length() {
  86. return this.buf.byteLength;
  87. }
  88. };
  89. SEA3D.Stream.prototype.getByte = function ( pos ) {
  90. return this.data.getInt8( pos );
  91. };
  92. SEA3D.Stream.prototype.readBytes = function ( len ) {
  93. var buf = this.buf.slice( this.position, this.position + len );
  94. this.position += len;
  95. return buf;
  96. };
  97. SEA3D.Stream.prototype.readByte = function () {
  98. return this.data.getInt8( this.position ++ );
  99. };
  100. SEA3D.Stream.prototype.readUByte = function () {
  101. return this.data.getUint8( this.position ++ );
  102. };
  103. SEA3D.Stream.prototype.readBool = function () {
  104. return this.data.getInt8( this.position ++ ) != 0;
  105. };
  106. SEA3D.Stream.prototype.readShort = function () {
  107. var v = this.data.getInt16( this.position, true );
  108. this.position += 2;
  109. return v;
  110. };
  111. SEA3D.Stream.prototype.readUShort = function () {
  112. var v = this.data.getUint16( this.position, true );
  113. this.position += 2;
  114. return v;
  115. };
  116. SEA3D.Stream.prototype.readUInt24 = function () {
  117. var v = this.data.getUint32( this.position, true ) & 0xFFFFFF;
  118. this.position += 3;
  119. return v;
  120. };
  121. SEA3D.Stream.prototype.readUInt24F = function () {
  122. return this.readUShort() | ( this.readUByte() << 16 );
  123. };
  124. SEA3D.Stream.prototype.readInt = function () {
  125. var v = this.data.getInt32( this.position, true );
  126. this.position += 4;
  127. return v;
  128. };
  129. SEA3D.Stream.prototype.readUInt = function () {
  130. var v = this.data.getUint32( this.position, true );
  131. this.position += 4;
  132. return v;
  133. };
  134. SEA3D.Stream.prototype.readFloat = function () {
  135. var v = this.data.getFloat32( this.position, true );
  136. this.position += 4;
  137. return v;
  138. };
  139. SEA3D.Stream.prototype.readUInteger = function () {
  140. var v = this.readUByte(),
  141. r = v & 0x7F;
  142. if ( ( v & 0x80 ) != 0 ) {
  143. v = this.readUByte();
  144. r |= ( v & 0x7F ) << 7;
  145. if ( ( v & 0x80 ) != 0 ) {
  146. v = this.readUByte();
  147. r |= ( v & 0x7F ) << 13;
  148. }
  149. }
  150. return r;
  151. };
  152. SEA3D.Stream.prototype.readVector2 = function () {
  153. return { x: this.readFloat(), y: this.readFloat() };
  154. };
  155. SEA3D.Stream.prototype.readVector3 = function () {
  156. return { x: this.readFloat(), y: this.readFloat(), z: this.readFloat() };
  157. };
  158. SEA3D.Stream.prototype.readVector4 = function () {
  159. return { x: this.readFloat(), y: this.readFloat(), z: this.readFloat(), w: this.readFloat() };
  160. };
  161. SEA3D.Stream.prototype.readMatrix = function () {
  162. var mtx = new Float32Array( 16 );
  163. mtx[ 0 ] = this.readFloat();
  164. mtx[ 1 ] = this.readFloat();
  165. mtx[ 2 ] = this.readFloat();
  166. mtx[ 3 ] = 0.0;
  167. mtx[ 4 ] = this.readFloat();
  168. mtx[ 5 ] = this.readFloat();
  169. mtx[ 6 ] = this.readFloat();
  170. mtx[ 7 ] = 0.0;
  171. mtx[ 8 ] = this.readFloat();
  172. mtx[ 9 ] = this.readFloat();
  173. mtx[ 10 ] = this.readFloat();
  174. mtx[ 11 ] = 0.0;
  175. mtx[ 12 ] = this.readFloat();
  176. mtx[ 13 ] = this.readFloat();
  177. mtx[ 14 ] = this.readFloat();
  178. mtx[ 15 ] = 1.0;
  179. return mtx;
  180. };
  181. SEA3D.Stream.prototype.readUTF8 = function ( len ) {
  182. var buffer = this.readBytes( len );
  183. return THREE.LoaderUtils.decodeText( new Uint8Array( buffer ) );
  184. };
  185. SEA3D.Stream.prototype.readExt = function () {
  186. return this.readUTF8( 4 ).replace( /\0/g, "" );
  187. };
  188. SEA3D.Stream.prototype.readUTF8Tiny = function () {
  189. return this.readUTF8( this.readUByte() );
  190. };
  191. SEA3D.Stream.prototype.readUTF8Short = function () {
  192. return this.readUTF8( this.readUShort() );
  193. };
  194. SEA3D.Stream.prototype.readUTF8Long = function () {
  195. return this.readUTF8( this.readUInt() );
  196. };
  197. SEA3D.Stream.prototype.readUByteArray = function ( length ) {
  198. var v = new Uint8Array( length );
  199. SEA3D.Stream.memcpy(
  200. v.buffer,
  201. 0,
  202. this.buffer,
  203. this.position,
  204. length
  205. );
  206. this.position += length;
  207. return v;
  208. };
  209. SEA3D.Stream.prototype.readUShortArray = function ( length ) {
  210. var v = new Uint16Array( length ),
  211. len = length * 2;
  212. SEA3D.Stream.memcpy(
  213. v.buffer,
  214. 0,
  215. this.buffer,
  216. this.position,
  217. len
  218. );
  219. this.position += len;
  220. return v;
  221. };
  222. SEA3D.Stream.prototype.readUInt24Array = function ( length ) {
  223. var v = new Uint32Array( length );
  224. for ( var i = 0; i < length; i ++ ) {
  225. v[ i ] = this.readUInt24();
  226. }
  227. return v;
  228. };
  229. SEA3D.Stream.prototype.readUIntArray = function ( length ) {
  230. var v = new Uint32Array( length ),
  231. len = length * 4;
  232. SEA3D.Stream.memcpy(
  233. v.buffer,
  234. 0,
  235. this.buffer,
  236. this.position,
  237. len
  238. );
  239. this.position += len;
  240. return v;
  241. };
  242. SEA3D.Stream.prototype.readFloatArray = function ( length ) {
  243. var v = new Float32Array( length ),
  244. len = length * 4;
  245. SEA3D.Stream.memcpy(
  246. v.buffer,
  247. 0,
  248. this.buffer,
  249. this.position,
  250. len
  251. );
  252. this.position += len;
  253. return v;
  254. };
  255. SEA3D.Stream.prototype.readBlendMode = function () {
  256. return SEA3D.Stream.BLEND_MODE[ this.readUByte() ];
  257. };
  258. SEA3D.Stream.prototype.readInterpolation = function () {
  259. return SEA3D.Stream.INTERPOLATION_TABLE[ this.readUByte() ];
  260. };
  261. SEA3D.Stream.prototype.readTags = function ( callback ) {
  262. var numTag = this.readUByte();
  263. for ( var i = 0; i < numTag; ++ i ) {
  264. var kind = this.readUShort();
  265. var size = this.readUInt();
  266. var pos = this.position;
  267. callback( kind, this, size );
  268. this.position = pos += size;
  269. }
  270. };
  271. SEA3D.Stream.prototype.readProperties = function ( sea3d ) {
  272. var count = this.readUByte(),
  273. props = {}, types = {};
  274. props.__type = types;
  275. for ( var i = 0; i < count; i ++ ) {
  276. var name = this.readUTF8Tiny(),
  277. type = this.readUByte();
  278. types[ name ] = type;
  279. props[ name ] = type == SEA3D.Stream.GROUP ? this.readProperties( sea3d ) : this.readToken( type, sea3d );
  280. }
  281. return props;
  282. };
  283. SEA3D.Stream.prototype.readAnimationList = function ( sea3d ) {
  284. var list = [],
  285. count = this.readUByte();
  286. var i = 0;
  287. while ( i < count ) {
  288. var attrib = this.readUByte(),
  289. anm = {};
  290. anm.relative = ( attrib & 1 ) != 0;
  291. if ( attrib & 2 ) anm.timeScale = this.readFloat();
  292. anm.tag = sea3d.getObject( this.readUInt() );
  293. list[ i ++ ] = anm;
  294. }
  295. return list;
  296. };
  297. SEA3D.Stream.prototype.readScriptList = function ( sea3d ) {
  298. var list = [],
  299. count = this.readUByte();
  300. var i = 0;
  301. while ( i < count ) {
  302. var attrib = this.readUByte(),
  303. script = {};
  304. script.priority = ( attrib & 1 ) | ( attrib & 2 );
  305. if ( attrib & 4 ) {
  306. var numParams = this.readUByte();
  307. script.params = {};
  308. for ( var j = 0; j < numParams; j ++ ) {
  309. var name = this.readUTF8Tiny();
  310. script.params[ name ] = this.readObject( sea3d );
  311. }
  312. }
  313. if ( attrib & 8 ) {
  314. script.method = this.readUTF8Tiny();
  315. }
  316. script.tag = sea3d.getObject( this.readUInt() );
  317. list[ i ++ ] = script;
  318. }
  319. return list;
  320. };
  321. SEA3D.Stream.prototype.readObject = function ( sea3d ) {
  322. return this.readToken( this.readUByte(), sea3d );
  323. };
  324. SEA3D.Stream.prototype.readToken = function ( type, sea3d ) {
  325. switch ( type ) {
  326. // 1D
  327. case SEA3D.Stream.BOOLEAN:
  328. return this.readBool();
  329. break;
  330. case SEA3D.Stream.UBYTE:
  331. return this.readUByte();
  332. break;
  333. case SEA3D.Stream.USHORT:
  334. return this.readUShort();
  335. break;
  336. case SEA3D.Stream.UINT24:
  337. return this.readUInt24();
  338. break;
  339. case SEA3D.Stream.INT:
  340. return this.readInt();
  341. break;
  342. case SEA3D.Stream.UINT:
  343. return this.readUInt();
  344. break;
  345. case SEA3D.Stream.FLOAT:
  346. return this.readFloat();
  347. break;
  348. // 3D
  349. case SEA3D.Stream.VECTOR3D:
  350. return this.readVector3();
  351. break;
  352. // 4D
  353. case SEA3D.Stream.VECTOR4D:
  354. return this.readVector4();
  355. break;
  356. // Undefined Values
  357. case SEA3D.Stream.STRING_TINY:
  358. return this.readUTF8Tiny();
  359. break;
  360. case SEA3D.Stream.STRING_SHORT:
  361. return this.readUTF8Short();
  362. break;
  363. case SEA3D.Stream.STRING_LONG:
  364. return this.readUTF8Long();
  365. break;
  366. case SEA3D.Stream.ASSET:
  367. var asset = this.readUInt();
  368. return asset > 0 ? sea3d.getObject( asset - 1 ) : null;
  369. break;
  370. default:
  371. console.error( "DataType not found!" );
  372. }
  373. return null;
  374. };
  375. SEA3D.Stream.prototype.readVector = function ( type, length, offset ) {
  376. var size = SEA3D.Stream.sizeOf( type ),
  377. i = offset * size,
  378. count = i + ( length * size );
  379. switch ( type ) {
  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 buffer = new ArrayBuffer( this.data.byteLength + data.byteLength );
  403. SEA3D.Stream.memcpy( buffer, 0, this.data.buffer, 0, this.data.byteLength );
  404. SEA3D.Stream.memcpy( buffer, this.data.byteLength, data, 0, data.byteLength );
  405. this.buffer = buffer;
  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. RAD_TO_DEG: 180 / Math.PI,
  445. DEG_TO_RAD: 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.angleDiff = function ( a, b ) {
  457. a *= this.DEG_TO_RAD;
  458. b *= this.DEG_TO_RAD;
  459. return Math.atan2( Math.sin( a - b ), Math.cos( a - b ) ) * this.RAD_TO_DEG;
  460. };
  461. SEA3D.Math.angleArea = function ( angle, target, area ) {
  462. return Math.abs( this.angleDiff( angle, target ) ) <= area;
  463. };
  464. SEA3D.Math.direction = function ( x1, y1, x2, y2 ) {
  465. return Math.atan2( y2 - y1, x2 - x1 );
  466. };
  467. SEA3D.Math.physicalLerp = function ( val, to, deltaTime, duration ) {
  468. var t = deltaTime / duration;
  469. if ( t > 1 ) t = 1;
  470. return val + ( ( to - val ) * t );
  471. };
  472. SEA3D.Math.physicalAngle = function ( val, to, deltaTime, duration ) {
  473. if ( Math.abs( val - to ) > 180 ) {
  474. if ( val > to ) {
  475. to += 360;
  476. } else {
  477. to -= 360;
  478. }
  479. }
  480. var t = deltaTime / duration;
  481. if ( t > 1 ) t = 1;
  482. return this.angle( val + ( ( to - val ) * t ) );
  483. };
  484. SEA3D.Math.zero = function ( value, precision ) {
  485. precision = precision || 1.0E-3;
  486. var pValue = value < 0 ? - value : value;
  487. if ( pValue - precision < 0 ) value = 0;
  488. return value;
  489. };
  490. SEA3D.Math.round = function ( value, precision ) {
  491. precision = Math.pow( 10, precision );
  492. return Math.round( value * precision ) / precision;
  493. };
  494. SEA3D.Math.lerpAngle = function ( val, tar, t ) {
  495. if ( Math.abs( val - tar ) > 180 ) {
  496. if ( val > tar ) {
  497. tar += 360;
  498. } else {
  499. tar -= 360;
  500. }
  501. }
  502. val += ( tar - val ) * t;
  503. return SEA3D.Math.angle( val );
  504. };
  505. SEA3D.Math.lerpColor = function ( val, tar, t ) {
  506. var a0 = val >> 24 & 0xff,
  507. r0 = val >> 16 & 0xff,
  508. g0 = val >> 8 & 0xff,
  509. b0 = val & 0xff;
  510. var a1 = tar >> 24 & 0xff,
  511. r1 = tar >> 16 & 0xff,
  512. g1 = tar >> 8 & 0xff,
  513. b1 = tar & 0xff;
  514. a0 += ( a1 - a0 ) * t;
  515. r0 += ( r1 - r0 ) * t;
  516. g0 += ( g1 - g0 ) * t;
  517. b0 += ( b1 - b0 ) * t;
  518. return a0 << 24 | r0 << 16 | g0 << 8 | b0;
  519. };
  520. SEA3D.Math.lerp = function ( val, tar, t ) {
  521. return val + ( ( tar - val ) * t );
  522. };
  523. //
  524. // Timer
  525. //
  526. SEA3D.Timer = function () {
  527. this.time = this.start = Date.now();
  528. };
  529. SEA3D.Timer.prototype = {
  530. constructor: SEA3D.Timer,
  531. get now() {
  532. return Date.now();
  533. },
  534. get deltaTime() {
  535. return Date.now() - this.time;
  536. },
  537. get elapsedTime() {
  538. return Date.now() - this.start;
  539. },
  540. update: function () {
  541. this.time = Date.now();
  542. }
  543. };
  544. //
  545. // Object
  546. //
  547. SEA3D.Object = function ( name, data, type, sea3d ) {
  548. this.name = name;
  549. this.data = data;
  550. this.type = type;
  551. this.sea3d = sea3d;
  552. };
  553. //
  554. // Geometry Base
  555. //
  556. SEA3D.GeometryBase = function ( name, data, sea3d ) {
  557. this.name = name;
  558. this.data = data;
  559. this.sea3d = sea3d;
  560. this.attrib = data.readUShort();
  561. this.isBig = ( this.attrib & 1 ) != 0;
  562. // variable uint
  563. data.readVInt = this.isBig ? data.readUInt : data.readUShort;
  564. this.numVertex = data.readVInt();
  565. this.length = this.numVertex * 3;
  566. };
  567. //
  568. // Geometry
  569. //
  570. SEA3D.Geometry = function ( name, data, sea3d ) {
  571. SEA3D.GeometryBase.call( this, name, data, sea3d );
  572. var i, j, len;
  573. // NORMAL
  574. if ( this.attrib & 4 ) {
  575. this.normal = data.readFloatArray( this.length );
  576. }
  577. // TANGENT
  578. if ( this.attrib & 8 ) {
  579. this.tangent = data.readFloatArray( this.length );
  580. }
  581. // UV
  582. if ( this.attrib & 32 ) {
  583. var uvCount = data.readUByte();
  584. if ( uvCount ) {
  585. this.uv = [];
  586. len = this.numVertex * 2;
  587. i = 0;
  588. while ( i < uvCount ) {
  589. // UV VERTEX DATA
  590. this.uv[ i ++ ] = data.readFloatArray( len );
  591. }
  592. }
  593. }
  594. // JOINT-INDEXES / WEIGHTS
  595. if ( this.attrib & 64 ) {
  596. this.jointPerVertex = data.readUByte();
  597. var jntLen = this.numVertex * this.jointPerVertex;
  598. this.joint = data.readUShortArray( jntLen );
  599. this.weight = data.readFloatArray( jntLen );
  600. }
  601. // VERTEX_COLOR
  602. if ( this.attrib & 128 ) {
  603. var colorAttrib = data.readUByte();
  604. var colorCount = data.readUByte();
  605. if ( colorCount ) {
  606. this.numColor = ( ( ( colorAttrib & 64 ) >> 6 ) | ( ( colorAttrib & 128 ) >> 6 ) ) + 1;
  607. this.color = [];
  608. for ( i = 0 & 15; i < colorCount; i ++ ) {
  609. this.color.push( data.readFloatArray( this.numVertex * this.numColor ) );
  610. }
  611. }
  612. }
  613. // VERTEX
  614. this.vertex = data.readFloatArray( this.length );
  615. // SUB-MESHES
  616. var count = data.readUByte();
  617. this.groups = [];
  618. if ( this.attrib & 1024 ) {
  619. // INDEXES
  620. for ( i = 0, len = 0; i < count; i ++ ) {
  621. j = data.readVInt() * 3;
  622. this.groups.push( {
  623. start: len,
  624. count: j
  625. } );
  626. len += j;
  627. }
  628. if ( ! ( this.attrib & 2048 ) ) {
  629. this.indexes = this.isBig ? data.readUIntArray( len ) : data.readUShortArray( len );
  630. }
  631. } else {
  632. // INDEXES
  633. var stride = this.isBig ? 4 : 2,
  634. bytearray = new SEA3D.UByteArray();
  635. for ( i = 0, j = 0; i < count; i ++ ) {
  636. len = data.readVInt() * 3;
  637. this.groups.push( {
  638. start: j,
  639. count: len
  640. } );
  641. j += len;
  642. bytearray.add( data.readUByteArray( len * stride ) );
  643. }
  644. this.indexes = this.isBig ? new Uint32Array( bytearray.toBuffer() ) : new Uint16Array( bytearray.toBuffer() );
  645. }
  646. };
  647. SEA3D.Geometry.prototype = Object.create( SEA3D.GeometryBase.prototype );
  648. SEA3D.Geometry.prototype.constructor = SEA3D.Geometry;
  649. SEA3D.Geometry.prototype.type = "geo";
  650. //
  651. // Object3D
  652. //
  653. SEA3D.Object3D = function ( name, data, sea3d ) {
  654. this.name = name;
  655. this.data = data;
  656. this.sea3d = sea3d;
  657. this.isStatic = false;
  658. this.visible = true;
  659. this.attrib = data.readUShort();
  660. if ( this.attrib & 1 ) this.parent = sea3d.getObject( data.readUInt() );
  661. if ( this.attrib & 2 ) this.animations = data.readAnimationList( sea3d );
  662. if ( this.attrib & 4 ) this.scripts = data.readScriptList( sea3d );
  663. if ( this.attrib & 16 ) this.attributes = sea3d.getObject( data.readUInt() );
  664. if ( this.attrib & 32 ) {
  665. var objectType = data.readUByte();
  666. this.isStatic = ( objectType & 1 ) != 0;
  667. this.visible = ( objectType & 2 ) == 0;
  668. }
  669. };
  670. SEA3D.Object3D.prototype.readTag = function ( kind, data, size ) {
  671. };
  672. //
  673. // Entity3D
  674. //
  675. SEA3D.Entity3D = function ( name, data, sea3d ) {
  676. SEA3D.Object3D.call( this, name, data, sea3d );
  677. this.castShadows = true;
  678. if ( this.attrib & 64 ) {
  679. var lightType = data.readUByte();
  680. this.castShadows = ( lightType & 1 ) == 0;
  681. }
  682. };
  683. SEA3D.Entity3D.prototype = Object.create( SEA3D.Object3D.prototype );
  684. SEA3D.Entity3D.prototype.constructor = SEA3D.Entity3D;
  685. //
  686. // Sound3D
  687. //
  688. SEA3D.Sound3D = function ( name, data, sea3d ) {
  689. SEA3D.Object3D.call( this, name, data, sea3d );
  690. this.autoPlay = ( this.attrib & 64 ) != 0;
  691. if ( this.attrib & 128 ) this.mixer = sea3d.getObject( data.readUInt() );
  692. this.sound = sea3d.getObject( data.readUInt() );
  693. this.volume = data.readFloat();
  694. };
  695. SEA3D.Sound3D.prototype = Object.create( SEA3D.Object3D.prototype );
  696. SEA3D.Sound3D.prototype.constructor = SEA3D.Sound3D;
  697. //
  698. // Sound Point
  699. //
  700. SEA3D.SoundPoint = function ( name, data, sea3d ) {
  701. SEA3D.Sound3D.call( this, name, data, sea3d );
  702. this.position = data.readVector3();
  703. this.distance = data.readFloat();
  704. data.readTags( this.readTag.bind( this ) );
  705. };
  706. SEA3D.SoundPoint.prototype = Object.create( SEA3D.Sound3D.prototype );
  707. SEA3D.SoundPoint.prototype.constructor = SEA3D.SoundPoint;
  708. SEA3D.SoundPoint.prototype.type = "sp";
  709. //
  710. // Container3D
  711. //
  712. SEA3D.Container3D = function ( name, data, sea3d ) {
  713. SEA3D.Object3D.call( this, name, data, sea3d );
  714. this.transform = data.readMatrix();
  715. data.readTags( this.readTag.bind( this ) );
  716. };
  717. SEA3D.Container3D.prototype = Object.create( SEA3D.Object3D.prototype );
  718. SEA3D.Container3D.prototype.constructor = SEA3D.Container3D;
  719. SEA3D.Container3D.prototype.type = "c3d";
  720. //
  721. // Script URL
  722. //
  723. SEA3D.ScriptURL = function ( name, data, sea3d ) {
  724. this.name = name;
  725. this.data = data;
  726. this.sea3d = sea3d;
  727. this.url = data.readUTF8( data.length );
  728. };
  729. SEA3D.ScriptURL.prototype.type = "src";
  730. //
  731. // Texture URL
  732. //
  733. SEA3D.TextureURL = function ( name, data, sea3d ) {
  734. this.name = name;
  735. this.data = data;
  736. this.sea3d = sea3d;
  737. this.url = sea3d.config.path + data.readUTF8( data.length );
  738. };
  739. SEA3D.TextureURL.prototype.type = "urlT";
  740. //
  741. // CubeMap URL
  742. //
  743. SEA3D.CubeMapURL = function ( name, data, sea3d ) {
  744. this.name = name;
  745. this.data = data;
  746. this.sea3d = sea3d;
  747. this.faces = [];
  748. for ( var i = 0; i < 6; i ++ ) {
  749. this.faces[ i ] = data.readUTF8Tiny();
  750. }
  751. };
  752. SEA3D.CubeMapURL.prototype.type = "cURL";
  753. //
  754. // Actions
  755. //
  756. SEA3D.Actions = function ( name, data, sea3d ) {
  757. this.name = name;
  758. this.data = data;
  759. this.sea3d = sea3d;
  760. this.count = data.readUInt();
  761. this.actions = [];
  762. for ( var i = 0; i < this.count; i ++ ) {
  763. var flag = data.readUByte();
  764. var kind = data.readUShort();
  765. var size = data.readUShort();
  766. var position = data.position;
  767. var act = this.actions[ i ] = { kind: kind };
  768. // range of animation
  769. if ( flag & 1 ) {
  770. // start and count in frames
  771. act.range = [ data.readUInt(), data.readUInt() ];
  772. }
  773. // time
  774. if ( flag & 2 ) {
  775. act.time = data.readUInt();
  776. }
  777. // easing
  778. if ( flag & 4 ) {
  779. act.intrpl = data.readInterpolation();
  780. if ( act.intrpl.indexOf( 'back.' ) == 0 ) {
  781. act.intrplParam0 = data.readFloat();
  782. } else if ( act.intrpl.indexOf( 'elastic.' ) == 0 ) {
  783. act.intrplParam0 = data.readFloat();
  784. act.intrplParam1 = data.readFloat();
  785. }
  786. }
  787. switch ( kind ) {
  788. case SEA3D.Actions.RTT_TARGET:
  789. act.source = sea3d.getObject( data.readUInt() );
  790. act.target = sea3d.getObject( data.readUInt() );
  791. break;
  792. case SEA3D.Actions.LOOK_AT:
  793. act.source = sea3d.getObject( data.readUInt() );
  794. act.target = sea3d.getObject( data.readUInt() );
  795. break;
  796. case SEA3D.Actions.PLAY_SOUND:
  797. act.sound = sea3d.getObject( data.readUInt() );
  798. act.offset = data.readUInt();
  799. break;
  800. case SEA3D.Actions.PLAY_ANIMATION:
  801. act.object = sea3d.getObject( data.readUInt() );
  802. act.name = data.readUTF8Tiny();
  803. break;
  804. case SEA3D.Actions.FOG:
  805. act.color = data.readUInt24();
  806. act.min = data.readFloat();
  807. act.max = data.readFloat();
  808. break;
  809. case SEA3D.Actions.ENVIRONMENT:
  810. act.texture = sea3d.getObject( data.readUInt() );
  811. break;
  812. case SEA3D.Actions.ENVIRONMENT_COLOR:
  813. act.color = data.readUInt24F();
  814. break;
  815. case SEA3D.Actions.CAMERA:
  816. act.camera = sea3d.getObject( data.readUInt() );
  817. break;
  818. case SEA3D.Actions.SCRIPTS:
  819. act.scripts = data.readScriptList( sea3d );
  820. break;
  821. case SEA3D.Actions.CLASS_OF:
  822. act.classof = sea3d.getObject( data.readUInt() );
  823. break;
  824. case SEA3D.Actions.ATTRIBUTES:
  825. act.attributes = sea3d.getObject( data.readUInt() );
  826. break;
  827. default:
  828. console.log( "Action \"" + kind + "\" not found." );
  829. break;
  830. }
  831. data.position = position + size;
  832. }
  833. };
  834. SEA3D.Actions.SCENE = 0;
  835. SEA3D.Actions.ENVIRONMENT_COLOR = 1;
  836. SEA3D.Actions.ENVIRONMENT = 2;
  837. SEA3D.Actions.FOG = 3;
  838. SEA3D.Actions.PLAY_ANIMATION = 4;
  839. SEA3D.Actions.PLAY_SOUND = 5;
  840. SEA3D.Actions.ANIMATION_AUDIO_SYNC = 6;
  841. SEA3D.Actions.LOOK_AT = 7;
  842. SEA3D.Actions.RTT_TARGET = 8;
  843. SEA3D.Actions.CAMERA = 9;
  844. SEA3D.Actions.SCRIPTS = 10;
  845. SEA3D.Actions.CLASS_OF = 11;
  846. SEA3D.Actions.ATTRIBUTES = 12;
  847. SEA3D.Actions.prototype.type = "act";
  848. //
  849. // Properties
  850. //
  851. SEA3D.Properties = function ( name, data, sea3d ) {
  852. this.name = name;
  853. this.data = data;
  854. this.sea3d = sea3d;
  855. this.props = data.readProperties( sea3d );
  856. this.props.__name = name;
  857. };
  858. SEA3D.Properties.prototype.type = "prop";
  859. //
  860. // File Info
  861. //
  862. SEA3D.FileInfo = function ( name, data, sea3d ) {
  863. this.name = name;
  864. this.data = data;
  865. this.sea3d = sea3d;
  866. this.info = data.readProperties( sea3d );
  867. this.info.__name = name;
  868. sea3d.info = this.info;
  869. };
  870. SEA3D.FileInfo.prototype.type = "info";
  871. //
  872. // Java Script
  873. //
  874. SEA3D.JavaScript = function ( name, data, sea3d ) {
  875. this.name = name;
  876. this.data = data;
  877. this.sea3d = sea3d;
  878. this.src = data.readUTF8( data.length );
  879. };
  880. SEA3D.JavaScript.prototype.type = "js";
  881. //
  882. // Java Script Method
  883. //
  884. SEA3D.JavaScriptMethod = function ( name, data, sea3d ) {
  885. this.name = name;
  886. this.data = data;
  887. this.sea3d = sea3d;
  888. var count = data.readUShort();
  889. this.methods = {};
  890. for ( var i = 0; i < count; i ++ ) {
  891. var flag = data.readUByte();
  892. var method = data.readUTF8Tiny();
  893. this.methods[ method ] = {
  894. src: data.readUTF8Long()
  895. };
  896. }
  897. };
  898. SEA3D.JavaScriptMethod.prototype.type = "jsm";
  899. //
  900. // GLSL
  901. //
  902. SEA3D.GLSL = function ( name, data, sea3d ) {
  903. this.name = name;
  904. this.data = data;
  905. this.sea3d = sea3d;
  906. this.src = data.readUTF8( data.length );
  907. };
  908. SEA3D.GLSL.prototype.type = "glsl";
  909. //
  910. // Dummy
  911. //
  912. SEA3D.Dummy = function ( name, data, sea3d ) {
  913. SEA3D.Object3D.call( this, name, data, sea3d );
  914. this.transform = data.readMatrix();
  915. this.width = data.readFloat();
  916. this.height = data.readFloat();
  917. this.depth = data.readFloat();
  918. data.readTags( this.readTag.bind( this ) );
  919. };
  920. SEA3D.Dummy.prototype = Object.create( SEA3D.Object3D.prototype );
  921. SEA3D.Dummy.prototype.constructor = SEA3D.Dummy;
  922. SEA3D.Dummy.prototype.type = "dmy";
  923. //
  924. // Line
  925. //
  926. SEA3D.Line = function ( name, data, sea3d ) {
  927. SEA3D.Object3D.call( this, name, data, sea3d );
  928. this.count = ( this.attrib & 64 ? data.readUInt() : data.readUShort() ) * 3;
  929. this.closed = ( this.attrib & 128 ) != 0;
  930. this.transform = data.readMatrix();
  931. this.vertex = [];
  932. var i = 0;
  933. while ( i < this.count ) {
  934. this.vertex[ i ++ ] = data.readFloat();
  935. }
  936. data.readTags( this.readTag.bind( this ) );
  937. };
  938. SEA3D.Line.prototype = Object.create( SEA3D.Object3D.prototype );
  939. SEA3D.Line.prototype.constructor = SEA3D.Line;
  940. SEA3D.Line.prototype.type = "line";
  941. //
  942. // Sprite
  943. //
  944. SEA3D.Sprite = function ( name, data, sea3d ) {
  945. SEA3D.Object3D.call( this, name, data, sea3d );
  946. if ( this.attrib & 256 ) {
  947. this.material = sea3d.getObject( data.readUInt() );
  948. }
  949. this.position = data.readVector3();
  950. this.width = data.readFloat();
  951. this.height = data.readFloat();
  952. data.readTags( this.readTag.bind( this ) );
  953. };
  954. SEA3D.Sprite.prototype = Object.create( SEA3D.Object3D.prototype );
  955. SEA3D.Sprite.prototype.constructor = SEA3D.Sprite;
  956. SEA3D.Sprite.prototype.type = "m2d";
  957. //
  958. // Mesh
  959. //
  960. SEA3D.Mesh = function ( name, data, sea3d ) {
  961. SEA3D.Entity3D.call( this, name, data, sea3d );
  962. // MATERIAL
  963. if ( this.attrib & 256 ) {
  964. this.material = [];
  965. var len = data.readUByte();
  966. if ( len == 1 ) this.material[ 0 ] = sea3d.getObject( data.readUInt() );
  967. else {
  968. var i = 0;
  969. while ( i < len ) {
  970. var matIndex = data.readUInt();
  971. if ( matIndex > 0 ) this.material[ i ++ ] = sea3d.getObject( matIndex - 1 );
  972. else this.material[ i ++ ] = undefined;
  973. }
  974. }
  975. }
  976. if ( this.attrib & 512 ) {
  977. this.modifiers = [];
  978. var len = data.readUByte();
  979. for ( var i = 0; i < len; i ++ ) {
  980. this.modifiers[ i ] = sea3d.getObject( data.readUInt() );
  981. }
  982. }
  983. if ( this.attrib & 1024 ) {
  984. this.reference = {
  985. type: data.readUByte(),
  986. ref: sea3d.getObject( data.readUInt() )
  987. };
  988. }
  989. this.transform = data.readMatrix();
  990. this.geometry = sea3d.getObject( data.readUInt() );
  991. data.readTags( this.readTag.bind( this ) );
  992. };
  993. SEA3D.Mesh.prototype = Object.create( SEA3D.Entity3D.prototype );
  994. SEA3D.Mesh.prototype.constructor = SEA3D.Mesh;
  995. SEA3D.Mesh.prototype.type = "m3d";
  996. //
  997. // Skeleton
  998. //
  999. SEA3D.Skeleton = function ( name, data, sea3d ) {
  1000. this.name = name;
  1001. this.data = data;
  1002. this.sea3d = sea3d;
  1003. var length = data.readUShort();
  1004. this.joint = [];
  1005. for ( var i = 0; i < length; i ++ ) {
  1006. this.joint[ i ] = {
  1007. name: data.readUTF8Tiny(),
  1008. parentIndex: data.readUShort() - 1,
  1009. inverseBindMatrix: data.readMatrix()
  1010. };
  1011. }
  1012. };
  1013. SEA3D.Skeleton.prototype.type = "skl";
  1014. //
  1015. // Skeleton Local
  1016. //
  1017. SEA3D.SkeletonLocal = function ( name, data, sea3d ) {
  1018. this.name = name;
  1019. this.data = data;
  1020. this.sea3d = sea3d;
  1021. var length = data.readUShort();
  1022. this.joint = [];
  1023. for ( var i = 0; i < length; i ++ ) {
  1024. this.joint[ i ] = {
  1025. name: data.readUTF8Tiny(),
  1026. parentIndex: data.readUShort() - 1,
  1027. // POSITION XYZ
  1028. x: data.readFloat(),
  1029. y: data.readFloat(),
  1030. z: data.readFloat(),
  1031. // QUATERNION XYZW
  1032. qx: data.readFloat(),
  1033. qy: data.readFloat(),
  1034. qz: data.readFloat(),
  1035. qw: data.readFloat()
  1036. };
  1037. }
  1038. };
  1039. SEA3D.SkeletonLocal.prototype.type = "sklq";
  1040. //
  1041. // Animation Base
  1042. //
  1043. SEA3D.AnimationBase = function ( name, data, sea3d ) {
  1044. this.name = name;
  1045. this.data = data;
  1046. this.sea3d = sea3d;
  1047. var flag = data.readUByte();
  1048. this.sequence = [];
  1049. if ( flag & 1 ) {
  1050. var count = data.readUShort();
  1051. for ( var i = 0; i < count; i ++ ) {
  1052. var seqFlag = data.readUByte();
  1053. this.sequence[ i ] = {
  1054. name: data.readUTF8Tiny(),
  1055. start: data.readUInt(),
  1056. count: data.readUInt(),
  1057. repeat: ( seqFlag & 1 ) != 0,
  1058. intrpl: ( seqFlag & 2 ) == 0
  1059. };
  1060. }
  1061. }
  1062. this.frameRate = data.readUByte();
  1063. this.numFrames = data.readUInt();
  1064. // no contains sequence
  1065. if ( this.sequence.length == 0 ) {
  1066. this.sequence[ 0 ] = { name: "root", start: 0, count: this.numFrames, repeat: true, intrpl: true };
  1067. }
  1068. };
  1069. //
  1070. // Animation
  1071. //
  1072. SEA3D.Animation = function ( name, data, sea3d ) {
  1073. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1074. this.dataList = [];
  1075. for ( var i = 0, l = data.readUByte(); i < l; i ++ ) {
  1076. var kind = data.readUShort(),
  1077. type = data.readUByte();
  1078. var anmRaw = data.readVector( type, this.numFrames, 0 );
  1079. this.dataList.push( {
  1080. kind: kind,
  1081. type: type,
  1082. blockSize: SEA3D.Stream.sizeOf( type ),
  1083. data: anmRaw
  1084. } );
  1085. }
  1086. };
  1087. SEA3D.Animation.POSITION = 0;
  1088. SEA3D.Animation.ROTATION = 1;
  1089. SEA3D.Animation.SCALE = 2;
  1090. SEA3D.Animation.COLOR = 3;
  1091. SEA3D.Animation.MULTIPLIER = 4;
  1092. SEA3D.Animation.ATTENUATION_START = 5;
  1093. SEA3D.Animation.ATTENUATION_END = 6;
  1094. SEA3D.Animation.FOV = 7;
  1095. SEA3D.Animation.OFFSET_U = 8;
  1096. SEA3D.Animation.OFFSET_V = 9;
  1097. SEA3D.Animation.SCALE_U = 10;
  1098. SEA3D.Animation.SCALE_V = 11;
  1099. SEA3D.Animation.ANGLE = 12;
  1100. SEA3D.Animation.ALPHA = 13;
  1101. SEA3D.Animation.VOLUME = 14;
  1102. SEA3D.Animation.MORPH = 250;
  1103. SEA3D.Animation.prototype = Object.create( SEA3D.AnimationBase.prototype );
  1104. SEA3D.Animation.prototype.constructor = SEA3D.Animation;
  1105. SEA3D.Animation.prototype.type = "anm";
  1106. //
  1107. // Skeleton Animation
  1108. //
  1109. SEA3D.SkeletonAnimation = function ( name, data, sea3d ) {
  1110. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1111. this.name = name;
  1112. this.data = data;
  1113. this.sea3d = sea3d;
  1114. this.numJoints = data.readUShort();
  1115. this.raw = data.readFloatArray( this.numFrames * this.numJoints * 7 );
  1116. };
  1117. SEA3D.SkeletonAnimation.prototype.type = "skla";
  1118. //
  1119. // UVW Animation
  1120. //
  1121. SEA3D.UVWAnimation = function ( name, data, sea3d ) {
  1122. SEA3D.Animation.call( this, name, data, sea3d );
  1123. };
  1124. SEA3D.UVWAnimation.prototype.type = "auvw";
  1125. //
  1126. // Morph
  1127. //
  1128. SEA3D.Morph = function ( name, data, sea3d ) {
  1129. SEA3D.GeometryBase.call( this, name, data, sea3d );
  1130. var useVertex = ( this.attrib & 2 ) != 0;
  1131. var useNormal = ( this.attrib & 4 ) != 0;
  1132. var nodeCount = data.readUShort();
  1133. this.node = [];
  1134. for ( var i = 0; i < nodeCount; i ++ ) {
  1135. var nodeName = data.readUTF8Tiny(),
  1136. verts, norms;
  1137. if ( useVertex ) verts = data.readFloatArray( this.length );
  1138. if ( useNormal ) norms = data.readFloatArray( this.length );
  1139. this.node[ i ] = { vertex: verts, normal: norms, name: nodeName };
  1140. }
  1141. };
  1142. SEA3D.Morph.prototype = Object.create( SEA3D.GeometryBase.prototype );
  1143. SEA3D.Morph.prototype.constructor = SEA3D.Morph;
  1144. SEA3D.Morph.prototype.type = "mph";
  1145. //
  1146. // Morph Animation
  1147. //
  1148. SEA3D.MorphAnimation = function ( name, data, sea3d ) {
  1149. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1150. this.dataList = [];
  1151. for ( var i = 0, l = data.readUByte(); i < l; i ++ ) {
  1152. this.dataList.push( {
  1153. kind: SEA3D.Animation.MORPH,
  1154. type: SEA3D.Stream.FLOAT,
  1155. name: data.readUTF8Tiny(),
  1156. blockSize: 1,
  1157. data: data.readVector( SEA3D.Stream.FLOAT, this.numFrames, 0 )
  1158. } );
  1159. }
  1160. };
  1161. SEA3D.MorphAnimation.prototype.type = "mpha";
  1162. //
  1163. // Vertex Animation
  1164. //
  1165. SEA3D.VertexAnimation = function ( name, data, sea3d ) {
  1166. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1167. var flags = data.readUByte();
  1168. this.isBig = ( flags & 1 ) != 0;
  1169. data.readVInt = this.isBig ? data.readUInt : data.readUShort;
  1170. this.numVertex = data.readVInt();
  1171. this.length = this.numVertex * 3;
  1172. var useVertex = ( flags & 2 ) != 0;
  1173. var useNormal = ( flags & 4 ) != 0;
  1174. this.frame = [];
  1175. var i, verts, norms;
  1176. for ( i = 0; i < this.numFrames; i ++ ) {
  1177. if ( useVertex ) verts = data.readFloatArray( this.length );
  1178. if ( useNormal ) norms = data.readFloatArray( this.length );
  1179. this.frame[ i ] = { vertex: verts, normal: norms };
  1180. }
  1181. };
  1182. SEA3D.VertexAnimation.prototype = Object.create( SEA3D.AnimationBase.prototype );
  1183. SEA3D.VertexAnimation.prototype.constructor = SEA3D.VertexAnimation;
  1184. SEA3D.VertexAnimation.prototype.type = "vtxa";
  1185. //
  1186. // Camera
  1187. //
  1188. SEA3D.Camera = function ( name, data, sea3d ) {
  1189. SEA3D.Object3D.call( this, name, data, sea3d );
  1190. if ( this.attrib & 64 ) {
  1191. this.dof = {
  1192. distance: data.readFloat(),
  1193. range: data.readFloat()
  1194. };
  1195. }
  1196. this.transform = data.readMatrix();
  1197. this.fov = data.readFloat();
  1198. data.readTags( this.readTag.bind( this ) );
  1199. };
  1200. SEA3D.Camera.prototype = Object.create( SEA3D.Object3D.prototype );
  1201. SEA3D.Camera.prototype.constructor = SEA3D.Camera;
  1202. SEA3D.Camera.prototype.type = "cam";
  1203. //
  1204. // Orthographic Camera
  1205. //
  1206. SEA3D.OrthographicCamera = function ( name, data, sea3d ) {
  1207. SEA3D.Object3D.call( this, name, data, sea3d );
  1208. this.transform = data.readMatrix();
  1209. this.height = data.readFloat();
  1210. data.readTags( this.readTag.bind( this ) );
  1211. };
  1212. SEA3D.OrthographicCamera.prototype = Object.create( SEA3D.Object3D.prototype );
  1213. SEA3D.OrthographicCamera.prototype.constructor = SEA3D.OrthographicCamera;
  1214. SEA3D.OrthographicCamera.prototype.type = "camo";
  1215. //
  1216. // Joint Object
  1217. //
  1218. SEA3D.JointObject = function ( name, data, sea3d ) {
  1219. SEA3D.Object3D.call( this, name, data, sea3d );
  1220. this.target = sea3d.getObject( data.readUInt() );
  1221. this.joint = data.readUShort();
  1222. data.readTags( this.readTag.bind( this ) );
  1223. };
  1224. SEA3D.JointObject.prototype = Object.create( SEA3D.Object3D.prototype );
  1225. SEA3D.JointObject.prototype.constructor = SEA3D.JointObject;
  1226. SEA3D.JointObject.prototype.type = "jnt";
  1227. //
  1228. // Light
  1229. //
  1230. SEA3D.Light = function ( name, data, sea3d ) {
  1231. SEA3D.Object3D.call( this, name, data, sea3d );
  1232. this.attenStart = Number.MAX_VALUE;
  1233. this.attenEnd = Number.MAX_VALUE;
  1234. if ( this.attrib & 64 ) {
  1235. var shadowHeader = data.readUByte();
  1236. this.shadow = {};
  1237. this.shadow.opacity = shadowHeader & 1 ? data.readFloat() : 1;
  1238. this.shadow.color = shadowHeader & 2 ? data.readUInt24() : 0x000000;
  1239. }
  1240. if ( this.attrib & 512 ) {
  1241. this.attenStart = data.readFloat();
  1242. this.attenEnd = data.readFloat();
  1243. }
  1244. this.color = data.readUInt24();
  1245. this.multiplier = data.readFloat();
  1246. };
  1247. SEA3D.Light.prototype = Object.create( SEA3D.Object3D.prototype );
  1248. SEA3D.Light.prototype.constructor = SEA3D.Light;
  1249. //
  1250. // Point Light
  1251. //
  1252. SEA3D.PointLight = function ( name, data, sea3d ) {
  1253. SEA3D.Light.call( this, name, data, sea3d );
  1254. if ( this.attrib & 128 ) {
  1255. this.attenuation = {
  1256. start: data.readFloat(),
  1257. end: data.readFloat()
  1258. };
  1259. }
  1260. this.position = data.readVector3();
  1261. data.readTags( this.readTag.bind( this ) );
  1262. };
  1263. SEA3D.PointLight.prototype = Object.create( SEA3D.Light.prototype );
  1264. SEA3D.PointLight.prototype.constructor = SEA3D.PointLight;
  1265. SEA3D.PointLight.prototype.type = "plht";
  1266. //
  1267. // Hemisphere Light
  1268. //
  1269. SEA3D.HemisphereLight = function ( name, data, sea3d ) {
  1270. SEA3D.Light.call( this, name, data, sea3d );
  1271. if ( this.attrib & 128 ) {
  1272. this.attenuation = {
  1273. start: data.readFloat(),
  1274. end: data.readFloat()
  1275. };
  1276. }
  1277. this.secondColor = data.readUInt24();
  1278. data.readTags( this.readTag.bind( this ) );
  1279. };
  1280. SEA3D.HemisphereLight.prototype = Object.create( SEA3D.Light.prototype );
  1281. SEA3D.HemisphereLight.prototype.constructor = SEA3D.HemisphereLight;
  1282. SEA3D.HemisphereLight.prototype.type = "hlht";
  1283. //
  1284. // Ambient Light
  1285. //
  1286. SEA3D.AmbientLight = function ( name, data, sea3d ) {
  1287. SEA3D.Light.call( this, name, data, sea3d );
  1288. data.readTags( this.readTag.bind( this ) );
  1289. };
  1290. SEA3D.AmbientLight.prototype = Object.create( SEA3D.Light.prototype );
  1291. SEA3D.AmbientLight.prototype.constructor = SEA3D.AmbientLight;
  1292. SEA3D.AmbientLight.prototype.type = "alht";
  1293. //
  1294. // Directional Light
  1295. //
  1296. SEA3D.DirectionalLight = function ( name, data, sea3d ) {
  1297. SEA3D.Light.call( this, name, data, sea3d );
  1298. this.transform = data.readMatrix();
  1299. data.readTags( this.readTag.bind( this ) );
  1300. };
  1301. SEA3D.DirectionalLight.prototype = Object.create( SEA3D.Light.prototype );
  1302. SEA3D.DirectionalLight.prototype.constructor = SEA3D.DirectionalLight;
  1303. SEA3D.DirectionalLight.prototype.type = "dlht";
  1304. //
  1305. // Material
  1306. //
  1307. SEA3D.Material = function ( name, data, sea3d ) {
  1308. this.name = name;
  1309. this.data = data;
  1310. this.sea3d = sea3d;
  1311. this.technique = [];
  1312. this.tecniquesDict = {};
  1313. this.attrib = data.readUShort();
  1314. this.alpha = 1;
  1315. this.blendMode = "normal";
  1316. this.doubleSided = ( this.attrib & 1 ) != 0;
  1317. this.receiveLights = ( this.attrib & 2 ) == 0;
  1318. this.receiveShadows = ( this.attrib & 4 ) == 0;
  1319. this.receiveFog = ( this.attrib & 8 ) == 0;
  1320. this.repeat = ( this.attrib & 16 ) == 0;
  1321. if ( this.attrib & 32 )
  1322. this.alpha = data.readFloat();
  1323. if ( this.attrib & 64 )
  1324. this.blendMode = data.readBlendMode();
  1325. if ( this.attrib & 128 )
  1326. this.animations = data.readAnimationList( sea3d );
  1327. this.depthWrite = ( this.attrib & 256 ) == 0;
  1328. this.depthTest = ( this.attrib & 512 ) == 0;
  1329. this.premultipliedAlpha = ( this.attrib & 1024 ) != 0;
  1330. var count = data.readUByte();
  1331. for ( var i = 0; i < count; ++ i ) {
  1332. var kind = data.readUShort();
  1333. var size = data.readUShort();
  1334. var pos = data.position;
  1335. var tech, methodAttrib;
  1336. switch ( kind ) {
  1337. case SEA3D.Material.PHONG:
  1338. tech = {
  1339. ambientColor: data.readUInt24(),
  1340. diffuseColor: data.readUInt24(),
  1341. specularColor: data.readUInt24(),
  1342. specular: data.readFloat(),
  1343. gloss: data.readFloat()
  1344. };
  1345. break;
  1346. case SEA3D.Material.PHYSICAL:
  1347. tech = {
  1348. color: data.readUInt24(),
  1349. roughness: data.readFloat(),
  1350. metalness: data.readFloat()
  1351. };
  1352. break;
  1353. case SEA3D.Material.ANISOTROPIC:
  1354. break;
  1355. case SEA3D.Material.COMPOSITE_TEXTURE:
  1356. tech = {
  1357. composite: sea3d.getObject( data.readUInt() )
  1358. };
  1359. break;
  1360. case SEA3D.Material.DIFFUSE_MAP:
  1361. case SEA3D.Material.SPECULAR_MAP:
  1362. case SEA3D.Material.NORMAL_MAP:
  1363. case SEA3D.Material.AMBIENT_MAP:
  1364. case SEA3D.Material.ALPHA_MAP:
  1365. case SEA3D.Material.EMISSIVE_MAP:
  1366. case SEA3D.Material.ROUGHNESS_MAP:
  1367. case SEA3D.Material.METALNESS_MAP:
  1368. tech = {
  1369. texture: sea3d.getObject( data.readUInt() )
  1370. };
  1371. break;
  1372. case SEA3D.Material.REFLECTION:
  1373. case SEA3D.Material.FRESNEL_REFLECTION:
  1374. tech = {
  1375. texture: sea3d.getObject( data.readUInt() ),
  1376. alpha: data.readFloat()
  1377. };
  1378. if ( kind == SEA3D.Material.FRESNEL_REFLECTION ) {
  1379. tech.power = data.readFloat();
  1380. tech.normal = data.readFloat();
  1381. }
  1382. break;
  1383. case SEA3D.Material.REFRACTION:
  1384. tech = {
  1385. texture: sea3d.getObject( data.readUInt() ),
  1386. alpha: data.readFloat(),
  1387. ior: data.readFloat()
  1388. };
  1389. break;
  1390. case SEA3D.Material.RIM:
  1391. tech = {
  1392. color: data.readUInt24(),
  1393. strength: data.readFloat(),
  1394. power: data.readFloat(),
  1395. blendMode: data.readBlendMode()
  1396. };
  1397. break;
  1398. case SEA3D.Material.LIGHT_MAP:
  1399. tech = {
  1400. texture: sea3d.getObject( data.readUInt() ),
  1401. channel: data.readUByte(),
  1402. blendMode: data.readBlendMode()
  1403. };
  1404. break;
  1405. case SEA3D.Material.DETAIL_MAP:
  1406. tech = {
  1407. texture: sea3d.getObject( data.readUInt() ),
  1408. scale: data.readFloat(),
  1409. blendMode: data.readBlendMode()
  1410. };
  1411. break;
  1412. case SEA3D.Material.CEL:
  1413. tech = {
  1414. color: data.readUInt24(),
  1415. levels: data.readUByte(),
  1416. size: data.readFloat(),
  1417. specularCutOff: data.readFloat(),
  1418. smoothness: data.readFloat()
  1419. };
  1420. break;
  1421. case SEA3D.Material.TRANSLUCENT:
  1422. tech = {
  1423. translucency: data.readFloat(),
  1424. scattering: data.readFloat()
  1425. };
  1426. break;
  1427. case SEA3D.Material.BLEND_NORMAL_MAP:
  1428. methodAttrib = data.readUByte();
  1429. tech = {
  1430. texture: sea3d.getObject( data.readUInt() ),
  1431. secondaryTexture: sea3d.getObject( data.readUInt() )
  1432. };
  1433. if ( methodAttrib & 1 ) {
  1434. tech.offsetX0 = data.readFloat();
  1435. tech.offsetY0 = data.readFloat();
  1436. tech.offsetX1 = data.readFloat();
  1437. tech.offsetY1 = data.readFloat();
  1438. } else {
  1439. tech.offsetX0 = tech.offsetY0 =
  1440. tech.offsetX1 = tech.offsetY1 = 0;
  1441. }
  1442. tech.animate = methodAttrib & 2;
  1443. break;
  1444. case SEA3D.Material.MIRROR_REFLECTION:
  1445. tech = {
  1446. texture: sea3d.getObject( data.readUInt() ),
  1447. alpha: data.readFloat()
  1448. };
  1449. break;
  1450. case SEA3D.Material.EMISSIVE:
  1451. tech = {
  1452. color: data.readUInt24F()
  1453. };
  1454. break;
  1455. case SEA3D.Material.VERTEX_COLOR:
  1456. tech = {
  1457. blendMode: data.readBlendMode()
  1458. };
  1459. break;
  1460. case SEA3D.Material.WRAP_LIGHTING:
  1461. tech = {
  1462. color: data.readUInt24(),
  1463. strength: data.readFloat()
  1464. };
  1465. break;
  1466. case SEA3D.Material.COLOR_REPLACE:
  1467. methodAttrib = data.readUByte();
  1468. tech = {
  1469. red: data.readUInt24(),
  1470. green: data.readUInt24(),
  1471. blue: data.readUInt24F()
  1472. };
  1473. if ( methodAttrib & 1 ) tech.mask = sea3d.getObject( data.readUInt() );
  1474. if ( methodAttrib & 2 ) tech.alpha = data.readFloat();
  1475. break;
  1476. case SEA3D.Material.REFLECTION_SPHERICAL:
  1477. tech = {
  1478. texture: sea3d.getObject( data.readUInt() ),
  1479. alpha: data.readFloat()
  1480. };
  1481. break;
  1482. case SEA3D.Material.REFLECTIVITY:
  1483. methodAttrib = data.readUByte();
  1484. tech = {
  1485. strength: data.readFloat()
  1486. };
  1487. if ( methodAttrib & 1 ) tech.mask = sea3d.getObject( data.readUInt() );
  1488. break;
  1489. case SEA3D.Material.CLEAR_COAT:
  1490. tech = {
  1491. strength: data.readFloat(),
  1492. roughness: data.readFloat()
  1493. };
  1494. break;
  1495. case SEA3D.Material.FLACCIDITY:
  1496. methodAttrib = data.readUByte();
  1497. tech = {
  1498. target: sea3d.getObject( data.readUInt() ),
  1499. scale: data.readFloat(),
  1500. spring: data.readFloat(),
  1501. damping: data.readFloat()
  1502. };
  1503. if ( methodAttrib & 1 ) tech.mask = sea3d.getObject( data.readUInt() );
  1504. break;
  1505. default:
  1506. console.warn( "SEA3D: MaterialTechnique not found:", kind.toString( 16 ) );
  1507. data.position = pos += size;
  1508. continue;
  1509. }
  1510. tech.kind = kind;
  1511. this.technique.push( tech );
  1512. this.tecniquesDict[ kind ] = tech;
  1513. data.position = pos += size;
  1514. }
  1515. };
  1516. SEA3D.Material.PHONG = 0;
  1517. SEA3D.Material.COMPOSITE_TEXTURE = 1;
  1518. SEA3D.Material.DIFFUSE_MAP = 2;
  1519. SEA3D.Material.SPECULAR_MAP = 3;
  1520. SEA3D.Material.REFLECTION = 4;
  1521. SEA3D.Material.REFRACTION = 5;
  1522. SEA3D.Material.NORMAL_MAP = 6;
  1523. SEA3D.Material.FRESNEL_REFLECTION = 7;
  1524. SEA3D.Material.RIM = 8;
  1525. SEA3D.Material.LIGHT_MAP = 9;
  1526. SEA3D.Material.DETAIL_MAP = 10;
  1527. SEA3D.Material.CEL = 11;
  1528. SEA3D.Material.TRANSLUCENT = 12;
  1529. SEA3D.Material.BLEND_NORMAL_MAP = 13;
  1530. SEA3D.Material.MIRROR_REFLECTION = 14;
  1531. SEA3D.Material.AMBIENT_MAP = 15;
  1532. SEA3D.Material.ALPHA_MAP = 16;
  1533. SEA3D.Material.EMISSIVE_MAP = 17;
  1534. SEA3D.Material.VERTEX_COLOR = 18;
  1535. SEA3D.Material.WRAP_LIGHTING = 19;
  1536. SEA3D.Material.COLOR_REPLACE = 20;
  1537. SEA3D.Material.REFLECTION_SPHERICAL = 21;
  1538. SEA3D.Material.ANISOTROPIC = 22;
  1539. SEA3D.Material.EMISSIVE = 23;
  1540. SEA3D.Material.PHYSICAL = 24;
  1541. SEA3D.Material.ROUGHNESS_MAP = 25;
  1542. SEA3D.Material.METALNESS_MAP = 26;
  1543. SEA3D.Material.REFLECTIVITY = 27;
  1544. SEA3D.Material.CLEAR_COAT = 28;
  1545. SEA3D.Material.FLACCIDITY = 29;
  1546. SEA3D.Material.prototype.type = "mat";
  1547. //
  1548. // Composite
  1549. //
  1550. SEA3D.Composite = function ( name, data, sea3d ) {
  1551. this.name = name;
  1552. this.data = data;
  1553. this.sea3d = sea3d;
  1554. var layerCount = data.readUByte();
  1555. this.layer = [];
  1556. for ( var i = 0; i < layerCount; i ++ ) {
  1557. this.layer[ i ] = new SEA3D.Composite.prototype.Layer( data, sea3d );
  1558. }
  1559. };
  1560. SEA3D.Composite.prototype.getLayerByName = function ( name ) {
  1561. for ( var i = 0; i < this.layer.length; i ++ ) {
  1562. if ( this.layer[ i ].name == name ) {
  1563. return this.layer[ i ];
  1564. }
  1565. }
  1566. };
  1567. SEA3D.Composite.prototype.Layer = function ( data, sea3d ) {
  1568. var attrib = data.readUShort();
  1569. if ( attrib & 1 ) this.texture = new SEA3D.Composite.LayerBitmap( data, sea3d );
  1570. else this.color = data.readUInt24();
  1571. if ( attrib & 2 ) {
  1572. this.mask = new SEA3D.Composite.LayerBitmap( data, sea3d );
  1573. }
  1574. if ( attrib & 4 ) {
  1575. this.name = data.readUTF8Tiny();
  1576. }
  1577. this.blendMode = attrib & 8 ? data.readBlendMode() : "normal";
  1578. this.opacity = attrib & 16 ? data.readFloat() : 1;
  1579. };
  1580. SEA3D.Composite.LayerBitmap = function ( data, sea3d ) {
  1581. this.map = sea3d.getObject( data.readUInt() );
  1582. var attrib = data.readUShort();
  1583. this.channel = attrib & 1 ? data.readUByte() : 0;
  1584. this.repeat = attrib & 2 == 0;
  1585. this.offsetU = attrib & 4 ? data.readFloat() : 0;
  1586. this.offsetV = attrib & 8 ? data.readFloat() : 0;
  1587. this.scaleU = attrib & 16 ? data.readFloat() : 1;
  1588. this.scaleV = attrib & 32 ? data.readFloat() : 1;
  1589. this.rotation = attrib & 64 ? data.readFloat() : 0;
  1590. if ( attrib & 128 ) this.animation = data.readAnimationList( sea3d );
  1591. };
  1592. SEA3D.Composite.prototype.type = "ctex";
  1593. //
  1594. // Planar Render
  1595. //
  1596. SEA3D.PlanarRender = function ( name, data, sea3d ) {
  1597. this.name = name;
  1598. this.data = data;
  1599. this.sea3d = sea3d;
  1600. this.attrib = data.readUByte();
  1601. this.quality = ( this.attrib & 1 ) | ( this.attrib & 2 );
  1602. this.transform = data.readMatrix();
  1603. };
  1604. SEA3D.PlanarRender.prototype.type = "rttp";
  1605. //
  1606. // Cube Render
  1607. //
  1608. SEA3D.CubeRender = function ( name, data, sea3d ) {
  1609. this.name = name;
  1610. this.data = data;
  1611. this.sea3d = sea3d;
  1612. this.attrib = data.readUByte();
  1613. this.quality = ( this.attrib & 1 ) | ( this.attrib & 2 );
  1614. this.position = data.readVector3();
  1615. };
  1616. SEA3D.CubeRender.prototype.type = "rttc";
  1617. //
  1618. // Cube Maps
  1619. //
  1620. SEA3D.CubeMap = function ( name, data, sea3d ) {
  1621. this.name = name;
  1622. this.data = data;
  1623. this.sea3d = sea3d;
  1624. this.transparent = false;
  1625. this.extension = data.readExt();
  1626. this.faces = [];
  1627. for ( var i = 0; i < 6; i ++ ) {
  1628. var size = data.readUInt();
  1629. this.faces[ i ] = data.concat( data.position, size );
  1630. data.position += size;
  1631. }
  1632. };
  1633. SEA3D.CubeMap.prototype.type = "cmap";
  1634. //
  1635. // JPEG
  1636. //
  1637. SEA3D.JPEG = function ( name, data, sea3d ) {
  1638. this.name = name;
  1639. this.data = data;
  1640. this.sea3d = sea3d;
  1641. this.transparent = false;
  1642. };
  1643. SEA3D.JPEG.prototype.type = "jpg";
  1644. //
  1645. // JPEG_XR
  1646. //
  1647. SEA3D.JPEG_XR = function ( name, data, sea3d ) {
  1648. this.name = name;
  1649. this.data = data;
  1650. this.sea3d = sea3d;
  1651. this.transparent = true;
  1652. };
  1653. SEA3D.JPEG_XR.prototype.type = "wdp";
  1654. //
  1655. // PNG
  1656. //
  1657. SEA3D.PNG = function ( name, data, sea3d ) {
  1658. this.name = name;
  1659. this.data = data;
  1660. this.sea3d = sea3d;
  1661. this.transparent = data.getByte( 25 ) == 0x06;
  1662. };
  1663. SEA3D.PNG.prototype.type = "png";
  1664. //
  1665. // GIF
  1666. //
  1667. SEA3D.GIF = function ( name, data, sea3d ) {
  1668. this.name = name;
  1669. this.data = data;
  1670. this.sea3d = sea3d;
  1671. this.transparent = data.getByte( 11 ) > 0;
  1672. };
  1673. SEA3D.GIF.prototype.type = "gif";
  1674. //
  1675. // OGG
  1676. //
  1677. SEA3D.OGG = function ( name, data, sea3d ) {
  1678. this.name = name;
  1679. this.data = data;
  1680. this.sea3d = sea3d;
  1681. };
  1682. SEA3D.OGG.prototype.type = "ogg";
  1683. //
  1684. // MP3
  1685. //
  1686. SEA3D.MP3 = function ( name, data, sea3d ) {
  1687. this.name = name;
  1688. this.data = data;
  1689. this.sea3d = sea3d;
  1690. };
  1691. SEA3D.MP3.prototype.type = "mp3";
  1692. //
  1693. // Texture Update
  1694. //
  1695. SEA3D.TextureUpdate = function ( name, data, sea3d ) {
  1696. this.name = name;
  1697. this.data = data;
  1698. this.sea3d = sea3d;
  1699. this.index = data.readUInt();
  1700. this.bytes = data.concat( data.position, data.length - data.position );
  1701. };
  1702. SEA3D.TextureUpdate.prototype.type = "uTex";
  1703. //
  1704. // FILE FORMAT
  1705. //
  1706. SEA3D.File = function ( config ) {
  1707. this.config = {
  1708. streaming: true,
  1709. timeLimit: 60,
  1710. progressive: false
  1711. };
  1712. if ( config ) {
  1713. if ( config.streaming !== undefined ) this.config.streaming = config.streaming;
  1714. if ( config.timeLimit !== undefined ) this.config.timeLimit = config.timeLimit;
  1715. if ( config.progressive !== undefined ) this.config.progressive = config.progressive;
  1716. if ( config.path !== undefined ) this.config.path = config.path;
  1717. }
  1718. this.version = SEA3D.VERSION;
  1719. this.objects = [];
  1720. this.typeClass = {};
  1721. this.typeRead = {};
  1722. this.typeUnique = {};
  1723. this.position =
  1724. this.dataPosition = 0;
  1725. this.scope = this;
  1726. // SEA3D
  1727. this.addClass( SEA3D.FileInfo, true );
  1728. this.addClass( SEA3D.Geometry, true );
  1729. this.addClass( SEA3D.Mesh );
  1730. this.addClass( SEA3D.Sprite );
  1731. this.addClass( SEA3D.Material );
  1732. this.addClass( SEA3D.Composite );
  1733. this.addClass( SEA3D.PointLight );
  1734. this.addClass( SEA3D.DirectionalLight );
  1735. this.addClass( SEA3D.HemisphereLight );
  1736. this.addClass( SEA3D.AmbientLight );
  1737. this.addClass( SEA3D.Animation, true );
  1738. this.addClass( SEA3D.Skeleton, true );
  1739. this.addClass( SEA3D.SkeletonLocal, true );
  1740. this.addClass( SEA3D.SkeletonAnimation, true );
  1741. this.addClass( SEA3D.UVWAnimation, true );
  1742. this.addClass( SEA3D.JointObject );
  1743. this.addClass( SEA3D.Camera );
  1744. this.addClass( SEA3D.OrthographicCamera );
  1745. this.addClass( SEA3D.Morph, true );
  1746. this.addClass( SEA3D.MorphAnimation, true );
  1747. this.addClass( SEA3D.VertexAnimation, true );
  1748. this.addClass( SEA3D.CubeMap, true );
  1749. this.addClass( SEA3D.Dummy );
  1750. this.addClass( SEA3D.Line );
  1751. this.addClass( SEA3D.SoundPoint );
  1752. this.addClass( SEA3D.PlanarRender );
  1753. this.addClass( SEA3D.CubeRender );
  1754. this.addClass( SEA3D.Actions );
  1755. this.addClass( SEA3D.Container3D );
  1756. this.addClass( SEA3D.Properties );
  1757. // URL BASED
  1758. this.addClass( SEA3D.ScriptURL, true );
  1759. this.addClass( SEA3D.TextureURL, true );
  1760. this.addClass( SEA3D.CubeMapURL, true );
  1761. // UNIVERSAL
  1762. this.addClass( SEA3D.JPEG, true );
  1763. this.addClass( SEA3D.JPEG_XR, true );
  1764. this.addClass( SEA3D.PNG, true );
  1765. this.addClass( SEA3D.GIF, true );
  1766. this.addClass( SEA3D.OGG, true );
  1767. this.addClass( SEA3D.MP3, true );
  1768. this.addClass( SEA3D.JavaScript, true );
  1769. this.addClass( SEA3D.JavaScriptMethod, true );
  1770. this.addClass( SEA3D.GLSL, true );
  1771. // Updaters
  1772. this.addClass( SEA3D.TextureUpdate, true );
  1773. // Extensions
  1774. var i = SEA3D.File.Extensions.length;
  1775. while ( i -- ) {
  1776. SEA3D.File.Extensions[ i ].call( this );
  1777. }
  1778. };
  1779. SEA3D.File.Extensions = [];
  1780. SEA3D.File.CompressionLibs = {};
  1781. SEA3D.File.DecompressionMethod = {};
  1782. SEA3D.File.setExtension = function ( callback ) {
  1783. SEA3D.File.Extensions.push( callback );
  1784. };
  1785. SEA3D.File.setDecompressionEngine = function ( id, name, method ) {
  1786. SEA3D.File.CompressionLibs[ id ] = name;
  1787. SEA3D.File.DecompressionMethod[ id ] = method;
  1788. };
  1789. SEA3D.File.prototype.addClass = function ( clazz, unique ) {
  1790. this.typeClass[ clazz.prototype.type ] = clazz;
  1791. this.typeUnique[ clazz.prototype.type ] = unique === true;
  1792. };
  1793. SEA3D.File.prototype.readHead = function () {
  1794. if ( this.stream.bytesAvailable < 16 )
  1795. return false;
  1796. if ( this.stream.readUTF8( 3 ) != "SEA" )
  1797. throw new Error( "Invalid SEA3D format." );
  1798. this.sign = this.stream.readUTF8( 3 );
  1799. this.version = this.stream.readUInt24();
  1800. if ( this.stream.readUByte() != 0 ) {
  1801. throw new Error( "Protection algorithm not compatible." );
  1802. }
  1803. this.compressionID = this.stream.readUByte();
  1804. this.compressionAlgorithm = SEA3D.File.CompressionLibs[ this.compressionID ];
  1805. this.decompressionMethod = SEA3D.File.DecompressionMethod[ this.compressionID ];
  1806. if ( this.compressionID > 0 && ! this.decompressionMethod ) {
  1807. throw new Error( "Compression algorithm not compatible." );
  1808. }
  1809. this.length = this.stream.readUInt();
  1810. this.dataPosition = this.stream.position;
  1811. this.objects.length = 0;
  1812. this.state = this.readBody;
  1813. if ( this.onHead ) {
  1814. this.onHead( {
  1815. file: this,
  1816. sign: this.sign
  1817. } );
  1818. }
  1819. return true;
  1820. };
  1821. SEA3D.File.prototype.getObject = function ( index ) {
  1822. return this.objects[ index ];
  1823. };
  1824. SEA3D.File.prototype.getObjectByName = function ( name ) {
  1825. return this.objects[ name ];
  1826. };
  1827. SEA3D.File.prototype.readSEAObject = function () {
  1828. if ( this.stream.bytesAvailable < 4 )
  1829. return null;
  1830. var size = this.stream.readUInt(),
  1831. position = this.stream.position;
  1832. if ( this.stream.bytesAvailable < size )
  1833. return null;
  1834. var flag = this.stream.readUByte(),
  1835. type = this.stream.readExt(),
  1836. meta = null;
  1837. var name = flag & 1 ? this.stream.readUTF8Tiny() : "",
  1838. compressed = ( flag & 2 ) != 0,
  1839. streaming = ( flag & 4 ) != 0;
  1840. if ( flag & 8 ) {
  1841. var metalen = this.stream.readUShort();
  1842. var metabytes = this.stream.concat( this.stream.position, metalen );
  1843. this.stream.position += metalen;
  1844. if ( compressed && this.decompressionMethod ) {
  1845. metabytes.buffer = this.decompressionMethod( metabytes.buffer );
  1846. }
  1847. meta = metabytes.readProperties( this );
  1848. }
  1849. size -= this.stream.position - position;
  1850. position = this.stream.position;
  1851. var data = this.stream.concat( position, size ),
  1852. obj;
  1853. if ( this.typeClass[ type ] ) {
  1854. if ( compressed && this.decompressionMethod ) {
  1855. data.buffer = this.decompressionMethod( data.buffer );
  1856. }
  1857. obj = new this.typeClass[ type ]( name, data, this );
  1858. if ( ( this.config.streaming && streaming || this.config.forceStreaming ) && this.typeRead[ type ] ) {
  1859. this.typeRead[ type ].call( this.scope, obj );
  1860. }
  1861. } else {
  1862. obj = new SEA3D.Object( name, data, type, this );
  1863. console.warn( "SEA3D: Unknown format \"" + type + "\" of file \"" + name + "\". Add a module referring for this format." );
  1864. }
  1865. obj.streaming = streaming;
  1866. obj.metadata = meta;
  1867. this.objects.push( this.objects[ obj.name + "." + obj.type ] = obj );
  1868. this.dataPosition = position + size;
  1869. ++ this.position;
  1870. return obj;
  1871. };
  1872. SEA3D.File.prototype.isDone = function () {
  1873. return this.position == this.length;
  1874. };
  1875. SEA3D.File.prototype.readBody = function () {
  1876. this.timer.update();
  1877. if ( ! this.resume ) return false;
  1878. while ( this.position < this.length ) {
  1879. if ( this.timer.deltaTime < this.config.timeLimit ) {
  1880. this.stream.position = this.dataPosition;
  1881. var sea = this.readSEAObject();
  1882. if ( sea ) this.dispatchCompleteObject( sea );
  1883. else return false;
  1884. } else return false;
  1885. }
  1886. this.state = this.readComplete;
  1887. return true;
  1888. };
  1889. SEA3D.File.prototype.initParse = function () {
  1890. this.timer = new SEA3D.Timer();
  1891. this.position = 0;
  1892. this.resume = true;
  1893. };
  1894. SEA3D.File.prototype.parse = function () {
  1895. this.initParse();
  1896. if ( isFinite( this.config.timeLimit ) ) requestAnimationFrame( this.parseObject.bind( this ) );
  1897. else this.parseObject();
  1898. };
  1899. SEA3D.File.prototype.parseObject = function () {
  1900. this.timer.update();
  1901. while ( this.position < this.length && this.timer.deltaTime < this.config.timeLimit ) {
  1902. var obj = this.objects[ this.position ++ ],
  1903. type = obj.type;
  1904. if ( ! this.typeUnique[ type ] ) delete obj.tag;
  1905. if ( ( obj.streaming || this.config.forceStreaming ) && this.typeRead[ type ] ) {
  1906. if ( obj.tag == undefined ) {
  1907. this.typeRead[ type ].call( this.scope, obj );
  1908. }
  1909. }
  1910. }
  1911. if ( this.position == this.length ) {
  1912. var elapsedTime = this.timer.elapsedTime;
  1913. var message = elapsedTime + "ms, " + this.objects.length + " objects";
  1914. if ( this.onParseComplete ) {
  1915. this.onParseComplete( {
  1916. file: this,
  1917. timeTotal: elapsedTime,
  1918. message: message
  1919. } );
  1920. } else console.log( "SEA3D Parse Complete:", message );
  1921. } else {
  1922. if ( this.onParseProgress ) {
  1923. this.onParseProgress( {
  1924. file: this,
  1925. loaded: this.position,
  1926. total: this.length
  1927. } );
  1928. }
  1929. setTimeout( this.parseObject.bind( this ), 10 );
  1930. }
  1931. };
  1932. SEA3D.File.prototype.readComplete = function () {
  1933. this.stream.position = this.dataPosition;
  1934. if ( this.stream.readUInt24F() != 0x5EA3D1 )
  1935. console.warn( "SEA3D file is corrupted." );
  1936. delete this.state;
  1937. return false;
  1938. };
  1939. SEA3D.File.prototype.readState = function () {
  1940. while ( this.state() ) continue;
  1941. if ( this.state ) {
  1942. requestAnimationFrame( this.readState.bind( this ) );
  1943. this.dispatchProgress();
  1944. } else {
  1945. this.dispatchComplete();
  1946. }
  1947. };
  1948. SEA3D.File.prototype.append = function( buffer ) {
  1949. if (this.state) {
  1950. this.stream.append( buffer );
  1951. } else {
  1952. this.read( buffer );
  1953. }
  1954. };
  1955. SEA3D.File.prototype.read = function ( buffer ) {
  1956. if ( ! buffer ) throw new Error( "No data found." );
  1957. this.initParse();
  1958. this.stream = new SEA3D.Stream( buffer );
  1959. this.state = this.readHead;
  1960. this.readState();
  1961. };
  1962. SEA3D.File.prototype.dispatchCompleteObject = function ( obj ) {
  1963. if ( ! this.onCompleteObject ) return;
  1964. this.onCompleteObject( {
  1965. file: this,
  1966. object: obj
  1967. } );
  1968. };
  1969. SEA3D.File.prototype.dispatchProgress = function () {
  1970. if ( ! this.onProgress ) return;
  1971. this.onProgress( {
  1972. file: this,
  1973. loaded: this.position,
  1974. total: this.length
  1975. } );
  1976. };
  1977. SEA3D.File.prototype.dispatchDownloadProgress = function ( position, length ) {
  1978. if ( ! this.onDownloadProgress ) return;
  1979. this.onDownloadProgress( {
  1980. file: this,
  1981. loaded: position,
  1982. total: length
  1983. } );
  1984. };
  1985. SEA3D.File.prototype.dispatchComplete = function () {
  1986. var elapsedTime = this.timer.elapsedTime;
  1987. var message = elapsedTime + "ms, " + this.objects.length + " objects";
  1988. if ( this.onComplete ) this.onComplete( {
  1989. file: this,
  1990. timeTotal: elapsedTime,
  1991. message: message
  1992. } );
  1993. else console.log( "SEA3D:", message );
  1994. };
  1995. SEA3D.File.prototype.dispatchError = function ( id, message ) {
  1996. if ( this.onError ) this.onError( { file: this, id: id, message: message } );
  1997. else console.error( "SEA3D: #" + id, message );
  1998. };
  1999. SEA3D.File.prototype.load = function ( url ) {
  2000. var self = this,
  2001. xhr = new XMLHttpRequest();
  2002. xhr.open( "GET", url, true );
  2003. if (!this.config.path) {
  2004. // this.config.path = THREE.Loader.prototype.extractUrlBase( url );
  2005. this.config.path = THREE.LoaderUtils.extractUrlBase( url );
  2006. }
  2007. if ( self.config.progressive ) {
  2008. var position = 0;
  2009. xhr.overrideMimeType( 'text/plain; charset=x-user-defined' );
  2010. } else {
  2011. xhr.responseType = 'arraybuffer';
  2012. }
  2013. xhr.onprogress = function ( e ) {
  2014. if ( self.config.progressive ) {
  2015. var binStr = xhr.responseText.substring( position ),
  2016. bytes = new Uint8Array( binStr.length );
  2017. for ( var i = 0; i < binStr.length; i ++ ) {
  2018. bytes[ i ] = binStr.charCodeAt( i ) & 0xFF;
  2019. }
  2020. position += binStr.length;
  2021. self.append( bytes.buffer );
  2022. }
  2023. self.dispatchDownloadProgress( e.loaded, e.total );
  2024. };
  2025. if ( ! self.config.progressive ) {
  2026. xhr.onreadystatechange = function () {
  2027. if ( xhr.readyState === 4 ) {
  2028. if ( xhr.status === 200 || xhr.status === 0 ) {
  2029. self.read( this.response );
  2030. } else {
  2031. this.dispatchError( 1001, "Couldn't load [" + url + "] [" + xhr.status + "]" );
  2032. }
  2033. }
  2034. };
  2035. }
  2036. xhr.send();
  2037. };