SEA3DLoader.js 54 KB

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