SEA3DLoader.js 68 KB

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