SEA3DLoader.js 59 KB

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