SEA3DLoader.js 74 KB

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