SEA3DLoader.js 75 KB

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