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