SEA3DLoader.js 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843
  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.BoxGeometry( 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, useVertexTexture ) {
  717. THREE.SkinnedMesh.call( this, geometry, material, useVertexTexture );
  718. this.updateAnimations( geometry.animations, new THREE.AnimationMixer( this ) );
  719. };
  720. THREE.SEA3D.SkinnedMesh.prototype = Object.assign( Object.create( THREE.SkinnedMesh.prototype ), THREE.SEA3D.Mesh.prototype, THREE.SEA3D.Animator.prototype, {
  721. constructor: THREE.SEA3D.SkinnedMesh,
  722. boneByName: function ( name ) {
  723. var bones = this.skeleton.bones;
  724. for ( var i = 0, bl = bones.length; i < bl; i ++ ) {
  725. if ( name == bones[ i ].name )
  726. return bones[ i ];
  727. }
  728. },
  729. copy: function ( source ) {
  730. THREE.SkinnedMesh.prototype.copy.call( this, source );
  731. this.attribs = source.attribs;
  732. this.scripts = source.scripts;
  733. if ( source.animator ) this.animator = source.animator.clone( this );
  734. return this;
  735. }
  736. } );
  737. //
  738. // Vertex Animation
  739. //
  740. THREE.SEA3D.VertexAnimationMesh = function ( geometry, material ) {
  741. THREE.Mesh.call( this, geometry, material );
  742. this.type = 'MorphAnimMesh';
  743. this.updateAnimations( geometry.animations, new THREE.AnimationMixer( this ) );
  744. };
  745. THREE.SEA3D.VertexAnimationMesh.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), THREE.SEA3D.Mesh.prototype, THREE.SEA3D.Animator.prototype, {
  746. constructor: THREE.SEA3D.VertexAnimationMesh,
  747. copy: function ( source ) {
  748. THREE.Mesh.prototype.copy.call( this, source );
  749. this.attribs = source.attribs;
  750. this.scripts = source.scripts;
  751. if ( source.animator ) this.animator = source.animator.clone( this );
  752. return this;
  753. }
  754. } );
  755. //
  756. // Camera
  757. //
  758. THREE.SEA3D.Camera = function ( fov, aspect, near, far ) {
  759. THREE.PerspectiveCamera.call( this, fov, aspect, near, far );
  760. };
  761. THREE.SEA3D.Camera.prototype = Object.assign( Object.create( THREE.PerspectiveCamera.prototype ), THREE.SEA3D.Object3D.prototype, {
  762. constructor: THREE.SEA3D.Camera,
  763. copy: function ( source ) {
  764. THREE.PerspectiveCamera.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. // Orthographic Camera
  773. //
  774. THREE.SEA3D.OrthographicCamera = function ( left, right, top, bottom, near, far ) {
  775. THREE.OrthographicCamera.call( this, left, right, top, bottom, near, far );
  776. };
  777. THREE.SEA3D.OrthographicCamera.prototype = Object.assign( Object.create( THREE.OrthographicCamera.prototype ), THREE.SEA3D.Object3D.prototype, {
  778. constructor: THREE.SEA3D.OrthographicCamera,
  779. copy: function ( source ) {
  780. THREE.OrthographicCamera.prototype.copy.call( this, source );
  781. this.attribs = source.attribs;
  782. this.scripts = source.scripts;
  783. if ( source.animator ) this.animator = source.animator.clone( this );
  784. return this;
  785. }
  786. } );
  787. //
  788. // PointLight
  789. //
  790. THREE.SEA3D.PointLight = function ( hex, intensity, distance, decay ) {
  791. THREE.PointLight.call( this, hex, intensity, distance, decay );
  792. };
  793. THREE.SEA3D.PointLight.prototype = Object.assign( Object.create( THREE.PointLight.prototype ), THREE.SEA3D.Object3D.prototype, {
  794. constructor: THREE.SEA3D.PointLight,
  795. copy: function ( source ) {
  796. THREE.PointLight.prototype.copy.call( this, source );
  797. this.attribs = source.attribs;
  798. this.scripts = source.scripts;
  799. if ( source.animator ) this.animator = source.animator.clone( this );
  800. return this;
  801. }
  802. } );
  803. //
  804. // Point Sound
  805. //
  806. THREE.SEA3D.PointSound = function ( listener, sound ) {
  807. THREE.PositionalAudio.call( this, listener );
  808. this.setSound( sound );
  809. };
  810. THREE.SEA3D.PointSound.prototype = Object.assign( Object.create( THREE.PositionalAudio.prototype ), THREE.SEA3D.Object3D.prototype, {
  811. constructor: THREE.SEA3D.PointSound,
  812. setSound: function ( sound ) {
  813. this.sound = sound;
  814. if ( sound ) {
  815. if ( sound.buffer ) {
  816. this.setBuffer( sound.buffer );
  817. } else {
  818. sound.addEventListener( "complete", function ( e ) {
  819. this.setBuffer( sound.buffer );
  820. }.bind( this ) );
  821. }
  822. }
  823. return this;
  824. },
  825. copy: function ( source ) {
  826. THREE.PositionalAudio.prototype.copy.call( this, source );
  827. this.attribs = source.attribs;
  828. this.scripts = source.scripts;
  829. if ( source.animator ) this.animator = source.animator.clone( this );
  830. return this;
  831. }
  832. } );
  833. //
  834. // Animation Handler
  835. //
  836. THREE.SEA3D.AnimationHandler = {
  837. animators: [],
  838. update: function ( dt ) {
  839. var i = 0;
  840. while ( i < this.animators.length ) {
  841. this.animators[ i ++ ].update( dt );
  842. }
  843. },
  844. add: function ( animator ) {
  845. var index = this.animators.indexOf( animator );
  846. if ( index === - 1 ) this.animators.push( animator );
  847. },
  848. remove: function ( animator ) {
  849. var index = this.animators.indexOf( animator );
  850. if ( index !== - 1 ) this.animators.splice( index, 1 );
  851. }
  852. };
  853. //
  854. // Sound
  855. //
  856. THREE.SEA3D.Sound = function ( src ) {
  857. this.uuid = THREE.Math.generateUUID();
  858. this.src = src;
  859. new THREE.AudioLoader().load( src, function ( buffer ) {
  860. this.buffer = buffer;
  861. this.dispatchEvent( { type: "complete" } );
  862. }.bind( this ) );
  863. };
  864. THREE.SEA3D.Sound.prototype = Object.assign( Object.create( THREE.EventDispatcher.prototype ), {
  865. constructor: THREE.SEA3D.Sound
  866. } );
  867. //
  868. // Output
  869. //
  870. THREE.SEA3D.Domain.prototype.getMesh = THREE.SEA3D.prototype.getMesh = function ( name ) {
  871. return this.objects[ "m3d/" + name ];
  872. };
  873. THREE.SEA3D.Domain.prototype.getDummy = THREE.SEA3D.prototype.getDummy = function ( name ) {
  874. return this.objects[ "dmy/" + name ];
  875. };
  876. THREE.SEA3D.Domain.prototype.getLine = THREE.SEA3D.prototype.getLine = function ( name ) {
  877. return this.objects[ "line/" + name ];
  878. };
  879. THREE.SEA3D.Domain.prototype.getSound3D = THREE.SEA3D.prototype.getSound3D = function ( name ) {
  880. return this.objects[ "sn3d/" + name ];
  881. };
  882. THREE.SEA3D.Domain.prototype.getMaterial = THREE.SEA3D.prototype.getMaterial = function ( name ) {
  883. return this.objects[ "mat/" + name ];
  884. };
  885. THREE.SEA3D.Domain.prototype.getLight = THREE.SEA3D.prototype.getLight = function ( name ) {
  886. return this.objects[ "lht/" + name ];
  887. };
  888. THREE.SEA3D.Domain.prototype.getGLSL = THREE.SEA3D.prototype.getGLSL = function ( name ) {
  889. return this.objects[ "glsl/" + name ];
  890. };
  891. THREE.SEA3D.Domain.prototype.getCamera = THREE.SEA3D.prototype.getCamera = function ( name ) {
  892. return this.objects[ "cam/" + name ];
  893. };
  894. THREE.SEA3D.Domain.prototype.getTexture = THREE.SEA3D.prototype.getTexture = function ( name ) {
  895. return this.objects[ "tex/" + name ];
  896. };
  897. THREE.SEA3D.Domain.prototype.getCubeMap = THREE.SEA3D.prototype.getCubeMap = function ( name ) {
  898. return this.objects[ "cmap/" + name ];
  899. };
  900. THREE.SEA3D.Domain.prototype.getJointObject = THREE.SEA3D.prototype.getJointObject = function ( name ) {
  901. return this.objects[ "jnt/" + name ];
  902. };
  903. THREE.SEA3D.Domain.prototype.getContainer3D = THREE.SEA3D.prototype.getContainer3D = function ( name ) {
  904. return this.objects[ "c3d/" + name ];
  905. };
  906. THREE.SEA3D.Domain.prototype.getSprite = THREE.SEA3D.prototype.getSprite = function ( name ) {
  907. return this.objects[ "m2d/" + name ];
  908. };
  909. THREE.SEA3D.Domain.prototype.getProperties = THREE.SEA3D.prototype.getProperties = function ( name ) {
  910. return this.objects[ "prop/" + name ];
  911. };
  912. //
  913. // Utils
  914. //
  915. THREE.SEA3D.prototype.isPowerOfTwo = function ( num ) {
  916. return num ? ( ( num & - num ) == num ) : false;
  917. };
  918. THREE.SEA3D.prototype.nearestPowerOfTwo = function ( num ) {
  919. return Math.pow( 2, Math.round( Math.log( num ) / Math.LN2 ) );
  920. };
  921. THREE.SEA3D.prototype.updateTransform = function ( obj3d, sea ) {
  922. var mtx = THREE.SEA3D.MTXBUF, vec = THREE.SEA3D.VECBUF;
  923. if ( sea.transform ) mtx.fromArray( sea.transform );
  924. else mtx.makeTranslation( sea.position.x, sea.position.y, sea.position.z );
  925. // matrix
  926. obj3d.position.setFromMatrixPosition( mtx );
  927. obj3d.scale.setFromMatrixScale( mtx );
  928. // ignore rotation scale
  929. obj3d.rotation.setFromRotationMatrix( THREE.SEA3D.identityMatrixScale( mtx ) );
  930. // optimize if is static
  931. if ( this.config.forceStatic || sea.isStatic ) {
  932. obj3d.updateMatrix();
  933. obj3d.matrixAutoUpdate = false;
  934. }
  935. };
  936. THREE.SEA3D.prototype.toVector3 = function ( data ) {
  937. return new THREE.Vector3( data.x, data.y, data.z );
  938. };
  939. THREE.SEA3D.prototype.toFaces = function ( faces ) {
  940. // xyz(- / +) to xyz(+ / -) sequence
  941. var f = [];
  942. f[ 0 ] = faces[ 1 ];
  943. f[ 1 ] = faces[ 0 ];
  944. f[ 2 ] = faces[ 3 ];
  945. f[ 3 ] = faces[ 2 ];
  946. f[ 4 ] = faces[ 5 ];
  947. f[ 5 ] = faces[ 4 ];
  948. return f;
  949. };
  950. THREE.SEA3D.prototype.updateScene = function () {
  951. if ( this.materials != undefined ) {
  952. for ( var i = 0, l = this.materials.length; i < l; ++ i ) {
  953. this.materials[ i ].needsUpdate = true;
  954. }
  955. }
  956. };
  957. THREE.SEA3D.prototype.addSceneObject = function ( sea, obj3d ) {
  958. obj3d = obj3d || sea.tag;
  959. obj3d.visible = sea.visible;
  960. if ( sea.parent ) sea.parent.tag.add( obj3d );
  961. else if ( this.config.container ) this.config.container.add( obj3d );
  962. if ( sea.attributes ) obj3d.attribs = sea.attributes.tag;
  963. if ( sea.scripts ) {
  964. obj3d.scripts = this.getJSMList( obj3d, sea.scripts );
  965. if ( this.config.scripts && this.config.runScripts ) this.domain.runJSMList( obj3d );
  966. }
  967. };
  968. THREE.SEA3D.prototype.createObjectURL = function ( raw, mime ) {
  969. return ( window.URL || window.webkitURL ).createObjectURL( new Blob( [ raw ], { type: mime } ) );
  970. };
  971. THREE.SEA3D.prototype.parsePath = function ( url ) {
  972. var paths = this.config.paths;
  973. for ( var name in paths ) {
  974. url = url.replace( new RegExp( "%" + name + "%", "g" ), paths[ name ] );
  975. }
  976. return url;
  977. };
  978. THREE.SEA3D.prototype.addDefaultAnimation = function ( sea, animatorClass ) {
  979. var scope = sea.tag;
  980. for ( var i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i ++ ) {
  981. var anm = sea.animations[ i ];
  982. switch ( anm.tag.type ) {
  983. case SEA3D.Animation.prototype.type:
  984. var animation = anm.tag.tag || this.getModifier( {
  985. sea: anm.tag,
  986. scope: scope,
  987. relative: anm.relative
  988. } );
  989. scope.animator = new animatorClass( animation, scope );
  990. scope.animator.setRelative( anm.relative );
  991. if ( this.config.autoPlay ) {
  992. scope.animator.play( 0 );
  993. }
  994. return scope.animator;
  995. break;
  996. }
  997. }
  998. };
  999. //
  1000. // Geometry
  1001. //
  1002. THREE.SEA3D.prototype.readGeometryBuffer = function ( sea ) {
  1003. var geo = sea.tag || new THREE.BufferGeometry();
  1004. for ( var i = 0; i < sea.groups.length; i ++ ) {
  1005. var g = sea.groups[ i ];
  1006. geo.addGroup( g.start, g.count, i );
  1007. }
  1008. // not indexes? use polygon soup
  1009. if ( sea.indexes ) geo.setIndex( new THREE.BufferAttribute( sea.indexes, 1 ) );
  1010. geo.addAttribute( 'position', new THREE.BufferAttribute( sea.vertex, 3 ) );
  1011. if ( sea.uv ) {
  1012. geo.addAttribute( 'uv', new THREE.BufferAttribute( sea.uv[ 0 ], 2 ) );
  1013. if ( sea.uv.length > 1 ) geo.addAttribute( 'uv2', new THREE.BufferAttribute( sea.uv[ 1 ], 2 ) );
  1014. }
  1015. if ( sea.normal ) geo.addAttribute( 'normal', new THREE.BufferAttribute( sea.normal, 3 ) );
  1016. else geo.computeVertexNormals();
  1017. if ( sea.tangent4 ) geo.addAttribute( 'tangent', new THREE.BufferAttribute( sea.tangent4, 4 ) );
  1018. if ( sea.color ) geo.addAttribute( 'color', new THREE.BufferAttribute( sea.color[ 0 ], sea.numColor ) );
  1019. if ( sea.joint ) {
  1020. geo.addAttribute( 'skinIndex', new THREE.Float32BufferAttribute( sea.joint, sea.jointPerVertex ) );
  1021. geo.addAttribute( 'skinWeight', new THREE.Float32BufferAttribute( sea.weight, sea.jointPerVertex ) );
  1022. }
  1023. if ( this.config.bounding ) {
  1024. geo.computeBoundingBox();
  1025. geo.computeBoundingSphere();
  1026. }
  1027. geo.name = sea.name;
  1028. this.domain.geometries = this.geometries = this.geometries || [];
  1029. this.geometries.push( this.objects[ "geo/" + sea.name ] = sea.tag = geo );
  1030. };
  1031. //
  1032. // Dummy
  1033. //
  1034. THREE.SEA3D.prototype.readDummy = function ( sea ) {
  1035. var dummy = new THREE.SEA3D.Dummy( sea.width, sea.height, sea.depth );
  1036. dummy.name = sea.name;
  1037. this.domain.dummys = this.dummys = this.dummys || [];
  1038. this.dummys.push( this.objects[ "dmy/" + sea.name ] = sea.tag = dummy );
  1039. this.addSceneObject( sea );
  1040. this.updateTransform( dummy, sea );
  1041. this.addDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1042. };
  1043. //
  1044. // Line
  1045. //
  1046. THREE.SEA3D.prototype.readLine = function ( sea ) {
  1047. var geo = new THREE.BufferGeometry();
  1048. if ( sea.closed )
  1049. sea.vertex.push( sea.vertex[ 0 ], sea.vertex[ 1 ], sea.vertex[ 2 ] );
  1050. geo.addAttribute( 'position', new THREE.Float32BufferAttribute( sea.vertex, 3 ) );
  1051. var line = new THREE.Line( geo, new THREE.LineBasicMaterial( { color: THREE.SEA3D.HELPER_COLOR, linewidth: 3 } ) );
  1052. line.name = sea.name;
  1053. this.lines = this.lines || [];
  1054. this.lines.push( this.objects[ "line/" + sea.name ] = sea.tag = line );
  1055. this.addSceneObject( sea );
  1056. this.updateTransform( line, sea );
  1057. this.addDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1058. };
  1059. //
  1060. // Container3D
  1061. //
  1062. THREE.SEA3D.prototype.readContainer3D = function ( sea ) {
  1063. var container = new THREE.SEA3D.Object3D();
  1064. this.domain.containers = this.containers = this.containers || [];
  1065. this.containers.push( this.objects[ "c3d/" + sea.name ] = sea.tag = container );
  1066. this.addSceneObject( sea );
  1067. this.updateTransform( container, sea );
  1068. this.addDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1069. };
  1070. //
  1071. // Sprite
  1072. //
  1073. THREE.SEA3D.prototype.readSprite = function ( sea ) {
  1074. var mat;
  1075. if ( sea.material ) {
  1076. if ( ! sea.material.tag.sprite ) {
  1077. mat = sea.material.tag.sprite = new THREE.SpriteMaterial();
  1078. this.setBlending( mat, sea.blendMode );
  1079. var map = sea.material.tag.map;
  1080. if ( map ) {
  1081. map.flipY = true;
  1082. mat.map = map;
  1083. }
  1084. mat.color.set( sea.material.tag.color );
  1085. mat.opacity = sea.material.tag.opacity;
  1086. mat.fog = sea.material.receiveFog;
  1087. } else {
  1088. mat = sea.material.tag.sprite;
  1089. }
  1090. }
  1091. var sprite = new THREE.Sprite( mat );
  1092. sprite.name = sea.name;
  1093. this.domain.sprites = this.sprites = this.sprites || [];
  1094. this.sprites.push( this.objects[ "m2d/" + sea.name ] = sea.tag = sprite );
  1095. this.addSceneObject( sea );
  1096. this.updateTransform( sprite, sea );
  1097. sprite.scale.set( sea.width, sea.height, 1 );
  1098. };
  1099. //
  1100. // Mesh
  1101. //
  1102. THREE.SEA3D.prototype.readMesh = function ( sea ) {
  1103. var i, count, geo = sea.geometry.tag, mesh, mat, skeleton, morpher, skeletonAnimation, vertexAnimation, uvwAnimationClips, morphAnimation;
  1104. for ( i = 0, count = sea.modifiers ? sea.modifiers.length : 0; i < count; i ++ ) {
  1105. var mod = sea.modifiers[ i ];
  1106. switch ( mod.type ) {
  1107. case SEA3D.Skeleton.prototype.type:
  1108. case SEA3D.SkeletonLocal.prototype.type:
  1109. skeleton = mod;
  1110. geo.bones = skeleton.tag;
  1111. break;
  1112. case SEA3D.Morph.prototype.type:
  1113. morpher = mod.tag || this.getModifier( {
  1114. sea: mod,
  1115. geometry: sea.geometry
  1116. } );
  1117. geo.morphAttributes = morpher.attribs;
  1118. geo.morphTargets = morpher.targets;
  1119. break;
  1120. }
  1121. }
  1122. for ( i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i ++ ) {
  1123. var anm = sea.animations[ i ],
  1124. anmTag = anm.tag;
  1125. switch ( anmTag.type ) {
  1126. case SEA3D.SkeletonAnimation.prototype.type:
  1127. skeletonAnimation = anmTag;
  1128. geo.animations = skeletonAnimation.tag || this.getModifier( {
  1129. sea: skeletonAnimation,
  1130. skeleton: skeleton,
  1131. relative: true
  1132. } );
  1133. break;
  1134. case SEA3D.VertexAnimation.prototype.type:
  1135. vertexAnimation = anmTag;
  1136. geo.morphAttributes = vertexAnimation.tag.attribs;
  1137. geo.morphTargets = vertexAnimation.tag.targets;
  1138. geo.animations = vertexAnimation.tag.animations;
  1139. break;
  1140. case SEA3D.UVWAnimation.prototype.type:
  1141. uvwAnimationClips = anmTag.tag || this.getModifier( {
  1142. sea: anmTag
  1143. } );
  1144. break;
  1145. case SEA3D.MorphAnimation.prototype.type:
  1146. morphAnimation = anmTag.tag || this.getModifier( {
  1147. sea: anmTag
  1148. } );
  1149. break;
  1150. }
  1151. }
  1152. var uMorph = morpher != undefined || vertexAnimation != undefined,
  1153. uMorphNormal =
  1154. ( morpher && morpher.attribs.normal != undefined ) ||
  1155. ( vertexAnimation && vertexAnimation.tag.attribs.normal != undefined );
  1156. if ( sea.material ) {
  1157. if ( sea.material.length > 1 ) {
  1158. var mats = [];
  1159. for ( i = 0; i < sea.material.length; i ++ ) {
  1160. mats[ i ] = sea.material[ i ].tag;
  1161. mats[ i ].skinning = skeleton != undefined;
  1162. mats[ i ].morphTargets = uMorph;
  1163. mats[ i ].morphNormals = uMorphNormal;
  1164. mats[ i ].vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  1165. }
  1166. mat = THREE.SEA3D.useMultiMaterial ? new THREE.MultiMaterial( mats ) : mats;
  1167. } else {
  1168. mat = sea.material[ 0 ].tag;
  1169. mat.skinning = skeleton != undefined;
  1170. mat.morphTargets = uMorph;
  1171. mat.morphNormals = uMorphNormal;
  1172. mat.vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  1173. }
  1174. }
  1175. if ( skeleton ) {
  1176. mesh = new THREE.SEA3D.SkinnedMesh( geo, mat, this.config.useVertexTexture );
  1177. if ( this.config.autoPlay && skeletonAnimation ) {
  1178. mesh.play( 0 );
  1179. }
  1180. } else if ( vertexAnimation ) {
  1181. mesh = new THREE.SEA3D.VertexAnimationMesh( geo, mat );
  1182. if ( this.config.autoPlay ) {
  1183. mesh.play( 0 );
  1184. }
  1185. } else {
  1186. mesh = new THREE.SEA3D.Mesh( geo, mat );
  1187. }
  1188. if ( uvwAnimationClips ) {
  1189. mesh.uvwAnimator = new THREE.SEA3D.Animator( uvwAnimationClips, new THREE.AnimationMixer( mat.map ) );
  1190. if ( this.config.autoPlay ) {
  1191. mesh.uvwAnimator.play( 0 );
  1192. }
  1193. }
  1194. if ( morphAnimation ) {
  1195. mesh.morphAnimator = new THREE.SEA3D.Animator( morphAnimation, new THREE.AnimationMixer( mesh ) );
  1196. if ( this.config.autoPlay ) {
  1197. mesh.morphAnimator.play( 0 );
  1198. }
  1199. }
  1200. mesh.name = sea.name;
  1201. mesh.castShadow = sea.castShadows;
  1202. mesh.receiveShadow = sea.material ? sea.material[ 0 ].receiveShadows : true;
  1203. this.domain.meshes = this.meshes = this.meshes || [];
  1204. this.meshes.push( this.objects[ "m3d/" + sea.name ] = sea.tag = mesh );
  1205. this.addSceneObject( sea );
  1206. this.updateTransform( mesh, sea );
  1207. this.addDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1208. };
  1209. //
  1210. // Sound Point
  1211. //
  1212. THREE.SEA3D.prototype.readSoundPoint = function ( sea ) {
  1213. if ( ! this.audioListener ) {
  1214. this.audioListener = new THREE.AudioListener();
  1215. if ( this.config.container ) {
  1216. this.config.container.add( this.audioListener );
  1217. }
  1218. }
  1219. var sound3d = new THREE.SEA3D.PointSound( this.audioListener );
  1220. sound3d.autoplay = sea.autoPlay;
  1221. sound3d.setLoop( sea.autoPlay );
  1222. sound3d.setVolume( sea.volume );
  1223. sound3d.setRefDistance( sea.distance );
  1224. sound3d.setRolloffFactor( this.config.audioRolloffFactor );
  1225. sound3d.setSound( sea.sound.tag );
  1226. sound3d.name = sea.name;
  1227. this.domain.sounds3d = this.sounds3d = this.sounds3d || [];
  1228. this.sounds3d.push( this.objects[ "sn3d/" + sea.name ] = sea.tag = sound3d );
  1229. this.addSceneObject( sea );
  1230. this.updateTransform( sound3d, sea );
  1231. this.addDefaultAnimation( sea, THREE.SEA3D.SoundAnimator );
  1232. };
  1233. //
  1234. // Cube Render
  1235. //
  1236. THREE.SEA3D.prototype.readCubeRender = function ( sea ) {
  1237. var cube = new THREE.CubeCamera( 0.1, 5000, THREE.SEA3D.RTT_SIZE );
  1238. cube.renderTarget.cubeCamera = cube;
  1239. sea.tag = cube.renderTarget;
  1240. this.domain.cubeRenderers = this.cubeRenderers = this.cubeRenderers || [];
  1241. this.cubeRenderers.push( this.objects[ "rttc/" + sea.name ] = cube );
  1242. this.addSceneObject( sea, cube );
  1243. this.updateTransform( cube, sea );
  1244. };
  1245. //
  1246. // Texture (WDP, JPEG, PNG and GIF)
  1247. //
  1248. THREE.SEA3D.prototype.readTexture = function ( sea ) {
  1249. var image = new Image(),
  1250. texture = new THREE.Texture();
  1251. texture.name = sea.name;
  1252. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1253. texture.flipY = false;
  1254. texture.image = image;
  1255. if ( this.config.anisotropy !== undefined ) texture.anisotropy = this.config.anisotropy;
  1256. image.onload = function () {
  1257. texture.needsUpdate = true;
  1258. };
  1259. image.src = this.createObjectURL( sea.data.buffer, "image/" + sea.type );
  1260. this.domain.textures = this.textures = this.textures || [];
  1261. this.textures.push( this.objects[ "tex/" + sea.name ] = sea.tag = texture );
  1262. };
  1263. //
  1264. // Cube Map
  1265. //
  1266. THREE.SEA3D.prototype.readCubeMap = function ( sea ) {
  1267. var faces = this.toFaces( sea.faces ), texture = new THREE.CubeTexture( [] );
  1268. var loaded = 0;
  1269. texture.name = sea.name;
  1270. texture.flipY = false;
  1271. texture.format = THREE.RGBFormat;
  1272. var onLoaded = function () {
  1273. if ( ++ loaded == 6 ) {
  1274. texture.needsUpdate = true;
  1275. if ( ! this.config.async ) this.file.resume = true;
  1276. }
  1277. }.bind( this );
  1278. for ( var i = 0; i < faces.length; ++ i ) {
  1279. var cubeImage = new Image();
  1280. cubeImage.onload = onLoaded;
  1281. cubeImage.src = this.createObjectURL( faces[ i ].buffer, "image/" + sea.extension );
  1282. texture.images[ i ] = cubeImage;
  1283. }
  1284. if ( ! this.config.async ) this.file.resume = false;
  1285. this.domain.cubemaps = this.cubemaps = this.cubemaps || [];
  1286. this.cubemaps.push( this.objects[ "cmap/" + sea.name ] = sea.tag = texture );
  1287. };
  1288. //
  1289. // Updaters
  1290. //
  1291. THREE.SEA3D.prototype.readTextureUpdate = function ( sea ) {
  1292. var obj = this.file.objects[ sea.index ],
  1293. tex = obj.tag;
  1294. var image = new Image();
  1295. image.onload = function () {
  1296. tex.image = image;
  1297. tex.needsUpdate = true;
  1298. };
  1299. image.src = this.createObjectURL( sea.bytes.buffer, "image/" + obj.type );
  1300. };
  1301. //
  1302. // Sound (MP3, OGG)
  1303. //
  1304. THREE.SEA3D.prototype.readSound = function ( sea ) {
  1305. var sound = new THREE.SEA3D.Sound( this.createObjectURL( sea.data.buffer, "audio/" + sea.type ) );
  1306. sound.name = sea.name;
  1307. this.domain.sounds = this.sounds = this.sounds || [];
  1308. this.sounds.push( this.objects[ "snd/" + sea.name ] = sea.tag = sound );
  1309. };
  1310. //
  1311. // Script URL
  1312. //
  1313. THREE.SEA3D.prototype.readScriptURL = function ( sea ) {
  1314. this.file.resume = false;
  1315. var loader = new THREE.FileLoader();
  1316. loader.setResponseType( "text" ).load( sea.url, function ( src ) {
  1317. this.file.resume = true;
  1318. this.domain.scripts = this.scripts = this.scripts || [];
  1319. this.scripts.push( this.objects[ "src/" + sea.name ] = sea.tag = src );
  1320. }.bind( this ) );
  1321. };
  1322. //
  1323. // Texture URL
  1324. //
  1325. THREE.SEA3D.prototype.readTextureURL = function ( sea ) {
  1326. var texture = new THREE.TextureLoader().load( this.parsePath( sea.url ) );
  1327. texture.name = sea.name;
  1328. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1329. texture.flipY = false;
  1330. if ( this.config.anisotropy !== undefined ) texture.anisotropy = this.config.anisotropy;
  1331. this.domain.textures = this.textures = this.textures || [];
  1332. this.textures.push( this.objects[ "tex/" + sea.name ] = sea.tag = texture );
  1333. };
  1334. //
  1335. // CubeMap URL
  1336. //
  1337. THREE.SEA3D.prototype.readCubeMapURL = function ( sea ) {
  1338. var faces = this.toFaces( sea.faces );
  1339. for ( var i = 0; i < faces.length; i ++ ) {
  1340. faces[ i ] = this.parsePath( faces[ i ] );
  1341. }
  1342. var texture, format = faces[ 0 ].substr( - 3 );
  1343. if ( format == "hdr" ) {
  1344. var usePMREM = THREE.PMREMGenerator != null;
  1345. this.file.resume = ! usePMREM;
  1346. texture = new THREE.HDRCubeTextureLoader().load( THREE.UnsignedByteType, faces, function ( texture ) {
  1347. if ( usePMREM ) {
  1348. var pmremGenerator = new THREE.PMREMGenerator( texture );
  1349. pmremGenerator.update( this.config.renderer );
  1350. var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
  1351. pmremCubeUVPacker.update( this.config.renderer );
  1352. texture.dispose();
  1353. this.objects[ "cmap/" + sea.name ] = sea.tag = pmremCubeUVPacker.CubeUVRenderTarget.texture;
  1354. this.file.resume = true;
  1355. }
  1356. }.bind( this ) );
  1357. } else {
  1358. texture = new THREE.CubeTextureLoader().load( faces );
  1359. }
  1360. texture.name = sea.name;
  1361. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1362. texture.flipY = false;
  1363. if ( this.config.anisotropy !== undefined ) texture.anisotropy = this.config.anisotropy;
  1364. this.domain.cubemaps = this.cubemaps = this.cubemaps || [];
  1365. this.cubemaps.push( this.objects[ "cmap/" + sea.name ] = sea.tag = texture );
  1366. };
  1367. //
  1368. // Runtime
  1369. //
  1370. THREE.SEA3D.prototype.getJSMList = function ( target, scripts ) {
  1371. var scriptTarget = [];
  1372. for ( var i = 0; i < scripts.length; i ++ ) {
  1373. var script = scripts[ i ];
  1374. if ( script.tag.type == SEA3D.JavaScriptMethod.prototype.type ) {
  1375. scriptTarget.push( script );
  1376. }
  1377. }
  1378. this.domain.scriptTargets = this.scriptTargets = this.scriptTargets || [];
  1379. this.scriptTargets.push( target );
  1380. return scriptTarget;
  1381. };
  1382. THREE.SEA3D.prototype.readJavaScriptMethod = function ( sea ) {
  1383. try {
  1384. var src =
  1385. '(function() {\n' +
  1386. 'var $METHOD = {}\n';
  1387. var declare =
  1388. 'function($INC, $REF, global, local, self, $PARAM) {\n' +
  1389. 'var watch = $INC["watch"],\n' +
  1390. 'scene = $INC["scene"],\n' +
  1391. 'sea3d = $INC["sea3d"],\n' +
  1392. 'print = $INC["print"];\n';
  1393. declare +=
  1394. 'var $SRC = $INC["source"],\n' +
  1395. 'addEventListener = $SRC.addEventListener.bind( $SRC ),\n' +
  1396. 'hasEventListener = $SRC.hasEventListener.bind( $SRC ),\n' +
  1397. 'removeEventListener = $SRC.removeEventListener.bind( $SRC ),\n' +
  1398. 'dispatchEvent = $SRC.dispatchEvent.bind( $SRC ),\n' +
  1399. 'dispose = $SRC.dispose.bind( $SRC );\n';
  1400. for ( var name in sea.methods ) {
  1401. src += '$METHOD["' + name + '"] = ' + declare + sea.methods[ name ].src + '}\n';
  1402. }
  1403. src += 'return $METHOD; })';
  1404. this.domain.methods = eval( src )();
  1405. } catch ( e ) {
  1406. console.error( 'SEA3D JavaScriptMethod: Error running "' + sea.name + '".' );
  1407. console.error( e );
  1408. }
  1409. };
  1410. //
  1411. // GLSL
  1412. //
  1413. THREE.SEA3D.prototype.readGLSL = function ( sea ) {
  1414. this.domain.glsl = this.glsl = this.glsl || [];
  1415. this.glsl.push( this.objects[ "glsl/" + sea.name ] = sea.tag = sea.src );
  1416. };
  1417. //
  1418. // Material
  1419. //
  1420. THREE.SEA3D.prototype.materialTechnique =
  1421. ( function () {
  1422. var techniques = {};
  1423. // FINAL
  1424. techniques.onComplete = function ( mat, sea ) {
  1425. if ( sea.alpha < 1 || mat.blending > THREE.NormalBlending ) {
  1426. mat.opacity = sea.alpha;
  1427. mat.transparent = true;
  1428. }
  1429. };
  1430. // PHYSICAL
  1431. techniques[ SEA3D.Material.PHYSICAL ] =
  1432. function ( mat, tech ) {
  1433. mat.color.setHex( tech.color );
  1434. mat.roughness = tech.roughness;
  1435. mat.metalness = tech.metalness;
  1436. };
  1437. // REFLECTIVITY
  1438. techniques[ SEA3D.Material.REFLECTIVITY ] =
  1439. function ( mat, tech ) {
  1440. mat.reflectivity = tech.strength;
  1441. };
  1442. // CLEAR_COAT
  1443. techniques[ SEA3D.Material.CLEAR_COAT ] =
  1444. function ( mat, tech ) {
  1445. mat.clearCoat = tech.strength;
  1446. mat.clearCoatRoughness = tech.roughness;
  1447. };
  1448. // PHONG
  1449. techniques[ SEA3D.Material.PHONG ] =
  1450. function ( mat, tech ) {
  1451. mat.color.setHex( tech.diffuseColor );
  1452. mat.specular.setHex( tech.specularColor ).multiplyScalar( tech.specular );
  1453. mat.shininess = tech.gloss;
  1454. };
  1455. // DIFFUSE_MAP
  1456. techniques[ SEA3D.Material.DIFFUSE_MAP ] =
  1457. function ( mat, tech, sea ) {
  1458. mat.map = tech.texture.tag;
  1459. mat.color.setHex( 0xFFFFFF );
  1460. mat.map.wrapS = mat.map.wrapT = sea.repeat ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  1461. if ( tech.texture.transparent ) {
  1462. mat.transparent = true;
  1463. }
  1464. };
  1465. // ROUGHNESS_MAP
  1466. techniques[ SEA3D.Material.ROUGHNESS_MAP ] =
  1467. function ( mat, tech ) {
  1468. mat.roughnessMap = tech.texture.tag;
  1469. };
  1470. // METALNESS_MAP
  1471. techniques[ SEA3D.Material.METALNESS_MAP ] =
  1472. function ( mat, tech ) {
  1473. mat.metalnessMap = tech.texture.tag;
  1474. };
  1475. // SPECULAR_MAP
  1476. techniques[ SEA3D.Material.SPECULAR_MAP ] =
  1477. function ( mat, tech ) {
  1478. if ( mat.specular ) {
  1479. mat.specularMap = tech.texture.tag;
  1480. mat.specular.setHex( 0xFFFFFF );
  1481. }
  1482. };
  1483. // NORMAL_MAP
  1484. techniques[ SEA3D.Material.NORMAL_MAP ] =
  1485. function ( mat, tech ) {
  1486. mat.normalMap = tech.texture.tag;
  1487. };
  1488. // REFLECTION
  1489. techniques[ SEA3D.Material.REFLECTION ] =
  1490. techniques[ SEA3D.Material.FRESNEL_REFLECTION ] =
  1491. function ( mat, tech ) {
  1492. mat.envMap = tech.texture.tag;
  1493. mat.envMap.mapping = THREE.CubeReflectionMapping;
  1494. mat.combine = THREE.MixOperation;
  1495. mat.reflectivity = tech.alpha;
  1496. };
  1497. // REFLECTION_SPHERICAL
  1498. techniques[ SEA3D.Material.REFLECTION_SPHERICAL ] =
  1499. function ( mat, tech ) {
  1500. mat.envMap = tech.texture.tag;
  1501. mat.envMap.mapping = THREE.SphericalReflectionMapping;
  1502. mat.combine = THREE.MixOperation;
  1503. mat.reflectivity = tech.alpha;
  1504. };
  1505. // REFRACTION
  1506. techniques[ SEA3D.Material.REFRACTION_MAP ] =
  1507. function ( mat, tech ) {
  1508. mat.envMap = tech.texture.tag;
  1509. mat.envMap.mapping = THREE.CubeRefractionMapping;
  1510. mat.refractionRatio = tech.ior;
  1511. mat.reflectivity = tech.alpha;
  1512. };
  1513. // LIGHT_MAP
  1514. techniques[ SEA3D.Material.LIGHT_MAP ] =
  1515. function ( mat, tech ) {
  1516. if ( tech.blendMode == "multiply" ) mat.aoMap = tech.texture.tag;
  1517. else mat.lightMap = tech.texture.tag;
  1518. };
  1519. // EMISSIVE
  1520. techniques[ SEA3D.Material.EMISSIVE ] =
  1521. function ( mat, tech ) {
  1522. mat.emissive.setHex( tech.color );
  1523. };
  1524. // EMISSIVE_MAP
  1525. techniques[ SEA3D.Material.EMISSIVE_MAP ] =
  1526. function ( mat, tech ) {
  1527. mat.emissiveMap = tech.texture.tag;
  1528. };
  1529. // ALPHA_MAP
  1530. techniques[ SEA3D.Material.ALPHA_MAP ] =
  1531. function ( mat, tech, sea ) {
  1532. mat.alphaMap = tech.texture.tag;
  1533. mat.transparent = true;
  1534. mat.alphaMap.wrapS = mat.alphaMap.wrapT = sea.repeat ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  1535. };
  1536. return techniques;
  1537. } )();
  1538. THREE.SEA3D.prototype.createMaterial = function ( sea ) {
  1539. if ( sea.tecniquesDict[ SEA3D.Material.REFLECTIVITY ] || sea.tecniquesDict[ SEA3D.Material.CLEAR_COAT ] ) {
  1540. return new THREE.MeshPhysicalMaterial();
  1541. } else if ( sea.tecniquesDict[ SEA3D.Material.PHYSICAL ] ) {
  1542. return new THREE.MeshStandardMaterial();
  1543. }
  1544. return new THREE.MeshPhongMaterial();
  1545. };
  1546. THREE.SEA3D.prototype.setBlending = function ( mat, blendMode ) {
  1547. if ( blendMode === "normal" ) return;
  1548. switch ( blendMode ) {
  1549. case "add":
  1550. mat.blending = THREE.AdditiveBlending;
  1551. break;
  1552. case "subtract":
  1553. mat.blending = THREE.SubtractiveBlending;
  1554. break;
  1555. case "multiply":
  1556. mat.blending = THREE.MultiplyBlending;
  1557. break;
  1558. case "screen":
  1559. mat.blending = THREE.CustomBlending;
  1560. mat.blendSrc = THREE.OneFactor;
  1561. mat.blendDst = THREE.OneMinusSrcColorFactor;
  1562. mat.blendEquation = THREE.AddEquation;
  1563. break;
  1564. }
  1565. mat.transparent = true;
  1566. };
  1567. THREE.SEA3D.prototype.readMaterial = function ( sea ) {
  1568. var mat = this.createMaterial( sea );
  1569. mat.name = sea.name;
  1570. mat.lights = sea.receiveLights;
  1571. mat.fog = sea.receiveFog;
  1572. mat.depthWrite = sea.depthWrite;
  1573. mat.depthTest = sea.depthTest;
  1574. mat.premultipliedAlpha = sea.premultipliedAlpha;
  1575. mat.side = sea.doubleSided ? THREE.DoubleSide : THREE.FrontSide;
  1576. this.setBlending( mat, sea.blendMode );
  1577. for ( var i = 0; i < sea.technique.length; i ++ ) {
  1578. var tech = sea.technique[ i ];
  1579. if ( this.materialTechnique[ tech.kind ] ) {
  1580. this.materialTechnique[ tech.kind ].call( this, mat, tech, sea );
  1581. }
  1582. }
  1583. if ( this.materialTechnique.onComplete ) {
  1584. this.materialTechnique.onComplete.call( this, mat, sea );
  1585. }
  1586. this.domain.materials = this.materials = this.materials || [];
  1587. this.materials.push( this.objects[ "mat/" + sea.name ] = sea.tag = mat );
  1588. };
  1589. //
  1590. // Point Light
  1591. //
  1592. THREE.SEA3D.prototype.readPointLight = function ( sea ) {
  1593. var light = new THREE.SEA3D.PointLight( sea.color, sea.multiplier * this.config.multiplier );
  1594. light.name = sea.name;
  1595. if ( sea.attenuation ) {
  1596. light.distance = sea.attenuation.end;
  1597. }
  1598. if ( sea.shadow ) this.setShadowMap( light );
  1599. this.domain.lights = this.lights = this.lights || [];
  1600. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1601. this.addSceneObject( sea );
  1602. this.updateTransform( light, sea );
  1603. this.addDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1604. this.updateScene();
  1605. };
  1606. //
  1607. // Hemisphere Light
  1608. //
  1609. THREE.SEA3D.prototype.readHemisphereLight = function ( sea ) {
  1610. var light = new THREE.HemisphereLight( sea.color, sea.secondColor, sea.multiplier * this.config.multiplier );
  1611. light.position.set( 0, 500, 0 );
  1612. light.name = sea.name;
  1613. this.domain.lights = this.lights = this.lights || [];
  1614. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1615. this.addSceneObject( sea );
  1616. this.addDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1617. this.updateScene();
  1618. };
  1619. //
  1620. // Ambient Light
  1621. //
  1622. THREE.SEA3D.prototype.readAmbientLight = function ( sea ) {
  1623. var light = new THREE.AmbientLight( sea.color, sea.multiplier * this.config.multiplier );
  1624. light.name = sea.name;
  1625. this.domain.lights = this.lights = this.lights || [];
  1626. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1627. this.addSceneObject( sea );
  1628. this.addDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1629. this.updateScene();
  1630. };
  1631. //
  1632. // Directional Light
  1633. //
  1634. THREE.SEA3D.prototype.readDirectionalLight = function ( sea ) {
  1635. var light = new THREE.DirectionalLight( sea.color, sea.multiplier * this.config.multiplier );
  1636. light.name = sea.name;
  1637. if ( sea.shadow ) {
  1638. this.setShadowMap( light );
  1639. }
  1640. this.domain.lights = this.lights = this.lights || [];
  1641. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1642. this.addSceneObject( sea );
  1643. this.updateTransform( light, sea );
  1644. this.addDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1645. this.updateScene();
  1646. };
  1647. //
  1648. // Camera
  1649. //
  1650. THREE.SEA3D.prototype.readCamera = function ( sea ) {
  1651. var camera = new THREE.SEA3D.Camera( sea.fov );
  1652. camera.name = sea.name;
  1653. this.domain.cameras = this.cameras = this.cameras || [];
  1654. this.cameras.push( this.objects[ "cam/" + sea.name ] = sea.tag = camera );
  1655. this.addSceneObject( sea );
  1656. this.updateTransform( camera, sea );
  1657. this.addDefaultAnimation( sea, THREE.SEA3D.CameraAnimator );
  1658. };
  1659. //
  1660. // Orthographic Camera
  1661. //
  1662. THREE.SEA3D.prototype.readOrthographicCamera = function ( sea ) {
  1663. var aspect, width, height;
  1664. var stageWidth = this.config.stageWidth !== undefined ? this.config.stageWidth : ( window ? window.innerWidth : 1024 );
  1665. var stageHeight = this.config.stageHeight !== undefined ? this.config.stageHeight : ( window ? window.innerHeight : 1024 );
  1666. if ( stageWidth > stageHeight ) {
  1667. aspect = stageWidth / stageHeight;
  1668. width = sea.height * aspect;
  1669. height = sea.height;
  1670. } else {
  1671. aspect = stageHeight / stageWidth;
  1672. width = sea.height;
  1673. height = sea.height * aspect;
  1674. }
  1675. var camera = new THREE.SEA3D.OrthographicCamera( - width, width, height, - height );
  1676. camera.name = sea.name;
  1677. this.domain.cameras = this.cameras = this.cameras || [];
  1678. this.cameras.push( this.objects[ "cam/" + sea.name ] = sea.tag = camera );
  1679. this.addSceneObject( sea );
  1680. this.updateTransform( camera, sea );
  1681. this.addDefaultAnimation( sea, THREE.SEA3D.CameraAnimator );
  1682. };
  1683. //
  1684. // Skeleton
  1685. //
  1686. THREE.SEA3D.prototype.getSkeletonFromBones = function ( bonesData ) {
  1687. var bones = [], bone, gbone;
  1688. var i, il;
  1689. for ( i = 0, il = bonesData.length; i < il; i ++ ) {
  1690. gbone = bonesData[ i ];
  1691. bone = new THREE.Bone();
  1692. bones.push( bone );
  1693. bone.name = gbone.name;
  1694. bone.position.fromArray( gbone.pos );
  1695. bone.quaternion.fromArray( gbone.rotq );
  1696. if ( gbone.scl !== undefined ) bone.scale.fromArray( gbone.scl );
  1697. }
  1698. for ( i = 0, il = bonesData.length; i < il; i ++ ) {
  1699. gbone = bonesData[ i ];
  1700. if ( ( gbone.parent !== - 1 ) && ( gbone.parent !== null ) && ( bones[ gbone.parent ] !== undefined ) ) {
  1701. bones[ gbone.parent ].add( bones[ i ] );
  1702. }
  1703. }
  1704. return new THREE.Skeleton( bones );
  1705. };
  1706. THREE.SEA3D.prototype.readSkeletonLocal = function ( sea ) {
  1707. var bones = [];
  1708. for ( var i = 0; i < sea.joint.length; i ++ ) {
  1709. var bone = sea.joint[ i ];
  1710. bones[ i ] = {
  1711. name: bone.name,
  1712. pos: [ bone.x, bone.y, bone.z ],
  1713. rotq: [ bone.qx, bone.qy, bone.qz, bone.qw ],
  1714. parent: bone.parentIndex
  1715. };
  1716. }
  1717. this.domain.bones = this.bones = this.bones || [];
  1718. this.bones.push( this.objects[ sea.name + '.sklq' ] = sea.tag = bones );
  1719. };
  1720. //
  1721. // Joint Object
  1722. //
  1723. THREE.SEA3D.prototype.readJointObject = function ( sea ) {
  1724. var mesh = sea.target.tag,
  1725. bone = mesh.skeleton.bones[ sea.joint ];
  1726. this.domain.joints = this.joints = this.joints || [];
  1727. this.joints.push( this.objects[ "jnt/" + sea.name ] = sea.tag = bone );
  1728. };
  1729. //
  1730. // Morph
  1731. //
  1732. THREE.SEA3D.prototype.readMorph = function ( sea ) {
  1733. var attribs = { position: [] }, targets = [];
  1734. for ( var i = 0; i < sea.node.length; i ++ ) {
  1735. var node = sea.node[ i ];
  1736. attribs.position[ i ] = new THREE.Float32BufferAttribute( node.vertex, 3 );
  1737. attribs.position[ i ].name = node.name;
  1738. if ( node.normal ) {
  1739. attribs.normal = attribs.normal || [];
  1740. attribs.normal[ i ] = new THREE.Float32BufferAttribute( node.normal, 3 );
  1741. }
  1742. targets[ i ] = { name: node.name };
  1743. }
  1744. sea.tag = {
  1745. attribs: attribs,
  1746. targets: targets
  1747. };
  1748. };
  1749. //
  1750. // Animation
  1751. //
  1752. THREE.SEA3D.prototype.readAnimation = function ( sea ) {
  1753. var animations = [], delta = ( 1000 / sea.frameRate ) / 1000;
  1754. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  1755. var seq = sea.sequence[ i ];
  1756. var tracks = [];
  1757. for ( var j = 0; j < sea.dataList.length; j ++ ) {
  1758. var anm = sea.dataList[ j ],
  1759. t, k, times, values,
  1760. data = anm.data,
  1761. start = seq.start * anm.blockSize,
  1762. end = start + ( seq.count * anm.blockSize ),
  1763. intrpl = seq.intrpl ? THREE.InterpolateLinear : false,
  1764. name = null;
  1765. switch ( anm.kind ) {
  1766. case SEA3D.Animation.POSITION:
  1767. name = '.position';
  1768. break;
  1769. case SEA3D.Animation.ROTATION:
  1770. name = '.quaternion';
  1771. break;
  1772. case SEA3D.Animation.SCALE:
  1773. name = '.scale';
  1774. break;
  1775. case SEA3D.Animation.COLOR:
  1776. name = '.color';
  1777. break;
  1778. case SEA3D.Animation.MULTIPLIER:
  1779. name = '.intensity';
  1780. break;
  1781. case SEA3D.Animation.FOV:
  1782. name = '.fov';
  1783. break;
  1784. case SEA3D.Animation.OFFSET_U:
  1785. name = '.offset[x]';
  1786. break;
  1787. case SEA3D.Animation.OFFSET_V:
  1788. name = '.offset[y]';
  1789. break;
  1790. case SEA3D.Animation.SCALE_U:
  1791. name = '.repeat[x]';
  1792. break;
  1793. case SEA3D.Animation.SCALE_V:
  1794. name = '.repeat[y]';
  1795. break;
  1796. case SEA3D.Animation.MORPH:
  1797. name = '.morphTargetInfluences[' + anm.name + ']';
  1798. break;
  1799. }
  1800. if ( ! name ) continue;
  1801. switch ( anm.type ) {
  1802. case SEA3D.Stream.BYTE:
  1803. case SEA3D.Stream.UBYTE:
  1804. case SEA3D.Stream.INT:
  1805. case SEA3D.Stream.UINT:
  1806. case SEA3D.Stream.FLOAT:
  1807. case SEA3D.Stream.DOUBLE:
  1808. case SEA3D.Stream.DECIMAL:
  1809. values = data.subarray( start, end );
  1810. times = new Float32Array( values.length );
  1811. t = 0;
  1812. for ( k = 0; k < times.length; k ++ ) {
  1813. times[ k ] = t;
  1814. t += delta;
  1815. }
  1816. tracks.push( new THREE.NumberKeyframeTrack( name, times, values, intrpl ) );
  1817. break;
  1818. case SEA3D.Stream.VECTOR3D:
  1819. values = data.subarray( start, end );
  1820. times = new Float32Array( values.length / anm.blockSize );
  1821. t = 0;
  1822. for ( k = 0; k < times.length; k ++ ) {
  1823. times[ k ] = t;
  1824. t += delta;
  1825. }
  1826. tracks.push( new THREE.VectorKeyframeTrack( name, times, values, intrpl ) );
  1827. break;
  1828. case SEA3D.Stream.VECTOR4D:
  1829. values = data.subarray( start, end );
  1830. times = new Float32Array( values.length / anm.blockSize );
  1831. t = 0;
  1832. for ( k = 0; k < times.length; k ++ ) {
  1833. times[ k ] = t;
  1834. t += delta;
  1835. }
  1836. tracks.push( new THREE.QuaternionKeyframeTrack( name, times, values, intrpl ) );
  1837. break;
  1838. case SEA3D.Stream.INT24:
  1839. case SEA3D.Stream.UINT24:
  1840. values = new Float32Array( ( end - start ) * 3 );
  1841. times = new Float32Array( values.length / 3 );
  1842. t = 0;
  1843. for ( k = 0; k < times.length; k ++ ) {
  1844. values[ ( k * 3 ) ] = ( ( data[ k ] >> 16 ) & 0xFF ) / 255;
  1845. values[ ( k * 3 ) + 1 ] = ( ( data[ k ] >> 8 ) & 0xFF ) / 255;
  1846. values[ ( k * 3 ) + 2 ] = ( data[ k ] & 0xFF ) / 255;
  1847. times[ k ] = t;
  1848. t += delta;
  1849. }
  1850. tracks.push( new THREE.VectorKeyframeTrack( name, times, values, intrpl ) );//ColorKeyframeTrack
  1851. break;
  1852. }
  1853. }
  1854. animations.push( new THREE.SEA3D.AnimationClip( seq.name, - 1, tracks, seq.repeat ) );
  1855. }
  1856. this.domain.clips = this.clips = this.clips || [];
  1857. this.clips.push( this.objects[ sea.name + '.anm' ] = sea.tag = animations );
  1858. };
  1859. //
  1860. // Skeleton Animation
  1861. //
  1862. THREE.SEA3D.prototype.readSkeletonAnimation = function ( sea, skl ) {
  1863. skl = ! skl && sea.metadata && sea.metadata.skeleton ? sea.metadata.skeleton : skl;
  1864. if ( ! skl || sea.tag ) return sea.tag;
  1865. var animations = [], delta = ( 1000 / sea.frameRate ) / 1000;
  1866. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  1867. var seq = sea.sequence[ i ];
  1868. var start = seq.start;
  1869. var end = start + seq.count;
  1870. var animation = {
  1871. name: seq.name,
  1872. fps: sea.frameRate,
  1873. length: delta * seq.count,
  1874. hierarchy: []
  1875. };
  1876. var numJoints = sea.numJoints,
  1877. raw = sea.raw;
  1878. for ( var j = 0; j < numJoints; j ++ ) {
  1879. var bone = skl.joint[ j ],
  1880. node = { parent: bone.parentIndex, keys: [] },
  1881. keys = node.keys,
  1882. time = 0;
  1883. for ( var frame = start; frame < end; frame ++ ) {
  1884. var idx = ( frame * numJoints * 7 ) + ( j * 7 );
  1885. keys.push( {
  1886. time: time,
  1887. pos: [ raw[ idx ], raw[ idx + 1 ], raw[ idx + 2 ] ],
  1888. rot: [ raw[ idx + 3 ], raw[ idx + 4 ], raw[ idx + 5 ], raw[ idx + 6 ] ],
  1889. scl: [ 1, 1, 1 ]
  1890. } );
  1891. time += delta;
  1892. }
  1893. animation.hierarchy[ j ] = node;
  1894. }
  1895. animations.push( THREE.SEA3D.AnimationClip.fromClip( THREE.AnimationClip.parseAnimation( animation, skl.tag ), seq.repeat ) );
  1896. }
  1897. this.domain.clips = this.clips = this.clips || [];
  1898. this.clips.push( this.objects[ sea.name + '.skla' ] = sea.tag = animations );
  1899. };
  1900. //
  1901. // Vertex Animation
  1902. //
  1903. THREE.SEA3D.prototype.readVertexAnimation = function ( sea ) {
  1904. var attribs = { position: [] }, targets = [], animations = [], i, j, l;
  1905. for ( i = 0, l = sea.frame.length; i < l; i ++ ) {
  1906. var frame = sea.frame[ i ];
  1907. attribs.position[ i ] = new THREE.Float32BufferAttribute( frame.vertex, 3 );
  1908. if ( frame.normal ) {
  1909. attribs.normal = attribs.normal || [];
  1910. attribs.normal[ i ] = new THREE.Float32BufferAttribute( frame.normal, 3 );
  1911. }
  1912. targets[ i ] = { name: i };
  1913. }
  1914. for ( i = 0; i < sea.sequence.length; i ++ ) {
  1915. var seq = sea.sequence[ i ];
  1916. var seqTargets = [];
  1917. for ( j = 0; j < seq.count; j ++ ) {
  1918. seqTargets[ j ] = targets[ seq.start + j ];
  1919. }
  1920. animations.push( THREE.SEA3D.AnimationClip.fromClip( THREE.AnimationClip.CreateFromMorphTargetSequence( seq.name, seqTargets, sea.frameRate ), seq.repeat ) );
  1921. }
  1922. sea.tag = {
  1923. attribs: attribs,
  1924. targets: targets,
  1925. animations: animations
  1926. };
  1927. this.domain.clips = this.clips = this.clips || [];
  1928. this.clips.push( this.objects[ sea.name + '.vtxa' ] = sea.tag );
  1929. };
  1930. //
  1931. // Selector
  1932. //
  1933. THREE.SEA3D.prototype.getModifier = function ( req ) {
  1934. var sea = req.sea;
  1935. switch ( sea.type ) {
  1936. case SEA3D.SkeletonAnimation.prototype.type:
  1937. this.readSkeletonAnimation( sea, req.skeleton );
  1938. break;
  1939. case SEA3D.Animation.prototype.type:
  1940. case SEA3D.MorphAnimation.prototype.type:
  1941. case SEA3D.UVWAnimation.prototype.type:
  1942. this.readAnimation( sea );
  1943. break;
  1944. case SEA3D.Morph.prototype.type:
  1945. this.readMorph( sea, req.geometry );
  1946. break;
  1947. }
  1948. return sea.tag;
  1949. };
  1950. //
  1951. // Actions
  1952. //
  1953. THREE.SEA3D.prototype.applyEnvironment = function ( envMap ) {
  1954. for ( var j = 0, l = this.materials.length; j < l; ++ j ) {
  1955. var mat = this.materials[ j ];
  1956. if ( mat instanceof THREE.MeshStandardMaterial ) {
  1957. if ( mat.envMap ) continue;
  1958. mat.envMap = envMap;
  1959. mat.envMap.mapping = THREE.CubeReflectionMapping;
  1960. mat.needsUpdate = true;
  1961. }
  1962. }
  1963. };
  1964. THREE.SEA3D.prototype.readActions = function ( sea ) {
  1965. for ( var i = 0; i < sea.actions.length; i ++ ) {
  1966. var act = sea.actions[ i ];
  1967. switch ( act.kind ) {
  1968. case SEA3D.Actions.ATTRIBUTES:
  1969. this.attribs = this.domain.attribs = act.attributes.tag;
  1970. break;
  1971. case SEA3D.Actions.SCRIPTS:
  1972. this.domain.scripts = this.getJSMList( this.domain, act.scripts );
  1973. if ( this.config.scripts && this.config.runScripts ) this.domain.runJSMList( this.domain );
  1974. break;
  1975. case SEA3D.Actions.CAMERA:
  1976. this.domain.camera = this.camera = act.camera.tag;
  1977. break;
  1978. case SEA3D.Actions.ENVIRONMENT_COLOR:
  1979. this.domain.background = this.background = this.background || {};
  1980. this.background.color = new THREE.Color( act.color );
  1981. break;
  1982. case SEA3D.Actions.ENVIRONMENT:
  1983. this.domain.background = this.background = this.background || {};
  1984. this.background.texture = act.texture.tag;
  1985. if ( this.config.useEnvironment && this.materials != undefined ) {
  1986. this.applyEnvironment( act.texture.tag );
  1987. }
  1988. break;
  1989. }
  1990. }
  1991. };
  1992. //
  1993. // Properties
  1994. //
  1995. THREE.SEA3D.prototype.updatePropertiesAssets = function ( sea, props ) {
  1996. for ( var name in props ) {
  1997. switch ( props.__type[ name ] ) {
  1998. case SEA3D.Stream.ASSET:
  1999. if ( ! props.__asset ) props.__asset = {};
  2000. if ( ! props.__asset[ name ] ) props.__asset[ name ] = props[ name ];
  2001. props[ name ] = props.__asset[ name ].tag;
  2002. break;
  2003. case SEA3D.Stream.GROUP:
  2004. props[ name ] = this.updatePropertiesAssets( sea, props[ name ] );
  2005. break;
  2006. }
  2007. }
  2008. return props;
  2009. };
  2010. THREE.SEA3D.prototype.readProperties = function ( sea ) {
  2011. var props = this.updatePropertiesAssets( sea, sea.props );
  2012. this.domain.properties = this.properties = this.properties || [];
  2013. this.properties.push( this.objects[ "prop/" + sea.name ] = sea.tag = props );
  2014. };
  2015. THREE.SEA3D.prototype.readFileInfo = function ( sea ) {
  2016. this.domain.info = this.updatePropertiesAssets( sea, sea.info );
  2017. };
  2018. //
  2019. // Events
  2020. //
  2021. THREE.SEA3D.Event = {
  2022. PROGRESS: "sea3d_progress",
  2023. LOAD_PROGRESS: "sea3d_load",
  2024. DOWNLOAD_PROGRESS: "sea3d_download",
  2025. COMPLETE: "sea3d_complete",
  2026. OBJECT_COMPLETE: "sea3d_object",
  2027. PARSE_PROGRESS: "parse_progress",
  2028. PARSE_COMPLETE: "parse_complete",
  2029. ERROR: "sea3d_error"
  2030. };
  2031. THREE.SEA3D.prototype.onProgress = function ( e ) {
  2032. e.status = e.type;
  2033. e.progress = e.loaded / e.total;
  2034. e.type = THREE.SEA3D.Event.PROGRESS;
  2035. this.dispatchEvent( e );
  2036. };
  2037. THREE.SEA3D.prototype.onLoadProgress = function ( e ) {
  2038. e.type = THREE.SEA3D.Event.LOAD_PROGRESS;
  2039. this.dispatchEvent( e );
  2040. this.onProgress( e );
  2041. };
  2042. THREE.SEA3D.prototype.onDownloadProgress = function ( e ) {
  2043. e.type = THREE.SEA3D.Event.DOWNLOAD_PROGRESS;
  2044. this.dispatchEvent( e );
  2045. this.onProgress( e );
  2046. };
  2047. THREE.SEA3D.prototype.onComplete = function ( e ) {
  2048. e.type = THREE.SEA3D.Event.COMPLETE;
  2049. this.dispatchEvent( e );
  2050. };
  2051. THREE.SEA3D.prototype.onCompleteObject = function ( e ) {
  2052. e.type = THREE.SEA3D.Event.OBJECT_COMPLETE;
  2053. this.dispatchEvent( e );
  2054. };
  2055. THREE.SEA3D.prototype.onParseProgress = function ( e ) {
  2056. e.type = THREE.SEA3D.Event.PARSE_PROGRESS;
  2057. this.dispatchEvent( e );
  2058. };
  2059. THREE.SEA3D.prototype.onParseComplete = function ( e ) {
  2060. e.type = THREE.SEA3D.Event.PARSE_COMPLETE;
  2061. this.dispatchEvent( e );
  2062. };
  2063. THREE.SEA3D.prototype.onError = function ( e ) {
  2064. e.type = THREE.SEA3D.Event.ERROR;
  2065. this.dispatchEvent( e );
  2066. };
  2067. //
  2068. // Loader
  2069. //
  2070. THREE.SEA3D.prototype.createDomain = function () {
  2071. return this.domain = new THREE.SEA3D.Domain(
  2072. this.config.id,
  2073. this.objects = {},
  2074. this.config.container
  2075. );
  2076. };
  2077. THREE.SEA3D.prototype.clone = function ( config, onParseComplete, onParseProgress ) {
  2078. if ( ! this.file.isDone() ) throw new Error( "Previous parse is not completed." );
  2079. this.config.container = config && config.container !== undefined ? config.container : new THREE.Object3D();
  2080. if ( config ) this.loadConfig( config );
  2081. var timeLimit = this.config.timeLimit;
  2082. this.config.timeLimit = config && config.timeLimit !== undefined ? config.timeLimit : Infinity;
  2083. this.parse( onParseComplete, onParseProgress );
  2084. this.config.timeLimit = timeLimit;
  2085. return this.domain;
  2086. };
  2087. THREE.SEA3D.prototype.loadConfig = function ( config ) {
  2088. for ( var name in config ) {
  2089. this.config[ name ] = config[ name ];
  2090. }
  2091. };
  2092. THREE.SEA3D.prototype.parse = function ( onParseComplete, onParseProgress ) {
  2093. delete this.cameras;
  2094. delete this.containers;
  2095. delete this.lights;
  2096. delete this.joints;
  2097. delete this.meshes;
  2098. delete this.materials;
  2099. delete this.sprites;
  2100. delete this.sounds3d;
  2101. delete this.cubeRenderers;
  2102. delete this.sounds;
  2103. delete this.glsl;
  2104. delete this.dummy;
  2105. delete this.camera;
  2106. delete this.background;
  2107. delete this.properties;
  2108. delete this.scriptTargets;
  2109. delete this.domain;
  2110. this.createDomain();
  2111. this.setTypeRead();
  2112. this.file.onParseComplete = ( function ( e ) {
  2113. if ( this.config.manager ) this.config.manager.add( this.domain );
  2114. ( onParseComplete || this.onParseComplete ).call( this, e );
  2115. } ).bind( this );
  2116. this.file.onParseProgress = onParseProgress || this.onParseProgress;
  2117. // EXTENSIONS
  2118. var i = THREE.SEA3D.EXTENSIONS_LOADER.length;
  2119. while ( i -- ) {
  2120. var loader = THREE.SEA3D.EXTENSIONS_LOADER[ i ];
  2121. if ( loader.parse ) loader.parse.call( this );
  2122. }
  2123. this.file.parse();
  2124. return this.domain;
  2125. };
  2126. THREE.SEA3D.prototype.onHead = function ( args ) {
  2127. if ( args.sign != 'TJS' ) {
  2128. throw new Error( "Sign '" + args.sign + "' not supported! Use SEA3D Studio to publish or SEA3DLegacy.js" );
  2129. }
  2130. };
  2131. THREE.SEA3D.EXTENSIONS_LOADER = [];
  2132. THREE.SEA3D.EXTENSIONS_DOMAIN = [];
  2133. THREE.SEA3D.prototype.setTypeRead = function () {
  2134. this.file.typeRead = {};
  2135. this.file.typeRead[ SEA3D.Geometry.prototype.type ] = this.readGeometryBuffer;
  2136. this.file.typeRead[ SEA3D.Mesh.prototype.type ] = this.readMesh;
  2137. this.file.typeRead[ SEA3D.Sprite.prototype.type ] = this.readSprite;
  2138. this.file.typeRead[ SEA3D.Container3D.prototype.type ] = this.readContainer3D;
  2139. this.file.typeRead[ SEA3D.Line.prototype.type ] = this.readLine;
  2140. this.file.typeRead[ SEA3D.Material.prototype.type ] = this.readMaterial;
  2141. this.file.typeRead[ SEA3D.Camera.prototype.type ] = this.readCamera;
  2142. this.file.typeRead[ SEA3D.OrthographicCamera.prototype.type ] = this.readOrthographicCamera;
  2143. this.file.typeRead[ SEA3D.SkeletonLocal.prototype.type ] = this.readSkeletonLocal;
  2144. this.file.typeRead[ SEA3D.SkeletonAnimation.prototype.type ] = this.readSkeletonAnimation;
  2145. this.file.typeRead[ SEA3D.JointObject.prototype.type ] = this.readJointObject;
  2146. this.file.typeRead[ SEA3D.CubeMap.prototype.type ] = this.readCubeMap;
  2147. this.file.typeRead[ SEA3D.CubeRender.prototype.type ] = this.readCubeRender;
  2148. this.file.typeRead[ SEA3D.Animation.prototype.type ] =
  2149. this.file.typeRead[ SEA3D.MorphAnimation.prototype.type ] =
  2150. this.file.typeRead[ SEA3D.UVWAnimation.prototype.type ] = this.readAnimation;
  2151. this.file.typeRead[ SEA3D.SoundPoint.prototype.type ] = this.readSoundPoint;
  2152. this.file.typeRead[ SEA3D.TextureURL.prototype.type ] = this.readTextureURL;
  2153. this.file.typeRead[ SEA3D.CubeMapURL.prototype.type ] = this.readCubeMapURL;
  2154. this.file.typeRead[ SEA3D.TextureUpdate.prototype.type ] = this.readTextureUpdate;
  2155. this.file.typeRead[ SEA3D.Morph.prototype.type ] = this.readMorph;
  2156. this.file.typeRead[ SEA3D.VertexAnimation.prototype.type ] = this.readVertexAnimation;
  2157. this.file.typeRead[ SEA3D.Actions.prototype.type ] = this.readActions;
  2158. this.file.typeRead[ SEA3D.FileInfo.prototype.type ] = this.readFileInfo;
  2159. this.file.typeRead[ SEA3D.Properties.prototype.type ] = this.readProperties;
  2160. if ( this.config.dummys ) {
  2161. this.file.typeRead[ SEA3D.Dummy.prototype.type ] = this.readDummy;
  2162. }
  2163. if ( this.config.scripts ) {
  2164. this.file.typeRead[ SEA3D.ScriptURL.prototype.type ] = this.readScriptURL;
  2165. this.file.typeRead[ SEA3D.JavaScriptMethod.prototype.type ] = this.readJavaScriptMethod;
  2166. }
  2167. if ( this.config.lights ) {
  2168. this.file.typeRead[ SEA3D.PointLight.prototype.type ] = this.readPointLight;
  2169. this.file.typeRead[ SEA3D.DirectionalLight.prototype.type ] = this.readDirectionalLight;
  2170. this.file.typeRead[ SEA3D.HemisphereLight.prototype.type ] = this.readHemisphereLight;
  2171. this.file.typeRead[ SEA3D.AmbientLight.prototype.type ] = this.readAmbientLight;
  2172. }
  2173. // UNIVERSAL
  2174. this.file.typeRead[ SEA3D.JPEG.prototype.type ] =
  2175. this.file.typeRead[ SEA3D.JPEG_XR.prototype.type ] =
  2176. this.file.typeRead[ SEA3D.PNG.prototype.type ] =
  2177. this.file.typeRead[ SEA3D.GIF.prototype.type ] = this.readTexture;
  2178. this.file.typeRead[ SEA3D.MP3.prototype.type ] = this.readSound;
  2179. this.file.typeRead[ SEA3D.GLSL.prototype.type ] = this.readGLSL;
  2180. // EXTENSIONS
  2181. var i = THREE.SEA3D.EXTENSIONS_LOADER.length;
  2182. while ( i -- ) {
  2183. var loader = THREE.SEA3D.EXTENSIONS_LOADER[ i ];
  2184. if ( loader.setTypeRead ) loader.setTypeRead.call( this );
  2185. }
  2186. };
  2187. THREE.SEA3D.prototype.load = function ( data ) {
  2188. this.file = new SEA3D.File();
  2189. this.file.scope = this;
  2190. this.file.config = this.config;
  2191. this.file.onProgress = this.onLoadProgress.bind( this );
  2192. this.file.onCompleteObject = this.onCompleteObject.bind( this );
  2193. this.file.onDownloadProgress = this.onDownloadProgress.bind( this );
  2194. this.file.onParseProgress = this.onParseProgress.bind( this );
  2195. this.file.onParseComplete = this.onParseComplete.bind( this );
  2196. this.file.onError = this.onError.bind( this );
  2197. this.file.onHead = this.onHead.bind( this );
  2198. this.file.onComplete = ( function ( e ) {
  2199. if ( this.config.manager ) this.config.manager.add( this.domain );
  2200. this.onComplete.call( this, e );
  2201. } ).bind( this );
  2202. // SEA3D
  2203. this.createDomain();
  2204. this.setTypeRead();
  2205. if ( data === undefined ) return;
  2206. if ( typeof data === "string" ) this.file.load( data );
  2207. else this.file.read( data );
  2208. };