SEA3D.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158
  1. /**
  2. * SEA3D SDK
  3. * @author Sunag / http://www.sunag.com.br/
  4. */
  5. 'use strict';
  6. var SEA3D = { VERSION: 18100 };
  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.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.readUTF8 = function ( len ) {
  180. return THREE.LoaderUtils.decodeText( new Uint8Array( this.readBytes( len ) ) );
  181. };
  182. SEA3D.Stream.prototype.readExt = function () {
  183. return this.readUTF8( 4 ).replace( /\0/g, "" );
  184. };
  185. SEA3D.Stream.prototype.readUTF8Tiny = function () {
  186. return this.readUTF8( this.readUByte() );
  187. };
  188. SEA3D.Stream.prototype.readUTF8Short = function () {
  189. return this.readUTF8( this.readUShort() );
  190. };
  191. SEA3D.Stream.prototype.readUTF8Long = function () {
  192. return this.readUTF8( 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, this, 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.readUTF8Tiny(),
  274. type = this.readUByte();
  275. types[ name ] = type;
  276. props[ name ] = type == SEA3D.Stream.GROUP ? this.readProperties( sea3d ) : this.readToken( type, sea3d );
  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.readUTF8Tiny();
  307. script.params[ name ] = this.readObject( sea3d );
  308. }
  309. }
  310. if ( attrib & 8 ) {
  311. script.method = this.readUTF8Tiny();
  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. // 1D
  324. case SEA3D.Stream.BOOLEAN:
  325. return this.readBool();
  326. break;
  327. case SEA3D.Stream.UBYTE:
  328. return this.readUByte();
  329. break;
  330. case SEA3D.Stream.USHORT:
  331. return this.readUShort();
  332. break;
  333. case SEA3D.Stream.UINT24:
  334. return this.readUInt24();
  335. break;
  336. case SEA3D.Stream.INT:
  337. return this.readInt();
  338. break;
  339. case SEA3D.Stream.UINT:
  340. return this.readUInt();
  341. break;
  342. case SEA3D.Stream.FLOAT:
  343. return this.readFloat();
  344. break;
  345. // 3D
  346. case SEA3D.Stream.VECTOR3D:
  347. return this.readVector3();
  348. break;
  349. // 4D
  350. case SEA3D.Stream.VECTOR4D:
  351. return this.readVector4();
  352. break;
  353. // Undefined Values
  354. case SEA3D.Stream.STRING_TINY:
  355. return this.readUTF8Tiny();
  356. break;
  357. case SEA3D.Stream.STRING_SHORT:
  358. return this.readUTF8Short();
  359. break;
  360. case SEA3D.Stream.STRING_LONG:
  361. return this.readUTF8Long();
  362. break;
  363. case SEA3D.Stream.ASSET:
  364. var asset = this.readUInt();
  365. return asset > 0 ? sea3d.getObject( asset - 1 ).tag : null;
  366. break;
  367. default:
  368. console.error( "DataType not found!" );
  369. }
  370. return null;
  371. };
  372. SEA3D.Stream.prototype.readVector = function ( type, length, offset ) {
  373. var size = SEA3D.Stream.sizeOf( type ),
  374. i = offset * size,
  375. count = i + ( length * size );
  376. switch ( type ) {
  377. // 1D
  378. case SEA3D.Stream.BOOLEAN:
  379. return this.readUByteArray( count );
  380. case SEA3D.Stream.UBYTE:
  381. return this.readUByteArray( count );
  382. case SEA3D.Stream.USHORT:
  383. return this.readUShortArray( count );
  384. case SEA3D.Stream.UINT24:
  385. return this.readUInt24Array( count );
  386. case SEA3D.Stream.UINT:
  387. return this.readUIntArray( count );
  388. case SEA3D.Stream.FLOAT:
  389. return this.readFloatArray( count );
  390. // 3D
  391. case SEA3D.Stream.VECTOR3D:
  392. return this.readFloatArray( count );
  393. // 4D
  394. case SEA3D.Stream.VECTOR4D:
  395. return this.readFloatArray( count );
  396. }
  397. };
  398. SEA3D.Stream.prototype.append = function ( data ) {
  399. var tmp = new ArrayBuffer( this.data.byteLength + data.byteLength );
  400. tmp.set( new ArrayBuffer( this.data ), 0 );
  401. tmp.set( new ArrayBuffer( data ), this.data.byteLength );
  402. this.data = tmp;
  403. };
  404. SEA3D.Stream.prototype.concat = function ( position, length ) {
  405. return new SEA3D.Stream( this.buffer.slice( position, position + length ) );
  406. };
  407. /**
  408. * @author DataStream.js
  409. */
  410. SEA3D.Stream.memcpy = function ( dst, dstOffset, src, srcOffset, byteLength ) {
  411. var dstU8 = new Uint8Array( dst, dstOffset, byteLength );
  412. var srcU8 = new Uint8Array( src, srcOffset, byteLength );
  413. dstU8.set( srcU8 );
  414. };
  415. //
  416. // UByteArray
  417. //
  418. SEA3D.UByteArray = function () {
  419. this.ubytes = [];
  420. this.length = 0;
  421. };
  422. SEA3D.UByteArray.prototype = {
  423. constructor: SEA3D.UByteArray,
  424. add: function ( ubytes ) {
  425. this.ubytes.push( ubytes );
  426. this.length += ubytes.byteLength;
  427. },
  428. toBuffer: function () {
  429. var memcpy = new Uint8Array( this.length );
  430. for ( var i = 0, offset = 0; i < this.ubytes.length; i ++ ) {
  431. memcpy.set( this.ubytes[ i ], offset );
  432. offset += this.ubytes[ i ].byteLength;
  433. }
  434. return memcpy.buffer;
  435. }
  436. };
  437. //
  438. // Math
  439. //
  440. SEA3D.Math = {
  441. DEGREES: 180 / Math.PI,
  442. RADIANS: Math.PI / 180
  443. };
  444. SEA3D.Math.angle = function ( val ) {
  445. var ang = 180,
  446. inv = val < 0;
  447. val = ( inv ? - val : val ) % 360;
  448. if ( val > ang ) {
  449. val = - ang + ( val - ang );
  450. }
  451. return ( inv ? - val : val );
  452. };
  453. SEA3D.Math.lerpAngle = function ( val, tar, t ) {
  454. if ( Math.abs( val - tar ) > 180 ) {
  455. if ( val > tar ) {
  456. tar += 360;
  457. } else {
  458. tar -= 360;
  459. }
  460. }
  461. val += ( tar - val ) * t;
  462. return SEA3D.Math.angle( val );
  463. };
  464. SEA3D.Math.lerpColor = function ( val, tar, t ) {
  465. var a0 = val >> 24 & 0xff,
  466. r0 = val >> 16 & 0xff,
  467. g0 = val >> 8 & 0xff,
  468. b0 = val & 0xff;
  469. var a1 = tar >> 24 & 0xff,
  470. r1 = tar >> 16 & 0xff,
  471. g1 = tar >> 8 & 0xff,
  472. b1 = tar & 0xff;
  473. a0 += ( a1 - a0 ) * t;
  474. r0 += ( r1 - r0 ) * t;
  475. g0 += ( g1 - g0 ) * t;
  476. b0 += ( b1 - b0 ) * t;
  477. return a0 << 24 | r0 << 16 | g0 << 8 | b0;
  478. };
  479. SEA3D.Math.lerp = function ( val, tar, t ) {
  480. return val + ( ( tar - val ) * t );
  481. };
  482. SEA3D.Math.lerp1x = function ( val, tar, t ) {
  483. val[ 0 ] += ( tar[ 0 ] - val[ 0 ] ) * t;
  484. };
  485. SEA3D.Math.lerp3x = function ( val, tar, t ) {
  486. val[ 0 ] += ( tar[ 0 ] - val[ 0 ] ) * t;
  487. val[ 1 ] += ( tar[ 1 ] - val[ 1 ] ) * t;
  488. val[ 2 ] += ( tar[ 2 ] - val[ 2 ] ) * t;
  489. };
  490. SEA3D.Math.lerpAng1x = function ( val, tar, t ) {
  491. val[ 0 ] = SEA3D.Math.lerpAngle( val[ 0 ], tar[ 0 ], t );
  492. };
  493. SEA3D.Math.lerpColor1x = function ( val, tar, t ) {
  494. val[ 0 ] = SEA3D.Math.lerpColor( val[ 0 ], tar[ 0 ], t );
  495. };
  496. SEA3D.Math.lerpQuat4x = function ( val, tar, t ) {
  497. var x1 = val[ 0 ],
  498. y1 = val[ 1 ],
  499. z1 = val[ 2 ],
  500. w1 = val[ 3 ];
  501. var x2 = tar[ 0 ],
  502. y2 = tar[ 1 ],
  503. z2 = tar[ 2 ],
  504. w2 = tar[ 3 ];
  505. var x, y, z, w, l;
  506. // shortest direction
  507. if ( x1 * x2 + y1 * y2 + z1 * z2 + w1 * w2 < 0 ) {
  508. x2 = - x2;
  509. y2 = - y2;
  510. z2 = - z2;
  511. w2 = - w2;
  512. }
  513. x = x1 + t * ( x2 - x1 );
  514. y = y1 + t * ( y2 - y1 );
  515. z = z1 + t * ( z2 - z1 );
  516. w = w1 + t * ( w2 - w1 );
  517. l = 1.0 / Math.sqrt( w * w + x * x + y * y + z * z );
  518. val[ 0 ] = x * l;
  519. val[ 1 ] = y * l;
  520. val[ 2 ] = z * l;
  521. val[ 3 ] = w * l;
  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. this.uv = [];
  584. this.uv.length = data.readUByte();
  585. len = this.numVertex * 2;
  586. i = 0;
  587. while ( i < this.uv.length ) {
  588. // UV VERTEX DATA
  589. this.uv[ i ++ ] = data.readFloatArray( len );
  590. }
  591. }
  592. // JOINT-INDEXES / WEIGHTS
  593. if ( this.attrib & 64 ) {
  594. this.jointPerVertex = data.readUByte();
  595. var jntLen = this.numVertex * this.jointPerVertex;
  596. this.joint = data.readUShortArray( jntLen );
  597. this.weight = data.readFloatArray( jntLen );
  598. }
  599. // VERTEX_COLOR
  600. if ( this.attrib & 128 ) {
  601. var colorAttrib = data.readUByte();
  602. this.numColor = ( ( ( colorAttrib & 64 ) >> 6 ) | ( ( colorAttrib & 128 ) >> 6 ) ) + 1;
  603. this.color = [];
  604. for ( i = 0, len = colorAttrib & 15; i < len; i ++ ) {
  605. this.color.push( data.readFloatArray( this.numVertex * this.numColor ) );
  606. }
  607. }
  608. // VERTEX
  609. this.vertex = data.readFloatArray( this.length );
  610. // SUB-MESHES
  611. var count = data.readUByte();
  612. this.groups = [];
  613. if ( this.attrib & 1024 ) {
  614. // INDEXES
  615. for ( i = 0, len = 0; i < count; i ++ ) {
  616. j = data.readVInt() * 3;
  617. this.groups.push( {
  618. start: len,
  619. count: j,
  620. } );
  621. len += j;
  622. }
  623. this.indexes = this.isBig ? data.readUIntArray( len ) : data.readUShortArray( len );
  624. } else {
  625. // INDEXES
  626. var stride = this.isBig ? 4 : 2,
  627. bytearray = new SEA3D.UByteArray();
  628. for ( i = 0, j = 0; i < count; i ++ ) {
  629. len = data.readVInt() * 3;
  630. this.groups.push( {
  631. start: j,
  632. count: len,
  633. } );
  634. j += len;
  635. bytearray.add( data.readUByteArray( len * stride ) );
  636. }
  637. this.indexes = this.isBig ? new Uint32Array( bytearray.toBuffer() ) : new Uint16Array( bytearray.toBuffer() );
  638. }
  639. };
  640. SEA3D.Geometry.prototype = Object.create( SEA3D.GeometryBase.prototype );
  641. SEA3D.Geometry.prototype.constructor = SEA3D.Geometry;
  642. SEA3D.Geometry.prototype.type = "geo";
  643. //
  644. // Object3D
  645. //
  646. SEA3D.Object3D = function ( name, data, sea3d ) {
  647. this.name = name;
  648. this.data = data;
  649. this.sea3d = sea3d;
  650. this.isStatic = false;
  651. this.visible = true;
  652. this.attrib = data.readUShort();
  653. if ( this.attrib & 1 ) this.parent = sea3d.getObject( data.readUInt() );
  654. if ( this.attrib & 2 ) this.animations = data.readAnimationList( sea3d );
  655. if ( this.attrib & 4 ) this.scripts = data.readScriptList( sea3d );
  656. if ( this.attrib & 16 ) this.attributes = sea3d.getObject( data.readUInt() );
  657. if ( this.attrib & 32 ) {
  658. var objectType = data.readUByte();
  659. this.isStatic = ( objectType & 1 ) != 0;
  660. this.visible = ( objectType & 2 ) == 0;
  661. }
  662. };
  663. SEA3D.Object3D.prototype.readTag = function ( kind, data, size ) {
  664. };
  665. //
  666. // Entity3D
  667. //
  668. SEA3D.Entity3D = function ( name, data, sea3d ) {
  669. SEA3D.Object3D.call( this, name, data, sea3d );
  670. this.castShadows = true;
  671. if ( this.attrib & 64 ) {
  672. var lightType = data.readUByte();
  673. this.castShadows = ( lightType & 1 ) == 0;
  674. }
  675. };
  676. SEA3D.Entity3D.prototype = Object.create( SEA3D.Object3D.prototype );
  677. SEA3D.Entity3D.prototype.constructor = SEA3D.Entity3D;
  678. //
  679. // Sound3D
  680. //
  681. SEA3D.Sound3D = function ( name, data, sea3d ) {
  682. SEA3D.Object3D.call( this, name, data, sea3d );
  683. this.autoPlay = ( this.attrib & 64 ) != 0;
  684. if ( this.attrib & 128 ) this.mixer = sea3d.getObject( data.readUInt() );
  685. this.sound = sea3d.getObject( data.readUInt() );
  686. this.volume = data.readFloat();
  687. };
  688. SEA3D.Sound3D.prototype = Object.create( SEA3D.Object3D.prototype );
  689. SEA3D.Sound3D.prototype.constructor = SEA3D.Sound3D;
  690. //
  691. // Sound Point
  692. //
  693. SEA3D.SoundPoint = function ( name, data, sea3d ) {
  694. SEA3D.Sound3D.call( this, name, data, sea3d );
  695. this.position = data.readVector3();
  696. this.distance = data.readFloat();
  697. data.readTags( this.readTag.bind( this ) );
  698. };
  699. SEA3D.SoundPoint.prototype = Object.create( SEA3D.Sound3D.prototype );
  700. SEA3D.SoundPoint.prototype.constructor = SEA3D.SoundPoint;
  701. SEA3D.SoundPoint.prototype.type = "sp";
  702. //
  703. // Container3D
  704. //
  705. SEA3D.Container3D = function ( name, data, sea3d ) {
  706. SEA3D.Object3D.call( this, name, data, sea3d );
  707. this.transform = data.readMatrix();
  708. data.readTags( this.readTag.bind( this ) );
  709. };
  710. SEA3D.Container3D.prototype = Object.create( SEA3D.Object3D.prototype );
  711. SEA3D.Container3D.prototype.constructor = SEA3D.Container3D;
  712. SEA3D.Container3D.prototype.type = "c3d";
  713. //
  714. // Script URL
  715. //
  716. SEA3D.ScriptURL = function ( name, data, sea3d ) {
  717. this.name = name;
  718. this.data = data;
  719. this.sea3d = sea3d;
  720. this.url = data.readUTF8( data.length );
  721. };
  722. SEA3D.ScriptURL.prototype.type = "src";
  723. //
  724. // Texture URL
  725. //
  726. SEA3D.TextureURL = function ( name, data, sea3d ) {
  727. this.name = name;
  728. this.data = data;
  729. this.sea3d = sea3d;
  730. this.url = data.readUTF8( data.length );
  731. };
  732. SEA3D.TextureURL.prototype.type = "urlT";
  733. //
  734. // CubeMap URL
  735. //
  736. SEA3D.CubeMapURL = function ( name, data, sea3d ) {
  737. this.name = name;
  738. this.data = data;
  739. this.sea3d = sea3d;
  740. this.faces = [];
  741. for ( var i = 0; i < 6; i ++ ) {
  742. this.faces[ i ] = data.readUTF8Tiny();
  743. }
  744. };
  745. SEA3D.CubeMapURL.prototype.type = "cURL";
  746. //
  747. // Actions
  748. //
  749. SEA3D.Actions = function ( name, data, sea3d ) {
  750. this.name = name;
  751. this.data = data;
  752. this.sea3d = sea3d;
  753. this.count = data.readUInt();
  754. this.actions = [];
  755. for ( var i = 0; i < this.count; i ++ ) {
  756. var flag = data.readUByte();
  757. var kind = data.readUShort();
  758. var size = data.readUShort();
  759. var position = data.position;
  760. var act = this.actions[ i ] = { kind: kind };
  761. // range of animation
  762. if ( flag & 1 ) {
  763. // start and count in frames
  764. act.range = [ data.readUInt(), data.readUInt() ];
  765. }
  766. // time
  767. if ( flag & 2 ) {
  768. act.time = data.readUInt();
  769. }
  770. // easing
  771. if ( flag & 4 ) {
  772. act.intrpl = data.readInterpolation();
  773. if ( act.intrpl.indexOf( 'back.' ) == 0 ) {
  774. act.intrplParam0 = data.readFloat();
  775. } else if ( act.intrpl.indexOf( 'elastic.' ) == 0 ) {
  776. act.intrplParam0 = data.readFloat();
  777. act.intrplParam1 = data.readFloat();
  778. }
  779. }
  780. switch ( kind ) {
  781. case SEA3D.Actions.RTT_TARGET:
  782. act.source = sea3d.getObject( data.readUInt() );
  783. act.target = sea3d.getObject( data.readUInt() );
  784. break;
  785. case SEA3D.Actions.LOOK_AT:
  786. act.source = sea3d.getObject( data.readUInt() );
  787. act.target = sea3d.getObject( data.readUInt() );
  788. break;
  789. case SEA3D.Actions.PLAY_SOUND:
  790. act.sound = sea3d.getObject( data.readUInt() );
  791. act.offset = data.readUInt();
  792. break;
  793. case SEA3D.Actions.PLAY_ANIMATION:
  794. act.object = sea3d.getObject( data.readUInt() );
  795. act.name = data.readUTF8Tiny();
  796. break;
  797. case SEA3D.Actions.FOG:
  798. act.color = data.readUInt24();
  799. act.min = data.readFloat();
  800. act.max = data.readFloat();
  801. break;
  802. case SEA3D.Actions.ENVIRONMENT:
  803. act.texture = sea3d.getObject( data.readUInt() );
  804. break;
  805. case SEA3D.Actions.ENVIRONMENT_COLOR:
  806. act.color = data.readUInt24F();
  807. break;
  808. case SEA3D.Actions.CAMERA:
  809. act.camera = sea3d.getObject( data.readUInt() );
  810. break;
  811. case SEA3D.Actions.SCRIPTS:
  812. act.scripts = data.readScriptList( sea3d );
  813. break;
  814. case SEA3D.Actions.CLASS_OF:
  815. act.classof = sea3d.getObject( data.readUInt() );
  816. break;
  817. case SEA3D.Actions.ATTRIBUTES:
  818. act.attributes = sea3d.getObject( data.readUInt() );
  819. break;
  820. default:
  821. console.log( "Action \"" + kind + "\" not found." );
  822. break;
  823. }
  824. data.position = position + size;
  825. }
  826. };
  827. SEA3D.Actions.SCENE = 0;
  828. SEA3D.Actions.ENVIRONMENT_COLOR = 1;
  829. SEA3D.Actions.ENVIRONMENT = 2;
  830. SEA3D.Actions.FOG = 3;
  831. SEA3D.Actions.PLAY_ANIMATION = 4;
  832. SEA3D.Actions.PLAY_SOUND = 5;
  833. SEA3D.Actions.ANIMATION_AUDIO_SYNC = 6;
  834. SEA3D.Actions.LOOK_AT = 7;
  835. SEA3D.Actions.RTT_TARGET = 8;
  836. SEA3D.Actions.CAMERA = 9;
  837. SEA3D.Actions.SCRIPTS = 10;
  838. SEA3D.Actions.CLASS_OF = 11;
  839. SEA3D.Actions.ATTRIBUTES = 12;
  840. SEA3D.Actions.prototype.type = "act";
  841. //
  842. // Properties
  843. //
  844. SEA3D.Properties = function ( name, data, sea3d ) {
  845. this.name = name;
  846. this.data = data;
  847. this.sea3d = sea3d;
  848. this.tag = data.readProperties( sea3d );
  849. this.tag.__name = name;
  850. };
  851. SEA3D.Properties.prototype.type = "prop";
  852. //
  853. // File Info
  854. //
  855. SEA3D.FileInfo = function ( name, data, sea3d ) {
  856. this.name = name;
  857. this.data = data;
  858. this.sea3d = sea3d;
  859. this.tag = data.readProperties( sea3d );
  860. this.tag.__name = name;
  861. sea3d.info = this.tag;
  862. };
  863. SEA3D.FileInfo.prototype.type = "info";
  864. //
  865. // Java Script
  866. //
  867. SEA3D.JavaScript = function ( name, data, sea3d ) {
  868. this.name = name;
  869. this.data = data;
  870. this.sea3d = sea3d;
  871. this.src = data.readUTF8( data.length );
  872. };
  873. SEA3D.JavaScript.prototype.type = "js";
  874. //
  875. // Java Script Method
  876. //
  877. SEA3D.JavaScriptMethod = function ( name, data, sea3d ) {
  878. this.name = name;
  879. this.data = data;
  880. this.sea3d = sea3d;
  881. var count = data.readUShort();
  882. this.methods = {};
  883. for ( var i = 0; i < count; i ++ ) {
  884. var flag = data.readUByte();
  885. var method = data.readUTF8Tiny();
  886. this.methods[ method ] = {
  887. src: data.readUTF8Long()
  888. };
  889. }
  890. };
  891. SEA3D.JavaScriptMethod.prototype.type = "jsm";
  892. //
  893. // GLSL
  894. //
  895. SEA3D.GLSL = function ( name, data, sea3d ) {
  896. this.name = name;
  897. this.data = data;
  898. this.sea3d = sea3d;
  899. this.src = data.readUTF8( data.length );
  900. };
  901. SEA3D.GLSL.prototype.type = "glsl";
  902. //
  903. // Dummy
  904. //
  905. SEA3D.Dummy = function ( name, data, sea3d ) {
  906. SEA3D.Object3D.call( this, name, data, sea3d );
  907. this.transform = data.readMatrix();
  908. this.width = data.readFloat();
  909. this.height = data.readFloat();
  910. this.depth = data.readFloat();
  911. data.readTags( this.readTag.bind( this ) );
  912. };
  913. SEA3D.Dummy.prototype = Object.create( SEA3D.Object3D.prototype );
  914. SEA3D.Dummy.prototype.constructor = SEA3D.Dummy;
  915. SEA3D.Dummy.prototype.type = "dmy";
  916. //
  917. // Line
  918. //
  919. SEA3D.Line = function ( name, data, sea3d ) {
  920. SEA3D.Object3D.call( this, name, data, sea3d );
  921. this.count = ( this.attrib & 64 ? data.readUInt() : data.readUShort() ) * 3;
  922. this.closed = ( this.attrib & 128 ) != 0;
  923. this.transform = data.readMatrix();
  924. this.vertex = [];
  925. var i = 0;
  926. while ( i < this.count ) {
  927. this.vertex[ i ++ ] = data.readFloat();
  928. }
  929. data.readTags( this.readTag.bind( this ) );
  930. };
  931. SEA3D.Line.prototype = Object.create( SEA3D.Object3D.prototype );
  932. SEA3D.Line.prototype.constructor = SEA3D.Line;
  933. SEA3D.Line.prototype.type = "line";
  934. //
  935. // Sprite
  936. //
  937. SEA3D.Sprite = function ( name, data, sea3d ) {
  938. SEA3D.Object3D.call( this, name, data, sea3d );
  939. if ( this.attrib & 256 ) {
  940. this.material = sea3d.getObject( data.readUInt() );
  941. }
  942. this.position = data.readVector3();
  943. this.width = data.readFloat();
  944. this.height = data.readFloat();
  945. data.readTags( this.readTag.bind( this ) );
  946. };
  947. SEA3D.Sprite.prototype = Object.create( SEA3D.Object3D.prototype );
  948. SEA3D.Sprite.prototype.constructor = SEA3D.Sprite;
  949. SEA3D.Sprite.prototype.type = "m2d";
  950. //
  951. // Mesh
  952. //
  953. SEA3D.Mesh = function ( name, data, sea3d ) {
  954. SEA3D.Entity3D.call( this, name, data, sea3d );
  955. // MATERIAL
  956. if ( this.attrib & 256 ) {
  957. this.material = [];
  958. var len = data.readUByte();
  959. if ( len == 1 ) this.material[ 0 ] = sea3d.getObject( data.readUInt() );
  960. else {
  961. var i = 0;
  962. while ( i < len ) {
  963. var matIndex = data.readUInt();
  964. if ( matIndex > 0 ) this.material[ i ++ ] = sea3d.getObject( matIndex - 1 );
  965. else this.material[ i ++ ] = undefined;
  966. }
  967. }
  968. }
  969. if ( this.attrib & 512 ) {
  970. this.modifiers = [];
  971. var len = data.readUByte();
  972. for ( var i = 0; i < len; i ++ ) {
  973. this.modifiers[ i ] = sea3d.getObject( data.readUInt() );
  974. }
  975. }
  976. if ( this.attrib & 1024 ) {
  977. this.reference = {
  978. type: data.readUByte(),
  979. ref: sea3d.getObject( data.readUInt() )
  980. };
  981. }
  982. this.transform = data.readMatrix();
  983. this.geometry = sea3d.getObject( data.readUInt() );
  984. data.readTags( this.readTag.bind( this ) );
  985. };
  986. SEA3D.Mesh.prototype = Object.create( SEA3D.Entity3D.prototype );
  987. SEA3D.Mesh.prototype.constructor = SEA3D.Mesh;
  988. SEA3D.Mesh.prototype.type = "m3d";
  989. //
  990. // Skeleton
  991. //
  992. SEA3D.Skeleton = function ( name, data, sea3d ) {
  993. this.name = name;
  994. this.data = data;
  995. this.sea3d = sea3d;
  996. var length = data.readUShort();
  997. this.joint = [];
  998. for ( var i = 0; i < length; i ++ ) {
  999. this.joint[ i ] = {
  1000. name: data.readUTF8Tiny(),
  1001. parentIndex: data.readUShort() - 1,
  1002. inverseBindMatrix: data.readMatrix()
  1003. };
  1004. }
  1005. };
  1006. SEA3D.Skeleton.prototype.type = "skl";
  1007. //
  1008. // Skeleton Local
  1009. //
  1010. SEA3D.SkeletonLocal = function ( name, data, sea3d ) {
  1011. this.name = name;
  1012. this.data = data;
  1013. this.sea3d = sea3d;
  1014. var length = data.readUShort();
  1015. this.joint = [];
  1016. for ( var i = 0; i < length; i ++ ) {
  1017. this.joint[ i ] = {
  1018. name: data.readUTF8Tiny(),
  1019. parentIndex: data.readUShort() - 1,
  1020. // POSITION XYZ
  1021. x: data.readFloat(),
  1022. y: data.readFloat(),
  1023. z: data.readFloat(),
  1024. // QUATERNION XYZW
  1025. qx: data.readFloat(),
  1026. qy: data.readFloat(),
  1027. qz: data.readFloat(),
  1028. qw: data.readFloat()
  1029. };
  1030. }
  1031. };
  1032. SEA3D.SkeletonLocal.prototype.type = "sklq";
  1033. //
  1034. // Animation Base
  1035. //
  1036. SEA3D.AnimationBase = function ( name, data, sea3d ) {
  1037. this.name = name;
  1038. this.data = data;
  1039. this.sea3d = sea3d;
  1040. var flag = data.readUByte();
  1041. this.sequence = [];
  1042. if ( flag & 1 ) {
  1043. var count = data.readUShort();
  1044. for ( var i = 0; i < count; i ++ ) {
  1045. flag = data.readUByte();
  1046. this.sequence[ i ] = {
  1047. name: data.readUTF8Tiny(),
  1048. start: data.readUInt(),
  1049. count: data.readUInt(),
  1050. repeat: ( flag & 1 ) != 0,
  1051. intrpl: ( flag & 2 ) == 0
  1052. };
  1053. }
  1054. }
  1055. this.frameRate = data.readUByte();
  1056. this.numFrames = data.readUInt();
  1057. // no contains sequence
  1058. if ( this.sequence.length == 0 ) {
  1059. this.sequence[ 0 ] = { name: "root", start: 0, count: this.numFrames, repeat: true, intrpl: true };
  1060. }
  1061. };
  1062. //
  1063. // Animation
  1064. //
  1065. SEA3D.Animation = function ( name, data, sea3d ) {
  1066. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1067. this.dataList = [];
  1068. for ( var i = 0, l = data.readUByte(); i < l; i ++ ) {
  1069. var kind = data.readUShort(),
  1070. type = data.readUByte();
  1071. var anmRaw = data.readVector( type, this.numFrames, 0 );
  1072. this.dataList.push( {
  1073. kind: kind,
  1074. type: type,
  1075. blockSize: SEA3D.Stream.sizeOf( type ),
  1076. data: anmRaw
  1077. } );
  1078. }
  1079. };
  1080. SEA3D.Animation.POSITION = 0;
  1081. SEA3D.Animation.ROTATION = 1;
  1082. SEA3D.Animation.SCALE = 2;
  1083. SEA3D.Animation.COLOR = 3;
  1084. SEA3D.Animation.MULTIPLIER = 4;
  1085. SEA3D.Animation.ATTENUATION_START = 5;
  1086. SEA3D.Animation.ATTENUATION_END = 6;
  1087. SEA3D.Animation.FOV = 7;
  1088. SEA3D.Animation.OFFSET_U = 8;
  1089. SEA3D.Animation.OFFSET_V = 9;
  1090. SEA3D.Animation.SCALE_U = 10;
  1091. SEA3D.Animation.SCALE_V = 11;
  1092. SEA3D.Animation.ANGLE = 12;
  1093. SEA3D.Animation.ALPHA = 13;
  1094. SEA3D.Animation.VOLUME = 14;
  1095. SEA3D.Animation.DefaultLerpFuncs = [
  1096. SEA3D.Math.lerp3x, // POSITION
  1097. SEA3D.Math.lerpQuat4x, // ROTATION
  1098. SEA3D.Math.lerp3x, // SCALE
  1099. SEA3D.Math.lerpColor1x, // COLOR
  1100. SEA3D.Math.lerp1x, // MULTIPLIER
  1101. SEA3D.Math.lerp1x, // ATTENUATION_START
  1102. SEA3D.Math.lerp1x, // ATTENUATION_END
  1103. SEA3D.Math.lerp1x, // FOV
  1104. SEA3D.Math.lerp1x, // OFFSET_U
  1105. SEA3D.Math.lerp1x, // OFFSET_V
  1106. SEA3D.Math.lerp1x, // SCALE_U
  1107. SEA3D.Math.lerp1x, // SCALE_V
  1108. SEA3D.Math.lerpAng1x, // ANGLE
  1109. SEA3D.Math.lerp1x, // ALPHA
  1110. SEA3D.Math.lerp1x // VOLUME
  1111. ];
  1112. SEA3D.Animation.prototype = Object.create( SEA3D.AnimationBase.prototype );
  1113. SEA3D.Animation.prototype.constructor = SEA3D.Animation;
  1114. SEA3D.Animation.prototype.type = "anm";
  1115. //
  1116. // Skeleton Animation
  1117. //
  1118. SEA3D.SkeletonAnimation = function ( name, data, sea3d ) {
  1119. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1120. this.name = name;
  1121. this.data = data;
  1122. this.sea3d = sea3d;
  1123. this.numJoints = data.readUShort();
  1124. this.raw = data.readFloatArray( this.numFrames * this.numJoints * 7 );
  1125. };
  1126. SEA3D.SkeletonAnimation.prototype.type = "skla";
  1127. //
  1128. // Morph
  1129. //
  1130. SEA3D.Morph = function ( name, data, sea3d ) {
  1131. SEA3D.GeometryBase.call( this, name, data, sea3d );
  1132. var useVertex = ( this.attrib & 2 ) != 0;
  1133. var useNormal = ( this.attrib & 4 ) != 0;
  1134. var nodeCount = data.readUShort();
  1135. this.node = [];
  1136. for ( var i = 0; i < nodeCount; i ++ ) {
  1137. var nodeName = data.readUTF8Tiny(),
  1138. verts, norms;
  1139. if ( useVertex ) verts = data.readFloatArray( this.length );
  1140. if ( useNormal ) norms = data.readFloatArray( this.length );
  1141. this.node[ i ] = { vertex: verts, normal: norms, name: nodeName };
  1142. }
  1143. };
  1144. SEA3D.Morph.prototype = Object.create( SEA3D.GeometryBase.prototype );
  1145. SEA3D.Morph.prototype.constructor = SEA3D.Morph;
  1146. SEA3D.Morph.prototype.type = "mph";
  1147. //
  1148. // Vertex Animation
  1149. //
  1150. SEA3D.VertexAnimation = function ( name, data, sea3d ) {
  1151. SEA3D.AnimationBase.call( this, name, data, sea3d );
  1152. var flags = data.readUByte();
  1153. this.isBig = ( flags & 1 ) != 0;
  1154. data.readVInt = this.isBig ? data.readUInt : data.readUShort;
  1155. this.numVertex = data.readVInt();
  1156. this.length = this.numVertex * 3;
  1157. var useVertex = ( flags & 2 ) != 0;
  1158. var useNormal = ( flags & 4 ) != 0;
  1159. this.frame = [];
  1160. var i, verts, norms;
  1161. for ( i = 0; i < this.numFrames; i ++ ) {
  1162. if ( useVertex ) verts = data.readFloatArray( this.length );
  1163. if ( useNormal ) norms = data.readFloatArray( this.length );
  1164. this.frame[ i ] = { vertex: verts, normal: norms };
  1165. }
  1166. };
  1167. SEA3D.VertexAnimation.prototype = Object.create( SEA3D.AnimationBase.prototype );
  1168. SEA3D.VertexAnimation.prototype.constructor = SEA3D.VertexAnimation;
  1169. SEA3D.VertexAnimation.prototype.type = "vtxa";
  1170. //
  1171. // Camera
  1172. //
  1173. SEA3D.Camera = function ( name, data, sea3d ) {
  1174. SEA3D.Object3D.call( this, name, data, sea3d );
  1175. if ( this.attrib & 64 ) {
  1176. this.dof = {
  1177. distance: data.readFloat(),
  1178. range: data.readFloat()
  1179. };
  1180. }
  1181. this.transform = data.readMatrix();
  1182. this.fov = data.readFloat();
  1183. data.readTags( this.readTag.bind( this ) );
  1184. };
  1185. SEA3D.Camera.prototype = Object.create( SEA3D.Object3D.prototype );
  1186. SEA3D.Camera.prototype.constructor = SEA3D.Camera;
  1187. SEA3D.Camera.prototype.type = "cam";
  1188. //
  1189. // Orthographic Camera
  1190. //
  1191. SEA3D.OrthographicCamera = function ( name, data, sea3d ) {
  1192. SEA3D.Object3D.call( this, name, data, sea3d );
  1193. this.transform = data.readMatrix();
  1194. this.height = data.readFloat();
  1195. data.readTags( this.readTag.bind( this ) );
  1196. };
  1197. SEA3D.OrthographicCamera.prototype = Object.create( SEA3D.Object3D.prototype );
  1198. SEA3D.OrthographicCamera.prototype.constructor = SEA3D.OrthographicCamera;
  1199. SEA3D.OrthographicCamera.prototype.type = "camo";
  1200. //
  1201. // Joint Object
  1202. //
  1203. SEA3D.JointObject = function ( name, data, sea3d ) {
  1204. SEA3D.Object3D.call( this, name, data, sea3d );
  1205. this.target = sea3d.getObject( data.readUInt() );
  1206. this.joint = data.readUShort();
  1207. data.readTags( this.readTag.bind( this ) );
  1208. };
  1209. SEA3D.JointObject.prototype = Object.create( SEA3D.Object3D.prototype );
  1210. SEA3D.JointObject.prototype.constructor = SEA3D.JointObject;
  1211. SEA3D.JointObject.prototype.type = "jnt";
  1212. //
  1213. // Light
  1214. //
  1215. SEA3D.Light = function ( name, data, sea3d ) {
  1216. SEA3D.Object3D.call( this, name, data, sea3d );
  1217. this.attenStart = Number.MAX_VALUE;
  1218. this.attenEnd = Number.MAX_VALUE;
  1219. if ( this.attrib & 64 ) {
  1220. var shadowHeader = data.readUByte();
  1221. this.shadow = {};
  1222. this.shadow.opacity = shadowHeader & 1 ? data.readFloat() : 1;
  1223. this.shadow.color = shadowHeader & 2 ? data.readUInt24() : 0x000000;
  1224. }
  1225. if ( this.attrib & 512 ) {
  1226. this.attenStart = data.readFloat();
  1227. this.attenEnd = data.readFloat();
  1228. }
  1229. this.color = data.readUInt24();
  1230. this.multiplier = data.readFloat();
  1231. };
  1232. SEA3D.Light.prototype = Object.create( SEA3D.Object3D.prototype );
  1233. SEA3D.Light.prototype.constructor = SEA3D.Light;
  1234. //
  1235. // Point Light
  1236. //
  1237. SEA3D.PointLight = function ( name, data, sea3d ) {
  1238. SEA3D.Light.call( this, name, data, sea3d );
  1239. if ( this.attrib & 128 ) {
  1240. this.attenuation = {
  1241. start: data.readFloat(),
  1242. end: data.readFloat()
  1243. };
  1244. }
  1245. this.position = data.readVector3();
  1246. data.readTags( this.readTag.bind( this ) );
  1247. };
  1248. SEA3D.PointLight.prototype = Object.create( SEA3D.Light.prototype );
  1249. SEA3D.PointLight.prototype.constructor = SEA3D.PointLight;
  1250. SEA3D.PointLight.prototype.type = "plht";
  1251. //
  1252. // Hemisphere Light
  1253. //
  1254. SEA3D.HemisphereLight = function ( name, data, sea3d ) {
  1255. SEA3D.Light.call( this, name, data, sea3d );
  1256. if ( this.attrib & 128 ) {
  1257. this.attenuation = {
  1258. start: data.readFloat(),
  1259. end: data.readFloat()
  1260. };
  1261. }
  1262. this.secondColor = data.readUInt24();
  1263. data.readTags( this.readTag.bind( this ) );
  1264. };
  1265. SEA3D.HemisphereLight.prototype = Object.create( SEA3D.Light.prototype );
  1266. SEA3D.HemisphereLight.prototype.constructor = SEA3D.HemisphereLight;
  1267. SEA3D.HemisphereLight.prototype.type = "hlht";
  1268. //
  1269. // Ambient Light
  1270. //
  1271. SEA3D.AmbientLight = function ( name, data, sea3d ) {
  1272. SEA3D.Light.call( this, name, data, sea3d );
  1273. data.readTags( this.readTag.bind( this ) );
  1274. };
  1275. SEA3D.AmbientLight.prototype = Object.create( SEA3D.Light.prototype );
  1276. SEA3D.AmbientLight.prototype.constructor = SEA3D.AmbientLight;
  1277. SEA3D.AmbientLight.prototype.type = "alht";
  1278. //
  1279. // Directional Light
  1280. //
  1281. SEA3D.DirectionalLight = function ( name, data, sea3d ) {
  1282. SEA3D.Light.call( this, name, data, sea3d );
  1283. this.transform = data.readMatrix();
  1284. data.readTags( this.readTag.bind( this ) );
  1285. };
  1286. SEA3D.DirectionalLight.prototype = Object.create( SEA3D.Light.prototype );
  1287. SEA3D.DirectionalLight.prototype.constructor = SEA3D.DirectionalLight;
  1288. SEA3D.DirectionalLight.prototype.type = "dlht";
  1289. //
  1290. // Material
  1291. //
  1292. SEA3D.Material = function ( name, data, sea3d ) {
  1293. this.name = name;
  1294. this.data = data;
  1295. this.sea3d = sea3d;
  1296. this.technique = [];
  1297. this.tecniquesDict = {};
  1298. this.attrib = data.readUShort();
  1299. this.alpha = 1;
  1300. this.blendMode = "normal";
  1301. this.physical = false;
  1302. this.anisotropy = false;
  1303. this.bothSides = ( this.attrib & 1 ) != 0;
  1304. this.receiveLights = ( this.attrib & 2 ) == 0;
  1305. this.receiveShadows = ( this.attrib & 4 ) == 0;
  1306. this.receiveFog = ( this.attrib & 8 ) == 0;
  1307. this.repeat = ( this.attrib & 16 ) == 0;
  1308. if ( this.attrib & 32 )
  1309. this.alpha = data.readFloat();
  1310. if ( this.attrib & 64 )
  1311. this.blendMode = data.readBlendMode();
  1312. if ( this.attrib & 128 )
  1313. this.animations = data.readAnimationList( sea3d );
  1314. this.depthWrite = ( this.attrib & 256 ) == 0;
  1315. this.depthTest = ( this.attrib & 512 ) == 0;
  1316. this.premultipliedAlpha = ( this.attrib & 1024 ) != 0;
  1317. var count = data.readUByte();
  1318. for ( var i = 0; i < count; ++ i ) {
  1319. var kind = data.readUShort();
  1320. var size = data.readUShort();
  1321. var pos = data.position;
  1322. var tech, methodAttrib;
  1323. switch ( kind ) {
  1324. case SEA3D.Material.PHONG:
  1325. tech = {
  1326. ambientColor: data.readUInt24(),
  1327. diffuseColor: data.readUInt24(),
  1328. specularColor: data.readUInt24(),
  1329. specular: data.readFloat(),
  1330. gloss: data.readFloat()
  1331. };
  1332. break;
  1333. case SEA3D.Material.PHYSICAL:
  1334. tech = {
  1335. color: data.readUInt24(),
  1336. roughness: data.readFloat(),
  1337. metalness: data.readFloat()
  1338. };
  1339. break;
  1340. case SEA3D.Material.ANISOTROPIC:
  1341. break;
  1342. case SEA3D.Material.COMPOSITE_TEXTURE:
  1343. tech = {
  1344. composite: sea3d.getObject( data.readUInt() )
  1345. };
  1346. break;
  1347. case SEA3D.Material.DIFFUSE_MAP:
  1348. tech = {
  1349. texture: sea3d.getObject( data.readUInt() )
  1350. };
  1351. break;
  1352. case SEA3D.Material.SPECULAR_MAP:
  1353. tech = {
  1354. texture: sea3d.getObject( data.readUInt() )
  1355. };
  1356. break;
  1357. case SEA3D.Material.NORMAL_MAP:
  1358. tech = {
  1359. texture: sea3d.getObject( data.readUInt() )
  1360. };
  1361. break;
  1362. case SEA3D.Material.REFLECTION:
  1363. case SEA3D.Material.FRESNEL_REFLECTION:
  1364. tech = {
  1365. texture: sea3d.getObject( data.readUInt() ),
  1366. alpha: data.readFloat()
  1367. };
  1368. if ( kind == SEA3D.Material.FRESNEL_REFLECTION ) {
  1369. tech.power = data.readFloat();
  1370. tech.normal = data.readFloat();
  1371. }
  1372. break;
  1373. case SEA3D.Material.REFRACTION:
  1374. tech = {
  1375. texture: sea3d.getObject( data.readUInt() ),
  1376. alpha: data.readFloat(),
  1377. ior: data.readFloat()
  1378. };
  1379. break;
  1380. case SEA3D.Material.RIM:
  1381. tech = {
  1382. color: data.readUInt24(),
  1383. strength: data.readFloat(),
  1384. power: data.readFloat(),
  1385. blendMode: data.readBlendMode()
  1386. };
  1387. break;
  1388. case SEA3D.Material.LIGHT_MAP:
  1389. tech = {
  1390. texture: sea3d.getObject( data.readUInt() ),
  1391. channel: data.readUByte(),
  1392. blendMode: data.readBlendMode()
  1393. };
  1394. break;
  1395. case SEA3D.Material.DETAIL_MAP:
  1396. tech = {
  1397. texture: sea3d.getObject( data.readUInt() ),
  1398. scale: data.readFloat(),
  1399. blendMode: data.readBlendMode()
  1400. };
  1401. break;
  1402. case SEA3D.Material.CEL:
  1403. tech = {
  1404. color: data.readUInt24(),
  1405. levels: data.readUByte(),
  1406. size: data.readFloat(),
  1407. specularCutOff: data.readFloat(),
  1408. smoothness: data.readFloat()
  1409. };
  1410. break;
  1411. case SEA3D.Material.TRANSLUCENT:
  1412. tech = {
  1413. translucency: data.readFloat(),
  1414. scattering: data.readFloat()
  1415. };
  1416. break;
  1417. case SEA3D.Material.BLEND_NORMAL_MAP:
  1418. methodAttrib = data.readUByte();
  1419. tech = {
  1420. texture: sea3d.getObject( data.readUInt() ),
  1421. secondaryTexture: sea3d.getObject( data.readUInt() )
  1422. };
  1423. if ( methodAttrib & 1 ) {
  1424. tech.offsetX0 = data.readFloat();
  1425. tech.offsetY0 = data.readFloat();
  1426. tech.offsetX1 = data.readFloat();
  1427. tech.offsetY1 = data.readFloat();
  1428. } else {
  1429. tech.offsetX0 = tech.offsetY0 =
  1430. tech.offsetX1 = tech.offsetY1 = 0;
  1431. }
  1432. tech.animate = methodAttrib & 2;
  1433. break;
  1434. case SEA3D.Material.MIRROR_REFLECTION:
  1435. tech = {
  1436. texture: sea3d.getObject( data.readUInt() ),
  1437. alpha: data.readFloat()
  1438. };
  1439. break;
  1440. case SEA3D.Material.AMBIENT_MAP:
  1441. tech = {
  1442. texture: sea3d.getObject( data.readUInt() )
  1443. };
  1444. break;
  1445. case SEA3D.Material.ALPHA_MAP:
  1446. tech = {
  1447. texture: sea3d.getObject( data.readUInt() )
  1448. };
  1449. break;
  1450. case SEA3D.Material.EMISSIVE:
  1451. tech = {
  1452. color: data.readUInt24()
  1453. };
  1454. break;
  1455. case SEA3D.Material.EMISSIVE_MAP:
  1456. tech = {
  1457. texture: sea3d.getObject( data.readUInt() )
  1458. };
  1459. break;
  1460. case SEA3D.Material.ROUGHNESS_MAP:
  1461. case SEA3D.Material.METALNESS_MAP:
  1462. tech = {
  1463. texture: sea3d.getObject( data.readUInt() )
  1464. };
  1465. break;
  1466. case SEA3D.Material.VERTEX_COLOR:
  1467. tech = {
  1468. blendMode: data.readBlendMode()
  1469. };
  1470. break;
  1471. case SEA3D.Material.WRAP_LIGHTING:
  1472. tech = {
  1473. color: data.readUInt24(),
  1474. strength: data.readFloat()
  1475. };
  1476. break;
  1477. case SEA3D.Material.COLOR_REPLACE:
  1478. methodAttrib = data.readUByte();
  1479. tech = {
  1480. red: data.readUInt24(),
  1481. green: data.readUInt24(),
  1482. blue: data.readUInt24F()
  1483. };
  1484. if ( methodAttrib & 1 ) tech.mask = sea3d.getObject( data.readUInt() );
  1485. if ( methodAttrib & 2 ) tech.alpha = data.readFloat();
  1486. break;
  1487. case SEA3D.Material.REFLECTION_SPHERICAL:
  1488. tech = {
  1489. texture: sea3d.getObject( data.readUInt() ),
  1490. alpha: data.readFloat()
  1491. };
  1492. break;
  1493. case SEA3D.Material.REFLECTIVITY:
  1494. methodAttrib = data.readUByte();
  1495. tech = {
  1496. strength: data.readFloat()
  1497. };
  1498. if ( methodAttrib & 1 ) tech.mask = sea3d.getObject( data.readUInt() );
  1499. break;
  1500. case SEA3D.Material.CLEAR_COAT:
  1501. tech = {
  1502. strength: data.readFloat(),
  1503. roughness: data.readFloat()
  1504. };
  1505. break;
  1506. default:
  1507. console.warn( "SEA3D: MaterialTechnique not found:", kind.toString( 16 ) );
  1508. data.position = pos += size;
  1509. continue;
  1510. }
  1511. tech.kind = kind;
  1512. this.technique.push( tech );
  1513. this.tecniquesDict[ kind ] = tech;
  1514. data.position = pos += size;
  1515. }
  1516. };
  1517. SEA3D.Material.PHONG = 0;
  1518. SEA3D.Material.COMPOSITE_TEXTURE = 1;
  1519. SEA3D.Material.DIFFUSE_MAP = 2;
  1520. SEA3D.Material.SPECULAR_MAP = 3;
  1521. SEA3D.Material.REFLECTION = 4;
  1522. SEA3D.Material.REFRACTION = 5;
  1523. SEA3D.Material.NORMAL_MAP = 6;
  1524. SEA3D.Material.FRESNEL_REFLECTION = 7;
  1525. SEA3D.Material.RIM = 8;
  1526. SEA3D.Material.LIGHT_MAP = 9;
  1527. SEA3D.Material.DETAIL_MAP = 10;
  1528. SEA3D.Material.CEL = 11;
  1529. SEA3D.Material.TRANSLUCENT = 12;
  1530. SEA3D.Material.BLEND_NORMAL_MAP = 13;
  1531. SEA3D.Material.MIRROR_REFLECTION = 14;
  1532. SEA3D.Material.AMBIENT_MAP = 15;
  1533. SEA3D.Material.ALPHA_MAP = 16;
  1534. SEA3D.Material.EMISSIVE_MAP = 17;
  1535. SEA3D.Material.VERTEX_COLOR = 18;
  1536. SEA3D.Material.WRAP_LIGHTING = 19;
  1537. SEA3D.Material.COLOR_REPLACE = 20;
  1538. SEA3D.Material.REFLECTION_SPHERICAL = 21;
  1539. SEA3D.Material.ANISOTROPIC = 22;
  1540. SEA3D.Material.EMISSIVE = 23;
  1541. SEA3D.Material.PHYSICAL = 24;
  1542. SEA3D.Material.ROUGHNESS_MAP = 25;
  1543. SEA3D.Material.METALNESS_MAP = 26;
  1544. SEA3D.Material.REFLECTIVITY = 27;
  1545. SEA3D.Material.CLEAR_COAT = 28;
  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. var ext = 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. // FILE FORMAT
  1694. //
  1695. SEA3D.File = function ( config ) {
  1696. this.config = {
  1697. streaming: true,
  1698. timeLimit: 60
  1699. };
  1700. if ( config ) {
  1701. if ( config.streaming !== undefined ) this.config.streaming = config.streaming;
  1702. if ( config.timeLimit !== undefined ) this.config.timeLimit = config.timeLimit;
  1703. }
  1704. this.version = SEA3D.VERSION;
  1705. this.objects = [];
  1706. this.typeClass = {};
  1707. this.typeRead = {};
  1708. this.typeUnique = {};
  1709. this.position =
  1710. this.dataPosition = 0;
  1711. this.scope = this;
  1712. // SEA3D
  1713. this.addClass( SEA3D.FileInfo, true );
  1714. this.addClass( SEA3D.Geometry, true );
  1715. this.addClass( SEA3D.Mesh );
  1716. this.addClass( SEA3D.Sprite );
  1717. this.addClass( SEA3D.Material );
  1718. this.addClass( SEA3D.Composite );
  1719. this.addClass( SEA3D.PointLight );
  1720. this.addClass( SEA3D.DirectionalLight );
  1721. this.addClass( SEA3D.HemisphereLight );
  1722. this.addClass( SEA3D.AmbientLight );
  1723. this.addClass( SEA3D.Skeleton, true );
  1724. this.addClass( SEA3D.SkeletonLocal, true );
  1725. this.addClass( SEA3D.SkeletonAnimation, true );
  1726. this.addClass( SEA3D.JointObject );
  1727. this.addClass( SEA3D.Camera );
  1728. this.addClass( SEA3D.OrthographicCamera );
  1729. this.addClass( SEA3D.Morph, true );
  1730. this.addClass( SEA3D.VertexAnimation, true );
  1731. this.addClass( SEA3D.CubeMap, true );
  1732. this.addClass( SEA3D.Animation );
  1733. this.addClass( SEA3D.Dummy );
  1734. this.addClass( SEA3D.Line );
  1735. this.addClass( SEA3D.SoundPoint );
  1736. this.addClass( SEA3D.PlanarRender );
  1737. this.addClass( SEA3D.CubeRender );
  1738. this.addClass( SEA3D.Actions );
  1739. this.addClass( SEA3D.Container3D );
  1740. this.addClass( SEA3D.Properties );
  1741. // URL BASED
  1742. this.addClass( SEA3D.ScriptURL, true );
  1743. this.addClass( SEA3D.TextureURL, true );
  1744. this.addClass( SEA3D.CubeMapURL, true );
  1745. // UNIVERSAL
  1746. this.addClass( SEA3D.JPEG, true );
  1747. this.addClass( SEA3D.JPEG_XR, true );
  1748. this.addClass( SEA3D.PNG, true );
  1749. this.addClass( SEA3D.GIF, true );
  1750. this.addClass( SEA3D.OGG, true );
  1751. this.addClass( SEA3D.MP3, true );
  1752. this.addClass( SEA3D.JavaScript, true );
  1753. this.addClass( SEA3D.JavaScriptMethod, true );
  1754. this.addClass( SEA3D.GLSL, true );
  1755. // Extensions
  1756. var i = SEA3D.File.Extensions.length;
  1757. while ( i -- ) {
  1758. SEA3D.File.Extensions[ i ].call( this );
  1759. }
  1760. };
  1761. SEA3D.File.Extensions = [];
  1762. SEA3D.File.CompressionLibs = {};
  1763. SEA3D.File.DecompressionMethod = {};
  1764. SEA3D.File.setExtension = function ( callback ) {
  1765. SEA3D.File.Extensions.push( callback );
  1766. };
  1767. SEA3D.File.setDecompressionEngine = function ( id, name, method ) {
  1768. SEA3D.File.CompressionLibs[ id ] = name;
  1769. SEA3D.File.DecompressionMethod[ id ] = method;
  1770. };
  1771. SEA3D.File.prototype.addClass = function ( clazz, unique ) {
  1772. this.typeClass[ clazz.prototype.type ] = clazz;
  1773. this.typeUnique[ clazz.prototype.type ] = unique === true;
  1774. };
  1775. SEA3D.File.prototype.readHead = function () {
  1776. if ( this.stream.bytesAvailable < 16 )
  1777. return false;
  1778. if ( this.stream.readUTF8( 3 ) != "SEA" )
  1779. throw new Error( "Invalid SEA3D format." );
  1780. this.sign = this.stream.readUTF8( 3 );
  1781. this.version = this.stream.readUInt24();
  1782. if ( this.stream.readUByte() != 0 ) {
  1783. throw new Error( "Protection algorithm not compatible." );
  1784. }
  1785. this.compressionID = this.stream.readUByte();
  1786. this.compressionAlgorithm = SEA3D.File.CompressionLibs[ this.compressionID ];
  1787. this.decompressionMethod = SEA3D.File.DecompressionMethod[ this.compressionID ];
  1788. if ( this.compressionID > 0 && ! this.decompressionMethod ) {
  1789. throw new Error( "Compression algorithm not compatible." );
  1790. }
  1791. this.length = this.stream.readUInt();
  1792. this.dataPosition = this.stream.position;
  1793. this.objects.length = 0;
  1794. this.state = this.readBody;
  1795. if ( this.onHead ) {
  1796. this.onHead( {
  1797. file: this,
  1798. sign: this.sign
  1799. } );
  1800. }
  1801. return true;
  1802. };
  1803. SEA3D.File.prototype.getObject = function ( index ) {
  1804. return this.objects[ index ];
  1805. };
  1806. SEA3D.File.prototype.getObjectByName = function ( name ) {
  1807. return this.objects[ name ];
  1808. };
  1809. SEA3D.File.prototype.readSEAObject = function () {
  1810. if ( this.stream.bytesAvailable < 4 )
  1811. return null;
  1812. var size = this.stream.readUInt();
  1813. var position = this.stream.position;
  1814. if ( this.stream.bytesAvailable < size )
  1815. return null;
  1816. var flag = this.stream.readUByte();
  1817. var type = this.stream.readExt();
  1818. var meta = null;
  1819. var name = flag & 1 ? this.stream.readUTF8Tiny() : "",
  1820. compressed = ( flag & 2 ) != 0,
  1821. streaming = ( flag & 4 ) != 0;
  1822. if ( flag & 8 ) {
  1823. var metalen = this.stream.readUShort();
  1824. var metabytes = this.stream.concat( this.stream.position, metalen );
  1825. this.stream.position += metalen;
  1826. if ( compressed && this.decompressionMethod ) {
  1827. metabytes.buffer = this.decompressionMethod( metabytes.buffer );
  1828. }
  1829. meta = metabytes.readProperties( this );
  1830. }
  1831. size -= this.stream.position - position;
  1832. position = this.stream.position;
  1833. var data = this.stream.concat( position, size ),
  1834. obj;
  1835. if ( this.typeClass[ type ] ) {
  1836. if ( compressed && this.decompressionMethod ) {
  1837. data.buffer = this.decompressionMethod( data.buffer );
  1838. }
  1839. obj = new this.typeClass[ type ]( name, data, this );
  1840. if ( ( this.config.streaming && streaming || this.config.forceStreaming ) && this.typeRead[ type ] ) {
  1841. this.typeRead[ type ].call( this.scope, obj );
  1842. }
  1843. } else {
  1844. obj = new SEA3D.Object( name, data, type, this );
  1845. console.warn( "SEA3D: Unknown format \"" + type + "\" of file \"" + name + "\". Add a module referring for this format." );
  1846. }
  1847. obj.streaming = streaming;
  1848. obj.metadata = meta;
  1849. this.objects.push( this.objects[ obj.name + "." + obj.type ] = obj );
  1850. this.dataPosition = position + size;
  1851. ++ this.position;
  1852. return obj;
  1853. };
  1854. SEA3D.File.prototype.isDone = function () {
  1855. return this.position == this.length;
  1856. };
  1857. SEA3D.File.prototype.readBody = function () {
  1858. this.timer.update();
  1859. if ( ! this.resume ) return false;
  1860. while ( this.position < this.length ) {
  1861. if ( this.timer.deltaTime < this.config.timeLimit ) {
  1862. this.stream.position = this.dataPosition;
  1863. var sea = this.readSEAObject();
  1864. if ( sea ) this.dispatchCompleteObject( sea );
  1865. else return false;
  1866. } else return false;
  1867. }
  1868. this.state = this.readComplete;
  1869. return true;
  1870. };
  1871. SEA3D.File.prototype.initParse = function () {
  1872. this.timer = new SEA3D.Timer();
  1873. this.position = 0;
  1874. this.resume = true;
  1875. };
  1876. SEA3D.File.prototype.parse = function () {
  1877. this.initParse();
  1878. if ( isFinite( this.config.timeLimit ) ) setTimeout( this.parseObject.bind( this ), 10 );
  1879. else this.parseObject();
  1880. };
  1881. SEA3D.File.prototype.parseObject = function () {
  1882. this.timer.update();
  1883. while ( this.position < this.length && this.timer.deltaTime < this.config.timeLimit ) {
  1884. var obj = this.objects[ this.position ++ ],
  1885. type = obj.type;
  1886. if ( ! this.typeUnique[ type ] ) delete obj.tag;
  1887. if ( ( obj.streaming || this.config.forceStreaming ) && this.typeRead[ type ] ) {
  1888. if ( obj.tag == undefined ) {
  1889. this.typeRead[ type ].call( this.scope, obj );
  1890. }
  1891. }
  1892. }
  1893. if ( this.position == this.length ) {
  1894. var elapsedTime = this.timer.elapsedTime;
  1895. var message = elapsedTime + "ms, " + this.objects.length + " objects";
  1896. if ( this.onParseComplete ) {
  1897. this.onParseComplete( {
  1898. file: this,
  1899. timeTotal: elapsedTime,
  1900. message: message
  1901. } );
  1902. } else console.log( "SEA3D Parse Complete:", message );
  1903. } else {
  1904. if ( this.onParseProgress ) {
  1905. this.onParseProgress( {
  1906. file: this,
  1907. loaded: this.position,
  1908. total: this.length
  1909. } );
  1910. }
  1911. setTimeout( this.parseObject.bind( this ), 10 );
  1912. }
  1913. };
  1914. SEA3D.File.prototype.readComplete = function () {
  1915. this.stream.position = this.dataPosition;
  1916. if ( this.stream.readUInt24F() != 0x5EA3D1 )
  1917. console.warn( "SEA3D file is corrupted." );
  1918. delete this.state;
  1919. return false;
  1920. };
  1921. SEA3D.File.prototype.readState = function () {
  1922. while ( this.state() ) continue;
  1923. if ( this.state ) {
  1924. requestAnimationFrame( this.readState.bind( this ) );
  1925. this.dispatchProgress();
  1926. } else {
  1927. this.dispatchComplete();
  1928. }
  1929. };
  1930. SEA3D.File.prototype.read = function ( buffer ) {
  1931. if ( ! buffer ) throw new Error( "No data found." );
  1932. this.initParse();
  1933. this.stream = new SEA3D.Stream( buffer );
  1934. this.state = this.readHead;
  1935. this.readState();
  1936. };
  1937. SEA3D.File.prototype.dispatchCompleteObject = function ( obj ) {
  1938. if ( ! this.onCompleteObject ) return;
  1939. this.onCompleteObject( {
  1940. file: this,
  1941. object: obj
  1942. } );
  1943. };
  1944. SEA3D.File.prototype.dispatchProgress = function () {
  1945. if ( ! this.onProgress ) return;
  1946. this.onProgress( {
  1947. file: this,
  1948. loaded: this.position,
  1949. total: this.length
  1950. } );
  1951. };
  1952. SEA3D.File.prototype.dispatchDownloadProgress = function ( position, length ) {
  1953. if ( ! this.onDownloadProgress ) return;
  1954. this.onDownloadProgress( {
  1955. file: this,
  1956. loaded: position,
  1957. total: length
  1958. } );
  1959. };
  1960. SEA3D.File.prototype.dispatchComplete = function () {
  1961. var elapsedTime = this.timer.elapsedTime;
  1962. var message = elapsedTime + "ms, " + this.objects.length + " objects";
  1963. if ( this.onComplete ) this.onComplete( {
  1964. file: this,
  1965. timeTotal: elapsedTime,
  1966. message: message
  1967. } );
  1968. else console.log( "SEA3D:", message );
  1969. };
  1970. SEA3D.File.prototype.dispatchError = function ( id, message ) {
  1971. if ( this.onError ) this.onError( { file: this, id: id, message: message } );
  1972. else console.error( "SEA3D: #" + id, message );
  1973. };
  1974. SEA3D.File.prototype.load = function ( url ) {
  1975. var file = this,
  1976. xhr = new XMLHttpRequest();
  1977. xhr.open( "GET", url, true );
  1978. xhr.responseType = 'arraybuffer';
  1979. xhr.onprogress = function ( e ) {
  1980. if ( e.lengthComputable ) {
  1981. file.dispatchDownloadProgress( e.loaded, e.total );
  1982. }
  1983. };
  1984. xhr.onreadystatechange = function () {
  1985. if ( xhr.readyState === 2 ) {
  1986. //xhr.getResponseHeader("Content-Length");
  1987. } else if ( xhr.readyState === 3 ) {
  1988. // progress
  1989. } else if ( xhr.readyState === 4 ) {
  1990. if ( xhr.status === 200 || xhr.status === 0 ) {
  1991. // complete
  1992. file.read( this.response );
  1993. } else {
  1994. this.dispatchError( 1001, "Couldn't load [" + url + "] [" + xhr.status + "]" );
  1995. }
  1996. }
  1997. };
  1998. xhr.send();
  1999. };