SEA3D.js 83 KB

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