SEA3DLoader.js 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909
  1. /**
  2. * SEA3D for Three.JS
  3. * @author Sunag / http://www.sunag.com.br/
  4. */
  5. 'use strict';
  6. //
  7. //
  8. // SEA3D
  9. //
  10. THREE.SEA3D = function ( config ) {
  11. this.config = {
  12. id: "",
  13. scripts: true,
  14. runScripts: true,
  15. autoPlay: false,
  16. dummys: true,
  17. multiplier: 1,
  18. bounding: true,
  19. audioRolloffFactor: 10,
  20. lights: true,
  21. useEnvironment: true,
  22. useVertexTexture: true,
  23. forceStatic: false,
  24. streaming: true,
  25. async: true,
  26. paths: {},
  27. timeLimit: 10
  28. };
  29. if ( config ) this.loadConfig( config );
  30. };
  31. //
  32. // Polyfills
  33. //
  34. if ( THREE.Float32BufferAttribute === undefined ) {
  35. THREE.Float32BufferAttribute = THREE.Float32Attribute;
  36. }
  37. THREE.SEA3D.useMultiMaterial = THREE.MultiMaterial.prototype.isMultiMaterial;
  38. //
  39. // Config
  40. //
  41. THREE.SEA3D.MTXBUF = new THREE.Matrix4();
  42. THREE.SEA3D.VECBUF = new THREE.Vector3();
  43. THREE.SEA3D.QUABUF = new THREE.Quaternion();
  44. THREE.SEA3D.BACKGROUND_COLOR = 0x333333;
  45. THREE.SEA3D.HELPER_COLOR = 0x9AB9E5;
  46. THREE.SEA3D.RTT_SIZE = 512;
  47. THREE.SEA3D.identityMatrixScale = function () {
  48. var scl = new THREE.Vector3();
  49. return function identityMatrixScale( matrix ) {
  50. scl.setFromMatrixScale( matrix );
  51. return matrix.scale( scl.set( 1 / scl.x, 1 / scl.y, 1 / scl.z ) );
  52. };
  53. }();
  54. THREE.SEA3D.prototype = Object.assign( Object.create( THREE.EventDispatcher.prototype ), {
  55. constructor: THREE.SEA3D,
  56. setShadowMap: function ( light ) {
  57. light.shadow.mapSize.width = 2048;
  58. light.shadow.mapSize.height = 1024;
  59. light.castShadow = true;
  60. light.shadow.camera.left = - 200;
  61. light.shadow.camera.right = 200;
  62. light.shadow.camera.top = 200;
  63. light.shadow.camera.bottom = - 200;
  64. light.shadow.camera.near = 1;
  65. light.shadow.camera.far = 3000;
  66. light.shadow.camera.fov = 45;
  67. light.shadow.bias = - 0.001;
  68. }
  69. } );
  70. Object.defineProperties( THREE.SEA3D.prototype, {
  71. container: {
  72. set: function ( val ) {
  73. this.config.container = val;
  74. },
  75. get: function () {
  76. return this.config.container;
  77. }
  78. },
  79. elapsedTime: {
  80. get: function () {
  81. return this.file.timer.elapsedTime;
  82. }
  83. }
  84. } );
  85. //
  86. // Domain
  87. //
  88. THREE.SEA3D.Domain = function ( id, objects, container ) {
  89. this.id = id;
  90. this.objects = objects;
  91. this.container = container;
  92. this.sources = [];
  93. this.local = {};
  94. this.scriptTargets = [];
  95. this.events = new THREE.EventDispatcher();
  96. };
  97. THREE.SEA3D.Domain.global = {};
  98. THREE.SEA3D.Domain.prototype = Object.assign( Object.create( THREE.EventDispatcher.prototype ), {
  99. constructor: THREE.SEA3D.Domain,
  100. add: function ( src ) {
  101. this.sources.push( src );
  102. },
  103. remove: function ( src ) {
  104. this.sources.splice( this.sources.indexOf( src ), 1 );
  105. },
  106. contains: function ( src ) {
  107. return this.sources.indexOf( src ) != - 1;
  108. },
  109. addEventListener: function ( type, listener ) {
  110. this.events.addEventListener( type, listener );
  111. },
  112. hasEventListener: function ( type, listener ) {
  113. return this.events.hasEventListener( type, listener );
  114. },
  115. removeEventListener: function ( type, listener ) {
  116. this.events.removeEventListener( type, listener );
  117. },
  118. print: function () {
  119. console.log.apply( console, arguments );
  120. },
  121. watch: function () {
  122. console.log.apply( console, 'watch:', arguments );
  123. },
  124. runScripts: function () {
  125. for ( var i = 0; i < this.scriptTargets.length; i ++ ) {
  126. this.runJSMList( this.scriptTargets[ i ] );
  127. }
  128. },
  129. runJSMList: function ( target ) {
  130. var scripts = target.scripts;
  131. for ( var i = 0; i < scripts.length; i ++ ) {
  132. this.runJSM( target, scripts[ i ] );
  133. }
  134. return scripts;
  135. },
  136. runJSM: function ( target, script ) {
  137. var include = {
  138. print: this.print,
  139. watch: this.watch,
  140. sea3d: this,
  141. scene: this.container,
  142. source: new THREE.SEA3D.ScriptDomain( this, target instanceof THREE.SEA3D.Domain )
  143. };
  144. Object.freeze( include.source );
  145. THREE.SEA3D.ScriptHandler.add( include.source );
  146. try {
  147. this.methods[ script.method ](
  148. include,
  149. this.getReference,
  150. THREE.SEA3D.Domain.global,
  151. this.local,
  152. target,
  153. script.params
  154. );
  155. } catch ( e ) {
  156. console.error( 'SEA3D JavaScript: Error running method "' + script.method + '".' );
  157. console.error( e );
  158. }
  159. },
  160. getReference: function ( ns ) {
  161. return eval( ns );
  162. },
  163. disposeList: function ( list ) {
  164. if ( ! list || ! list.length ) return;
  165. list = list.concat();
  166. var i = list.length;
  167. while ( i -- ) {
  168. list[ i ].dispose();
  169. }
  170. },
  171. dispatchEvent: function ( event ) {
  172. event.domain = this;
  173. var sources = this.sources.concat(),
  174. i = sources.length;
  175. while ( i -- ) {
  176. sources[ i ].dispatchEvent( event );
  177. }
  178. this.events.dispatchEvent( event );
  179. },
  180. dispose: function () {
  181. this.disposeList( this.sources );
  182. while ( this.container.children.length ) {
  183. this.container.remove( this.container.children[ 0 ] );
  184. }
  185. var i = THREE.SEA3D.EXTENSIONS_DOMAIN.length;
  186. while ( i -- ) {
  187. var domain = THREE.SEA3D.EXTENSIONS_DOMAIN[ i ];
  188. if ( domain.dispose ) domain.dispose.call( this );
  189. }
  190. this.disposeList( this.materials );
  191. this.disposeList( this.dummys );
  192. this.dispatchEvent( { type: "dispose" } );
  193. }
  194. } );
  195. //
  196. // Domain Manager
  197. //
  198. THREE.SEA3D.DomainManager = function ( autoDisposeRootDomain ) {
  199. this.domains = [];
  200. this.autoDisposeRootDomain = autoDisposeRootDomain !== undefined ? autoDisposeRootDomain : true;
  201. };
  202. Object.assign( THREE.SEA3D.DomainManager.prototype, {
  203. onDisposeDomain: function ( e ) {
  204. this.remove( e.domain );
  205. if ( this.autoDisposeRootDomain && this.domains.length == 1 ) {
  206. this.dispose();
  207. }
  208. },
  209. add: function ( domain ) {
  210. this._onDisposeDomain = this._onDisposeDomain || this.onDisposeDomain.bind( this );
  211. domain.on( "dispose", this._onDisposeDomain );
  212. this.domains.push( domain );
  213. this.textures = this.textures || domain.textures;
  214. this.cubemaps = this.cubemaps || domain.cubemaps;
  215. this.geometries = this.geometries || domain.geometries;
  216. },
  217. remove: function ( domain ) {
  218. domain.removeEvent( "dispose", this._onDisposeDomain );
  219. this.domains.splice( this.domains.indexOf( domain ), 1 );
  220. },
  221. contains: function ( domain ) {
  222. return this.domains.indexOf( domain ) != - 1;
  223. },
  224. disposeList: function ( list ) {
  225. if ( ! list || ! list.length ) return;
  226. list = list.concat();
  227. var i = list.length;
  228. while ( i -- ) {
  229. list[ i ].dispose();
  230. }
  231. },
  232. dispose: function () {
  233. this.disposeList( this.domains );
  234. this.disposeList( this.textures );
  235. this.disposeList( this.cubemaps );
  236. this.disposeList( this.geometries );
  237. }
  238. } );
  239. //
  240. // Script ( closure for private functions )
  241. //
  242. THREE.SEA3D.ScriptDomain = function ( domain, root ) {
  243. domain = domain || new THREE.SEA3D.Domain();
  244. domain.add( this );
  245. var events = new THREE.EventDispatcher();
  246. this.getId = function () {
  247. return domain.id;
  248. };
  249. this.isRoot = function () {
  250. return root;
  251. };
  252. this.addEventListener = function ( type, listener ) {
  253. events.addEventListener( type, listener );
  254. };
  255. this.hasEventListener = function ( type, listener ) {
  256. return events.hasEventListener( type, listener );
  257. };
  258. this.removeEventListener = function ( type, listener ) {
  259. events.removeEventListener( type, listener );
  260. };
  261. this.dispatchEvent = function ( event ) {
  262. event.script = this;
  263. events.dispatchEvent( event );
  264. };
  265. this.dispose = function () {
  266. domain.remove( this );
  267. if ( root ) domain.dispose();
  268. this.dispatchEvent( { type: "dispose" } );
  269. };
  270. };
  271. //
  272. // Script Manager ( closure for private functions )
  273. //
  274. THREE.SEA3D.ScriptManager = function () {
  275. this.scripts = [];
  276. var onDisposeScript = ( function ( e ) {
  277. this.remove( e.script );
  278. } ).bind( this );
  279. this.add = function ( src ) {
  280. src.addEventListener( "dispose", onDisposeScript );
  281. this.scripts.push( src );
  282. };
  283. this.remove = function ( src ) {
  284. src.removeEventListener( "dispose", onDisposeScript );
  285. this.scripts.splice( this.scripts.indexOf( src ), 1 );
  286. };
  287. this.contains = function ( src ) {
  288. return this.scripts.indexOf( src ) > - 1;
  289. };
  290. this.dispatchEvent = function ( event ) {
  291. var scripts = this.scripts.concat(),
  292. i = scripts.length;
  293. while ( i -- ) {
  294. scripts[ i ].dispatchEvent( event );
  295. }
  296. };
  297. };
  298. //
  299. // Script Handler
  300. //
  301. THREE.SEA3D.ScriptHandler = new THREE.SEA3D.ScriptManager();
  302. THREE.SEA3D.ScriptHandler.dispatchUpdate = function ( delta ) {
  303. this.dispatchEvent( {
  304. type: "update",
  305. delta: delta
  306. } );
  307. };
  308. //
  309. // Animation Clip
  310. //
  311. THREE.SEA3D.AnimationClip = function ( name, duration, tracks, repeat ) {
  312. THREE.AnimationClip.call( this, name, duration, tracks );
  313. this.repeat = repeat !== undefined ? repeat : true;
  314. };
  315. THREE.SEA3D.AnimationClip.fromClip = function ( clip, repeat ) {
  316. return new THREE.SEA3D.AnimationClip( clip.name, clip.duration, clip.tracks, repeat );
  317. };
  318. THREE.SEA3D.AnimationClip.prototype = Object.assign( Object.create( THREE.AnimationClip.prototype ), {
  319. constructor: THREE.SEA3D.AnimationClip
  320. } );
  321. //
  322. // Animation
  323. //
  324. THREE.SEA3D.Animation = function ( clip, timeScale ) {
  325. this.clip = clip;
  326. this.timeScale = timeScale !== undefined ? timeScale : 1;
  327. };
  328. THREE.SEA3D.Animation.COMPLETE = "animationComplete";
  329. THREE.SEA3D.Animation.prototype = Object.assign( Object.create( THREE.EventDispatcher.prototype ), {
  330. constructor: THREE.SEA3D.Animation,
  331. onComplete: function ( scope ) {
  332. this.dispatchEvent( { type: THREE.SEA3D.Animation.COMPLETE, target: this } );
  333. }
  334. } );
  335. Object.defineProperties( THREE.SEA3D.Animation.prototype, {
  336. name: {
  337. get: function () {
  338. return this.clip.name;
  339. }
  340. },
  341. repeat: {
  342. get: function () {
  343. return this.clip.repeat;
  344. }
  345. },
  346. duration: {
  347. get: function () {
  348. return this.clip.duration;
  349. }
  350. },
  351. mixer: {
  352. set: function ( val ) {
  353. if ( this.mx ) {
  354. this.mx.uncacheClip( this.clip );
  355. delete this.mx;
  356. }
  357. if ( val ) {
  358. this.mx = val;
  359. this.mx.clipAction( this.clip );
  360. }
  361. },
  362. get: function () {
  363. return this.mx;
  364. }
  365. }
  366. } );
  367. //
  368. // Animator
  369. //
  370. THREE.SEA3D.Animator = function ( clips, mixer ) {
  371. this.updateAnimations( clips, mixer );
  372. this.clone = function ( scope ) {
  373. return new this.constructor( this.clips, new THREE.AnimationMixer( scope ) ).copyFrom( this );
  374. }.bind( this );
  375. };
  376. Object.assign( THREE.SEA3D.Animator.prototype, {
  377. update: function ( dt ) {
  378. this.mixer.update( dt || 0 );
  379. if ( this.currentAnimationAction && this.currentAnimationAction.paused ) {
  380. this.pause();
  381. if ( this.currentAnimation ) {
  382. this.currentAnimation.onComplete( this );
  383. }
  384. }
  385. return this;
  386. },
  387. updateAnimations: function ( clips, mixer ) {
  388. if ( this.playing ) this.stop();
  389. if ( this.mixer ) THREE.SEA3D.AnimationHandler.remove( this );
  390. this.mixer = mixer;
  391. this.relative = false;
  392. this.playing = false;
  393. this.paused = false;
  394. this.timeScale = 1;
  395. this.animations = [];
  396. this.animation = {};
  397. this.clips = [];
  398. if ( clips ) {
  399. for ( var i = 0; i < clips.length; i ++ ) {
  400. this.addAnimation( clips[ i ] );
  401. }
  402. }
  403. return this;
  404. },
  405. addAnimation: function ( animation ) {
  406. if ( animation instanceof THREE.AnimationClip ) {
  407. this.clips.push( animation );
  408. animation = new THREE.SEA3D.Animation( animation );
  409. }
  410. this.animations.push( animation );
  411. this.animation[ animation.name ] = animation;
  412. animation.mixer = this.mixer;
  413. return animation;
  414. },
  415. removeAnimation: function ( animation ) {
  416. if ( animation instanceof THREE.AnimationClip ) {
  417. animation = this.getAnimationByClip( animation );
  418. }
  419. this.clips.splice( this.clips.indexOf( animation.clip ), 1 );
  420. delete this.animation[ animation.name ];
  421. this.animations.splice( this.animations.indexOf( animation ), 1 );
  422. animation.mixer = null;
  423. return animation;
  424. },
  425. getAnimationByClip: function ( clip ) {
  426. for ( var i = 0; i < this.animations.length; i ++ ) {
  427. if ( this.animations[ i ].clip === clip ) return clip;
  428. }
  429. },
  430. getAnimationByName: function ( name ) {
  431. return typeof name === "number" ? this.animations[ name ] : this.animation[ name ];
  432. },
  433. setAnimationWeight: function ( name, val ) {
  434. this.mixer.clipAction( this.getAnimationByName( name ).clip ).setEffectiveWeight( val );
  435. },
  436. getAnimationWeight: function ( name ) {
  437. return this.mixer.clipAction( this.getAnimationByName( name ).clip ).getEffectiveWeight();
  438. },
  439. pause: function () {
  440. if ( this.playing && this.currentAnimation ) {
  441. THREE.SEA3D.AnimationHandler.remove( this );
  442. this.playing = false;
  443. }
  444. return this;
  445. },
  446. resume: function () {
  447. if ( ! this.playing && this.currentAnimation ) {
  448. THREE.SEA3D.AnimationHandler.add( this );
  449. this.playing = true;
  450. }
  451. return this;
  452. },
  453. setTimeScale: function ( val ) {
  454. this.timeScale = val;
  455. if ( this.currentAnimationAction ) this.updateTimeScale();
  456. return this;
  457. },
  458. getTimeScale: function () {
  459. return this.timeScale;
  460. },
  461. updateTimeScale: function () {
  462. this.currentAnimationAction.setEffectiveTimeScale( this.timeScale * ( this.currentAnimation ? this.currentAnimation.timeScale : 1 ) );
  463. return this;
  464. },
  465. play: function ( name, crossfade, offset, weight ) {
  466. var animation = this.getAnimationByName( name );
  467. if ( ! animation ) throw new Error( 'Animation "' + name + '" not found.' );
  468. if ( animation == this.currentAnimation ) {
  469. if ( offset !== undefined || ! animation.repeat ) this.currentAnimationAction.time = offset !== undefined ? offset :
  470. ( this.currentAnimationAction.timeScale >= 0 ? 0 : this.currentAnimation.duration );
  471. this.currentAnimationAction.setEffectiveWeight( weight !== undefined ? weight : 1 );
  472. this.currentAnimationAction.paused = false;
  473. return this.resume();
  474. } else {
  475. this.previousAnimation = this.currentAnimation;
  476. this.currentAnimation = animation;
  477. this.previousAnimationAction = this.currentAnimationAction;
  478. this.currentAnimationAction = this.mixer.clipAction( animation.clip ).setLoop( animation.repeat ? THREE.LoopRepeat : THREE.LoopOnce, Infinity ).reset();
  479. this.currentAnimationAction.clampWhenFinished = ! animation.repeat;
  480. this.currentAnimationAction.paused = false;
  481. this.updateTimeScale();
  482. if ( offset !== undefined || ! animation.repeat ) this.currentAnimationAction.time = offset !== undefined ? offset :
  483. ( this.currentAnimationAction.timeScale >= 0 ? 0 : this.currentAnimation.duration );
  484. this.currentAnimationAction.setEffectiveWeight( weight !== undefined ? weight : 1 );
  485. this.currentAnimationAction.play();
  486. if ( ! this.playing ) this.mixer.update( 0 );
  487. this.playing = true;
  488. if ( this.previousAnimation ) this.previousAnimationAction.crossFadeTo( this.currentAnimationAction, crossfade || 0, false );
  489. THREE.SEA3D.AnimationHandler.add( this );
  490. }
  491. return this;
  492. },
  493. stop: function () {
  494. if ( this.playing ) THREE.SEA3D.AnimationHandler.remove( this );
  495. if ( this.currentAnimation ) {
  496. this.currentAnimationAction.stop();
  497. this.previousAnimation = this.currentAnimation;
  498. this.previousAnimationAction = this.currentAnimationAction;
  499. delete this.currentAnimationAction;
  500. delete this.currentAnimation;
  501. this.playing = false;
  502. }
  503. return this;
  504. },
  505. playw: function ( name, weight ) {
  506. if ( ! this.playing && ! this.paused ) THREE.SEA3D.AnimationHandler.add( this );
  507. var animation = this.getAnimationByName( name );
  508. this.playing = true;
  509. var clip = this.mixer.clipAction( animation.clip );
  510. clip.setLoop( animation.repeat ? THREE.LoopRepeat : THREE.LoopOnce, Infinity ).reset();
  511. clip.clampWhenFinished = ! animation.repeat;
  512. clip.paused = false;
  513. clip.setEffectiveWeight( weight ).play();
  514. return clip;
  515. },
  516. crossFade: function ( fromAnimName, toAnimName, duration, wrap ) {
  517. this.mixer.stopAllAction();
  518. var fromAction = this.playw( fromAnimName, 1 );
  519. var toAction = this.playw( toAnimName, 1 );
  520. fromAction.crossFadeTo( toAction, duration, wrap !== undefined ? wrap : false );
  521. return this;
  522. },
  523. stopAll: function () {
  524. this.stop().mixer.stopAllAction();
  525. this.playing = false;
  526. return this;
  527. },
  528. unPauseAll: function () {
  529. this.mixer.timeScale = 1;
  530. this.playing = true;
  531. this.paused = false;
  532. return this;
  533. },
  534. pauseAll: function () {
  535. this.mixer.timeScale = 0;
  536. this.playing = false;
  537. this.paused = true;
  538. return this;
  539. },
  540. setRelative: function ( val ) {
  541. if ( this.relative == val ) return;
  542. this.stop();
  543. this.relative = val;
  544. return this;
  545. },
  546. getRelative: function () {
  547. return this.relative;
  548. },
  549. copyFrom: function ( scope ) {
  550. for ( var i = 0; i < this.animations.length; i ++ ) {
  551. this.animations[ i ].timeScale = scope.animations[ i ].timeScale;
  552. }
  553. return this;
  554. }
  555. } );
  556. //
  557. // Object3D Animator
  558. //
  559. THREE.SEA3D.Object3DAnimator = function ( clips, object3d ) {
  560. this.object3d = object3d;
  561. THREE.SEA3D.Animator.call( this, clips, new THREE.AnimationMixer( object3d ) );
  562. this.clone = function ( scope ) {
  563. return new this.constructor( this.clips, scope ).copyFrom( this );
  564. }.bind( this );
  565. };
  566. THREE.SEA3D.Object3DAnimator.prototype = Object.assign( Object.create( THREE.SEA3D.Animator.prototype ), {
  567. constructor: THREE.SEA3D.Object3DAnimator,
  568. stop: function () {
  569. if ( this.currentAnimation ) {
  570. var animate = this.object3d.animate;
  571. if ( animate && this instanceof THREE.SEA3D.Object3DAnimator ) {
  572. animate.position.set( 0, 0, 0 );
  573. animate.quaternion.set( 0, 0, 0, 1 );
  574. animate.scale.set( 1, 1, 1 );
  575. }
  576. }
  577. THREE.SEA3D.Animator.prototype.stop.call( this );
  578. },
  579. setRelative: function ( val ) {
  580. THREE.SEA3D.Animator.prototype.setRelative.call( this, val );
  581. this.object3d.setAnimator( this.relative );
  582. this.updateAnimations( this.clips, new THREE.AnimationMixer( this.relative ? this.object3d.animate : this.object3d ) );
  583. }
  584. } );
  585. //
  586. // Camera Animator
  587. //
  588. THREE.SEA3D.CameraAnimator = function ( clips, object3d ) {
  589. THREE.SEA3D.Object3DAnimator.call( this, clips, object3d );
  590. };
  591. THREE.SEA3D.CameraAnimator.prototype = Object.assign( Object.create( THREE.SEA3D.Object3DAnimator.prototype ), {
  592. constructor: THREE.SEA3D.CameraAnimator
  593. } );
  594. //
  595. // Sound Animator
  596. //
  597. THREE.SEA3D.SoundAnimator = function ( clips, object3d ) {
  598. THREE.SEA3D.Object3DAnimator.call( this, clips, object3d );
  599. };
  600. THREE.SEA3D.SoundAnimator.prototype = Object.assign( Object.create( THREE.SEA3D.Object3DAnimator.prototype ), {
  601. constructor: THREE.SEA3D.SoundAnimator
  602. } );
  603. //
  604. // Light Animator
  605. //
  606. THREE.SEA3D.LightAnimator = function ( clips, object3d ) {
  607. THREE.SEA3D.Object3DAnimator.call( this, clips, object3d );
  608. };
  609. THREE.SEA3D.LightAnimator.prototype = Object.assign( Object.create( THREE.SEA3D.Object3DAnimator.prototype ), {
  610. constructor: THREE.SEA3D.LightAnimator
  611. } );
  612. //
  613. // Container
  614. //
  615. THREE.SEA3D.Object3D = function ( ) {
  616. THREE.Object3D.call( this );
  617. };
  618. THREE.SEA3D.Object3D.prototype = Object.assign( Object.create( THREE.Object3D.prototype ), {
  619. constructor: THREE.SEA3D.Object3D,
  620. // Relative Animation Extension ( Only used if relative animation is enabled )
  621. // TODO: It can be done with shader
  622. updateAnimateMatrix: function ( force ) {
  623. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  624. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  625. if ( this.parent === null ) {
  626. this.matrixWorld.copy( this.matrix );
  627. } else {
  628. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  629. }
  630. this.animate.updateMatrix();
  631. this.matrixWorld.multiplyMatrices( this.matrixWorld, this.animate.matrix );
  632. this.matrixWorldNeedsUpdate = false;
  633. force = true;
  634. }
  635. // update children
  636. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  637. this.children[ i ].updateMatrixWorld( force );
  638. }
  639. },
  640. setAnimator: function ( val ) {
  641. if ( this.getAnimator() == val )
  642. return;
  643. if ( val ) {
  644. this.animate = new THREE.Object3D();
  645. this.updateMatrixWorld = THREE.SEA3D.Object3D.prototype.updateAnimateMatrix;
  646. } else {
  647. delete this.animate;
  648. this.updateMatrixWorld = THREE.Object3D.prototype.updateMatrixWorld;
  649. }
  650. this.matrixWorldNeedsUpdate = true;
  651. },
  652. getAnimator: function () {
  653. return this.animate != undefined;
  654. },
  655. copy: function ( source ) {
  656. THREE.Object3D.prototype.copy.call( this, source );
  657. this.attribs = source.attribs;
  658. this.scripts = source.scripts;
  659. if ( source.animator ) this.animator = source.animator.clone( this );
  660. return this;
  661. }
  662. } );
  663. //
  664. // Dummy
  665. //
  666. THREE.SEA3D.Dummy = function ( width, height, depth ) {
  667. this.width = width != undefined ? width : 100;
  668. this.height = height != undefined ? height : 100;
  669. this.depth = depth != undefined ? depth : 100;
  670. var geo = new THREE.BoxBufferGeometry( this.width, this.height, this.depth, 1, 1, 1 );
  671. geo.computeBoundingBox();
  672. geo.computeBoundingSphere();
  673. THREE.Mesh.call( this, geo, THREE.SEA3D.Dummy.MATERIAL );
  674. };
  675. THREE.SEA3D.Dummy.MATERIAL = new THREE.MeshBasicMaterial( { wireframe: true, color: THREE.SEA3D.HELPER_COLOR } );
  676. THREE.SEA3D.Dummy.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), THREE.SEA3D.Object3D.prototype, {
  677. constructor: THREE.SEA3D.Dummy,
  678. copy: function ( source ) {
  679. THREE.Mesh.prototype.copy.call( this, source );
  680. this.attribs = source.attribs;
  681. this.scripts = source.scripts;
  682. if ( source.animator ) this.animator = source.animator.clone( this );
  683. return this;
  684. },
  685. dispose: function () {
  686. this.geometry.dispose();
  687. }
  688. } );
  689. //
  690. // Mesh
  691. //
  692. THREE.SEA3D.Mesh = function ( geometry, material ) {
  693. THREE.Mesh.call( this, geometry, material );
  694. };
  695. THREE.SEA3D.Mesh.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), THREE.SEA3D.Object3D.prototype, {
  696. constructor: THREE.SEA3D.Mesh,
  697. setWeight: function ( name, val ) {
  698. var index = typeof name === "number" ? name : this.morphTargetDictionary[ name ];
  699. this.morphTargetInfluences[ index ] = val;
  700. },
  701. getWeight: function ( name ) {
  702. var index = typeof name === "number" ? name : this.morphTargetDictionary[ name ];
  703. return this.morphTargetInfluences[ index ];
  704. },
  705. copy: function ( source ) {
  706. THREE.Mesh.prototype.copy.call( this, source );
  707. this.attribs = source.attribs;
  708. this.scripts = source.scripts;
  709. if ( source.animator ) this.animator = source.animator.clone( this );
  710. return this;
  711. }
  712. } );
  713. //
  714. // Skinning
  715. //
  716. THREE.SEA3D.SkinnedMesh = function ( geometry, material ) {
  717. THREE.SkinnedMesh.call( this, geometry, material );
  718. this.bind( new THREE.Skeleton( this.initBones() ), this.matrixWorld );
  719. this.updateAnimations( geometry.animations, new THREE.AnimationMixer( this ) );
  720. };
  721. THREE.SEA3D.SkinnedMesh.prototype = Object.assign( Object.create( THREE.SkinnedMesh.prototype ), THREE.SEA3D.Mesh.prototype, THREE.SEA3D.Animator.prototype, {
  722. constructor: THREE.SEA3D.SkinnedMesh,
  723. initBones: function () {
  724. var bones = [], bone, gbone;
  725. var i, il;
  726. if ( this.geometry && this.geometry.bones !== undefined ) {
  727. // first, create array of 'Bone' objects from geometry data
  728. for ( i = 0, il = this.geometry.bones.length; i < il; i ++ ) {
  729. gbone = this.geometry.bones[ i ];
  730. // create new 'Bone' object
  731. bone = new THREE.Bone();
  732. bones.push( bone );
  733. // apply values
  734. bone.name = gbone.name;
  735. bone.position.fromArray( gbone.pos );
  736. bone.quaternion.fromArray( gbone.rotq );
  737. if ( gbone.scl !== undefined ) bone.scale.fromArray( gbone.scl );
  738. }
  739. // second, create bone hierarchy
  740. for ( i = 0, il = this.geometry.bones.length; i < il; i ++ ) {
  741. gbone = this.geometry.bones[ i ];
  742. if ( ( gbone.parent !== - 1 ) && ( gbone.parent !== null ) && ( bones[ gbone.parent ] !== undefined ) ) {
  743. // subsequent bones in the hierarchy
  744. bones[ gbone.parent ].add( bones[ i ] );
  745. } else {
  746. // topmost bone, immediate child of the skinned mesh
  747. this.add( bones[ i ] );
  748. }
  749. }
  750. }
  751. // now the bones are part of the scene graph and children of the skinned mesh.
  752. // let's update the corresponding matrices
  753. this.updateMatrixWorld( true );
  754. return bones;
  755. },
  756. boneByName: function ( name ) {
  757. var bones = this.skeleton.bones;
  758. for ( var i = 0, bl = bones.length; i < bl; i ++ ) {
  759. if ( name == bones[ i ].name )
  760. return bones[ i ];
  761. }
  762. },
  763. copy: function ( source ) {
  764. THREE.SkinnedMesh.prototype.copy.call( this, source );
  765. this.attribs = source.attribs;
  766. this.scripts = source.scripts;
  767. if ( source.animator ) this.animator = source.animator.clone( this );
  768. return this;
  769. }
  770. } );
  771. //
  772. // Vertex Animation
  773. //
  774. THREE.SEA3D.VertexAnimationMesh = function ( geometry, material ) {
  775. THREE.Mesh.call( this, geometry, material );
  776. this.type = 'MorphAnimMesh';
  777. this.updateAnimations( geometry.animations, new THREE.AnimationMixer( this ) );
  778. };
  779. THREE.SEA3D.VertexAnimationMesh.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), THREE.SEA3D.Mesh.prototype, THREE.SEA3D.Animator.prototype, {
  780. constructor: THREE.SEA3D.VertexAnimationMesh,
  781. copy: function ( source ) {
  782. THREE.Mesh.prototype.copy.call( this, source );
  783. this.attribs = source.attribs;
  784. this.scripts = source.scripts;
  785. if ( source.animator ) this.animator = source.animator.clone( this );
  786. return this;
  787. }
  788. } );
  789. //
  790. // Camera
  791. //
  792. THREE.SEA3D.Camera = function ( fov, aspect, near, far ) {
  793. THREE.PerspectiveCamera.call( this, fov, aspect, near, far );
  794. };
  795. THREE.SEA3D.Camera.prototype = Object.assign( Object.create( THREE.PerspectiveCamera.prototype ), THREE.SEA3D.Object3D.prototype, {
  796. constructor: THREE.SEA3D.Camera,
  797. copy: function ( source ) {
  798. THREE.PerspectiveCamera.prototype.copy.call( this, source );
  799. this.attribs = source.attribs;
  800. this.scripts = source.scripts;
  801. if ( source.animator ) this.animator = source.animator.clone( this );
  802. return this;
  803. }
  804. } );
  805. //
  806. // Orthographic Camera
  807. //
  808. THREE.SEA3D.OrthographicCamera = function ( left, right, top, bottom, near, far ) {
  809. THREE.OrthographicCamera.call( this, left, right, top, bottom, near, far );
  810. };
  811. THREE.SEA3D.OrthographicCamera.prototype = Object.assign( Object.create( THREE.OrthographicCamera.prototype ), THREE.SEA3D.Object3D.prototype, {
  812. constructor: THREE.SEA3D.OrthographicCamera,
  813. copy: function ( source ) {
  814. THREE.OrthographicCamera.prototype.copy.call( this, source );
  815. this.attribs = source.attribs;
  816. this.scripts = source.scripts;
  817. if ( source.animator ) this.animator = source.animator.clone( this );
  818. return this;
  819. }
  820. } );
  821. //
  822. // PointLight
  823. //
  824. THREE.SEA3D.PointLight = function ( hex, intensity, distance, decay ) {
  825. THREE.PointLight.call( this, hex, intensity, distance, decay );
  826. };
  827. THREE.SEA3D.PointLight.prototype = Object.assign( Object.create( THREE.PointLight.prototype ), THREE.SEA3D.Object3D.prototype, {
  828. constructor: THREE.SEA3D.PointLight,
  829. copy: function ( source ) {
  830. THREE.PointLight.prototype.copy.call( this, source );
  831. this.attribs = source.attribs;
  832. this.scripts = source.scripts;
  833. if ( source.animator ) this.animator = source.animator.clone( this );
  834. return this;
  835. }
  836. } );
  837. //
  838. // Point Sound
  839. //
  840. THREE.SEA3D.PointSound = function ( listener, sound ) {
  841. THREE.PositionalAudio.call( this, listener );
  842. this.setSound( sound );
  843. };
  844. THREE.SEA3D.PointSound.prototype = Object.assign( Object.create( THREE.PositionalAudio.prototype ), THREE.SEA3D.Object3D.prototype, {
  845. constructor: THREE.SEA3D.PointSound,
  846. setSound: function ( sound ) {
  847. this.sound = sound;
  848. if ( sound ) {
  849. if ( sound.buffer ) {
  850. this.setBuffer( sound.buffer );
  851. } else {
  852. sound.addEventListener( "complete", function ( e ) {
  853. this.setBuffer( sound.buffer );
  854. }.bind( this ) );
  855. }
  856. }
  857. return this;
  858. },
  859. copy: function ( source ) {
  860. THREE.PositionalAudio.prototype.copy.call( this, source );
  861. this.attribs = source.attribs;
  862. this.scripts = source.scripts;
  863. if ( source.animator ) this.animator = source.animator.clone( this );
  864. return this;
  865. }
  866. } );
  867. //
  868. // Animation Handler
  869. //
  870. THREE.SEA3D.AnimationHandler = {
  871. animators: [],
  872. update: function ( dt ) {
  873. var i = 0;
  874. while ( i < this.animators.length ) {
  875. this.animators[ i ++ ].update( dt );
  876. }
  877. },
  878. add: function ( animator ) {
  879. var index = this.animators.indexOf( animator );
  880. if ( index === - 1 ) this.animators.push( animator );
  881. },
  882. remove: function ( animator ) {
  883. var index = this.animators.indexOf( animator );
  884. if ( index !== - 1 ) this.animators.splice( index, 1 );
  885. }
  886. };
  887. //
  888. // Sound
  889. //
  890. THREE.SEA3D.Sound = function ( src ) {
  891. this.uuid = THREE.Math.generateUUID();
  892. this.src = src;
  893. new THREE.AudioLoader().load( src, function ( buffer ) {
  894. this.buffer = buffer;
  895. this.dispatchEvent( { type: "complete" } );
  896. }.bind( this ) );
  897. };
  898. THREE.SEA3D.Sound.prototype = Object.assign( Object.create( THREE.EventDispatcher.prototype ), {
  899. constructor: THREE.SEA3D.Sound
  900. } );
  901. //
  902. // Output
  903. //
  904. THREE.SEA3D.Domain.prototype.getMesh = THREE.SEA3D.prototype.getMesh = function ( name ) {
  905. return this.objects[ "m3d/" + name ];
  906. };
  907. THREE.SEA3D.Domain.prototype.getDummy = THREE.SEA3D.prototype.getDummy = function ( name ) {
  908. return this.objects[ "dmy/" + name ];
  909. };
  910. THREE.SEA3D.Domain.prototype.getLine = THREE.SEA3D.prototype.getLine = function ( name ) {
  911. return this.objects[ "line/" + name ];
  912. };
  913. THREE.SEA3D.Domain.prototype.getSound3D = THREE.SEA3D.prototype.getSound3D = function ( name ) {
  914. return this.objects[ "sn3d/" + name ];
  915. };
  916. THREE.SEA3D.Domain.prototype.getMaterial = THREE.SEA3D.prototype.getMaterial = function ( name ) {
  917. return this.objects[ "mat/" + name ];
  918. };
  919. THREE.SEA3D.Domain.prototype.getLight = THREE.SEA3D.prototype.getLight = function ( name ) {
  920. return this.objects[ "lht/" + name ];
  921. };
  922. THREE.SEA3D.Domain.prototype.getGLSL = THREE.SEA3D.prototype.getGLSL = function ( name ) {
  923. return this.objects[ "glsl/" + name ];
  924. };
  925. THREE.SEA3D.Domain.prototype.getCamera = THREE.SEA3D.prototype.getCamera = function ( name ) {
  926. return this.objects[ "cam/" + name ];
  927. };
  928. THREE.SEA3D.Domain.prototype.getTexture = THREE.SEA3D.prototype.getTexture = function ( name ) {
  929. return this.objects[ "tex/" + name ];
  930. };
  931. THREE.SEA3D.Domain.prototype.getCubeMap = THREE.SEA3D.prototype.getCubeMap = function ( name ) {
  932. return this.objects[ "cmap/" + name ];
  933. };
  934. THREE.SEA3D.Domain.prototype.getJointObject = THREE.SEA3D.prototype.getJointObject = function ( name ) {
  935. return this.objects[ "jnt/" + name ];
  936. };
  937. THREE.SEA3D.Domain.prototype.getContainer3D = THREE.SEA3D.prototype.getContainer3D = function ( name ) {
  938. return this.objects[ "c3d/" + name ];
  939. };
  940. THREE.SEA3D.Domain.prototype.getSprite = THREE.SEA3D.prototype.getSprite = function ( name ) {
  941. return this.objects[ "m2d/" + name ];
  942. };
  943. THREE.SEA3D.Domain.prototype.getProperties = THREE.SEA3D.prototype.getProperties = function ( name ) {
  944. return this.objects[ "prop/" + name ];
  945. };
  946. //
  947. // Utils
  948. //
  949. THREE.SEA3D.prototype.isPowerOfTwo = function ( num ) {
  950. return num ? ( ( num & - num ) == num ) : false;
  951. };
  952. THREE.SEA3D.prototype.nearestPowerOfTwo = function ( num ) {
  953. return Math.pow( 2, Math.round( Math.log( num ) / Math.LN2 ) );
  954. };
  955. THREE.SEA3D.prototype.updateTransform = function ( obj3d, sea ) {
  956. var mtx = THREE.SEA3D.MTXBUF, vec = THREE.SEA3D.VECBUF;
  957. if ( sea.transform ) mtx.fromArray( sea.transform );
  958. else mtx.makeTranslation( sea.position.x, sea.position.y, sea.position.z );
  959. // matrix
  960. obj3d.position.setFromMatrixPosition( mtx );
  961. obj3d.scale.setFromMatrixScale( mtx );
  962. // ignore rotation scale
  963. obj3d.quaternion.setFromRotationMatrix( THREE.SEA3D.identityMatrixScale( mtx ) );
  964. // optimize if is static
  965. if ( this.config.forceStatic || sea.isStatic ) {
  966. obj3d.updateMatrix();
  967. obj3d.matrixAutoUpdate = false;
  968. }
  969. };
  970. THREE.SEA3D.prototype.toVector3 = function ( data ) {
  971. return new THREE.Vector3( data.x, data.y, data.z );
  972. };
  973. THREE.SEA3D.prototype.toFaces = function ( faces ) {
  974. // xyz(- / +) to xyz(+ / -) sequence
  975. var f = [];
  976. f[ 0 ] = faces[ 1 ];
  977. f[ 1 ] = faces[ 0 ];
  978. f[ 2 ] = faces[ 3 ];
  979. f[ 3 ] = faces[ 2 ];
  980. f[ 4 ] = faces[ 5 ];
  981. f[ 5 ] = faces[ 4 ];
  982. return f;
  983. };
  984. THREE.SEA3D.prototype.updateScene = function () {
  985. if ( this.materials != undefined ) {
  986. for ( var i = 0, l = this.materials.length; i < l; ++ i ) {
  987. this.materials[ i ].needsUpdate = true;
  988. }
  989. }
  990. };
  991. THREE.SEA3D.prototype.addSceneObject = function ( sea, obj3d ) {
  992. obj3d = obj3d || sea.tag;
  993. obj3d.visible = sea.visible;
  994. if ( sea.parent ) sea.parent.tag.add( obj3d );
  995. else if ( this.config.container ) this.config.container.add( obj3d );
  996. if ( sea.attributes ) obj3d.attribs = sea.attributes.tag;
  997. if ( sea.scripts ) {
  998. obj3d.scripts = this.getJSMList( obj3d, sea.scripts );
  999. if ( this.config.scripts && this.config.runScripts ) this.domain.runJSMList( obj3d );
  1000. }
  1001. };
  1002. THREE.SEA3D.prototype.createObjectURL = function ( raw, mime ) {
  1003. return ( window.URL || window.webkitURL ).createObjectURL( new Blob( [ raw ], { type: mime } ) );
  1004. };
  1005. THREE.SEA3D.prototype.parsePath = function ( url ) {
  1006. var paths = this.config.paths;
  1007. for ( var name in paths ) {
  1008. url = url.replace( new RegExp( "%" + name + "%", "g" ), paths[ name ] );
  1009. }
  1010. return url;
  1011. };
  1012. THREE.SEA3D.prototype.addDefaultAnimation = function ( sea, animatorClass ) {
  1013. var scope = sea.tag;
  1014. for ( var i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i ++ ) {
  1015. var anm = sea.animations[ i ];
  1016. switch ( anm.tag.type ) {
  1017. case SEA3D.Animation.prototype.type:
  1018. var animation = anm.tag.tag || this.getModifier( {
  1019. sea: anm.tag,
  1020. scope: scope,
  1021. relative: anm.relative
  1022. } );
  1023. scope.animator = new animatorClass( animation, scope );
  1024. scope.animator.setRelative( anm.relative );
  1025. if ( this.config.autoPlay ) {
  1026. scope.animator.play( 0 );
  1027. }
  1028. return scope.animator;
  1029. break;
  1030. }
  1031. }
  1032. };
  1033. //
  1034. // Geometry
  1035. //
  1036. THREE.SEA3D.prototype.readGeometryBuffer = function ( sea ) {
  1037. var geo = sea.tag || new THREE.BufferGeometry();
  1038. for ( var i = 0; i < sea.groups.length; i ++ ) {
  1039. var g = sea.groups[ i ];
  1040. geo.addGroup( g.start, g.count, i );
  1041. }
  1042. // not indexes? use polygon soup
  1043. if ( sea.indexes ) geo.setIndex( new THREE.BufferAttribute( sea.indexes, 1 ) );
  1044. geo.addAttribute( 'position', new THREE.BufferAttribute( sea.vertex, 3 ) );
  1045. if ( sea.uv ) {
  1046. geo.addAttribute( 'uv', new THREE.BufferAttribute( sea.uv[ 0 ], 2 ) );
  1047. if ( sea.uv.length > 1 ) geo.addAttribute( 'uv2', new THREE.BufferAttribute( sea.uv[ 1 ], 2 ) );
  1048. }
  1049. if ( sea.normal ) geo.addAttribute( 'normal', new THREE.BufferAttribute( sea.normal, 3 ) );
  1050. else geo.computeVertexNormals();
  1051. if ( sea.tangent4 ) geo.addAttribute( 'tangent', new THREE.BufferAttribute( sea.tangent4, 4 ) );
  1052. if ( sea.color ) geo.addAttribute( 'color', new THREE.BufferAttribute( sea.color[ 0 ], sea.numColor ) );
  1053. if ( sea.joint ) {
  1054. geo.addAttribute( 'skinIndex', new THREE.Float32BufferAttribute( sea.joint, sea.jointPerVertex ) );
  1055. geo.addAttribute( 'skinWeight', new THREE.Float32BufferAttribute( sea.weight, sea.jointPerVertex ) );
  1056. }
  1057. if ( this.config.bounding ) {
  1058. geo.computeBoundingBox();
  1059. geo.computeBoundingSphere();
  1060. }
  1061. geo.name = sea.name;
  1062. this.domain.geometries = this.geometries = this.geometries || [];
  1063. this.geometries.push( this.objects[ "geo/" + sea.name ] = sea.tag = geo );
  1064. };
  1065. //
  1066. // Dummy
  1067. //
  1068. THREE.SEA3D.prototype.readDummy = function ( sea ) {
  1069. var dummy = new THREE.SEA3D.Dummy( sea.width, sea.height, sea.depth );
  1070. dummy.name = sea.name;
  1071. this.domain.dummys = this.dummys = this.dummys || [];
  1072. this.dummys.push( this.objects[ "dmy/" + sea.name ] = sea.tag = dummy );
  1073. this.addSceneObject( sea );
  1074. this.updateTransform( dummy, sea );
  1075. this.addDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1076. };
  1077. //
  1078. // Line
  1079. //
  1080. THREE.SEA3D.prototype.readLine = function ( sea ) {
  1081. var geo = new THREE.BufferGeometry();
  1082. if ( sea.closed )
  1083. sea.vertex.push( sea.vertex[ 0 ], sea.vertex[ 1 ], sea.vertex[ 2 ] );
  1084. geo.addAttribute( 'position', new THREE.Float32BufferAttribute( sea.vertex, 3 ) );
  1085. var line = new THREE.Line( geo, new THREE.LineBasicMaterial( { color: THREE.SEA3D.HELPER_COLOR, linewidth: 3 } ) );
  1086. line.name = sea.name;
  1087. this.lines = this.lines || [];
  1088. this.lines.push( this.objects[ "line/" + sea.name ] = sea.tag = line );
  1089. this.addSceneObject( sea );
  1090. this.updateTransform( line, sea );
  1091. this.addDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1092. };
  1093. //
  1094. // Container3D
  1095. //
  1096. THREE.SEA3D.prototype.readContainer3D = function ( sea ) {
  1097. var container = new THREE.SEA3D.Object3D();
  1098. this.domain.containers = this.containers = this.containers || [];
  1099. this.containers.push( this.objects[ "c3d/" + sea.name ] = sea.tag = container );
  1100. this.addSceneObject( sea );
  1101. this.updateTransform( container, sea );
  1102. this.addDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1103. };
  1104. //
  1105. // Sprite
  1106. //
  1107. THREE.SEA3D.prototype.readSprite = function ( sea ) {
  1108. var mat;
  1109. if ( sea.material ) {
  1110. if ( ! sea.material.tag.sprite ) {
  1111. mat = sea.material.tag.sprite = new THREE.SpriteMaterial();
  1112. this.setBlending( mat, sea.blendMode );
  1113. var map = sea.material.tag.map;
  1114. if ( map ) {
  1115. map.flipY = true;
  1116. mat.map = map;
  1117. }
  1118. mat.color.set( sea.material.tag.color );
  1119. mat.opacity = sea.material.tag.opacity;
  1120. mat.fog = sea.material.receiveFog;
  1121. } else {
  1122. mat = sea.material.tag.sprite;
  1123. }
  1124. }
  1125. var sprite = new THREE.Sprite( mat );
  1126. sprite.name = sea.name;
  1127. this.domain.sprites = this.sprites = this.sprites || [];
  1128. this.sprites.push( this.objects[ "m2d/" + sea.name ] = sea.tag = sprite );
  1129. this.addSceneObject( sea );
  1130. this.updateTransform( sprite, sea );
  1131. sprite.scale.set( sea.width, sea.height, 1 );
  1132. };
  1133. //
  1134. // Mesh
  1135. //
  1136. THREE.SEA3D.prototype.readMesh = function ( sea ) {
  1137. var i, count, geo = sea.geometry.tag, mesh, mat, skeleton, morpher, skeletonAnimation, vertexAnimation, uvwAnimationClips, morphAnimation;
  1138. for ( i = 0, count = sea.modifiers ? sea.modifiers.length : 0; i < count; i ++ ) {
  1139. var mod = sea.modifiers[ i ];
  1140. switch ( mod.type ) {
  1141. case SEA3D.Skeleton.prototype.type:
  1142. case SEA3D.SkeletonLocal.prototype.type:
  1143. skeleton = mod;
  1144. geo.bones = skeleton.tag;
  1145. break;
  1146. case SEA3D.Morph.prototype.type:
  1147. morpher = mod.tag || this.getModifier( {
  1148. sea: mod,
  1149. geometry: sea.geometry
  1150. } );
  1151. geo.morphAttributes = morpher.attribs;
  1152. geo.morphTargets = morpher.targets;
  1153. break;
  1154. }
  1155. }
  1156. for ( i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i ++ ) {
  1157. var anm = sea.animations[ i ],
  1158. anmTag = anm.tag;
  1159. switch ( anmTag.type ) {
  1160. case SEA3D.SkeletonAnimation.prototype.type:
  1161. skeletonAnimation = anmTag;
  1162. geo.animations = skeletonAnimation.tag || this.getModifier( {
  1163. sea: skeletonAnimation,
  1164. skeleton: skeleton,
  1165. relative: true
  1166. } );
  1167. break;
  1168. case SEA3D.VertexAnimation.prototype.type:
  1169. vertexAnimation = anmTag;
  1170. geo.morphAttributes = vertexAnimation.tag.attribs;
  1171. geo.morphTargets = vertexAnimation.tag.targets;
  1172. geo.animations = vertexAnimation.tag.animations;
  1173. break;
  1174. case SEA3D.UVWAnimation.prototype.type:
  1175. uvwAnimationClips = anmTag.tag || this.getModifier( {
  1176. sea: anmTag
  1177. } );
  1178. break;
  1179. case SEA3D.MorphAnimation.prototype.type:
  1180. morphAnimation = anmTag.tag || this.getModifier( {
  1181. sea: anmTag
  1182. } );
  1183. break;
  1184. }
  1185. }
  1186. var uMorph = morpher != undefined || vertexAnimation != undefined,
  1187. uMorphNormal =
  1188. ( morpher && morpher.attribs.normal != undefined ) ||
  1189. ( vertexAnimation && vertexAnimation.tag.attribs.normal != undefined );
  1190. if ( sea.material ) {
  1191. if ( sea.material.length > 1 ) {
  1192. var mats = [];
  1193. for ( i = 0; i < sea.material.length; i ++ ) {
  1194. mats[ i ] = sea.material[ i ].tag;
  1195. mats[ i ].skinning = skeleton != undefined;
  1196. mats[ i ].morphTargets = uMorph;
  1197. mats[ i ].morphNormals = uMorphNormal;
  1198. mats[ i ].vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  1199. }
  1200. mat = THREE.SEA3D.useMultiMaterial ? new THREE.MultiMaterial( mats ) : mats;
  1201. } else {
  1202. mat = sea.material[ 0 ].tag;
  1203. mat.skinning = skeleton != undefined;
  1204. mat.morphTargets = uMorph;
  1205. mat.morphNormals = uMorphNormal;
  1206. mat.vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  1207. }
  1208. }
  1209. if ( skeleton ) {
  1210. mesh = new THREE.SEA3D.SkinnedMesh( geo, mat, this.config.useVertexTexture );
  1211. if ( this.config.autoPlay && skeletonAnimation ) {
  1212. mesh.play( 0 );
  1213. }
  1214. } else if ( vertexAnimation ) {
  1215. mesh = new THREE.SEA3D.VertexAnimationMesh( geo, mat );
  1216. if ( this.config.autoPlay ) {
  1217. mesh.play( 0 );
  1218. }
  1219. } else {
  1220. mesh = new THREE.SEA3D.Mesh( geo, mat );
  1221. }
  1222. if ( uvwAnimationClips ) {
  1223. mesh.uvwAnimator = new THREE.SEA3D.Animator( uvwAnimationClips, new THREE.AnimationMixer( mat.map ) );
  1224. if ( this.config.autoPlay ) {
  1225. mesh.uvwAnimator.play( 0 );
  1226. }
  1227. }
  1228. if ( morphAnimation ) {
  1229. mesh.morphAnimator = new THREE.SEA3D.Animator( morphAnimation, new THREE.AnimationMixer( mesh ) );
  1230. if ( this.config.autoPlay ) {
  1231. mesh.morphAnimator.play( 0 );
  1232. }
  1233. }
  1234. mesh.name = sea.name;
  1235. mesh.castShadow = sea.castShadows;
  1236. mesh.receiveShadow = sea.material ? sea.material[ 0 ].receiveShadows : true;
  1237. this.domain.meshes = this.meshes = this.meshes || [];
  1238. this.meshes.push( this.objects[ "m3d/" + sea.name ] = sea.tag = mesh );
  1239. this.addSceneObject( sea );
  1240. this.updateTransform( mesh, sea );
  1241. this.addDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1242. };
  1243. //
  1244. // Sound Point
  1245. //
  1246. THREE.SEA3D.prototype.readSoundPoint = function ( sea ) {
  1247. if ( ! this.audioListener ) {
  1248. this.audioListener = new THREE.AudioListener();
  1249. if ( this.config.container ) {
  1250. this.config.container.add( this.audioListener );
  1251. }
  1252. }
  1253. var sound3d = new THREE.SEA3D.PointSound( this.audioListener );
  1254. sound3d.autoplay = sea.autoPlay;
  1255. sound3d.setLoop( sea.autoPlay );
  1256. sound3d.setVolume( sea.volume );
  1257. sound3d.setRefDistance( sea.distance );
  1258. sound3d.setRolloffFactor( this.config.audioRolloffFactor );
  1259. sound3d.setSound( sea.sound.tag );
  1260. sound3d.name = sea.name;
  1261. this.domain.sounds3d = this.sounds3d = this.sounds3d || [];
  1262. this.sounds3d.push( this.objects[ "sn3d/" + sea.name ] = sea.tag = sound3d );
  1263. this.addSceneObject( sea );
  1264. this.updateTransform( sound3d, sea );
  1265. this.addDefaultAnimation( sea, THREE.SEA3D.SoundAnimator );
  1266. };
  1267. //
  1268. // Cube Render
  1269. //
  1270. THREE.SEA3D.prototype.readCubeRender = function ( sea ) {
  1271. var cube = new THREE.CubeCamera( 0.1, 5000, THREE.SEA3D.RTT_SIZE );
  1272. cube.renderTarget.cubeCamera = cube;
  1273. sea.tag = cube.renderTarget;
  1274. this.domain.cubeRenderers = this.cubeRenderers = this.cubeRenderers || [];
  1275. this.cubeRenderers.push( this.objects[ "rttc/" + sea.name ] = cube );
  1276. this.addSceneObject( sea, cube );
  1277. this.updateTransform( cube, sea );
  1278. };
  1279. //
  1280. // Texture (WDP, JPEG, PNG and GIF)
  1281. //
  1282. THREE.SEA3D.prototype.readTexture = function ( sea ) {
  1283. var image = new Image(),
  1284. texture = new THREE.Texture();
  1285. texture.name = sea.name;
  1286. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1287. texture.flipY = false;
  1288. texture.image = image;
  1289. if ( this.config.anisotropy !== undefined ) texture.anisotropy = this.config.anisotropy;
  1290. image.onload = function () {
  1291. texture.needsUpdate = true;
  1292. };
  1293. image.src = this.createObjectURL( sea.data.buffer, "image/" + sea.type );
  1294. this.domain.textures = this.textures = this.textures || [];
  1295. this.textures.push( this.objects[ "tex/" + sea.name ] = sea.tag = texture );
  1296. };
  1297. //
  1298. // Cube Map
  1299. //
  1300. THREE.SEA3D.prototype.readCubeMap = function ( sea ) {
  1301. var faces = this.toFaces( sea.faces ), texture = new THREE.CubeTexture( [] );
  1302. var loaded = 0;
  1303. texture.name = sea.name;
  1304. texture.flipY = false;
  1305. texture.format = THREE.RGBFormat;
  1306. var onLoaded = function () {
  1307. if ( ++ loaded == 6 ) {
  1308. texture.needsUpdate = true;
  1309. if ( ! this.config.async ) this.file.resume = true;
  1310. }
  1311. }.bind( this );
  1312. for ( var i = 0; i < faces.length; ++ i ) {
  1313. var cubeImage = new Image();
  1314. cubeImage.onload = onLoaded;
  1315. cubeImage.src = this.createObjectURL( faces[ i ].buffer, "image/" + sea.extension );
  1316. texture.images[ i ] = cubeImage;
  1317. }
  1318. if ( ! this.config.async ) this.file.resume = false;
  1319. this.domain.cubemaps = this.cubemaps = this.cubemaps || [];
  1320. this.cubemaps.push( this.objects[ "cmap/" + sea.name ] = sea.tag = texture );
  1321. };
  1322. //
  1323. // Updaters
  1324. //
  1325. THREE.SEA3D.prototype.readTextureUpdate = function ( sea ) {
  1326. var obj = this.file.objects[ sea.index ],
  1327. tex = obj.tag;
  1328. var image = new Image();
  1329. image.onload = function () {
  1330. tex.image = image;
  1331. tex.needsUpdate = true;
  1332. };
  1333. image.src = this.createObjectURL( sea.bytes.buffer, "image/" + obj.type );
  1334. };
  1335. //
  1336. // Sound (MP3, OGG)
  1337. //
  1338. THREE.SEA3D.prototype.readSound = function ( sea ) {
  1339. var sound = new THREE.SEA3D.Sound( this.createObjectURL( sea.data.buffer, "audio/" + sea.type ) );
  1340. sound.name = sea.name;
  1341. this.domain.sounds = this.sounds = this.sounds || [];
  1342. this.sounds.push( this.objects[ "snd/" + sea.name ] = sea.tag = sound );
  1343. };
  1344. //
  1345. // Script URL
  1346. //
  1347. THREE.SEA3D.prototype.readScriptURL = function ( sea ) {
  1348. this.file.resume = false;
  1349. var loader = new THREE.FileLoader();
  1350. loader.setResponseType( "text" ).load( sea.url, function ( src ) {
  1351. this.file.resume = true;
  1352. this.domain.scripts = this.scripts = this.scripts || [];
  1353. this.scripts.push( this.objects[ "src/" + sea.name ] = sea.tag = src );
  1354. }.bind( this ) );
  1355. };
  1356. //
  1357. // Texture URL
  1358. //
  1359. THREE.SEA3D.prototype.readTextureURL = function ( sea ) {
  1360. var texture = new THREE.TextureLoader().load( this.parsePath( sea.url ) );
  1361. texture.name = sea.name;
  1362. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1363. texture.flipY = false;
  1364. if ( this.config.anisotropy !== undefined ) texture.anisotropy = this.config.anisotropy;
  1365. this.domain.textures = this.textures = this.textures || [];
  1366. this.textures.push( this.objects[ "tex/" + sea.name ] = sea.tag = texture );
  1367. };
  1368. //
  1369. // CubeMap URL
  1370. //
  1371. THREE.SEA3D.prototype.readCubeMapURL = function ( sea ) {
  1372. var faces = this.toFaces( sea.faces );
  1373. for ( var i = 0; i < faces.length; i ++ ) {
  1374. faces[ i ] = this.parsePath( faces[ i ] );
  1375. }
  1376. var texture, format = faces[ 0 ].substr( - 3 );
  1377. if ( format == "hdr" ) {
  1378. var usePMREM = THREE.PMREMGenerator != null;
  1379. this.file.resume = ! usePMREM;
  1380. texture = new THREE.HDRCubeTextureLoader()
  1381. .setType( THREE.UnsignedByteType )
  1382. .load( faces, function ( texture ) {
  1383. if ( usePMREM ) {
  1384. var pmremGenerator = new THREE.PMREMGenerator( texture );
  1385. pmremGenerator.update( this.config.renderer );
  1386. var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
  1387. pmremCubeUVPacker.update( this.config.renderer );
  1388. this.objects[ "cmap/" + sea.name ] = sea.tag = pmremCubeUVPacker.CubeUVRenderTarget.texture;
  1389. this.file.resume = true;
  1390. texture.dispose();
  1391. pmremGenerator.dispose();
  1392. pmremCubeUVPacker.dispose();
  1393. }
  1394. }.bind( this ) );
  1395. } else {
  1396. texture = new THREE.CubeTextureLoader().load( faces );
  1397. }
  1398. texture.name = sea.name;
  1399. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1400. texture.flipY = false;
  1401. if ( this.config.anisotropy !== undefined ) texture.anisotropy = this.config.anisotropy;
  1402. this.domain.cubemaps = this.cubemaps = this.cubemaps || [];
  1403. this.cubemaps.push( this.objects[ "cmap/" + sea.name ] = sea.tag = texture );
  1404. };
  1405. //
  1406. // Runtime
  1407. //
  1408. THREE.SEA3D.prototype.getJSMList = function ( target, scripts ) {
  1409. var scriptTarget = [];
  1410. for ( var i = 0; i < scripts.length; i ++ ) {
  1411. var script = scripts[ i ];
  1412. if ( script.tag.type == SEA3D.JavaScriptMethod.prototype.type ) {
  1413. scriptTarget.push( script );
  1414. }
  1415. }
  1416. this.domain.scriptTargets = this.scriptTargets = this.scriptTargets || [];
  1417. this.scriptTargets.push( target );
  1418. return scriptTarget;
  1419. };
  1420. THREE.SEA3D.prototype.readJavaScriptMethod = function ( sea ) {
  1421. try {
  1422. var src =
  1423. '(function() {\n' +
  1424. 'var $METHOD = {}\n';
  1425. var declare =
  1426. 'function($INC, $REF, global, local, self, $PARAM) {\n' +
  1427. 'var watch = $INC["watch"],\n' +
  1428. 'scene = $INC["scene"],\n' +
  1429. 'sea3d = $INC["sea3d"],\n' +
  1430. 'print = $INC["print"];\n';
  1431. declare +=
  1432. 'var $SRC = $INC["source"],\n' +
  1433. 'addEventListener = $SRC.addEventListener.bind( $SRC ),\n' +
  1434. 'hasEventListener = $SRC.hasEventListener.bind( $SRC ),\n' +
  1435. 'removeEventListener = $SRC.removeEventListener.bind( $SRC ),\n' +
  1436. 'dispatchEvent = $SRC.dispatchEvent.bind( $SRC ),\n' +
  1437. 'dispose = $SRC.dispose.bind( $SRC );\n';
  1438. for ( var name in sea.methods ) {
  1439. src += '$METHOD["' + name + '"] = ' + declare + sea.methods[ name ].src + '}\n';
  1440. }
  1441. src += 'return $METHOD; })';
  1442. this.domain.methods = eval( src )();
  1443. } catch ( e ) {
  1444. console.error( 'SEA3D JavaScriptMethod: Error running "' + sea.name + '".' );
  1445. console.error( e );
  1446. }
  1447. };
  1448. //
  1449. // GLSL
  1450. //
  1451. THREE.SEA3D.prototype.readGLSL = function ( sea ) {
  1452. this.domain.glsl = this.glsl = this.glsl || [];
  1453. this.glsl.push( this.objects[ "glsl/" + sea.name ] = sea.tag = sea.src );
  1454. };
  1455. //
  1456. // Material
  1457. //
  1458. THREE.SEA3D.prototype.materialTechnique =
  1459. ( function () {
  1460. var techniques = {};
  1461. // FINAL
  1462. techniques.onComplete = function ( mat, sea ) {
  1463. if ( sea.alpha < 1 || mat.blending > THREE.NormalBlending ) {
  1464. mat.opacity = sea.alpha;
  1465. mat.transparent = true;
  1466. }
  1467. };
  1468. // PHYSICAL
  1469. techniques[ SEA3D.Material.PHYSICAL ] =
  1470. function ( mat, tech ) {
  1471. mat.color.setHex( tech.color );
  1472. mat.roughness = tech.roughness;
  1473. mat.metalness = tech.metalness;
  1474. };
  1475. // REFLECTIVITY
  1476. techniques[ SEA3D.Material.REFLECTIVITY ] =
  1477. function ( mat, tech ) {
  1478. mat.reflectivity = tech.strength;
  1479. };
  1480. // CLEAR_COAT
  1481. techniques[ SEA3D.Material.CLEAR_COAT ] =
  1482. function ( mat, tech ) {
  1483. mat.clearCoat = tech.strength;
  1484. mat.clearCoatRoughness = tech.roughness;
  1485. };
  1486. // PHONG
  1487. techniques[ SEA3D.Material.PHONG ] =
  1488. function ( mat, tech ) {
  1489. mat.color.setHex( tech.diffuseColor );
  1490. mat.specular.setHex( tech.specularColor ).multiplyScalar( tech.specular );
  1491. mat.shininess = tech.gloss;
  1492. };
  1493. // DIFFUSE_MAP
  1494. techniques[ SEA3D.Material.DIFFUSE_MAP ] =
  1495. function ( mat, tech, sea ) {
  1496. mat.map = tech.texture.tag;
  1497. mat.color.setHex( 0xFFFFFF );
  1498. mat.map.wrapS = mat.map.wrapT = sea.repeat ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  1499. if ( tech.texture.transparent ) {
  1500. mat.transparent = true;
  1501. }
  1502. };
  1503. // ROUGHNESS_MAP
  1504. techniques[ SEA3D.Material.ROUGHNESS_MAP ] =
  1505. function ( mat, tech ) {
  1506. mat.roughnessMap = tech.texture.tag;
  1507. };
  1508. // METALNESS_MAP
  1509. techniques[ SEA3D.Material.METALNESS_MAP ] =
  1510. function ( mat, tech ) {
  1511. mat.metalnessMap = tech.texture.tag;
  1512. };
  1513. // SPECULAR_MAP
  1514. techniques[ SEA3D.Material.SPECULAR_MAP ] =
  1515. function ( mat, tech ) {
  1516. if ( mat.specular ) {
  1517. mat.specularMap = tech.texture.tag;
  1518. mat.specular.setHex( 0xFFFFFF );
  1519. }
  1520. };
  1521. // NORMAL_MAP
  1522. techniques[ SEA3D.Material.NORMAL_MAP ] =
  1523. function ( mat, tech ) {
  1524. mat.normalMap = tech.texture.tag;
  1525. };
  1526. // REFLECTION
  1527. techniques[ SEA3D.Material.REFLECTION ] =
  1528. techniques[ SEA3D.Material.FRESNEL_REFLECTION ] =
  1529. function ( mat, tech ) {
  1530. mat.envMap = tech.texture.tag;
  1531. mat.envMap.mapping = THREE.CubeReflectionMapping;
  1532. mat.combine = THREE.MixOperation;
  1533. mat.reflectivity = tech.alpha;
  1534. };
  1535. // REFLECTION_SPHERICAL
  1536. techniques[ SEA3D.Material.REFLECTION_SPHERICAL ] =
  1537. function ( mat, tech ) {
  1538. mat.envMap = tech.texture.tag;
  1539. mat.envMap.mapping = THREE.SphericalReflectionMapping;
  1540. mat.combine = THREE.MixOperation;
  1541. mat.reflectivity = tech.alpha;
  1542. };
  1543. // REFRACTION
  1544. techniques[ SEA3D.Material.REFRACTION_MAP ] =
  1545. function ( mat, tech ) {
  1546. mat.envMap = tech.texture.tag;
  1547. mat.envMap.mapping = THREE.CubeRefractionMapping;
  1548. mat.refractionRatio = tech.ior;
  1549. mat.reflectivity = tech.alpha;
  1550. };
  1551. // LIGHT_MAP
  1552. techniques[ SEA3D.Material.LIGHT_MAP ] =
  1553. function ( mat, tech ) {
  1554. if ( tech.blendMode == "multiply" ) mat.aoMap = tech.texture.tag;
  1555. else mat.lightMap = tech.texture.tag;
  1556. };
  1557. // EMISSIVE
  1558. techniques[ SEA3D.Material.EMISSIVE ] =
  1559. function ( mat, tech ) {
  1560. mat.emissive.setHex( tech.color );
  1561. };
  1562. // EMISSIVE_MAP
  1563. techniques[ SEA3D.Material.EMISSIVE_MAP ] =
  1564. function ( mat, tech ) {
  1565. mat.emissiveMap = tech.texture.tag;
  1566. };
  1567. // ALPHA_MAP
  1568. techniques[ SEA3D.Material.ALPHA_MAP ] =
  1569. function ( mat, tech, sea ) {
  1570. mat.alphaMap = tech.texture.tag;
  1571. mat.transparent = true;
  1572. mat.alphaMap.wrapS = mat.alphaMap.wrapT = sea.repeat ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  1573. };
  1574. return techniques;
  1575. } )();
  1576. THREE.SEA3D.prototype.createMaterial = function ( sea ) {
  1577. if ( sea.tecniquesDict[ SEA3D.Material.REFLECTIVITY ] || sea.tecniquesDict[ SEA3D.Material.CLEAR_COAT ] ) {
  1578. return new THREE.MeshPhysicalMaterial();
  1579. } else if ( sea.tecniquesDict[ SEA3D.Material.PHYSICAL ] ) {
  1580. return new THREE.MeshStandardMaterial();
  1581. }
  1582. return new THREE.MeshPhongMaterial();
  1583. };
  1584. THREE.SEA3D.prototype.setBlending = function ( mat, blendMode ) {
  1585. if ( blendMode === "normal" ) return;
  1586. switch ( blendMode ) {
  1587. case "add":
  1588. mat.blending = THREE.AdditiveBlending;
  1589. break;
  1590. case "subtract":
  1591. mat.blending = THREE.SubtractiveBlending;
  1592. break;
  1593. case "multiply":
  1594. mat.blending = THREE.MultiplyBlending;
  1595. break;
  1596. case "screen":
  1597. mat.blending = THREE.CustomBlending;
  1598. mat.blendSrc = THREE.OneFactor;
  1599. mat.blendDst = THREE.OneMinusSrcColorFactor;
  1600. mat.blendEquation = THREE.AddEquation;
  1601. break;
  1602. }
  1603. mat.transparent = true;
  1604. };
  1605. THREE.SEA3D.prototype.readMaterial = function ( sea ) {
  1606. var mat = this.createMaterial( sea );
  1607. mat.name = sea.name;
  1608. mat.lights = sea.receiveLights;
  1609. mat.fog = sea.receiveFog;
  1610. mat.depthWrite = sea.depthWrite;
  1611. mat.depthTest = sea.depthTest;
  1612. mat.premultipliedAlpha = sea.premultipliedAlpha;
  1613. mat.side = sea.doubleSided ? THREE.DoubleSide : THREE.FrontSide;
  1614. this.setBlending( mat, sea.blendMode );
  1615. for ( var i = 0; i < sea.technique.length; i ++ ) {
  1616. var tech = sea.technique[ i ];
  1617. if ( this.materialTechnique[ tech.kind ] ) {
  1618. this.materialTechnique[ tech.kind ].call( this, mat, tech, sea );
  1619. }
  1620. }
  1621. if ( this.materialTechnique.onComplete ) {
  1622. this.materialTechnique.onComplete.call( this, mat, sea );
  1623. }
  1624. this.domain.materials = this.materials = this.materials || [];
  1625. this.materials.push( this.objects[ "mat/" + sea.name ] = sea.tag = mat );
  1626. };
  1627. //
  1628. // Point Light
  1629. //
  1630. THREE.SEA3D.prototype.readPointLight = function ( sea ) {
  1631. var light = new THREE.SEA3D.PointLight( sea.color, sea.multiplier * this.config.multiplier );
  1632. light.name = sea.name;
  1633. if ( sea.attenuation ) {
  1634. light.distance = sea.attenuation.end;
  1635. }
  1636. if ( sea.shadow ) this.setShadowMap( light );
  1637. this.domain.lights = this.lights = this.lights || [];
  1638. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1639. this.addSceneObject( sea );
  1640. this.updateTransform( light, sea );
  1641. this.addDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1642. this.updateScene();
  1643. };
  1644. //
  1645. // Hemisphere Light
  1646. //
  1647. THREE.SEA3D.prototype.readHemisphereLight = function ( sea ) {
  1648. var light = new THREE.HemisphereLight( sea.color, sea.secondColor, sea.multiplier * this.config.multiplier );
  1649. light.position.set( 0, 500, 0 );
  1650. light.name = sea.name;
  1651. this.domain.lights = this.lights = this.lights || [];
  1652. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1653. this.addSceneObject( sea );
  1654. this.addDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1655. this.updateScene();
  1656. };
  1657. //
  1658. // Ambient Light
  1659. //
  1660. THREE.SEA3D.prototype.readAmbientLight = function ( sea ) {
  1661. var light = new THREE.AmbientLight( sea.color, sea.multiplier * this.config.multiplier );
  1662. light.name = sea.name;
  1663. this.domain.lights = this.lights = this.lights || [];
  1664. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1665. this.addSceneObject( sea );
  1666. this.addDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1667. this.updateScene();
  1668. };
  1669. //
  1670. // Directional Light
  1671. //
  1672. THREE.SEA3D.prototype.readDirectionalLight = function ( sea ) {
  1673. var light = new THREE.DirectionalLight( sea.color, sea.multiplier * this.config.multiplier );
  1674. light.name = sea.name;
  1675. if ( sea.shadow ) {
  1676. this.setShadowMap( light );
  1677. }
  1678. this.domain.lights = this.lights = this.lights || [];
  1679. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1680. this.addSceneObject( sea );
  1681. this.updateTransform( light, sea );
  1682. this.addDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1683. this.updateScene();
  1684. };
  1685. //
  1686. // Camera
  1687. //
  1688. THREE.SEA3D.prototype.readCamera = function ( sea ) {
  1689. var camera = new THREE.SEA3D.Camera( sea.fov );
  1690. camera.name = sea.name;
  1691. this.domain.cameras = this.cameras = this.cameras || [];
  1692. this.cameras.push( this.objects[ "cam/" + sea.name ] = sea.tag = camera );
  1693. this.addSceneObject( sea );
  1694. this.updateTransform( camera, sea );
  1695. this.addDefaultAnimation( sea, THREE.SEA3D.CameraAnimator );
  1696. };
  1697. //
  1698. // Orthographic Camera
  1699. //
  1700. THREE.SEA3D.prototype.readOrthographicCamera = function ( sea ) {
  1701. var aspect, width, height;
  1702. var stageWidth = this.config.stageWidth !== undefined ? this.config.stageWidth : ( window ? window.innerWidth : 1024 );
  1703. var stageHeight = this.config.stageHeight !== undefined ? this.config.stageHeight : ( window ? window.innerHeight : 1024 );
  1704. if ( stageWidth > stageHeight ) {
  1705. aspect = stageWidth / stageHeight;
  1706. width = sea.height * aspect;
  1707. height = sea.height;
  1708. } else {
  1709. aspect = stageHeight / stageWidth;
  1710. width = sea.height;
  1711. height = sea.height * aspect;
  1712. }
  1713. var camera = new THREE.SEA3D.OrthographicCamera( - width, width, height, - height );
  1714. camera.name = sea.name;
  1715. this.domain.cameras = this.cameras = this.cameras || [];
  1716. this.cameras.push( this.objects[ "cam/" + sea.name ] = sea.tag = camera );
  1717. this.addSceneObject( sea );
  1718. this.updateTransform( camera, sea );
  1719. this.addDefaultAnimation( sea, THREE.SEA3D.CameraAnimator );
  1720. };
  1721. //
  1722. // Skeleton
  1723. //
  1724. THREE.SEA3D.prototype.getSkeletonFromBones = function ( bonesData ) {
  1725. var bones = [], bone, gbone;
  1726. var i, il;
  1727. for ( i = 0, il = bonesData.length; i < il; i ++ ) {
  1728. gbone = bonesData[ i ];
  1729. bone = new THREE.Bone();
  1730. bones.push( bone );
  1731. bone.name = gbone.name;
  1732. bone.position.fromArray( gbone.pos );
  1733. bone.quaternion.fromArray( gbone.rotq );
  1734. if ( gbone.scl !== undefined ) bone.scale.fromArray( gbone.scl );
  1735. }
  1736. for ( i = 0, il = bonesData.length; i < il; i ++ ) {
  1737. gbone = bonesData[ i ];
  1738. if ( ( gbone.parent !== - 1 ) && ( gbone.parent !== null ) && ( bones[ gbone.parent ] !== undefined ) ) {
  1739. bones[ gbone.parent ].add( bones[ i ] );
  1740. }
  1741. }
  1742. return new THREE.Skeleton( bones );
  1743. };
  1744. THREE.SEA3D.prototype.readSkeletonLocal = function ( sea ) {
  1745. var bones = [];
  1746. for ( var i = 0; i < sea.joint.length; i ++ ) {
  1747. var bone = sea.joint[ i ];
  1748. bones[ i ] = {
  1749. name: bone.name,
  1750. pos: [ bone.x, bone.y, bone.z ],
  1751. rotq: [ bone.qx, bone.qy, bone.qz, bone.qw ],
  1752. parent: bone.parentIndex
  1753. };
  1754. }
  1755. this.domain.bones = this.bones = this.bones || [];
  1756. this.bones.push( this.objects[ sea.name + '.sklq' ] = sea.tag = bones );
  1757. };
  1758. //
  1759. // Joint Object
  1760. //
  1761. THREE.SEA3D.prototype.readJointObject = function ( sea ) {
  1762. var mesh = sea.target.tag,
  1763. bone = mesh.skeleton.bones[ sea.joint ];
  1764. this.domain.joints = this.joints = this.joints || [];
  1765. this.joints.push( this.objects[ "jnt/" + sea.name ] = sea.tag = bone );
  1766. };
  1767. //
  1768. // Morph
  1769. //
  1770. THREE.SEA3D.prototype.readMorph = function ( sea ) {
  1771. var attribs = { position: [] }, targets = [];
  1772. for ( var i = 0; i < sea.node.length; i ++ ) {
  1773. var node = sea.node[ i ];
  1774. attribs.position[ i ] = new THREE.Float32BufferAttribute( node.vertex, 3 );
  1775. attribs.position[ i ].name = node.name;
  1776. if ( node.normal ) {
  1777. attribs.normal = attribs.normal || [];
  1778. attribs.normal[ i ] = new THREE.Float32BufferAttribute( node.normal, 3 );
  1779. }
  1780. targets[ i ] = { name: node.name };
  1781. }
  1782. sea.tag = {
  1783. attribs: attribs,
  1784. targets: targets
  1785. };
  1786. };
  1787. //
  1788. // Animation
  1789. //
  1790. THREE.SEA3D.prototype.readAnimation = function ( sea ) {
  1791. var animations = [], delta = ( 1000 / sea.frameRate ) / 1000;
  1792. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  1793. var seq = sea.sequence[ i ];
  1794. var tracks = [];
  1795. for ( var j = 0; j < sea.dataList.length; j ++ ) {
  1796. var anm = sea.dataList[ j ],
  1797. t, k, times, values,
  1798. data = anm.data,
  1799. start = seq.start * anm.blockSize,
  1800. end = start + ( seq.count * anm.blockSize ),
  1801. intrpl = seq.intrpl ? THREE.InterpolateLinear : false,
  1802. name = null;
  1803. switch ( anm.kind ) {
  1804. case SEA3D.Animation.POSITION:
  1805. name = '.position';
  1806. break;
  1807. case SEA3D.Animation.ROTATION:
  1808. name = '.quaternion';
  1809. break;
  1810. case SEA3D.Animation.SCALE:
  1811. name = '.scale';
  1812. break;
  1813. case SEA3D.Animation.COLOR:
  1814. name = '.color';
  1815. break;
  1816. case SEA3D.Animation.MULTIPLIER:
  1817. name = '.intensity';
  1818. break;
  1819. case SEA3D.Animation.FOV:
  1820. name = '.fov';
  1821. break;
  1822. case SEA3D.Animation.OFFSET_U:
  1823. name = '.offset[x]';
  1824. break;
  1825. case SEA3D.Animation.OFFSET_V:
  1826. name = '.offset[y]';
  1827. break;
  1828. case SEA3D.Animation.SCALE_U:
  1829. name = '.repeat[x]';
  1830. break;
  1831. case SEA3D.Animation.SCALE_V:
  1832. name = '.repeat[y]';
  1833. break;
  1834. case SEA3D.Animation.MORPH:
  1835. name = '.morphTargetInfluences[' + anm.name + ']';
  1836. break;
  1837. }
  1838. if ( ! name ) continue;
  1839. switch ( anm.type ) {
  1840. case SEA3D.Stream.BYTE:
  1841. case SEA3D.Stream.UBYTE:
  1842. case SEA3D.Stream.INT:
  1843. case SEA3D.Stream.UINT:
  1844. case SEA3D.Stream.FLOAT:
  1845. case SEA3D.Stream.DOUBLE:
  1846. case SEA3D.Stream.DECIMAL:
  1847. values = data.subarray( start, end );
  1848. times = new Float32Array( values.length );
  1849. t = 0;
  1850. for ( k = 0; k < times.length; k ++ ) {
  1851. times[ k ] = t;
  1852. t += delta;
  1853. }
  1854. tracks.push( new THREE.NumberKeyframeTrack( name, times, values, intrpl ) );
  1855. break;
  1856. case SEA3D.Stream.VECTOR3D:
  1857. values = data.subarray( start, end );
  1858. times = new Float32Array( values.length / anm.blockSize );
  1859. t = 0;
  1860. for ( k = 0; k < times.length; k ++ ) {
  1861. times[ k ] = t;
  1862. t += delta;
  1863. }
  1864. tracks.push( new THREE.VectorKeyframeTrack( name, times, values, intrpl ) );
  1865. break;
  1866. case SEA3D.Stream.VECTOR4D:
  1867. values = data.subarray( start, end );
  1868. times = new Float32Array( values.length / anm.blockSize );
  1869. t = 0;
  1870. for ( k = 0; k < times.length; k ++ ) {
  1871. times[ k ] = t;
  1872. t += delta;
  1873. }
  1874. tracks.push( new THREE.QuaternionKeyframeTrack( name, times, values, intrpl ) );
  1875. break;
  1876. case SEA3D.Stream.INT24:
  1877. case SEA3D.Stream.UINT24:
  1878. values = new Float32Array( ( end - start ) * 3 );
  1879. times = new Float32Array( values.length / 3 );
  1880. t = 0;
  1881. for ( k = 0; k < times.length; k ++ ) {
  1882. values[ ( k * 3 ) ] = ( ( data[ k ] >> 16 ) & 0xFF ) / 255;
  1883. values[ ( k * 3 ) + 1 ] = ( ( data[ k ] >> 8 ) & 0xFF ) / 255;
  1884. values[ ( k * 3 ) + 2 ] = ( data[ k ] & 0xFF ) / 255;
  1885. times[ k ] = t;
  1886. t += delta;
  1887. }
  1888. tracks.push( new THREE.VectorKeyframeTrack( name, times, values, intrpl ) );//ColorKeyframeTrack
  1889. break;
  1890. }
  1891. }
  1892. animations.push( new THREE.SEA3D.AnimationClip( seq.name, - 1, tracks, seq.repeat ) );
  1893. }
  1894. this.domain.clips = this.clips = this.clips || [];
  1895. this.clips.push( this.objects[ sea.name + '.anm' ] = sea.tag = animations );
  1896. };
  1897. //
  1898. // Skeleton Animation
  1899. //
  1900. THREE.SEA3D.prototype.readSkeletonAnimation = function ( sea, skl ) {
  1901. skl = ! skl && sea.metadata && sea.metadata.skeleton ? sea.metadata.skeleton : skl;
  1902. if ( ! skl || sea.tag ) return sea.tag;
  1903. var animations = [], delta = ( 1000 / sea.frameRate ) / 1000;
  1904. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  1905. var seq = sea.sequence[ i ];
  1906. var start = seq.start;
  1907. var end = start + seq.count;
  1908. var animation = {
  1909. name: seq.name,
  1910. fps: sea.frameRate,
  1911. length: delta * seq.count,
  1912. hierarchy: []
  1913. };
  1914. var numJoints = sea.numJoints,
  1915. raw = sea.raw;
  1916. for ( var j = 0; j < numJoints; j ++ ) {
  1917. var bone = skl.joint[ j ],
  1918. node = { parent: bone.parentIndex, keys: [] },
  1919. keys = node.keys,
  1920. time = 0;
  1921. for ( var frame = start; frame < end; frame ++ ) {
  1922. var idx = ( frame * numJoints * 7 ) + ( j * 7 );
  1923. keys.push( {
  1924. time: time,
  1925. pos: [ raw[ idx ], raw[ idx + 1 ], raw[ idx + 2 ] ],
  1926. rot: [ raw[ idx + 3 ], raw[ idx + 4 ], raw[ idx + 5 ], raw[ idx + 6 ] ],
  1927. scl: [ 1, 1, 1 ]
  1928. } );
  1929. time += delta;
  1930. }
  1931. animation.hierarchy[ j ] = node;
  1932. }
  1933. animations.push( THREE.SEA3D.AnimationClip.fromClip( THREE.AnimationClip.parseAnimation( animation, skl.tag ), seq.repeat ) );
  1934. }
  1935. this.domain.clips = this.clips = this.clips || [];
  1936. this.clips.push( this.objects[ sea.name + '.skla' ] = sea.tag = animations );
  1937. };
  1938. //
  1939. // Vertex Animation
  1940. //
  1941. THREE.SEA3D.prototype.readVertexAnimation = function ( sea ) {
  1942. var attribs = { position: [] }, targets = [], animations = [], i, j, l;
  1943. for ( i = 0, l = sea.frame.length; i < l; i ++ ) {
  1944. var frame = sea.frame[ i ];
  1945. attribs.position[ i ] = new THREE.Float32BufferAttribute( frame.vertex, 3 );
  1946. if ( frame.normal ) {
  1947. attribs.normal = attribs.normal || [];
  1948. attribs.normal[ i ] = new THREE.Float32BufferAttribute( frame.normal, 3 );
  1949. }
  1950. targets[ i ] = { name: i };
  1951. }
  1952. for ( i = 0; i < sea.sequence.length; i ++ ) {
  1953. var seq = sea.sequence[ i ];
  1954. var seqTargets = [];
  1955. for ( j = 0; j < seq.count; j ++ ) {
  1956. seqTargets[ j ] = targets[ seq.start + j ];
  1957. }
  1958. animations.push( THREE.SEA3D.AnimationClip.fromClip( THREE.AnimationClip.CreateFromMorphTargetSequence( seq.name, seqTargets, sea.frameRate ), seq.repeat ) );
  1959. }
  1960. sea.tag = {
  1961. attribs: attribs,
  1962. targets: targets,
  1963. animations: animations
  1964. };
  1965. this.domain.clips = this.clips = this.clips || [];
  1966. this.clips.push( this.objects[ sea.name + '.vtxa' ] = sea.tag );
  1967. };
  1968. //
  1969. // Selector
  1970. //
  1971. THREE.SEA3D.prototype.getModifier = function ( req ) {
  1972. var sea = req.sea;
  1973. switch ( sea.type ) {
  1974. case SEA3D.SkeletonAnimation.prototype.type:
  1975. this.readSkeletonAnimation( sea, req.skeleton );
  1976. break;
  1977. case SEA3D.Animation.prototype.type:
  1978. case SEA3D.MorphAnimation.prototype.type:
  1979. case SEA3D.UVWAnimation.prototype.type:
  1980. this.readAnimation( sea );
  1981. break;
  1982. case SEA3D.Morph.prototype.type:
  1983. this.readMorph( sea, req.geometry );
  1984. break;
  1985. }
  1986. return sea.tag;
  1987. };
  1988. //
  1989. // Actions
  1990. //
  1991. THREE.SEA3D.prototype.applyEnvironment = function ( envMap ) {
  1992. for ( var j = 0, l = this.materials.length; j < l; ++ j ) {
  1993. var mat = this.materials[ j ];
  1994. if ( mat instanceof THREE.MeshStandardMaterial ) {
  1995. if ( mat.envMap ) continue;
  1996. mat.envMap = envMap;
  1997. mat.envMap.mapping = THREE.CubeReflectionMapping;
  1998. mat.needsUpdate = true;
  1999. }
  2000. }
  2001. };
  2002. THREE.SEA3D.prototype.readActions = function ( sea ) {
  2003. for ( var i = 0; i < sea.actions.length; i ++ ) {
  2004. var act = sea.actions[ i ];
  2005. switch ( act.kind ) {
  2006. case SEA3D.Actions.ATTRIBUTES:
  2007. this.attribs = this.domain.attribs = act.attributes.tag;
  2008. break;
  2009. case SEA3D.Actions.SCRIPTS:
  2010. this.domain.scripts = this.getJSMList( this.domain, act.scripts );
  2011. if ( this.config.scripts && this.config.runScripts ) this.domain.runJSMList( this.domain );
  2012. break;
  2013. case SEA3D.Actions.CAMERA:
  2014. this.domain.camera = this.camera = act.camera.tag;
  2015. break;
  2016. case SEA3D.Actions.ENVIRONMENT_COLOR:
  2017. this.domain.background = this.background = this.background || {};
  2018. this.background.color = new THREE.Color( act.color );
  2019. break;
  2020. case SEA3D.Actions.ENVIRONMENT:
  2021. this.domain.background = this.background = this.background || {};
  2022. this.background.texture = act.texture.tag;
  2023. if ( this.config.useEnvironment && this.materials != undefined ) {
  2024. this.applyEnvironment( act.texture.tag );
  2025. }
  2026. break;
  2027. }
  2028. }
  2029. };
  2030. //
  2031. // Properties
  2032. //
  2033. THREE.SEA3D.prototype.updatePropertiesAssets = function ( sea, props ) {
  2034. for ( var name in props ) {
  2035. switch ( props.__type[ name ] ) {
  2036. case SEA3D.Stream.ASSET:
  2037. if ( ! props.__asset ) props.__asset = {};
  2038. if ( ! props.__asset[ name ] ) props.__asset[ name ] = props[ name ];
  2039. props[ name ] = props.__asset[ name ].tag;
  2040. break;
  2041. case SEA3D.Stream.GROUP:
  2042. props[ name ] = this.updatePropertiesAssets( sea, props[ name ] );
  2043. break;
  2044. }
  2045. }
  2046. return props;
  2047. };
  2048. THREE.SEA3D.prototype.readProperties = function ( sea ) {
  2049. var props = this.updatePropertiesAssets( sea, sea.props );
  2050. this.domain.properties = this.properties = this.properties || [];
  2051. this.properties.push( this.objects[ "prop/" + sea.name ] = sea.tag = props );
  2052. };
  2053. THREE.SEA3D.prototype.readFileInfo = function ( sea ) {
  2054. this.domain.info = this.updatePropertiesAssets( sea, sea.info );
  2055. };
  2056. //
  2057. // Events
  2058. //
  2059. THREE.SEA3D.Event = {
  2060. PROGRESS: "sea3d_progress",
  2061. LOAD_PROGRESS: "sea3d_load",
  2062. DOWNLOAD_PROGRESS: "sea3d_download",
  2063. COMPLETE: "sea3d_complete",
  2064. OBJECT_COMPLETE: "sea3d_object",
  2065. PARSE_PROGRESS: "parse_progress",
  2066. PARSE_COMPLETE: "parse_complete",
  2067. ERROR: "sea3d_error"
  2068. };
  2069. THREE.SEA3D.prototype.onProgress = function ( e ) {
  2070. e.status = e.type;
  2071. e.progress = e.loaded / e.total;
  2072. e.type = THREE.SEA3D.Event.PROGRESS;
  2073. this.dispatchEvent( e );
  2074. };
  2075. THREE.SEA3D.prototype.onLoadProgress = function ( e ) {
  2076. e.type = THREE.SEA3D.Event.LOAD_PROGRESS;
  2077. this.dispatchEvent( e );
  2078. this.onProgress( e );
  2079. };
  2080. THREE.SEA3D.prototype.onDownloadProgress = function ( e ) {
  2081. e.type = THREE.SEA3D.Event.DOWNLOAD_PROGRESS;
  2082. this.dispatchEvent( e );
  2083. this.onProgress( e );
  2084. };
  2085. THREE.SEA3D.prototype.onComplete = function ( e ) {
  2086. e.type = THREE.SEA3D.Event.COMPLETE;
  2087. this.dispatchEvent( e );
  2088. };
  2089. THREE.SEA3D.prototype.onCompleteObject = function ( e ) {
  2090. e.type = THREE.SEA3D.Event.OBJECT_COMPLETE;
  2091. this.dispatchEvent( e );
  2092. };
  2093. THREE.SEA3D.prototype.onParseProgress = function ( e ) {
  2094. e.type = THREE.SEA3D.Event.PARSE_PROGRESS;
  2095. this.dispatchEvent( e );
  2096. };
  2097. THREE.SEA3D.prototype.onParseComplete = function ( e ) {
  2098. e.type = THREE.SEA3D.Event.PARSE_COMPLETE;
  2099. this.dispatchEvent( e );
  2100. };
  2101. THREE.SEA3D.prototype.onError = function ( e ) {
  2102. e.type = THREE.SEA3D.Event.ERROR;
  2103. this.dispatchEvent( e );
  2104. };
  2105. //
  2106. // Loader
  2107. //
  2108. THREE.SEA3D.prototype.createDomain = function () {
  2109. return this.domain = new THREE.SEA3D.Domain(
  2110. this.config.id,
  2111. this.objects = {},
  2112. this.config.container
  2113. );
  2114. };
  2115. THREE.SEA3D.prototype.clone = function ( config, onParseComplete, onParseProgress ) {
  2116. if ( ! this.file.isDone() ) throw new Error( "Previous parse is not completed." );
  2117. this.config.container = config && config.container !== undefined ? config.container : new THREE.Object3D();
  2118. if ( config ) this.loadConfig( config );
  2119. var timeLimit = this.config.timeLimit;
  2120. this.config.timeLimit = config && config.timeLimit !== undefined ? config.timeLimit : Infinity;
  2121. this.parse( onParseComplete, onParseProgress );
  2122. this.config.timeLimit = timeLimit;
  2123. return this.domain;
  2124. };
  2125. THREE.SEA3D.prototype.loadConfig = function ( config ) {
  2126. for ( var name in config ) {
  2127. this.config[ name ] = config[ name ];
  2128. }
  2129. };
  2130. THREE.SEA3D.prototype.parse = function ( onParseComplete, onParseProgress ) {
  2131. delete this.cameras;
  2132. delete this.containers;
  2133. delete this.lights;
  2134. delete this.joints;
  2135. delete this.meshes;
  2136. delete this.materials;
  2137. delete this.sprites;
  2138. delete this.sounds3d;
  2139. delete this.cubeRenderers;
  2140. delete this.sounds;
  2141. delete this.glsl;
  2142. delete this.dummy;
  2143. delete this.camera;
  2144. delete this.background;
  2145. delete this.properties;
  2146. delete this.scriptTargets;
  2147. delete this.domain;
  2148. this.createDomain();
  2149. this.setTypeRead();
  2150. this.file.onParseComplete = ( function ( e ) {
  2151. if ( this.config.manager ) this.config.manager.add( this.domain );
  2152. ( onParseComplete || this.onParseComplete ).call( this, e );
  2153. } ).bind( this );
  2154. this.file.onParseProgress = onParseProgress || this.onParseProgress;
  2155. // EXTENSIONS
  2156. var i = THREE.SEA3D.EXTENSIONS_LOADER.length;
  2157. while ( i -- ) {
  2158. var loader = THREE.SEA3D.EXTENSIONS_LOADER[ i ];
  2159. if ( loader.parse ) loader.parse.call( this );
  2160. }
  2161. this.file.parse();
  2162. return this.domain;
  2163. };
  2164. THREE.SEA3D.prototype.onHead = function ( args ) {
  2165. if ( args.sign != 'TJS' ) {
  2166. throw new Error( "Sign '" + args.sign + "' not supported! Use SEA3D Studio to publish or SEA3DLegacy.js" );
  2167. }
  2168. };
  2169. THREE.SEA3D.EXTENSIONS_LOADER = [];
  2170. THREE.SEA3D.EXTENSIONS_DOMAIN = [];
  2171. THREE.SEA3D.prototype.setTypeRead = function () {
  2172. this.file.typeRead = {};
  2173. this.file.typeRead[ SEA3D.Geometry.prototype.type ] = this.readGeometryBuffer;
  2174. this.file.typeRead[ SEA3D.Mesh.prototype.type ] = this.readMesh;
  2175. this.file.typeRead[ SEA3D.Sprite.prototype.type ] = this.readSprite;
  2176. this.file.typeRead[ SEA3D.Container3D.prototype.type ] = this.readContainer3D;
  2177. this.file.typeRead[ SEA3D.Line.prototype.type ] = this.readLine;
  2178. this.file.typeRead[ SEA3D.Material.prototype.type ] = this.readMaterial;
  2179. this.file.typeRead[ SEA3D.Camera.prototype.type ] = this.readCamera;
  2180. this.file.typeRead[ SEA3D.OrthographicCamera.prototype.type ] = this.readOrthographicCamera;
  2181. this.file.typeRead[ SEA3D.SkeletonLocal.prototype.type ] = this.readSkeletonLocal;
  2182. this.file.typeRead[ SEA3D.SkeletonAnimation.prototype.type ] = this.readSkeletonAnimation;
  2183. this.file.typeRead[ SEA3D.JointObject.prototype.type ] = this.readJointObject;
  2184. this.file.typeRead[ SEA3D.CubeMap.prototype.type ] = this.readCubeMap;
  2185. this.file.typeRead[ SEA3D.CubeRender.prototype.type ] = this.readCubeRender;
  2186. this.file.typeRead[ SEA3D.Animation.prototype.type ] =
  2187. this.file.typeRead[ SEA3D.MorphAnimation.prototype.type ] =
  2188. this.file.typeRead[ SEA3D.UVWAnimation.prototype.type ] = this.readAnimation;
  2189. this.file.typeRead[ SEA3D.SoundPoint.prototype.type ] = this.readSoundPoint;
  2190. this.file.typeRead[ SEA3D.TextureURL.prototype.type ] = this.readTextureURL;
  2191. this.file.typeRead[ SEA3D.CubeMapURL.prototype.type ] = this.readCubeMapURL;
  2192. this.file.typeRead[ SEA3D.TextureUpdate.prototype.type ] = this.readTextureUpdate;
  2193. this.file.typeRead[ SEA3D.Morph.prototype.type ] = this.readMorph;
  2194. this.file.typeRead[ SEA3D.VertexAnimation.prototype.type ] = this.readVertexAnimation;
  2195. this.file.typeRead[ SEA3D.Actions.prototype.type ] = this.readActions;
  2196. this.file.typeRead[ SEA3D.FileInfo.prototype.type ] = this.readFileInfo;
  2197. this.file.typeRead[ SEA3D.Properties.prototype.type ] = this.readProperties;
  2198. if ( this.config.dummys ) {
  2199. this.file.typeRead[ SEA3D.Dummy.prototype.type ] = this.readDummy;
  2200. }
  2201. if ( this.config.scripts ) {
  2202. this.file.typeRead[ SEA3D.ScriptURL.prototype.type ] = this.readScriptURL;
  2203. this.file.typeRead[ SEA3D.JavaScriptMethod.prototype.type ] = this.readJavaScriptMethod;
  2204. }
  2205. if ( this.config.lights ) {
  2206. this.file.typeRead[ SEA3D.PointLight.prototype.type ] = this.readPointLight;
  2207. this.file.typeRead[ SEA3D.DirectionalLight.prototype.type ] = this.readDirectionalLight;
  2208. this.file.typeRead[ SEA3D.HemisphereLight.prototype.type ] = this.readHemisphereLight;
  2209. this.file.typeRead[ SEA3D.AmbientLight.prototype.type ] = this.readAmbientLight;
  2210. }
  2211. // UNIVERSAL
  2212. this.file.typeRead[ SEA3D.JPEG.prototype.type ] =
  2213. this.file.typeRead[ SEA3D.JPEG_XR.prototype.type ] =
  2214. this.file.typeRead[ SEA3D.PNG.prototype.type ] =
  2215. this.file.typeRead[ SEA3D.GIF.prototype.type ] = this.readTexture;
  2216. this.file.typeRead[ SEA3D.MP3.prototype.type ] = this.readSound;
  2217. this.file.typeRead[ SEA3D.GLSL.prototype.type ] = this.readGLSL;
  2218. // EXTENSIONS
  2219. var i = THREE.SEA3D.EXTENSIONS_LOADER.length;
  2220. while ( i -- ) {
  2221. var loader = THREE.SEA3D.EXTENSIONS_LOADER[ i ];
  2222. if ( loader.setTypeRead ) loader.setTypeRead.call( this );
  2223. }
  2224. };
  2225. THREE.SEA3D.prototype.load = function ( data ) {
  2226. this.file = new SEA3D.File();
  2227. this.file.scope = this;
  2228. this.file.config = this.config;
  2229. this.file.onProgress = this.onLoadProgress.bind( this );
  2230. this.file.onCompleteObject = this.onCompleteObject.bind( this );
  2231. this.file.onDownloadProgress = this.onDownloadProgress.bind( this );
  2232. this.file.onParseProgress = this.onParseProgress.bind( this );
  2233. this.file.onParseComplete = this.onParseComplete.bind( this );
  2234. this.file.onError = this.onError.bind( this );
  2235. this.file.onHead = this.onHead.bind( this );
  2236. this.file.onComplete = ( function ( e ) {
  2237. if ( this.config.manager ) this.config.manager.add( this.domain );
  2238. this.onComplete.call( this, e );
  2239. } ).bind( this );
  2240. // SEA3D
  2241. this.createDomain();
  2242. this.setTypeRead();
  2243. if ( data === undefined ) return;
  2244. if ( typeof data === "string" ) this.file.load( data );
  2245. else this.file.read( data );
  2246. };