SEA3DLoader.js 45 KB

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