SEA3D.js 83 KB

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