SEA3DLoader.js 58 KB

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