SEA3DLoader.js 68 KB

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