SEA3DLoader.js 45 KB

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