SEA3DLoader.js 60 KB

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