SEA3DLoader.js 67 KB

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