SEA3DLoader.js 74 KB

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