SEA3DLoader.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  1. /**
  2. * SEA3D for Three.JS
  3. * @author Sunag / http://www.sunag.com.br/
  4. */
  5. 'use strict';
  6. //
  7. // SEA3D
  8. //
  9. THREE.SEA3D = function( config ) {
  10. this.config = config || {};
  11. if ( this.config.script == undefined ) this.config.script = true;
  12. if ( this.config.autoPlay == undefined ) this.config.autoPlay = false;
  13. if ( this.config.multiplier == undefined ) this.config.multiplier = 1;
  14. if ( this.config.bounding == undefined ) this.config.bounding = true;
  15. if ( this.config.standardMaterial == undefined ) this.config.standardMaterial = true;
  16. if ( this.config.audioRolloffFactor == undefined ) this.config.audioRolloffFactor = 10;
  17. if ( this.config.timeLimit == undefined ) this.config.timeLimit = 10;
  18. if ( this.config.streaming == undefined ) this.config.streaming = true;
  19. if ( this.config.lights == undefined ) this.config.lights = true;
  20. if ( this.config.useVertexTexture == undefined ) this.config.useVertexTexture = true;
  21. };
  22. THREE.SEA3D.prototype = {
  23. constructor: THREE.SEA3D,
  24. addEventListener: THREE.EventDispatcher.prototype.addEventListener,
  25. hasEventListener: THREE.EventDispatcher.prototype.hasEventListener,
  26. removeEventListener: THREE.EventDispatcher.prototype.removeEventListener,
  27. dispatchEvent: THREE.EventDispatcher.prototype.dispatchEvent,
  28. set container ( val ) {
  29. this.config.container = val;
  30. },
  31. get container () {
  32. return this.config.container;
  33. }
  34. };
  35. //
  36. // Defaults
  37. //
  38. THREE.SEA3D.BACKGROUND_COLOR = 0x333333;
  39. THREE.SEA3D.HELPER_COLOR = 0x9AB9E5;
  40. THREE.SEA3D.RTT_SIZE = 512;
  41. //
  42. // Shader
  43. //
  44. //
  45. // Shader
  46. //
  47. THREE.SEA3D.ShaderLib = {};
  48. THREE.SEA3D.ShaderLib.replaceCode = function( src, target, replace ) {
  49. for ( var i = 0; i < target.length; i ++ ) {
  50. var tar = target[ i ],
  51. rep = replace[ i ],
  52. index = src.indexOf( tar );
  53. if ( index > - 1 ) {
  54. src = src.substring( 0, index ) + rep + src.substring( index + tar.length );
  55. }
  56. }
  57. return src;
  58. };
  59. // TODO: Emissive to Ambient Color Extension
  60. THREE.SEA3D.ShaderLib.fragStdMtl = THREE.SEA3D.ShaderLib.replaceCode( THREE.ShaderLib.phong.fragmentShader, [
  61. // Target
  62. 'vec3 outgoingLight = ( reflectedLight.directDiffuse + reflectedLight.indirectDiffuse ) * specular + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight;', // METAL
  63. 'vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight;'
  64. ], [
  65. // Replace To
  66. 'vec3 outgoingLight = ( reflectedLight.directDiffuse + reflectedLight.indirectDiffuse ) * specular + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight * specular;', // METAL
  67. 'vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight * diffuse;'
  68. ] );
  69. //
  70. // Standard Material
  71. //
  72. THREE.SEA3D.StandardMaterial = function () {
  73. THREE.MeshPhongMaterial.call( this );
  74. };
  75. THREE.SEA3D.StandardMaterial.prototype = Object.create( THREE.MeshPhongMaterial.prototype );
  76. THREE.SEA3D.StandardMaterial.prototype.constructor = THREE.SEA3D.StandardMaterial;
  77. THREE.SEA3D.StandardMaterial.prototype.lightMapUV = 1;
  78. THREE.SEA3D.StandardMaterial.prototype.__defineSetter__( "__webglShader", function( val ) {
  79. val.fragmentShader = THREE.SEA3D.ShaderLib.fragStdMtl;
  80. if ( this.lightMapUV == 0 ) {
  81. val.fragmentShader = THREE.SEA3D.ShaderLib.replaceCode( val.fragmentShader, "texture2D( aoMap, vUv2 )", "texture2D( aoMap, vUv )" );
  82. }
  83. this.__webglShader__ = val;
  84. } );
  85. THREE.SEA3D.StandardMaterial.prototype.__defineGetter__( "__webglShader", function() {
  86. return this.__webglShader__;
  87. } );
  88. THREE.SEA3D.StandardMaterial.prototype.copy = function ( source ) {
  89. THREE.MeshPhongMaterial.prototype.copy.call( this, source );
  90. return this;
  91. };
  92. THREE.SEA3D.StandardMaterial.prototype.clone = function() {
  93. return new this.constructor().copy( this );
  94. };
  95. //
  96. // Container
  97. //
  98. THREE.SEA3D.Object3D = function ( ) {
  99. THREE.Object3D.call( this );
  100. };
  101. THREE.SEA3D.Object3D.prototype = Object.create( THREE.Object3D.prototype );
  102. THREE.SEA3D.Object3D.prototype.constructor = THREE.SEA3D.Object3D;
  103. // Relative Animation Extension
  104. // TODO: It can be done with shader
  105. THREE.SEA3D.Object3D.prototype.updateAnimateMatrix = function( force ) {
  106. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  107. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  108. if ( this.parent === null ) {
  109. this.matrixWorld.copy( this.matrix );
  110. } else {
  111. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  112. }
  113. this.animateMatrix.compose( this.animatePosition, this.animateQuaternion, this.animateScale );
  114. this.matrixWorld.multiplyMatrices( this.matrixWorld, this.animateMatrix );
  115. this.matrixWorldNeedsUpdate = false;
  116. force = true;
  117. }
  118. // update children
  119. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  120. this.children[ i ].updateMatrixWorld( force );
  121. }
  122. };
  123. THREE.SEA3D.Object3D.prototype.setAnimateMatrix = function( val ) {
  124. if ( this.getAnimateMatrix() == val )
  125. return;
  126. if ( val ) {
  127. this.animateMatrix = new THREE.Matrix4();
  128. this.animatePosition = new THREE.Vector3();
  129. this.animateQuaternion = new THREE.Quaternion();
  130. this.animateScale = new THREE.Vector3( 1, 1, 1 );
  131. this.updateMatrixWorld = THREE.SEA3D.Object3D.prototype.updateAnimateMatrix;
  132. } else {
  133. delete this.animateMatrix;
  134. delete this.animatePosition;
  135. delete this.animateQuaternion;
  136. delete this.animateScale;
  137. this.updateMatrixWorld = THREE.Object3D.prototype.updateMatrixWorld;
  138. }
  139. this.matrixWorldNeedsUpdate = true;
  140. };
  141. THREE.SEA3D.Object3D.prototype.getAnimateMatrix = function() {
  142. return this.animateMatrix != undefined;
  143. };
  144. //
  145. // Dummy
  146. //
  147. THREE.SEA3D.Dummy = function ( width, height, depth ) {
  148. this.width = width != undefined ? width : 100;
  149. this.height = height != undefined ? height : 100;
  150. this.depth = depth != undefined ? depth : 100;
  151. var geo = new THREE.BoxGeometry( this.width, this.height, this.depth, 1, 1, 1 );
  152. THREE.Mesh.call( this, geo, THREE.SEA3D.Dummy.MATERIAL );
  153. };
  154. THREE.SEA3D.Dummy.prototype = Object.create( THREE.Mesh.prototype );
  155. THREE.SEA3D.Dummy.prototype.constructor = THREE.Dummy;
  156. THREE.SEA3D.Dummy.prototype.setAnimateMatrix = THREE.SEA3D.Object3D.prototype.setAnimateMatrix;
  157. THREE.SEA3D.Dummy.prototype.getAnimateMatrix = THREE.SEA3D.Object3D.prototype.getAnimateMatrix;
  158. THREE.SEA3D.Dummy.MATERIAL = new THREE.MeshBasicMaterial( { wireframe: true, color: THREE.SEA3D.HELPER_COLOR } );
  159. THREE.SEA3D.Dummy.prototype.clone = function ( object ) {
  160. return new this.constructor( this.width, this.height, this.depth ).copy( this );
  161. };
  162. THREE.SEA3D.Dummy.prototype.dispose = function () {
  163. this.geometry.dispose();
  164. };
  165. //
  166. // Mesh
  167. //
  168. THREE.SEA3D.Mesh = function ( geometry, material ) {
  169. THREE.Mesh.call( this, geometry, material );
  170. };
  171. THREE.SEA3D.Mesh.prototype = Object.create( THREE.Mesh.prototype );
  172. THREE.SEA3D.Mesh.prototype.constructor = THREE.Mesh;
  173. THREE.SEA3D.Mesh.prototype.setAnimateMatrix = THREE.SEA3D.Object3D.prototype.setAnimateMatrix;
  174. THREE.SEA3D.Mesh.prototype.getAnimateMatrix = THREE.SEA3D.Object3D.prototype.getAnimateMatrix;
  175. THREE.SEA3D.Mesh.prototype.setWeight = function( name, val ) {
  176. this.morphTargetInfluences[ this.morphTargetDictionary[ name ] ] = val;
  177. };
  178. THREE.SEA3D.Mesh.prototype.getWeight = function( name ) {
  179. return this.morphTargetInfluences[ this.morphTargetDictionary[ name ] ];
  180. };
  181. THREE.SEA3D.Mesh.prototype.copy = function ( source ) {
  182. THREE.Mesh.prototype.copy.call( this, source );
  183. if ( this.animation )
  184. this.animation = source.animation.clone( this );
  185. return this;
  186. };
  187. THREE.SEA3D.Mesh.prototype.clone = function ( object ) {
  188. return new this.constructor( this.geometry, this.material ).copy( this );
  189. };
  190. //
  191. // Skinning
  192. //
  193. THREE.SEA3D.SkinnedMesh = function ( geometry, material, useVertexTexture ) {
  194. THREE.SkinnedMesh.call( this, geometry, material, useVertexTexture );
  195. };
  196. THREE.SEA3D.SkinnedMesh.prototype = Object.create( THREE.SkinnedMesh.prototype );
  197. THREE.SEA3D.SkinnedMesh.prototype.constructor = THREE.SEA3D.SkinnedMesh;
  198. THREE.SEA3D.SkinnedMesh.prototype.setAnimateMatrix = THREE.SEA3D.Object3D.prototype.setAnimateMatrix;
  199. THREE.SEA3D.SkinnedMesh.prototype.getAnimateMatrix = THREE.SEA3D.Object3D.prototype.getAnimateMatrix;
  200. THREE.SEA3D.SkinnedMesh.prototype.setWeight = THREE.SEA3D.Mesh.prototype.setWeight;
  201. THREE.SEA3D.SkinnedMesh.prototype.getWeight = THREE.SEA3D.Mesh.prototype.getWeight;
  202. THREE.SEA3D.SkinnedMesh.prototype.isPlaying = false;
  203. THREE.SEA3D.SkinnedMesh.prototype.stop = function() {
  204. if ( this.currentAnimation ) {
  205. this.currentAnimation.stop();
  206. delete this.currentAnimation;
  207. this.isPlaying = false;
  208. }
  209. };
  210. THREE.SEA3D.SkinnedMesh.prototype.pause = function() {
  211. if ( this.isPlaying ) {
  212. this.currentAnimation.pause();
  213. this.isPlaying = false;
  214. }
  215. };
  216. THREE.SEA3D.SkinnedMesh.prototype.resume = function() {
  217. if ( ! this.isPlaying && this.currentAnimation ) {
  218. this.currentAnimation.pause();
  219. this.isPlaying = true;
  220. }
  221. };
  222. THREE.SEA3D.SkinnedMesh.prototype.play = function( name, crossfade, offset ) {
  223. var animation = this.animations[ name ];
  224. if ( ! animation )
  225. throw new Error( 'Animation "' + name + '" not found.' );
  226. animation.play( offset !== undefined ? offset : animation.currentTime, animation.weight );
  227. this.currentAnimation = animation;
  228. this.isPlaying = true;
  229. THREE.SEA3D.AnimationHandler.addCrossfade( this, crossfade !== undefined ? crossfade : 0 );
  230. };
  231. THREE.SEA3D.SkinnedMesh.prototype.setAnimations = function( animations ) {
  232. var nsIndex = animations[ 0 ].name.indexOf( "/" ) + 1;
  233. this.animations = [];
  234. this.easeSpeed = 2;
  235. this.blendMethod = SEA3D.AnimationBlendMethod.LINEAR;
  236. this.animationNamespace = animations[ 0 ].name.substring( 0, nsIndex );
  237. for ( var i = 0; i < animations.length; i ++ ) {
  238. var ns = animations[ i ].name;
  239. var name = ns.substring( nsIndex );
  240. this.animations[ i ] = new THREE.SEA3D.Animation( this, animations[ i ] );
  241. this.animations[ i ].loop = animations[ i ].repeat;
  242. this.animations[ i ].name = name;
  243. this.animations[ name ] = this.animations[ i ];
  244. }
  245. };
  246. THREE.SEA3D.SkinnedMesh.prototype.boneByName = function( name ) {
  247. var bones = this.skeleton.bones;
  248. for ( var i = 0, bl = bones.length; i < bl; i ++ ) {
  249. if ( name == bones[ i ].name )
  250. return bones[ i ];
  251. }
  252. };
  253. THREE.SEA3D.SkinnedMesh.prototype.copy = function ( source ) {
  254. THREE.SkinnedMesh.prototype.copy.call( this, source );
  255. if ( this.animation )
  256. this.animation = source.animation.clone( this );
  257. if ( this.geometry.animations ) {
  258. this.setAnimations( this.geometry.animations );
  259. }
  260. return this;
  261. };
  262. THREE.SEA3D.SkinnedMesh.prototype.clone = function ( object ) {
  263. return new this.constructor( this.geometry, this.material, this.useVertexTexture ).copy( this );
  264. };
  265. //
  266. // Vertex Animation
  267. //
  268. THREE.SEA3D.VertexAnimationMesh = function ( geometry, material, fps ) {
  269. THREE.MorphAnimMesh.call( this, geometry, material );
  270. this.fps = fps !== undefined ? fps : 30;
  271. this.animations = geometry.animations;
  272. this.isPlaying = false;
  273. this.totalTime = 0;
  274. this.playingCallback = this.updateAnimation.bind( this );
  275. };
  276. THREE.SEA3D.VertexAnimationMesh.prototype = Object.create( THREE.MorphAnimMesh.prototype );
  277. THREE.SEA3D.VertexAnimationMesh.prototype.constructor = THREE.SEA3D.VertexAnimationMesh;
  278. THREE.SEA3D.VertexAnimationMesh.prototype.setAnimateMatrix = THREE.SEA3D.Object3D.prototype.setAnimateMatrix;
  279. THREE.SEA3D.VertexAnimationMesh.prototype.getAnimateMatrix = THREE.SEA3D.Object3D.prototype.getAnimateMatrix;
  280. THREE.SEA3D.VertexAnimationMesh.prototype.play = function( name, offset ) {
  281. var animation = this.animations[ name ];
  282. this.setFrameRange( animation.start ? animation.start : 1, animation.end - 1 );
  283. this.duration = ( animation.end - animation.start ) / this.fps;
  284. this.time = offset !== undefined ? offset : this.time;
  285. this.resume();
  286. };
  287. THREE.SEA3D.VertexAnimationMesh.prototype.pause = function() {
  288. if ( this.isPlaying ) {
  289. this.isPlaying = false;
  290. THREE.SEA3D.AnimationHandler.removeUpdate( this.playingCallback );
  291. }
  292. };
  293. THREE.SEA3D.VertexAnimationMesh.prototype.resume = function() {
  294. if ( ! this.isPlaying ) {
  295. this.isPlaying = true;
  296. THREE.SEA3D.AnimationHandler.addUpdate( this.playingCallback );
  297. }
  298. };
  299. THREE.SEA3D.VertexAnimationMesh.prototype.stop = function() {
  300. this.pause();
  301. this.time = 0;
  302. };
  303. THREE.SEA3D.VertexAnimationMesh.prototype.clone = function ( object ) {
  304. return new this.constructor( this.geometry, this.material, this.fps ).copy( this );
  305. };
  306. //
  307. // Camera
  308. //
  309. THREE.SEA3D.Camera = function ( fov, aspect, near, far ) {
  310. THREE.PerspectiveCamera.call( this, fov, aspect, near, far );
  311. };
  312. THREE.SEA3D.Camera.prototype = Object.create( THREE.PerspectiveCamera.prototype );
  313. THREE.SEA3D.Camera.prototype.constructor = THREE.SEA3D.Camera;
  314. THREE.SEA3D.Camera.prototype.setAnimateMatrix = THREE.SEA3D.Object3D.prototype.setAnimateMatrix;
  315. THREE.SEA3D.Camera.prototype.getAnimateMatrix = THREE.SEA3D.Object3D.prototype.getAnimateMatrix;
  316. THREE.SEA3D.Camera.prototype.copy = function ( source ) {
  317. THREE.PerspectiveCamera.prototype.copy.call( this, source );
  318. return this;
  319. };
  320. //
  321. // Animation Update
  322. //
  323. THREE.SEA3D.AnimationHandler = {
  324. crossfade : [],
  325. updates : [],
  326. update : function( dt ) {
  327. var i, j, cf = THREE.SEA3D.AnimationHandler.crossfade, ups = THREE.SEA3D.AnimationHandler.updates;
  328. // crossfade
  329. i = 0;
  330. while ( i < cf.length ) {
  331. var mesh = cf[ i ],
  332. len = mesh.animations.length,
  333. weight = 1,
  334. delta = Math.abs( dt ) / mesh.crossfade;
  335. if ( mesh.blendMethod === SEA3D.AnimationBlendMethod.EASING ) {
  336. delta *= mesh.easeSpeed;
  337. }
  338. while ( len ) {
  339. var state = mesh.animations[ -- len ];
  340. if ( state.weight > 0 && state != mesh.currentAnimation ) {
  341. if ( mesh.blendMethod === SEA3D.AnimationBlendMethod.LINEAR ) {
  342. state.weight -= delta;
  343. }
  344. else if ( mesh.blendMethod === SEA3D.AnimationBlendMethod.EASING ) {
  345. state.weight -= state.weight * delta;
  346. }
  347. if ( state.weight < 0 ) state.weight = 0;
  348. weight -= state.weight;
  349. }
  350. }
  351. if ( weight < 0 ) weight = 0;
  352. mesh.currentAnimation.weight = weight;
  353. if ( weight == 1 ) {
  354. cf.splice( i, 1 );
  355. delete mesh.crossfade;
  356. if ( mesh.onCrossfadeComplete ) mesh.onCrossfadeComplete( mesh );
  357. }
  358. else ++ i;
  359. }
  360. // updates
  361. i = 0;
  362. while ( i < ups.length ) {
  363. ups[ i ++ ]( dt );
  364. }
  365. SEA3D.AnimationHandler.update( dt );
  366. },
  367. addCrossfade : function( mesh, crossfade ) {
  368. var fadelist = THREE.SEA3D.AnimationHandler.crossfade;
  369. if ( crossfade > 0 )
  370. {
  371. if ( ! mesh.crossfade ) fadelist.push( mesh );
  372. mesh.crossfade = crossfade;
  373. }
  374. else
  375. {
  376. var len = mesh.animations.length;
  377. while ( len ) {
  378. mesh.animations[ -- len ].weight = 0;
  379. }
  380. if ( mesh.crossfade ) {
  381. fadelist.splice( fadelist.indexOf( mesh ), 1 );
  382. delete mesh.crossfade;
  383. if ( mesh.onCrossfadeComplete ) mesh.onCrossfadeComplete( mesh );
  384. }
  385. mesh.currentAnimation.weight = 1;
  386. }
  387. },
  388. addUpdate : function( func ) {
  389. THREE.SEA3D.AnimationHandler.updates.push( func );
  390. },
  391. removeUpdate : function( func ) {
  392. var index = THREE.SEA3D.AnimationHandler.updates.indexOf( func );
  393. if ( index !== - 1 ) {
  394. THREE.SEA3D.AnimationHandler.updates.splice( THREE.SEA3D.AnimationHandler.updates.indexOf( func ), 1 );
  395. }
  396. }
  397. };
  398. //
  399. // Animation Event
  400. //
  401. THREE.SEA3D.Animation = function ( root, data ) {
  402. THREE.Animation.call( this, root, data );
  403. };
  404. THREE.SEA3D.Animation.prototype = Object.create( THREE.Animation.prototype );
  405. THREE.SEA3D.Animation.prototype.constructor = THREE.SEA3D.Animation;
  406. THREE.SEA3D.Animation.prototype.stop = function() {
  407. if ( this.onComplete ) this.onComplete( this );
  408. THREE.Animation.prototype.stop.call( this );
  409. };
  410. THREE.SEA3D.Animation.prototype.reset = function() {
  411. if ( this.onReset ) this.onReset( this );
  412. THREE.Animation.prototype.reset.call( this );
  413. };
  414. //
  415. // Config
  416. //
  417. THREE.SEA3D.MTXBUF = new THREE.Matrix4();
  418. THREE.SEA3D.VECBUF = new THREE.Vector3();
  419. THREE.SEA3D.QUABUF = new THREE.Quaternion();
  420. THREE.SEA3D.prototype.setShadowMap = function( light, opacity ) {
  421. light.shadowMapWidth =
  422. light.shadowMapHeight = 2048;
  423. light.castShadow = true;
  424. light.shadowDarkness = opacity == undefined ? 1 : opacity;
  425. };
  426. //
  427. // Output
  428. //
  429. THREE.SEA3D.prototype.getMesh = function( name ) {
  430. return this.objects[ "m3d/" + name ];
  431. };
  432. THREE.SEA3D.prototype.getDummy = function( name ) {
  433. return this.objects[ "dmy/" + name ];
  434. };
  435. THREE.SEA3D.prototype.getLine = function( name ) {
  436. return this.objects[ "line/" + name ];
  437. };
  438. THREE.SEA3D.prototype.getSound3D = function( name ) {
  439. return this.objects[ "sn3d/" + name ];
  440. };
  441. THREE.SEA3D.prototype.getMaterial = function( name ) {
  442. return this.objects[ "mat/" + name ];
  443. };
  444. THREE.SEA3D.prototype.getLight = function( name ) {
  445. return this.objects[ "lht/" + name ];
  446. };
  447. THREE.SEA3D.prototype.getGLSL = function( name ) {
  448. return this.objects[ "glsl/" + name ];
  449. };
  450. THREE.SEA3D.prototype.getCamera = function( name ) {
  451. return this.objects[ "cam/" + name ];
  452. };
  453. THREE.SEA3D.prototype.getTexture = function( name ) {
  454. return this.objects[ "tex/" + name ];
  455. };
  456. THREE.SEA3D.prototype.getCubeMap = function( name ) {
  457. return this.objects[ "cmap/" + name ];
  458. };
  459. THREE.SEA3D.prototype.getJointObject = function( name ) {
  460. return this.objects[ "jnt/" + name ];
  461. };
  462. THREE.SEA3D.prototype.getContainer3D = function( name ) {
  463. return this.objects[ "c3d/" + name ];
  464. };
  465. THREE.SEA3D.prototype.getSprite = function( name ) {
  466. return this.objects[ "m2d/" + name ];
  467. };
  468. THREE.SEA3D.prototype.getProperty = function( name ) {
  469. return this.objects[ "prop/" + name ];
  470. };
  471. //
  472. // Utils
  473. //
  474. THREE.SEA3D.prototype.isPowerOfTwo = function( num ) {
  475. return num ? ( ( num & - num ) == num ) : false;
  476. };
  477. THREE.SEA3D.prototype.nearestPowerOfTwo = function( num ) {
  478. return Math.pow( 2, Math.round( Math.log( num ) / Math.LN2 ) );
  479. };
  480. THREE.SEA3D.prototype.updateTransform = function( obj3d, sea ) {
  481. var mtx = THREE.SEA3D.MTXBUF, vec = THREE.SEA3D.VECBUF;
  482. if ( sea.transform ) mtx.elements.set( sea.transform );
  483. else mtx.makeTranslation( sea.position.x, sea.position.y, sea.position.z );
  484. // matrix
  485. obj3d.position.setFromMatrixPosition( mtx );
  486. obj3d.scale.setFromMatrixScale( mtx );
  487. // ignore rotation scale
  488. mtx.scale( vec.set( 1 / obj3d.scale.x, 1 / obj3d.scale.y, 1 / obj3d.scale.z ) );
  489. obj3d.rotation.setFromRotationMatrix( mtx );
  490. // optimize if is static
  491. if ( sea.isStatic ) {
  492. obj3d.updateMatrixWorld();
  493. obj3d.matrixAutoUpdate = false;
  494. }
  495. };
  496. THREE.SEA3D.prototype.toVector3 = function( data ) {
  497. return new THREE.Vector3( data.x, data.y, data.z );
  498. };
  499. THREE.SEA3D.prototype.scaleColor = function( color, scale ) {
  500. var r = ( color >> 16 ) * scale;
  501. var g = ( color >> 8 & 0xFF ) * scale;
  502. var b = ( color & 0xFF ) * scale;
  503. return ( r << 16 | g << 8 | b );
  504. };
  505. THREE.SEA3D.prototype.updateScene = function() {
  506. if ( this.materials != undefined ) {
  507. for ( var i = 0, l = this.materials.length; i < l; ++ i ) {
  508. this.materials[ i ].needsUpdate = true;
  509. }
  510. }
  511. };
  512. THREE.SEA3D.prototype.addSceneObject = function( sea ) {
  513. var obj3d = sea.tag;
  514. obj3d.userData = sea.properties;
  515. if ( this.config.script && sea.scripts ) {
  516. this.runJSMList( obj3d, sea.scripts );
  517. }
  518. if ( sea.parent )
  519. sea.parent.tag.add( obj3d );
  520. else if ( this.config.container )
  521. this.config.container.add( obj3d );
  522. obj3d.visible = sea.visible;
  523. };
  524. THREE.SEA3D.prototype.createObjectURL = function( raw, mime ) {
  525. return ( window.URL || window.webkitURL ).createObjectURL( new Blob( [ raw ], { type: mime } ) );
  526. };
  527. THREE.SEA3D.prototype.bufferToTexture = function( raw ) {
  528. return this.createObjectURL( raw, "image" );
  529. };
  530. THREE.SEA3D.prototype.bufferToSound = function( raw ) {
  531. return this.createObjectURL( raw, "audio" );
  532. };
  533. THREE.SEA3D.prototype.applyDefaultAnimation = function( sea, animatorClass ) {
  534. var obj = sea.tag;
  535. for ( var i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i ++ ) {
  536. var anm = sea.animations[ i ];
  537. switch ( anm.tag.type ) {
  538. case SEA3D.Animation.prototype.type:
  539. obj.animation = new animatorClass( obj, anm.tag.tag );
  540. obj.animation.setRelative( anm.relative );
  541. if ( this.config.autoPlay ) {
  542. obj.animation.play( obj.animation.getStateNameByIndex( 0 ) );
  543. }
  544. return obj.animation;
  545. break;
  546. }
  547. }
  548. };
  549. //
  550. // Animation
  551. //
  552. THREE.SEA3D.prototype.readAnimation = function( sea ) {
  553. var anmSet = new SEA3D.AnimationSet();
  554. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  555. var seq = sea.sequence[ i ],
  556. node = new SEA3D.AnimationNode( seq.name, sea.frameRate, seq.count, seq.repeat, seq.intrpl );
  557. for ( var j = 0; j < sea.dataList.length; j ++ ) {
  558. var anmData = sea.dataList[ j ];
  559. node.addData( new SEA3D.AnimationData( anmData.kind, anmData.type, anmData.data, seq.start * anmData.blockSize ) );
  560. }
  561. anmSet.addAnimation( node );
  562. }
  563. this.domain.animationSets = this.animationSets = this.animationSets || [];
  564. this.animationSets.push( this.objects[ sea.name + '.#anm' ] = sea.tag = anmSet );
  565. };
  566. //
  567. // Object3D Animator
  568. //
  569. THREE.SEA3D.Object3DAnimator = function( object3d, animationSet ) {
  570. SEA3D.AnimationHandler.call( this, animationSet );
  571. this.object3d = object3d;
  572. };
  573. THREE.SEA3D.Object3DAnimator.prototype = Object.create( SEA3D.AnimationHandler.prototype );
  574. THREE.SEA3D.Object3DAnimator.prototype.constructor = THREE.SEA3D.Object3DAnimator;
  575. THREE.SEA3D.Object3DAnimator.prototype.stop = function() {
  576. if ( this.relative ) {
  577. this.object3d.animatePosition = new THREE.Vector3();
  578. this.object3d.animateQuaternion = new THREE.Quaternion();
  579. this.object3d.animateScale = new THREE.Vector3( 1, 1, 1 );
  580. }
  581. SEA3D.AnimationHandler.prototype.stop.call( this );
  582. };
  583. THREE.SEA3D.Object3DAnimator.prototype.setRelative = function( val ) {
  584. this.object3d.setAnimateMatrix( this.relative = val );
  585. };
  586. THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame = function( frame, kind ) {
  587. if ( this.relative ) {
  588. switch ( kind ) {
  589. case SEA3D.Animation.POSITION:
  590. var v = frame.toVector();
  591. this.object3d.animatePosition.set( v.x, v.y, v.z );
  592. break;
  593. case SEA3D.Animation.ROTATION:
  594. var v = frame.toVector();
  595. this.object3d.animateQuaternion.set( v.x, v.y, v.z, v.w );
  596. break;
  597. case SEA3D.Animation.SCALE:
  598. var v = frame.toVector();
  599. this.object3d.animateScale.set( v.x, v.y, v.z );
  600. break;
  601. }
  602. this.object3d.matrixWorldNeedsUpdate = true;
  603. } else {
  604. switch ( kind ) {
  605. case SEA3D.Animation.POSITION:
  606. var v = frame.toVector();
  607. this.object3d.position.set( v.x, v.y, v.z );
  608. break;
  609. case SEA3D.Animation.ROTATION:
  610. var v = frame.toVector();
  611. this.object3d.quaternion.set( v.x, v.y, v.z, v.w );
  612. break;
  613. case SEA3D.Animation.SCALE:
  614. var v = frame.toVector();
  615. this.object3d.scale.set( v.x, v.y, v.z );
  616. break;
  617. }
  618. }
  619. };
  620. //
  621. // Camera Animator
  622. //
  623. THREE.SEA3D.CameraAnimator = function( object3d, animationSet ) {
  624. THREE.SEA3D.Object3DAnimator.call( this, object3d, animationSet );
  625. };
  626. THREE.SEA3D.CameraAnimator.prototype = Object.create( THREE.SEA3D.Object3DAnimator.prototype );
  627. THREE.SEA3D.CameraAnimator.prototype.constructor = THREE.SEA3D.Object3DAnimator;
  628. THREE.SEA3D.CameraAnimator.prototype.updateAnimationFrame = function( frame, kind ) {
  629. switch ( kind ) {
  630. case SEA3D.Animation.FOV:
  631. this.object3d.fov = frame.getX();
  632. break;
  633. default:
  634. THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame.call( this, frame, kind );
  635. break;
  636. }
  637. };
  638. //
  639. // Light Animator
  640. //
  641. THREE.SEA3D.LightAnimator = function( object3d, animationSet ) {
  642. THREE.SEA3D.Object3DAnimator.call( this, object3d, animationSet );
  643. };
  644. THREE.SEA3D.LightAnimator.prototype = Object.create( THREE.SEA3D.Object3DAnimator.prototype );
  645. THREE.SEA3D.LightAnimator.prototype.constructor = THREE.SEA3D.Object3DAnimator;
  646. THREE.SEA3D.LightAnimator.prototype.updateAnimationFrame = function( frame, kind ) {
  647. switch ( kind ) {
  648. case SEA3D.Animation.COLOR:
  649. this.object3d.color.setHex( frame.getX() );
  650. break;
  651. case SEA3D.Animation.MULTIPLIER:
  652. this.object3d.intensity = frame.getX();
  653. break;
  654. default:
  655. THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame.call( this, frame, kind );
  656. break;
  657. }
  658. };
  659. //
  660. // Geometry
  661. //
  662. THREE.SEA3D.prototype.readGeometryBuffer = function( sea ) {
  663. var geo = new THREE.BufferGeometry();
  664. for ( var i = 0; i < sea.groups.length; i ++ ) {
  665. var g = sea.groups[ i ];
  666. geo.addGroup( g.start, g.count, i );
  667. }
  668. geo.setIndex( new THREE.BufferAttribute( sea.indexes, 1 ) );
  669. geo.addAttribute( 'position', new THREE.BufferAttribute( sea.vertex, 3 ) );
  670. if ( sea.uv ) {
  671. geo.addAttribute( 'uv', new THREE.BufferAttribute( sea.uv[ 0 ], 2 ) );
  672. if ( sea.uv.length > 1 ) geo.addAttribute( 'uv2', new THREE.BufferAttribute( sea.uv[ 1 ], 2 ) );
  673. }
  674. if ( sea.normal ) geo.addAttribute( 'normal', new THREE.BufferAttribute( sea.normal, 3 ) );
  675. else geo.computeVertexNormals();
  676. if ( sea.tangent4 ) geo.addAttribute( 'tangent', new THREE.BufferAttribute( sea.tangent4, 4 ) );
  677. if ( sea.color ) geo.addAttribute( 'color', new THREE.BufferAttribute( sea.color[ 0 ], sea.numColor ) );
  678. if ( sea.joint ) {
  679. geo.addAttribute( 'skinIndex', new THREE.Float32Attribute( sea.joint, sea.jointPerVertex ) );
  680. geo.addAttribute( 'skinWeight', new THREE.Float32Attribute( sea.weight, sea.jointPerVertex ) );
  681. }
  682. if ( this.config.bounding ) {
  683. geo.computeBoundingBox();
  684. geo.computeBoundingSphere();
  685. }
  686. geo.name = sea.name;
  687. this.domain.geometries = this.geometries = this.geometries || [];
  688. this.geometries.push( this.objects[ "geo/" + sea.name ] = sea.tag = geo );
  689. };
  690. //
  691. // Dummy
  692. //
  693. THREE.SEA3D.prototype.readDummy = function( sea ) {
  694. var dummy = new THREE.SEA3D.Dummy( sea.width, sea.height, sea.depth );
  695. dummy.name = sea.name;
  696. this.domain.dummys = this.dummys = this.dummys || [];
  697. this.dummys.push( this.objects[ "dmy/" + sea.name ] = sea.tag = dummy );
  698. this.addSceneObject( sea );
  699. this.updateTransform( dummy, sea );
  700. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  701. };
  702. //
  703. // Line
  704. //
  705. THREE.SEA3D.prototype.readLine = function( sea ) {
  706. var geo = new THREE.BufferGeometry();
  707. if ( sea.closed )
  708. sea.vertex.push( sea.vertex[ 0 ], sea.vertex[ 1 ], sea.vertex[ 2 ] );
  709. geo.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( sea.vertex ), 3 ) );
  710. var line = new THREE.Line( geo, new THREE.LineBasicMaterial( { color: THREE.SEA3D.HELPER_COLOR, linewidth: 3 } ) );
  711. line.name = sea.name;
  712. this.lines = this.lines || [];
  713. this.lines.push( this.objects[ "line/" + sea.name ] = sea.tag = line );
  714. this.addSceneObject( sea );
  715. this.updateTransform( line, sea );
  716. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  717. };
  718. //
  719. // Container3D
  720. //
  721. THREE.SEA3D.prototype.readContainer3D = function( sea ) {
  722. var container = new THREE.SEA3D.Object3D();
  723. this.domain.containers = this.containers = this.containers || [];
  724. this.containers.push( this.objects[ "c3d/" + sea.name ] = sea.tag = container );
  725. this.addSceneObject( sea );
  726. this.updateTransform( container, sea );
  727. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  728. };
  729. //
  730. // Mesh2D | Sprite
  731. //
  732. THREE.SEA3D.prototype.readMesh2D = function( sea ) {
  733. var material;
  734. if ( sea.material ) {
  735. if ( ! sea.material.tag.sprite ) {
  736. material = sea.material.tag.sprite = new THREE.SpriteMaterial();
  737. material.map = sea.material.tag.map;
  738. material.map.flipY = true;
  739. material.color = sea.material.tag.emissive;
  740. material.opacity = sea.material.tag.opacity;
  741. material.blending = sea.material.tag.blending;
  742. }
  743. else material = sea.material.tag.sprite;
  744. }
  745. var sprite = new THREE.Sprite( material );
  746. sprite.name = sea.name;
  747. this.domain.sprites = this.sprites = this.sprites || [];
  748. this.sprites.push( this.objects[ "m2d/" + sea.name ] = sea.tag = sprite );
  749. this.addSceneObject( sea );
  750. this.updateTransform( sprite, sea );
  751. sprite.scale.set( sea.width, sea.height, 1 );
  752. };
  753. //
  754. // Mesh
  755. //
  756. THREE.SEA3D.prototype.readMesh = function( sea ) {
  757. var i, count, geo = sea.geometry.tag,
  758. mesh, mat, skeleton, skeletonAnimation, vertexAnimation, morpher;
  759. for ( i = 0, count = sea.modifiers ? sea.modifiers.length : 0; i < count; i ++ ) {
  760. var mod = sea.modifiers[ i ];
  761. switch ( mod.type ) {
  762. case SEA3D.Skeleton.prototype.type:
  763. case SEA3D.SkeletonLocal.prototype.type:
  764. skeleton = mod;
  765. geo.bones = skeleton.tag;
  766. break;
  767. case SEA3D.Morph.prototype.type:
  768. morpher = mod;
  769. geo.morphAttributes = morpher.tag.attribs;
  770. geo.morphTargets = morpher.tag.targets;
  771. break;
  772. }
  773. }
  774. for ( i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i ++ ) {
  775. var anm = sea.animations[ i ];
  776. switch ( anm.tag.type ) {
  777. case SEA3D.SkeletonAnimation.prototype.type:
  778. skeletonAnimation = anm.tag;
  779. geo.animations = this.getSkeletonAnimation( skeletonAnimation, skeleton );
  780. break;
  781. case SEA3D.VertexAnimation.prototype.type:
  782. vertexAnimation = anm.tag;
  783. geo.morphAttributes = vertexAnimation.tag.attribs;
  784. geo.morphTargets = vertexAnimation.tag.targets;
  785. geo.animations = vertexAnimation.tag.animations;
  786. break;
  787. }
  788. }
  789. var uMorph = morpher != undefined || vertexAnimation != undefined,
  790. uMorphNormal =
  791. ( morpher && morpher.tag.attribs.normal != undefined ) ||
  792. ( vertexAnimation && vertexAnimation.tag.attribs.normal != undefined );
  793. if ( sea.material ) {
  794. if ( sea.material.length > 1 ) {
  795. var mats = [];
  796. for ( i = 0; i < sea.material.length; i ++ ) {
  797. mats[ i ] = sea.material[ i ].tag;
  798. mats[ i ].skinning = skeleton != undefined;
  799. mats[ i ].morphTargets = uMorph;
  800. mats[ i ].morphNormals = uMorphNormal;
  801. mats[ i ].vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  802. }
  803. mat = new THREE.MultiMaterial( mats );
  804. } else {
  805. mat = sea.material[ 0 ].tag;
  806. mat.skinning = skeleton != undefined;
  807. mat.morphTargets = uMorph;
  808. mat.morphNormals = uMorphNormal;
  809. mat.vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  810. }
  811. }
  812. if ( skeleton ) {
  813. mesh = new THREE.SEA3D.SkinnedMesh( geo, mat, this.config.useVertexTexture );
  814. if ( skeletonAnimation ) {
  815. mesh.setAnimations( geo.animations );
  816. if ( this.config.autoPlay ) {
  817. mesh.play( mesh.animations[ 0 ].name );
  818. }
  819. }
  820. } else if ( vertexAnimation ) {
  821. mesh = new THREE.SEA3D.VertexAnimationMesh( geo, mat, vertexAnimation.frameRate );
  822. if ( this.config.autoPlay ) {
  823. mesh.play( mesh.animations[ 0 ].name );
  824. }
  825. } else {
  826. mesh = new THREE.SEA3D.Mesh( geo, mat );
  827. }
  828. mesh.name = sea.name;
  829. mesh.castShadow = sea.castShadows;
  830. mesh.receiveShadow = sea.material ? sea.material[ 0 ].receiveShadows : true;
  831. this.domain.meshes = this.meshes = this.meshes || [];
  832. this.meshes.push( this.objects[ "m3d/" + sea.name ] = sea.tag = mesh );
  833. this.addSceneObject( sea );
  834. this.updateTransform( mesh, sea );
  835. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  836. };
  837. //
  838. // Sound Point
  839. //
  840. THREE.SEA3D.prototype.readSoundPoint = function( sea ) {
  841. if ( ! this.audioListener ) {
  842. this.audioListener = new THREE.AudioListener();
  843. if ( this.config.container ) {
  844. this.config.container.add( this.audioListener );
  845. }
  846. }
  847. var sound3d = new THREE.PositionalAudio( this.audioListener );
  848. sound3d.load( sea.sound.tag );
  849. sound3d.autoplay = sea.autoPlay;
  850. sound3d.setLoop( sea.autoPlay );
  851. sound3d.setVolume( sea.volume );
  852. sound3d.setRefDistance( sea.distance );
  853. sound3d.setRolloffFactor( this.config.audioRolloffFactor );
  854. sound3d.name = sea.name;
  855. this.domain.sounds3d = this.sounds3d = this.sounds3d || [];
  856. this.sounds3d.push( this.objects[ "sn3d/" + sea.name ] = sea.tag = sound3d );
  857. this.addSceneObject( sea );
  858. this.updateTransform( sound3d, sea );
  859. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  860. };
  861. //
  862. // Cube Render
  863. //
  864. THREE.SEA3D.prototype.readCubeRender = function( sea ) {
  865. var cube = new THREE.CubeCamera( 0.1, 5000, THREE.SEA3D.RTT_SIZE );
  866. cube.renderTarget.cubeCamera = cube;
  867. this.domain.cubeRenderers = this.cubeRenderers = this.cubeRenderers || [];
  868. this.cubeRenderers.push( this.objects[ "rttc/" + sea.name ] = sea.tag = cube.renderTarget );
  869. this.addSceneObject( sea );
  870. this.updateTransform( cube, sea );
  871. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  872. };
  873. //
  874. // Images (WDP, JPEG, PNG and GIF)
  875. //
  876. THREE.SEA3D.prototype.readImage = function( sea ) {
  877. var image = new Image(), texture = new THREE.Texture();
  878. image.src = this.bufferToTexture( sea.data.buffer );
  879. texture.name = sea.name;
  880. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  881. texture.flipY = false;
  882. texture.image = image;
  883. texture.needsUpdate = true;
  884. this.domain.textures = this.textures = this.textures || [];
  885. this.textures.push( this.objects[ "tex/" + sea.name ] = sea.tag = texture );
  886. };
  887. //
  888. // Cube Map
  889. //
  890. THREE.SEA3D.prototype.readCubeMap = function( sea ) {
  891. var images = [],
  892. texture = new THREE.Texture();
  893. // xyz(- / +) to xyz(+ / -) sequence
  894. var faces = [];
  895. faces[ 0 ] = sea.faces[ 1 ];
  896. faces[ 1 ] = sea.faces[ 0 ];
  897. faces[ 2 ] = sea.faces[ 3 ];
  898. faces[ 3 ] = sea.faces[ 2 ];
  899. faces[ 4 ] = sea.faces[ 5 ];
  900. faces[ 5 ] = sea.faces[ 4 ];
  901. images.loadedCount = 0;
  902. texture.name = sea.name;
  903. texture.image = images;
  904. texture.flipY = false;
  905. for ( var i = 0, il = faces.length; i < il; ++ i ) {
  906. var cubeImage = new Image();
  907. images[ i ] = cubeImage;
  908. cubeImage.onload = function () {
  909. if ( ++ images.loadedCount == 6 ) {
  910. texture.needsUpdate = true;
  911. }
  912. }
  913. cubeImage.src = this.bufferToTexture( faces[ i ].buffer );
  914. }
  915. this.domain.cubemaps = this.cubemaps = this.cubemaps || [];
  916. this.cubemaps.push( this.objects[ "cmap/" + sea.name ] = sea.tag = texture );
  917. };
  918. //
  919. // Sound (MP3, OGG)
  920. //
  921. THREE.SEA3D.prototype.readSound = function( sea ) {
  922. var sound = this.bufferToSound( sea.data.buffer );
  923. this.domain.sounds = this.sounds = this.sounds || [];
  924. this.sounds.push( this.objects[ "snd/" + sea.name ] = sea.tag = sound );
  925. };
  926. //
  927. // Texture URL
  928. //
  929. THREE.SEA3D.prototype.readTextureURL = function( sea ) {
  930. var texture = new THREE.TextureLoader().load( sea.url );
  931. texture.name = sea.name;
  932. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  933. texture.flipY = false;
  934. this.domain.textures = this.textures = this.textures || [];
  935. this.textures.push( this.objects[ "tex/" + sea.name ] = sea.tag = texture );
  936. };
  937. //
  938. // Java Script
  939. //
  940. THREE.SEA3D.SCRIPT = new SEA3D.ScriptManager();
  941. THREE.SEA3D.SCRIPT.dispatchUpdate = function( delta ) {
  942. this.dispatchEvent( {
  943. type : "update",
  944. delta : delta
  945. } );
  946. };
  947. THREE.SEA3D.Domain = function( id, objects, container, extensions ) {
  948. SEA3D.Domain.call( this, id );
  949. this.objects = objects;
  950. this.container = container;
  951. this.extensions = extensions || [];
  952. };
  953. THREE.SEA3D.Domain.prototype = Object.create( SEA3D.Domain.prototype );
  954. THREE.SEA3D.Domain.prototype.constructor = THREE.SEA3D.Domain;
  955. THREE.SEA3D.Domain.prototype.disposeExtensions = function() {
  956. extensions = extensions.concat();
  957. var i = list.length;
  958. while ( i -- ) list[ i ].dispose();
  959. };
  960. THREE.SEA3D.Domain.prototype.disposeList = function( list ) {
  961. if ( ! list || ! list.length ) return;
  962. list = list.concat();
  963. var i = list.length;
  964. while ( i -- ) list[ i ].dispose();
  965. };
  966. THREE.SEA3D.Domain.prototype.dispose = function() {
  967. SEA3D.Domain.prototype.dispose.call( this );
  968. while ( this.container.children.length ) {
  969. this.container.remove( this.container.children[ 0 ] );
  970. }
  971. var i = this.extensions.length;
  972. while ( i -- ) this.extensions[ i ].dispose.call( this );
  973. this.disposeList( this.materials );
  974. this.disposeList( this.dummys );
  975. };
  976. SEA3D.Domain.prototype.getMesh = THREE.SEA3D.prototype.getMesh;
  977. SEA3D.Domain.prototype.getDummy = THREE.SEA3D.prototype.getDummy;
  978. SEA3D.Domain.prototype.getLine = THREE.SEA3D.prototype.getLine;
  979. SEA3D.Domain.prototype.getSound3D = THREE.SEA3D.prototype.getSound3D;
  980. SEA3D.Domain.prototype.getMaterial = THREE.SEA3D.prototype.getMaterial;
  981. SEA3D.Domain.prototype.getLight = THREE.SEA3D.prototype.getLight;
  982. SEA3D.Domain.prototype.getGLSL = THREE.SEA3D.prototype.getGLSL;
  983. SEA3D.Domain.prototype.getCamera = THREE.SEA3D.prototype.getCamera;
  984. SEA3D.Domain.prototype.getTexture = THREE.SEA3D.prototype.getTexture;
  985. SEA3D.Domain.prototype.getCubeMap = THREE.SEA3D.prototype.getCubeMap;
  986. SEA3D.Domain.prototype.getJointObject = THREE.SEA3D.prototype.getJointObject;
  987. SEA3D.Domain.prototype.getContainer3D = THREE.SEA3D.prototype.getContainer3D;
  988. SEA3D.Domain.prototype.getSprite = THREE.SEA3D.prototype.getSprite;
  989. SEA3D.Domain.prototype.getProperty = THREE.SEA3D.prototype.getProperty;
  990. THREE.SEA3D.DomainManager = function( autoDisposeRootDomain ) {
  991. SEA3D.DomainManager.call( this, autoDisposeRootDomain );
  992. };
  993. THREE.SEA3D.DomainManager.prototype = Object.create( SEA3D.DomainManager.prototype );
  994. THREE.SEA3D.DomainManager.prototype.constructor = THREE.SEA3D.DomainManager;
  995. THREE.SEA3D.DomainManager.prototype.add = function( domain ) {
  996. SEA3D.DomainManager.prototype.add.call( this, domain );
  997. this.textures = this.textures || domain.textures;
  998. this.cubemaps = this.cubemaps || domain.cubemaps;
  999. this.geometries = this.geometries || domain.geometries;
  1000. };
  1001. THREE.SEA3D.DomainManager.prototype.disposeList = THREE.SEA3D.Domain.prototype.disposeList;
  1002. THREE.SEA3D.DomainManager.prototype.dispose = function() {
  1003. SEA3D.DomainManager.prototype.dispose.call( this );
  1004. this.disposeList( this.textures );
  1005. this.disposeList( this.cubemaps );
  1006. this.disposeList( this.geometries );
  1007. };
  1008. //
  1009. // Runtime
  1010. //
  1011. THREE.SEA3D.prototype.runJSMList = function( target, scripts, root ) {
  1012. for ( var i = 0; i < scripts.length; i ++ ) {
  1013. var script = scripts[ i ];
  1014. if ( script.tag.type == SEA3D.JavaScriptMethod.prototype.type ) {
  1015. this.runJSM( target, script, root );
  1016. }
  1017. }
  1018. };
  1019. THREE.SEA3D.prototype.runJSM = function( target, script, root ) {
  1020. if ( target.local == undefined ) target.local = {};
  1021. var include = {
  1022. print : this.domain.print,
  1023. watch : this.domain.watch,
  1024. sea3d : this.domain,
  1025. scene : this.config.container,
  1026. source : new SEA3D.Script( this.domain, root == true )
  1027. };
  1028. Object.freeze( include.source );
  1029. THREE.SEA3D.SCRIPT.add( include.source );
  1030. try {
  1031. this.script[ script.method ] (
  1032. include,
  1033. this.domain.getReference,
  1034. this.domain.global,
  1035. target.local,
  1036. target,
  1037. script.params
  1038. );
  1039. }
  1040. catch ( e ) {
  1041. console.error( 'SEA3D JavaScript: Error running method "' + script.method + '".' );
  1042. console.error( e );
  1043. }
  1044. };
  1045. THREE.SEA3D.prototype.readJavaScriptMethod = function( sea ) {
  1046. try {
  1047. var src =
  1048. '(function() {\n' +
  1049. 'var $METHOD = {}\n';
  1050. var declare =
  1051. 'function($INC, $REF, global, local, $his, $PARAM) {\n' +
  1052. 'var watch = $INC["watch"],\n' +
  1053. 'scene = $INC["scene"],\n' +
  1054. 'sea3d = $INC["sea3d"],\n' +
  1055. 'print = $INC["print"];\n';
  1056. declare +=
  1057. 'var $SRC = $INC["source"],\n' +
  1058. 'addEvent = $SRC.addEvent.bind( $SRC ),\n' +
  1059. 'hasEvent = $SRC.hasEvent.bind( $SRC ),\n' +
  1060. 'dispatchEvent = $SRC.dispatchEvent.bind( $SRC ),\n' +
  1061. 'removeEvent = $SRC.removeEvent.bind( $SRC ),\n' +
  1062. 'dispose = $SRC.dispose.bind( $SRC );\n'
  1063. for ( var name in sea.methods ) {
  1064. src += '$METHOD["' + name + '"] = ' + declare + sea.methods[ name ].src + '}\n';
  1065. }
  1066. src += 'return $METHOD; })'
  1067. this.script = eval( src )();
  1068. }
  1069. catch ( e ) {
  1070. console.error( 'SEA3D JavaScriptMethod: Error running "' + sea.name + '".' );
  1071. console.error( e );
  1072. }
  1073. };
  1074. //
  1075. // GLSL
  1076. //
  1077. THREE.SEA3D.prototype.readGLSL = function( sea ) {
  1078. this.domain.glsl = this.glsl = this.glsl || [];
  1079. this.glsl.push( this.objects[ "glsl/" + sea.name ] = sea.tag = sea.src );
  1080. };
  1081. //
  1082. // Material
  1083. //
  1084. THREE.SEA3D.prototype.blendMode = {
  1085. normal: THREE.NormalBlending,
  1086. add: THREE.AdditiveBlending,
  1087. subtract: THREE.SubtractiveBlending,
  1088. multiply: THREE.MultiplyBlending,
  1089. screen: THREE.AdditiveBlending
  1090. };
  1091. THREE.SEA3D.prototype.materialTechnique =
  1092. ( function() {
  1093. var techniques = {}
  1094. // DEFAULT
  1095. techniques[ SEA3D.Material.DEFAULT ] =
  1096. function( tech, mat ) {
  1097. mat.emissive.setHex( tech.ambientColor );
  1098. mat.color.setHex( tech.diffuseColor );
  1099. mat.specular.setHex( this.scaleColor( tech.specularColor, tech.specular ) );
  1100. mat.shininess = tech.gloss;
  1101. }
  1102. // DIFFUSE_MAP
  1103. techniques[ SEA3D.Material.DIFFUSE_MAP ] =
  1104. function( tech, mat ) {
  1105. mat.map = tech.texture.tag;
  1106. mat.transparent = tech.texture.transparent;
  1107. mat.color.setHex( 0xFFFFFF );
  1108. }
  1109. // SPECULAR_MAP
  1110. techniques[ SEA3D.Material.SPECULAR_MAP ] =
  1111. function( tech, mat ) {
  1112. mat.specularMap = tech.texture.tag;
  1113. }
  1114. // NORMAL_MAP
  1115. techniques[ SEA3D.Material.NORMAL_MAP ] =
  1116. function( tech, mat ) {
  1117. mat.normalMap = tech.texture.tag;
  1118. }
  1119. // REFLECTION
  1120. techniques[ SEA3D.Material.REFLECTION ] =
  1121. techniques[ SEA3D.Material.FRESNEL_REFLECTION ] =
  1122. function( tech, mat ) {
  1123. mat.envMap = tech.texture.tag;
  1124. mat.envMap.mapping = THREE.CubeReflectionMapping;
  1125. mat.combine = THREE.MixOperation;
  1126. mat.reflectivity = tech.alpha;
  1127. //if (tech.kind == SEA3D.Material.FRESNEL_REFLECTION) {
  1128. // not implemented
  1129. //}
  1130. }
  1131. // REFLECTION_SPHERICAL
  1132. techniques[ SEA3D.Material.REFLECTION_SPHERICAL ] =
  1133. function( tech, mat ) {
  1134. mat.envMap = tech.texture.tag;
  1135. mat.envMap.mapping = THREE.SphericalReflectionMapping;
  1136. mat.combine = THREE.MixOperation;
  1137. mat.reflectivity = tech.alpha;
  1138. }
  1139. // REFRACTION
  1140. techniques[ SEA3D.Material.REFRACTION_MAP ] =
  1141. function( tech, mat ) {
  1142. mat.envMap = tech.texture.tag;
  1143. mat.envMap.mapping = THREE.CubeRefractionMapping();
  1144. mat.refractionRatio = tech.ior;
  1145. mat.reflectivity = tech.alpha;
  1146. }
  1147. // LIGHT_MAP
  1148. techniques[ SEA3D.Material.LIGHT_MAP ] =
  1149. function( tech, mat ) {
  1150. if ( tech.blendMode == "multiply" ) mat.aoMap = tech.texture.tag;
  1151. else mat.lightMap = tech.texture.tag;
  1152. mat.lightMapUV = tech.channel;
  1153. }
  1154. return techniques;
  1155. } )();
  1156. THREE.SEA3D.prototype.readMaterial = function( sea ) {
  1157. var mat = this.config.standardMaterial ? new THREE.SEA3D.StandardMaterial() : new THREE.MeshPhongMaterial();
  1158. mat.emissiveToAmbientColor = this.config.ambientColor;
  1159. mat.name = sea.name;
  1160. mat.side = sea.bothSides ? THREE.DoubleSide : THREE.FrontSide;
  1161. mat.shading = sea.smooth ? THREE.SmoothShading : THREE.FlatShading;
  1162. if ( sea.blendMode != "normal" && this.blendMode[ sea.blendMode ] ) {
  1163. mat.blending = this.blendMode[ sea.blendMode ];
  1164. }
  1165. if ( sea.alpha < 1 || mat.blending > THREE.NormalBlending ) {
  1166. mat.opacity = sea.alpha;
  1167. mat.transparent = true;
  1168. }
  1169. for ( var i = 0; i < sea.technique.length; i ++ ) {
  1170. var tech = sea.technique[ i ];
  1171. if ( this.materialTechnique[ tech.kind ] ) {
  1172. this.materialTechnique[ tech.kind ].call( this, tech, mat );
  1173. }
  1174. }
  1175. if ( mat.transparent ) {
  1176. mat.alphaTest = sea.alphaThreshold;
  1177. }
  1178. this.domain.materials = this.materials = this.materials || [];
  1179. this.materials.push( this.objects[ "mat/" + sea.name ] = sea.tag = mat );
  1180. };
  1181. //
  1182. // Point Light
  1183. //
  1184. THREE.SEA3D.prototype.readPointLight = function( sea ) {
  1185. var light = new THREE.PointLight( sea.color, sea.multiplier * this.config.multiplier );
  1186. light.name = sea.name;
  1187. if ( sea.attenuation ) {
  1188. light.distance = sea.attenuation.end;
  1189. }
  1190. if ( sea.shadow ) {
  1191. this.setShadowMap( light, sea.shadow.opacity );
  1192. }
  1193. this.domain.lights = this.lights = this.lights || [];
  1194. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1195. if ( this.config.lights ) this.addSceneObject( sea );
  1196. this.updateTransform( light, sea );
  1197. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1198. this.updateScene();
  1199. };
  1200. //
  1201. // Hemisphere Light
  1202. //
  1203. THREE.SEA3D.prototype.readHemisphereLight = function( sea ) {
  1204. var light = new THREE.HemisphereLight( sea.color, sea.secondColor, sea.multiplier * this.config.multiplier );
  1205. light.name = sea.name;
  1206. this.domain.lights = this.lights = this.lights || [];
  1207. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1208. if ( this.config.lights ) this.addSceneObject( sea );
  1209. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1210. this.updateScene();
  1211. };
  1212. //
  1213. // Directional Light
  1214. //
  1215. THREE.SEA3D.prototype.readDirectionalLight = function( sea ) {
  1216. var light = new THREE.DirectionalLight( sea.color, sea.multiplier * this.config.multiplier );
  1217. light.name = sea.name;
  1218. if ( sea.shadow ) {
  1219. this.setShadowMap( light, sea.shadow.opacity );
  1220. }
  1221. this.domain.lights = this.lights = this.lights || [];
  1222. this.lights.push( this.objects[ "lht/" + sea.name ] = sea.tag = light );
  1223. if ( this.config.lights ) this.addSceneObject( sea );
  1224. this.updateTransform( light, sea );
  1225. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1226. this.updateScene();
  1227. };
  1228. //
  1229. // Camera
  1230. //
  1231. THREE.SEA3D.prototype.readCamera = function( sea ) {
  1232. var camera = new THREE.SEA3D.Camera( sea.fov );
  1233. camera.name = sea.name;
  1234. this.domain.cameras = this.cameras = this.cameras || [];
  1235. this.cameras.push( this.objects[ "cam/" + sea.name ] = sea.tag = camera );
  1236. this.addSceneObject( sea );
  1237. this.updateTransform( camera, sea );
  1238. this.applyDefaultAnimation( sea, THREE.SEA3D.CameraAnimator );
  1239. };
  1240. //
  1241. // Skeleton
  1242. //
  1243. THREE.SEA3D.prototype.readSkeletonLocal = function( sea ) {
  1244. var bones = [];
  1245. for ( var i = 0; i < sea.joint.length; i ++ ) {
  1246. var bone = sea.joint[ i ];
  1247. bones[ i ] = {
  1248. name: bone.name,
  1249. pos: [ bone.x, bone.y, bone.z ],
  1250. rotq: [ bone.qx, bone.qy, bone.qz, bone.qw ],
  1251. parent: bone.parentIndex
  1252. };
  1253. }
  1254. sea.tag = bones;
  1255. };
  1256. //
  1257. // Joint Object
  1258. //
  1259. THREE.SEA3D.prototype.readJointObject = function( sea ) {
  1260. var mesh = sea.target.tag,
  1261. bone = mesh.skeleton.bones[ sea.joint ];
  1262. this.domain.joints = this.joints = this.joints || [];
  1263. this.joints.push( this.objects[ "jnt/" + sea.name ] = sea.tag = bone );
  1264. };
  1265. //
  1266. // Skeleton Animation
  1267. //
  1268. THREE.SEA3D.prototype.getSkeletonAnimation = function( sea, skl ) {
  1269. if ( sea.tag ) return sea.tag;
  1270. var animations = [],
  1271. delta = ( 1000 / sea.frameRate ) / 1000;
  1272. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  1273. var seq = sea.sequence[ i ];
  1274. var start = seq.start;
  1275. var end = start + seq.count;
  1276. var ns = sea.name + "/" + seq.name;
  1277. var animation = {
  1278. name: ns,
  1279. repeat: seq.repeat,
  1280. fps: sea.frameRate,
  1281. JIT: 0,
  1282. length: delta * ( seq.count - 1 ),
  1283. hierarchy: []
  1284. };
  1285. var numJoints = sea.numJoints,
  1286. raw = sea.raw;
  1287. for ( var j = 0; j < numJoints; j ++ ) {
  1288. var bone = skl.joint[ j ],
  1289. node = { parent: bone.parentIndex, keys: [] },
  1290. keys = node.keys,
  1291. time = 0;
  1292. for ( var frame = start; frame < end; frame ++ ) {
  1293. var idx = ( frame * numJoints * 7 ) + ( j * 7 );
  1294. keys.push( {
  1295. time: time,
  1296. pos: [ raw[ idx ], raw[ idx + 1 ], raw[ idx + 2 ] ],
  1297. rot: [ raw[ idx + 3 ], raw[ idx + 4 ], raw[ idx + 5 ], raw[ idx + 6 ] ],
  1298. scl: [ 1, 1, 1 ]
  1299. } );
  1300. time += delta;
  1301. }
  1302. animation.hierarchy[ j ] = node;
  1303. }
  1304. animations.push( animation );
  1305. }
  1306. return sea.tag = animations;
  1307. };
  1308. //
  1309. // Morpher
  1310. //
  1311. THREE.SEA3D.prototype.readMorpher = function( sea ) {
  1312. var attribs = {
  1313. position : []
  1314. },
  1315. targets = [];
  1316. for ( var i = 0; i < sea.node.length; i ++ ) {
  1317. var node = sea.node[ i ];
  1318. attribs.position[ i ] = new THREE.Float32Attribute( new Float32Array( node.vertex ), 3 );
  1319. if ( node.normal ) {
  1320. attribs.normal = attribs.normal || [];
  1321. attribs.normal[ i ] = new THREE.Float32Attribute( new Float32Array( node.normal ), 3 );
  1322. }
  1323. targets[ i ] = { name: node.name };
  1324. }
  1325. sea.tag = {
  1326. attribs : attribs,
  1327. targets : targets
  1328. }
  1329. };
  1330. //
  1331. // Vertex Animation
  1332. //
  1333. THREE.SEA3D.prototype.readVertexAnimation = function( sea ) {
  1334. var attribs = {
  1335. position : []
  1336. },
  1337. targets = [],
  1338. animations = [];
  1339. for ( var i = 0, l = sea.frame.length; i < l; i ++ ) {
  1340. var frame = sea.frame[ i ];
  1341. attribs.position[ i ] = new THREE.Float32Attribute( new Float32Array( frame.vertex ), 3 );
  1342. if ( frame.normal ) {
  1343. attribs.normal = attribs.normal || [];
  1344. attribs.normal[ i ] = new THREE.Float32Attribute( new Float32Array( frame.normal ), 3 );
  1345. }
  1346. targets[ i ] = { name: i };
  1347. }
  1348. for ( var i = 0; i < sea.sequence.length; i ++ ) {
  1349. var seq = sea.sequence[ i ];
  1350. animations[ i ] = animations[ seq.name ] = {
  1351. name : seq.name,
  1352. start : seq.start,
  1353. end : seq.start + seq.count,
  1354. repeat : seq.repeat
  1355. }
  1356. }
  1357. sea.tag = {
  1358. attribs : attribs,
  1359. targets : targets,
  1360. animations : animations,
  1361. frameRate : sea.frameRate
  1362. };
  1363. };
  1364. //
  1365. // Actions
  1366. //
  1367. THREE.SEA3D.prototype.readActions = function( sea ) {
  1368. for ( var i = 0; i < sea.actions.length; i ++ ) {
  1369. var act = sea.actions[ i ];
  1370. switch ( act.kind ) {
  1371. case SEA3D.Actions.SCRIPTS:
  1372. this.runJSMList( this.domain, act.scripts, true );
  1373. break;
  1374. }
  1375. }
  1376. };
  1377. //
  1378. // Events
  1379. //
  1380. THREE.SEA3D.Event = {
  1381. LOAD_PROGRESS: "sea3d_progress",
  1382. DOWNLOAD_PROGRESS: "sea3d_download",
  1383. COMPLETE: "sea3d_complete",
  1384. OBJECT_COMPLETE: "sea3d_object",
  1385. PARSE_PROGRESS: "parse_progress",
  1386. PARSE_COMPLETE: "parse_complete",
  1387. ERROR: "sea3d_error"
  1388. };
  1389. THREE.SEA3D.prototype.onProgress = undefined;
  1390. THREE.SEA3D.prototype.onComplete = function( args ) {
  1391. args.file = this.scope; args.type = THREE.SEA3D.Event.COMPLETE;
  1392. args.file.dispatchEvent( args );
  1393. };
  1394. THREE.SEA3D.prototype.onLoadProgress = function( args ) {
  1395. args.file = this.scope; args.type = THREE.SEA3D.Event.LOAD_PROGRESS;
  1396. args.file.dispatchEvent( args );
  1397. if ( args.file.onProgress ) args.file.onProgress( args );
  1398. };
  1399. THREE.SEA3D.prototype.onDownloadProgress = function( args ) {
  1400. args.file = this.scope; args.type = THREE.SEA3D.Event.DOWNLOAD_PROGRESS;
  1401. args.file.dispatchEvent( args );
  1402. if ( args.file.onProgress ) args.file.onProgress( args );
  1403. };
  1404. THREE.SEA3D.prototype.onCompleteObject = function( args ) {
  1405. args.file = this.scope; args.type = THREE.SEA3D.Event.OBJECT_COMPLETE;
  1406. args.file.dispatchEvent( args );
  1407. };
  1408. THREE.SEA3D.prototype.onParseProgress = function( args ) {
  1409. args.file = this.scope; args.type = THREE.SEA3D.Event.PARSE_PROGRESS;
  1410. args.file.dispatchEvent( args );
  1411. };
  1412. THREE.SEA3D.prototype.onParseComplete = function( args ) {
  1413. args.file = this.scope; args.type = THREE.SEA3D.Event.PARSE_COMPLETE;
  1414. args.file.dispatchEvent( args );
  1415. };
  1416. THREE.SEA3D.prototype.onError = function( args ) {
  1417. args.file = this.scope; args.type = THREE.SEA3D.Event.ERROR;
  1418. args.file.dispatchEvent( args );
  1419. };
  1420. //
  1421. // Loader
  1422. //
  1423. THREE.SEA3D.prototype.newDomain = function() {
  1424. this.domain = new THREE.SEA3D.Domain(
  1425. this.config.id,
  1426. this.objects = {},
  1427. this.config.container,
  1428. THREE.SEA3D.EXTENSIONS_DOMAIN
  1429. );
  1430. }
  1431. THREE.SEA3D.prototype.parse = function( onParseComplete, onParseProgress ) {
  1432. delete this.cameras;
  1433. delete this.containers;
  1434. delete this.lights;
  1435. delete this.joints;
  1436. delete this.meshes;
  1437. delete this.materials;
  1438. delete this.animationSets;
  1439. delete this.sprites;
  1440. delete this.sounds3d;
  1441. delete this.cubeRenderers;
  1442. delete this.sounds;
  1443. delete this.glsl;
  1444. delete this.dummy;
  1445. delete this.domain;
  1446. this.newDomain();
  1447. this.file.onParseComplete = ( function( e ) {
  1448. if ( this.config.manager ) this.config.manager.add( this.domain );
  1449. ( onParseComplete || this.onParseComplete ).call( this.file, e );
  1450. } ).bind( this );
  1451. this.file.onParseProgress = onParseProgress || this.onParseProgress;
  1452. // EXTENSIONS
  1453. for ( var i = 0; i < THREE.SEA3D.EXTENSIONS_PARSE.length; i ++ ) {
  1454. THREE.SEA3D.EXTENSIONS_PARSE[ i ].call( this );
  1455. }
  1456. this.file.parse();
  1457. return this;
  1458. };
  1459. THREE.SEA3D.prototype.load = function( url ) {
  1460. this.loadBytes();
  1461. this.file.load( url );
  1462. };
  1463. THREE.SEA3D.prototype.onHead = function( args ) {
  1464. if ( args.sign != 'TJS' ) {
  1465. throw new Error( "Sign '" + args.sign + "' not supported! Use SEA3D Studio to publish or SEA3DLegacy.js" );
  1466. }
  1467. };
  1468. THREE.SEA3D.EXTENSIONS = [];
  1469. THREE.SEA3D.EXTENSIONS_PARSE = [];
  1470. THREE.SEA3D.EXTENSIONS_DOMAIN = [];
  1471. THREE.SEA3D.prototype.loadBytes = function( data ) {
  1472. this.file = new SEA3D.File();
  1473. this.file.scope = this;
  1474. this.file.config = this.config;
  1475. this.file.streaming = this.config.streaming;
  1476. this.file.timeLimit = this.config.timeLimit;
  1477. this.file.onProgress = this.onLoadProgress;
  1478. this.file.onCompleteObject = this.onCompleteObject;
  1479. this.file.onDownloadProgress = this.onDownloadProgress;
  1480. this.file.onParseProgress = this.onParseProgress;
  1481. this.file.onParseComplete = this.onParseComplete;
  1482. this.file.onError = this.onError;
  1483. this.file.onHead = this.onHead;
  1484. this.file.onComplete = ( function( e ) {
  1485. if ( this.config.manager ) this.config.manager.add( this.domain );
  1486. this.onComplete.call( this.file, e );
  1487. } ).bind( this );
  1488. // SEA3D
  1489. this.newDomain();
  1490. this.file.typeRead[ SEA3D.Geometry.prototype.type ] =
  1491. this.file.typeRead[ SEA3D.GeometryDelta.prototype.type ] = this.readGeometryBuffer;
  1492. this.file.typeRead[ SEA3D.Mesh.prototype.type ] = this.readMesh;
  1493. this.file.typeRead[ SEA3D.Mesh2D.prototype.type ] = this.readMesh2D;
  1494. this.file.typeRead[ SEA3D.Container3D.prototype.type ] = this.readContainer3D;
  1495. this.file.typeRead[ SEA3D.Dummy.prototype.type ] = this.readDummy;
  1496. this.file.typeRead[ SEA3D.Line.prototype.type ] = this.readLine;
  1497. this.file.typeRead[ SEA3D.Material.prototype.type ] = this.readMaterial;
  1498. this.file.typeRead[ SEA3D.Camera.prototype.type ] = this.readCamera;
  1499. this.file.typeRead[ SEA3D.SkeletonLocal.prototype.type ] = this.readSkeletonLocal;
  1500. this.file.typeRead[ SEA3D.JointObject.prototype.type ] = this.readJointObject;
  1501. this.file.typeRead[ SEA3D.CubeMap.prototype.type ] = this.readCubeMap;
  1502. this.file.typeRead[ SEA3D.CubeRender.prototype.type ] = this.readCubeRender;
  1503. this.file.typeRead[ SEA3D.Animation.prototype.type ] = this.readAnimation;
  1504. this.file.typeRead[ SEA3D.SoundPoint.prototype.type ] = this.readSoundPoint;
  1505. this.file.typeRead[ SEA3D.TextureURL.prototype.type ] = this.readTextureURL;
  1506. this.file.typeRead[ SEA3D.Morph.prototype.type ] = this.readMorpher;
  1507. this.file.typeRead[ SEA3D.VertexAnimation.prototype.type ] = this.readVertexAnimation;
  1508. this.file.typeRead[ SEA3D.PointLight.prototype.type ] = this.readPointLight;
  1509. this.file.typeRead[ SEA3D.DirectionalLight.prototype.type ] = this.readDirectionalLight;
  1510. this.file.typeRead[ SEA3D.HemisphereLight.prototype.type ] = this.readHemisphereLight;
  1511. this.file.typeRead[ SEA3D.Actions.prototype.type ] = this.readActions;
  1512. // UNIVERSAL
  1513. this.file.typeRead[ SEA3D.JPEG.prototype.type ] =
  1514. this.file.typeRead[ SEA3D.JPEG_XR.prototype.type ] =
  1515. this.file.typeRead[ SEA3D.PNG.prototype.type ] =
  1516. this.file.typeRead[ SEA3D.GIF.prototype.type ] = this.readImage;
  1517. this.file.typeRead[ SEA3D.MP3.prototype.type ] = this.readSound;
  1518. this.file.typeRead[ SEA3D.GLSL.prototype.type ] = this.readGLSL;
  1519. this.file.typeRead[ SEA3D.JavaScriptMethod.prototype.type ] = this.readJavaScriptMethod;
  1520. // EXTENSIONS
  1521. for ( var i = 0; i < THREE.SEA3D.EXTENSIONS.length; i ++ ) {
  1522. THREE.SEA3D.EXTENSIONS[ i ].call( this );
  1523. }
  1524. this.file.read( data );
  1525. };