SEA3DLoader.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  1. /**
  2. * SEA3D.JS + Three.JS
  3. * Copyright (C) 2015 Sunag Entertainment
  4. *
  5. * http://sea3d.poonya.com/
  6. */
  7. //
  8. // Shader
  9. //
  10. THREE.ShaderLib.replaceCode = function(src, target, replace) {
  11. for (var i = 0; i < target.length; i++) {
  12. var tar = target[i],
  13. rep = replace[i],
  14. index = src.indexOf(tar);
  15. if (index > -1)
  16. src = src.substring(0, index) + rep + src.substring(index + tar.length);
  17. }
  18. return src;
  19. }
  20. THREE.ShaderLib['phong'].fragmentShader_StandardMaterial =
  21. THREE.ShaderLib.replaceCode( THREE.ShaderLib['phong'].fragmentShader, [
  22. // Target
  23. 'outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + ambientLightColor ) * specular + totalSpecularLight + emissive;', // METAL
  24. 'outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + ambientLightColor ) + totalSpecularLight + emissive;'
  25. ], [
  26. // Replace To
  27. 'outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + ambientLightColor + emissive ) * specular + totalSpecularLight;', // METAL
  28. 'outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + ambientLightColor + emissive ) + totalSpecularLight;'
  29. ] );
  30. //
  31. // Material ( Emissive to Ambient Color Extension )
  32. //
  33. THREE.MeshPhongMaterial.prototype.emissiveToAmbientColor = false;
  34. THREE.MeshPhongMaterial.prototype.CLONE = THREE.MeshPhongMaterial.prototype.clone;
  35. THREE.MeshPhongMaterial.prototype.clone = function() {
  36. var mat = this.CLONE();
  37. mat.emissiveToAmbientColor = this.emissiveToAmbientColor;
  38. return mat;
  39. }
  40. THREE.MeshPhongMaterial.prototype.__defineSetter__("__webglShader", function(val){
  41. if (this.emissiveToAmbientColor) val.fragmentShader = THREE.ShaderLib['phong'].fragmentShader_StandardMaterial;
  42. this.__webglShader__ = val;
  43. });
  44. THREE.MeshPhongMaterial.prototype.__defineGetter__("__webglShader", function(){
  45. return this.__webglShader__;
  46. });
  47. //
  48. // Mesh
  49. //
  50. // Local Animation
  51. THREE.Object3D.prototype.UPDATEMATRIXWORLD = THREE.Mesh.prototype.updateMatrixWorld;
  52. THREE.Object3D.prototype.updateMatrixWorld = function(force) {
  53. if (this.animateMatrix) {
  54. this.UPDATEMATRIXWORLD(force);
  55. this.animateMatrix.compose( this.animatePosition, this.animateQuaternion, this.animateScale );
  56. this.matrixWorld.multiplyMatrices( this.matrixWorld, this.animateMatrix );
  57. }
  58. else this.UPDATEMATRIXWORLD(force);
  59. }
  60. THREE.Object3D.prototype.setAnimateMatrix = function(val) {
  61. if (this.getAnimateMatrix() == val) return;
  62. if (val) {
  63. this.animateMatrix = new THREE.Matrix4();
  64. this.animatePosition = new THREE.Vector3();
  65. this.animateQuaternion = new THREE.Quaternion();
  66. this.animateScale = new THREE.Vector3(1,1,1);
  67. } else {
  68. delete this.animateMatrix;
  69. delete this.animatePosition;
  70. delete this.animateQuaternion;
  71. delete this.animateScale;
  72. }
  73. this.matrixWorldNeedsUpdate = true;
  74. }
  75. THREE.Object3D.prototype.getAnimateMatrix = function() {
  76. return this.animateMatrix != null;
  77. }
  78. THREE.Mesh.prototype.setWeight = function(name, val) {
  79. this.morphTargetInfluences[ this.geometry.morphTargets[name] ] = val;
  80. }
  81. THREE.Mesh.prototype.getWeight = function(name) {
  82. return this.morphTargetInfluences[ this.geometry.morphTargets[name] ];
  83. }
  84. THREE.Mesh.prototype.DISPOSE = THREE.Mesh.prototype.dispose;
  85. THREE.Mesh.prototype.dispose = function () {
  86. if (this.animation) this.animation.dispose();
  87. this.DISPOSE();
  88. }
  89. THREE.Mesh.prototype.CLONE = THREE.Mesh.prototype.clone;
  90. THREE.Mesh.prototype.clone = function ( object ) {
  91. var cloned = THREE.Mesh.prototype.CLONE.call( this, object );
  92. if (cloned.animation)
  93. cloned.animation = this.animation.clone( cloned );
  94. return cloned;
  95. }
  96. //
  97. // Skinning
  98. //
  99. THREE.SkinnedMesh.prototype.stop = function() {
  100. if (this.currentAnimation) {
  101. this.currentAnimation.stop();
  102. this.currentAnimation = null;
  103. this.isPlaying = false;
  104. }
  105. }
  106. THREE.SkinnedMesh.prototype.pause = function() {
  107. if (this.isPlaying) {
  108. this.currentAnimation.pause();
  109. this.isPlaying = false;
  110. }
  111. }
  112. THREE.SkinnedMesh.prototype.resume = function() {
  113. if (!this.isPlaying && this.currentAnimation) {
  114. this.currentAnimation.pause();
  115. this.isPlaying = true;
  116. }
  117. }
  118. THREE.SkinnedMesh.prototype.isPlaying = false;
  119. THREE.SkinnedMesh.prototype.play = function(name, crossfade, offset) {
  120. this.previousAnimation = this.currentAnimation;
  121. this.currentAnimation = this.animations[name];
  122. if (!this.currentAnimation)
  123. throw new Error('Animation "' + name + '" not found.');
  124. if (this.previousAnimation && this.previousAnimation !== this.currentAnimation && crossfade > 0) {
  125. this.previousAnimation.play(this.previousAnimation.currentTime, this.previousAnimation.weight);
  126. this.currentAnimation.play(offset !== undefined ? offset : this.currentAnimation.currentTime, this.currentAnimation.weight);
  127. THREE.AnimationHandler.addCrossfade( this, crossfade );
  128. } else {
  129. this.currentAnimation.play(offset !== undefined ? offset : this.currentAnimation.currentTime, 1);
  130. }
  131. this.isPlaying = true;
  132. }
  133. THREE.SkinnedMesh.prototype.setAnimations = function(animations) {
  134. this.animations = [];
  135. this.weightSchedule = [];
  136. this.warpSchedule = [];
  137. var nsIndex = animations[0].name.indexOf("/")+1;
  138. this.animationNamespace = animations[0].name.substring(0, nsIndex);
  139. for (var i = 0; i < animations.length; i++) {
  140. var ns = animations[i].name;
  141. var name = ns.substring(nsIndex);
  142. this.animations[i] = new THREE.Animation( this, animations[i]);
  143. this.animations[i].loop = animations[i].repeat;
  144. this.animations[i].name = name;
  145. this.animations[name] = this.animations[i];
  146. }
  147. }
  148. THREE.SkinnedMesh.prototype.DISPOSE = THREE.SkinnedMesh.prototype.dispose;
  149. THREE.SkinnedMesh.prototype.dispose = function () {
  150. this.stop();
  151. this.animations = null;
  152. this.DISPOSE();
  153. }
  154. THREE.SkinnedMesh.prototype.CLONE = THREE.SkinnedMesh.prototype.clone;
  155. THREE.SkinnedMesh.prototype.clone = function ( object ) {
  156. var cloned = THREE.SkinnedMesh.prototype.CLONE.call( this, object );
  157. if (cloned.animation)
  158. cloned.animation = this.animation.clone( cloned );
  159. cloned.animations = [];
  160. if (this.geometry.animations) {
  161. var refAnimations = this.geometry.animations;
  162. var nsIndex = refAnimations[0].name.indexOf("/")+1;
  163. for (var i = 0; i < refAnimations.length; i++) {
  164. var name = refAnimations[i].name.substring(nsIndex);
  165. var data = refAnimations[i];
  166. data.initialized = false;
  167. cloned.animations[i] = new THREE.Animation( cloned, data );
  168. cloned.animations[i].loop = refAnimations[i].repeat;
  169. cloned.animations[i].name = name;
  170. }
  171. }
  172. return cloned;
  173. }
  174. //
  175. // Three.JS Skeleton Animation Crossfade Extension
  176. //
  177. THREE.AnimationHandler.crossfade = [];
  178. THREE.AnimationHandler.UPDATE = THREE.AnimationHandler.update;
  179. THREE.AnimationHandler.update = function( dt ) {
  180. var i, cf = THREE.AnimationHandler.crossfade;
  181. // Crossfade
  182. i = 0;
  183. while ( i < cf.length ) {
  184. var mesh = cf[i];
  185. mesh.currentAnimation.weight += dt / mesh.crossfade;
  186. if (mesh.currentAnimation.weight > 1) {
  187. mesh.previousAnimation.weight = 0;
  188. mesh.currentAnimation.weight = 1;
  189. if (mesh.onCrossfadeComplete)
  190. mesh.onCrossfadeComplete( mesh );
  191. cf.splice( i, 1 );
  192. delete mesh.crossfade;
  193. } else
  194. ++i;
  195. mesh.previousAnimation.weight = 1 - mesh.currentAnimation.weight;
  196. }
  197. THREE.AnimationHandler.UPDATE( dt );
  198. }
  199. THREE.AnimationHandler.addCrossfade = function( mesh, crossfade ) {
  200. if (mesh.crossfade !== undefined)
  201. THREE.AnimationHandler.crossfade.splice( THREE.AnimationHandler.crossfade.indexOf( mesh ), 1 );
  202. mesh.crossfade = crossfade;
  203. THREE.AnimationHandler.crossfade.push( mesh );
  204. }
  205. //
  206. // Animation Event Extension
  207. //
  208. THREE.Animation.prototype.STOP = THREE.Animation.prototype.stop;
  209. THREE.Animation.prototype.stop = function() {
  210. if (this.onComplete)
  211. this.onComplete( this );
  212. this.STOP();
  213. }
  214. THREE.Animation.prototype.RESET = THREE.Animation.prototype.reset;
  215. THREE.Animation.prototype.reset = function() {
  216. if (this.onReset)
  217. this.onReset( this );
  218. this.RESET();
  219. }
  220. //
  221. // Sound3D
  222. //
  223. THREE.Sound3D = function( src, volume, distance ) {
  224. THREE.Object3D.call( this );
  225. this.audio = new Audio();
  226. this.audio.src = src;
  227. this.audio.load();
  228. this.distance = distance !== undefined ? distance : 1000;
  229. this.volume = volume !== undefined ? volume : 1;
  230. this.playing = false;
  231. }
  232. THREE.Sound3D.prototype = Object.create( THREE.Object3D.prototype );
  233. THREE.Sound3D.prototype.loop = false;
  234. THREE.Sound3D.prototype.play = function(offset) {
  235. if (offset !== undefined && this.audio.duration > 0)
  236. {
  237. this.audio.currentTime = offset;
  238. }
  239. this.audio.loop = this.loop;
  240. this.audio.play();
  241. if (!this.playing)
  242. {
  243. this.index = THREE.Sound3D.sounds.length;
  244. THREE.Sound3D.sounds.push( this );
  245. this.playing = true;
  246. }
  247. }
  248. THREE.Sound3D.prototype.stop = function() {
  249. if (this.audio.duration > 0)
  250. this.audio.currentTime = 0;
  251. this.pause();
  252. }
  253. THREE.Sound3D.prototype.pause = function() {
  254. this.audio.pause();
  255. if (this.playing)
  256. {
  257. THREE.Sound3D.sounds.splice( this.index, 1 );
  258. this.playing = false;
  259. }
  260. }
  261. THREE.Sound3D.prototype.update = function( camera ) {
  262. var soundPosition = new THREE.Vector3();
  263. soundPosition.setFromMatrixPosition( this.matrixWorld );
  264. var cameraPosition = new THREE.Vector3();
  265. cameraPosition.setFromMatrixPosition( camera.matrixWorld );
  266. var distance = soundPosition.distanceTo( cameraPosition );
  267. var volume = this.volume * (1 - ( distance / (this.distance * 3) ));
  268. this.audio.volume = Math.max(0, Math.min(1, volume));
  269. }
  270. THREE.Sound3D.sounds = [];
  271. THREE.Sound3D.update = function( camera ) {
  272. var sounds = THREE.Sound3D.sounds;
  273. for(var i = 0; i < sounds.length; i++) {
  274. sounds[i].update( camera );
  275. }
  276. }
  277. //
  278. // Bone Extension (Joint Object)
  279. //
  280. THREE.Bone.fromName = function(mesh, name) {
  281. var bones = mesh.bones;
  282. for(var i = 0, bl = bones.length; i < bl; i++)
  283. {
  284. if (name == bones[i].name)
  285. {
  286. return bones[i];
  287. }
  288. }
  289. }
  290. //
  291. // SEA3D
  292. //
  293. THREE.SEA3D = function(config) {
  294. this.config = config || {};
  295. if (this.config.autoPlay === undefined) this.config.autoPlay = false;
  296. if (this.config.flip === undefined) this.config.flip = true;
  297. if (this.config.parser == undefined) this.config.parser = THREE.SEA3D.AUTO;
  298. if (this.config.multiplier == undefined) this.config.multiplier = 1;
  299. if (this.config.tangent == undefined) this.config.tangent = true;
  300. if (this.config.bounding == undefined) this.config.bounding = true;
  301. if (this.config.ambientColor == undefined) this.config.ambientColor = true;
  302. this.container = this.config.container || new THREE.Object3D();
  303. this.objects = {};
  304. }
  305. THREE.SEA3D.prototype = {
  306. constructor: THREE.SEA3D,
  307. addEventListener: THREE.EventDispatcher.prototype.addEventListener,
  308. hasEventListener: THREE.EventDispatcher.prototype.hasEventListener,
  309. removeEventListener: THREE.EventDispatcher.prototype.removeEventListener,
  310. dispatchEvent: THREE.EventDispatcher.prototype.dispatchEvent
  311. }
  312. //
  313. // Config
  314. //
  315. THREE.SEA3D.BUFFER0 = new THREE.Matrix4();
  316. THREE.SEA3D.BUFFER1 = new THREE.Matrix4();
  317. THREE.SEA3D.BUFFER2 = new THREE.Matrix4();
  318. THREE.SEA3D.BUFFER3 = new THREE.Matrix4();
  319. THREE.SEA3D.QUABUF0 = new THREE.Quaternion();
  320. THREE.SEA3D.QUABUF1 = new THREE.Quaternion();
  321. THREE.SEA3D.VECBUF0 = new THREE.Vector3();
  322. THREE.SEA3D.VECBUF1 = new THREE.Vector3();
  323. THREE.SEA3D.CONTAINER = new THREE.Object3D();
  324. THREE.SEA3D.VECZERO = new THREE.Vector3();
  325. THREE.SEA3D.AUTO = 'auto';
  326. THREE.SEA3D.DEFAULT = 'default';
  327. THREE.SEA3D.BUFFER = 'buffer';
  328. THREE.SEA3D.BACKGROUND_COLOR = 0x333333;
  329. THREE.SEA3D.HELPER_COLOR = 0x9AB9E5;
  330. THREE.SEA3D.TEXTURE_SIZE = 512;
  331. THREE.SEA3D.prototype.setShadowMap = function(light, opacity) {
  332. light.shadowMapWidth =
  333. light.shadowMapHeight = 2048;
  334. light.castShadow = true;
  335. light.shadowDarkness = opacity !== undefined ? opacity : 1;
  336. }
  337. //
  338. // Output
  339. //
  340. THREE.SEA3D.prototype.getMesh = function(name) {
  341. return this.objects["m3d/" + name];
  342. }
  343. THREE.SEA3D.prototype.getDummy = function(name) {
  344. return this.objects["dmy/" + name];
  345. }
  346. THREE.SEA3D.prototype.getLine = function(name) {
  347. return this.objects["line/" + name];
  348. }
  349. THREE.SEA3D.prototype.getSound3D = function(name) {
  350. return this.objects["sn3d/" + name];
  351. }
  352. THREE.SEA3D.prototype.getMaterial = function(name) {
  353. return this.objects["mat/" + name];
  354. }
  355. THREE.SEA3D.prototype.getLight = function(name) {
  356. return this.objects["lht/" + name];
  357. }
  358. THREE.SEA3D.prototype.getGLSL = function(name) {
  359. return this.objects["glsl/" + name];
  360. }
  361. THREE.SEA3D.prototype.getCamera = function(name) {
  362. return this.objects["cam/" + name];
  363. }
  364. THREE.SEA3D.prototype.getTexture = function(name) {
  365. return this.objects["tex/" + name];
  366. }
  367. THREE.SEA3D.prototype.getCubeMap = function(name) {
  368. return this.objects["cmap/" + name];
  369. }
  370. THREE.SEA3D.prototype.getJointObject = function(name) {
  371. return this.objects["jnt/" + name];
  372. }
  373. THREE.SEA3D.prototype.getContainer3D = function(name) {
  374. return this.objects["c3d/" + name];
  375. }
  376. THREE.SEA3D.prototype.getSprite = function(name) {
  377. return this.objects["m2d/" + name];
  378. }
  379. THREE.SEA3D.prototype.getProperty = function(name) {
  380. return this.objects["prop/" + name];
  381. }
  382. //
  383. // Utils
  384. //
  385. THREE.SEA3D.prototype.isPowerOfTwo = function(num) {
  386. return num ? ((num & -num) == num) : false;
  387. }
  388. THREE.SEA3D.prototype.nearestPowerOfTwo = function(num) {
  389. return Math.pow( 2, Math.round( Math.log( num ) / Math.LN2 ) );
  390. }
  391. THREE.SEA3D.prototype.vectorToVector3 = function(list) {
  392. var n = [];
  393. var i = 0, j = 0;
  394. while(i < list.length)
  395. n[j++] = new THREE.Vector3(list[i++], list[i++], list[i++]);
  396. return n;
  397. }
  398. THREE.SEA3D.prototype.vectorToVector3Inv = function(list) {
  399. var n = [];
  400. var i = 0, j = 0;
  401. while(i < list.length)
  402. n[j++] = new THREE.Vector3(list[i++], list[i++], -list[i++]);
  403. return n;
  404. }
  405. THREE.SEA3D.prototype.flipMatrixScale = function(mtx) {
  406. mtx.decompose( THREE.SEA3D.VECBUF0, THREE.SEA3D.QUABUF0, THREE.SEA3D.VECBUF1 );
  407. THREE.SEA3D.VECBUF1.z = -THREE.SEA3D.VECBUF1.z;
  408. mtx.compose( THREE.SEA3D.VECBUF0, THREE.SEA3D.QUABUF0, THREE.SEA3D.VECBUF1 );
  409. return mtx;
  410. }
  411. THREE.SEA3D.prototype.flipMatrixGlobal = function(mtx) {
  412. mtx.decompose( THREE.SEA3D.VECBUF0, THREE.SEA3D.QUABUF0, THREE.SEA3D.VECBUF1 );
  413. THREE.SEA3D.VECBUF1.z = -THREE.SEA3D.VECBUF1.z;
  414. THREE.SEA3D.VECBUF1.y = -THREE.SEA3D.VECBUF1.y;
  415. THREE.SEA3D.VECBUF1.x = -THREE.SEA3D.VECBUF1.x;
  416. mtx.compose( THREE.SEA3D.VECBUF0, THREE.SEA3D.QUABUF0, THREE.SEA3D.VECBUF1 );
  417. return mtx;
  418. }
  419. THREE.SEA3D.prototype.flipMatrix = function(mtx) {
  420. var mtx_data = THREE.SEA3D.BUFFER0.copy( mtx );
  421. mtx.setPosition( THREE.SEA3D.VECZERO );
  422. mtx.multiplyMatrices( THREE.SEA3D.BUFFER1.makeRotationAxis( THREE.SEA3D.VECBUF0.set(0, 0, 1), THREE.Math.degToRad( 180 ) ), mtx );
  423. mtx.makeRotationFromQuaternion( THREE.SEA3D.QUABUF0.setFromRotationMatrix( mtx ) );
  424. var pos = THREE.SEA3D.VECBUF0.setFromMatrixPosition( mtx_data );
  425. pos.z = -pos.z;
  426. mtx.setPosition(pos);
  427. return mtx;
  428. }
  429. THREE.SEA3D.prototype.applyMatrix = function(obj3d, mtx) {
  430. var vec = THREE.SEA3D.VECBUF0;
  431. obj3d.position.setFromMatrixPosition( mtx );
  432. obj3d.scale.setFromMatrixScale( mtx );
  433. // ignore rotation scale
  434. mtx.scale( vec.set( 1 / obj3d.scale.x, 1 / obj3d.scale.y, 1 / obj3d.scale.z ) );
  435. obj3d.rotation.setFromRotationMatrix( mtx );
  436. }
  437. THREE.SEA3D.prototype.updateMatrix = function(obj3d) {
  438. var buf = THREE.SEA3D.BUFFER2;
  439. var buf1 = THREE.SEA3D.BUFFER3;
  440. // convert to global
  441. obj3d.updateMatrixWorld();
  442. buf.copy( obj3d.matrixWorld );
  443. this.flipMatrixScale( buf ); // flip matrix
  444. // convert to local
  445. buf1.copy( obj3d.parent.matrixWorld );
  446. if (obj3d.parent instanceof THREE.Bone)
  447. this.flipMatrixGlobal( buf1 );
  448. else
  449. this.flipMatrixScale( buf1 );
  450. buf1.getInverse( buf1 );
  451. buf.multiplyMatrices( buf1, buf );
  452. this.applyMatrix(obj3d, buf);
  453. }
  454. THREE.SEA3D.prototype.updateTransform = function(obj3d, sea) {
  455. var buf = THREE.SEA3D.BUFFER2;
  456. if (sea.transform)
  457. buf.elements = sea.transform;
  458. else
  459. buf.makeTranslation(sea.position.x, sea.position.y, sea.position.z);
  460. // matrix
  461. this.applyMatrix(obj3d, buf);
  462. // flip matrix
  463. this.updateMatrix(obj3d);
  464. // optimize if is static
  465. if (sea.isStatic) {
  466. obj3d.updateMatrixWorld();
  467. obj3d.matrixAutoUpdate = false;
  468. }
  469. }
  470. THREE.SEA3D.prototype.updateAnimationSet = function(obj3d) {
  471. var buf = THREE.SEA3D.BUFFER2;
  472. var anmSet = obj3d.animation.animationSet;
  473. if (anmSet.flip)
  474. return;
  475. var anms = anmSet.animations;
  476. for (var i = 0; i < anms.length; i++) {
  477. var dataList = anms[i].dataList;
  478. var t_anm = [], j;
  479. for (j = 0; j < dataList.length; j++) {
  480. var data = dataList[i];
  481. var raw = dataList[i].data;
  482. var kind = data.kind;
  483. var numFrames = raw.length / data.blockLength;
  484. switch(kind) {
  485. case SEA3D.Animation.POSITION:
  486. case SEA3D.Animation.ROTATION:
  487. t_anm.push( {
  488. kind : kind,
  489. numFrames : numFrames,
  490. raw : raw
  491. } );
  492. break;
  493. }
  494. }
  495. if (t_anm.length > 0) {
  496. var numFrames = t_anm[0].numFrames,
  497. ct = THREE.SEA3D.CONTAINER;
  498. ct.position.set(0, 0, 0);
  499. ct.rotation.set(0, 0, 0);
  500. ct.scale.set(1, 1, 1);
  501. obj3d.add( ct );
  502. for (var f = 0, t, c; f < numFrames; f++) {
  503. for (t = 0; t < t_anm.length; t++) {
  504. var raw = t_anm[t].raw,
  505. kind = t_anm[t].kind;
  506. switch(kind) {
  507. case SEA3D.Animation.POSITION:
  508. c = f * 3;
  509. ct.position.set(
  510. raw[c ],
  511. raw[c + 1],
  512. raw[c + 2]
  513. );
  514. break;
  515. case SEA3D.Animation.ROTATION:
  516. c = f * 4;
  517. ct.quaternion.set(
  518. raw[c ],
  519. raw[c + 1],
  520. raw[c + 2],
  521. raw[c + 3]
  522. );
  523. break;
  524. }
  525. }
  526. this.updateMatrix( ct );
  527. for (t = 0; t < t_anm.length; t++) {
  528. var raw = t_anm[t].raw,
  529. kind = t_anm[t].kind;
  530. switch(kind) {
  531. case SEA3D.Animation.POSITION:
  532. c = f * 3;
  533. raw[c ] = ct.position.x;
  534. raw[c + 1] = ct.position.y;
  535. raw[c + 2] = ct.position.z;
  536. break;
  537. case SEA3D.Animation.ROTATION:
  538. c = f * 4;
  539. raw[c ] = ct.quaternion.x;
  540. raw[c + 1] = ct.quaternion.y;
  541. raw[c + 2] = ct.quaternion.z;
  542. raw[c + 3] = ct.quaternion.w;
  543. break;
  544. }
  545. }
  546. }
  547. obj3d.remove( ct );
  548. }
  549. }
  550. anmSet.flip = true;
  551. }
  552. THREE.SEA3D.prototype.vectorToColor = function(list) {
  553. var n = [];
  554. var i = 0, j = 0;
  555. var r, g, b, a;
  556. while(i < list.length) {
  557. r = list[i++] * 0xFF;
  558. g = list[i++] * 0xFF;
  559. b = list[i++] * 0xFF;
  560. a = list[i++] * 0xFF;
  561. n[j++] = new THREE.Color(a << 24 | r << 16 | g << 8 | b);
  562. }
  563. return n;
  564. }
  565. THREE.SEA3D.prototype.vectorToUV = function(list) {
  566. var uvs = [];
  567. for(var ch=0;ch<list.length;ch++) {
  568. var uv_ch = uvs[ch] = [];
  569. var uv = list[ch];
  570. for(var i=0,j=0;i<uv.length;i+=2) {
  571. uv_ch[j++] = new THREE.Vector2(uv[i], uv[i+1]);
  572. }
  573. }
  574. return uvs;
  575. }
  576. THREE.SEA3D.prototype.toVector3 = function(data) {
  577. return new THREE.Vector3(data.x, data.y, data.z);
  578. }
  579. THREE.SEA3D.prototype.scaleColor = function(color, scale) {
  580. var r = (color >> 16) * scale;
  581. var g = (color >> 8 & 0xFF) * scale;
  582. var b = (color & 0xFF) * scale;
  583. return (r << 16 | g << 8 | b);
  584. }
  585. THREE.SEA3D.prototype.updateScene = function () {
  586. if (this.materials != undefined) {
  587. for(var i = 0, l = this.materials.length; i < l; ++i) {
  588. this.materials[i].needsUpdate = true;
  589. }
  590. }
  591. }
  592. THREE.SEA3D.prototype.addSceneObject = function(sea) {
  593. var obj3d = sea.tag;
  594. obj3d.props = sea.properties;
  595. if (sea.scripts)
  596. {
  597. for(var i = 0; i < sea.scripts.length; i++)
  598. {
  599. var script = sea.scripts[i];
  600. if (script.tag.type == 'js')
  601. this.runJS( obj3d, script );
  602. }
  603. }
  604. if (sea.parent)
  605. sea.parent.tag.add( obj3d );
  606. else if (this.config.container)
  607. this.config.container.add( obj3d );
  608. else
  609. this.container.add( obj3d );
  610. }
  611. THREE.SEA3D.prototype.bufferToTexture = function(raw) {
  612. return "data:image/png;base64," + SEA3D.Stream.bufferToBase64(raw);
  613. }
  614. THREE.SEA3D.prototype.bufferToSound = function(raw) {
  615. return "data:audio/mp3;base64," + SEA3D.Stream.bufferToBase64(raw);
  616. }
  617. THREE.SEA3D.prototype.applyDefaultAnimation = function(sea, ANIMATOR_CLASS) {
  618. var obj = sea.tag;
  619. for(var i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i++) {
  620. var anm = sea.animations[i];
  621. switch(anm.tag.type) {
  622. case SEA3D.Animation.prototype.type:
  623. obj.animation = new ANIMATOR_CLASS(obj, anm.tag.tag);
  624. obj.animation.setRelative( anm.relative );
  625. if (this.config.flip)
  626. this.updateAnimationSet(obj);
  627. if (this.config.autoPlay)
  628. obj.animation.play( obj.animation.getStateNameByIndex(0) );
  629. return obj.animation;
  630. break;
  631. }
  632. }
  633. }
  634. //
  635. // Animation
  636. //
  637. THREE.SEA3D.prototype.readAnimation = function(sea) {
  638. var anmSet = new SEA3D.AnimationSet();
  639. for(var i = 0; i < sea.sequence.length; i++) {
  640. var seq = sea.sequence[i],
  641. node = new SEA3D.AnimationNode(seq.name, sea.frameRate, seq.count, seq.repeat, seq.intrpl);
  642. for(var j = 0; j < sea.dataList.length; j++) {
  643. var anmData = sea.dataList[j];
  644. node.addData( new SEA3D.AnimationData(anmData.kind, anmData.type, anmData.data, seq.start * anmData.blockSize) );
  645. }
  646. anmSet.addAnimation( node );
  647. }
  648. this.animationSets = this.animationSets || [];
  649. this.animationSets.push(this.objects[sea.name + '.#anm'] = sea.tag = anmSet);
  650. }
  651. //
  652. // Object3D Animator
  653. //
  654. THREE.SEA3D.Object3DAnimator = function(object3d, animationSet) {
  655. SEA3D.AnimationHandler.call( this, animationSet );
  656. this.object3d = object3d;
  657. }
  658. THREE.SEA3D.Object3DAnimator.prototype = Object.create( SEA3D.AnimationHandler.prototype );
  659. THREE.SEA3D.Object3DAnimator.prototype.STOP = THREE.SEA3D.Object3DAnimator.prototype.stop;
  660. THREE.SEA3D.Object3DAnimator.prototype.stop = function() {
  661. if (this.relative) {
  662. this.object3d.animatePosition = new THREE.Vector3();
  663. this.object3d.animateQuaternion = new THREE.Quaternion();
  664. this.object3d.animateScale = new THREE.Vector3(1,1,1);
  665. }
  666. this.STOP();
  667. }
  668. THREE.SEA3D.Object3DAnimator.prototype.setRelative = function(val) {
  669. this.object3d.setAnimateMatrix( this.relative = val );
  670. }
  671. THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame = function(frame, kind) {
  672. if (this.relative) {
  673. switch(kind) {
  674. case SEA3D.Animation.POSITION:
  675. var v = frame.toVector();
  676. this.object3d.animatePosition.set(v.x, v.y, v.z);
  677. break;
  678. case SEA3D.Animation.ROTATION:
  679. var v = frame.toVector();
  680. this.object3d.animateQuaternion.set(v.x, v.y, v.z, v.w);
  681. break;
  682. case SEA3D.Animation.SCALE:
  683. var v = frame.toVector();
  684. this.object3d.animateScale.set(v.x, v.y, v.z);
  685. break;
  686. }
  687. this.object3d.matrixWorldNeedsUpdate = true;
  688. } else {
  689. switch(kind) {
  690. case SEA3D.Animation.POSITION:
  691. var v = frame.toVector();
  692. this.object3d.position.set(v.x, v.y, v.z);
  693. break;
  694. case SEA3D.Animation.ROTATION:
  695. var v = frame.toVector();
  696. this.object3d.quaternion.set(v.x, v.y, v.z, v.w);
  697. break;
  698. case SEA3D.Animation.SCALE:
  699. var v = frame.toVector();
  700. this.object3d.scale.set(v.x, v.y, v.z);
  701. break;
  702. }
  703. }
  704. }
  705. //
  706. // Camera Animator
  707. //
  708. THREE.SEA3D.CameraAnimator = function(object3d, animationSet) {
  709. THREE.SEA3D.Object3DAnimator.call( this, object3d, animationSet );
  710. }
  711. THREE.SEA3D.CameraAnimator.prototype = Object.create( THREE.SEA3D.Object3DAnimator.prototype );
  712. THREE.SEA3D.CameraAnimator.prototype.updateAnimationFrame = function(frame, kind) {
  713. switch(kind) {
  714. case SEA3D.Animation.FOV:
  715. this.object3d.fov = frame.getX();
  716. break;
  717. default:
  718. this.$updateAnimationFrame(frame, kind);
  719. break;
  720. }
  721. }
  722. THREE.SEA3D.CameraAnimator.prototype.$updateAnimationFrame = THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame;
  723. //
  724. // Light Animator
  725. //
  726. THREE.SEA3D.LightAnimator = function(object3d, animationSet) {
  727. THREE.SEA3D.Object3DAnimator.call( this, object3d, animationSet );
  728. }
  729. THREE.SEA3D.LightAnimator.prototype = Object.create( THREE.SEA3D.Object3DAnimator.prototype );
  730. THREE.SEA3D.LightAnimator.prototype.updateAnimationFrame = function(frame, kind) {
  731. switch(kind) {
  732. case SEA3D.Animation.COLOR:
  733. this.object3d.color.setHex( frame.getX() );
  734. break;
  735. case SEA3D.Animation.MULTIPLIER:
  736. this.object3d.intensity = frame.getX();
  737. break;
  738. default:
  739. this.$updateAnimationFrame(frame, kind);
  740. break;
  741. }
  742. }
  743. THREE.SEA3D.LightAnimator.prototype.$updateAnimationFrame = THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame;
  744. //
  745. // Geometry
  746. //
  747. THREE.SEA3D.prototype.readGeometrySwitch = function(sea) {
  748. if (sea.numVertex < 0xFFFE && !sea.joint && (!sea.uv || sea.uv.length === 1) && sea.indexes.length === 1)
  749. {
  750. this.readGeometryBuffer(sea);
  751. }
  752. else
  753. {
  754. this.readGeometry(sea);
  755. }
  756. }
  757. THREE.SEA3D.prototype.readGeometryBuffer = function(sea) {
  758. var index = sea.indexes[0],
  759. count = index.length,
  760. geo = new THREE.BufferGeometry();
  761. var indices, position, normals, uv, colors;
  762. geo.attributes = {
  763. index : {
  764. itemSize: 1,
  765. array: indices = new Uint16Array( count )
  766. },
  767. position : {
  768. itemSize: 3,
  769. array: position = new Float32Array( count * 3 )
  770. },
  771. uv : {
  772. itemSize: 2,
  773. array: uv = new Float32Array( count * 2 )
  774. }
  775. }
  776. var a, b, c,
  777. v = sea.vertex,
  778. n = sea.normal,
  779. t = sea.tangent,
  780. u = sea.uv ? sea.uv[0] : undefined;
  781. var vc = sea.color ? sea.color[0] : undefined;
  782. if (n)
  783. {
  784. geo.attributes.normal = {
  785. itemSize: 3,
  786. array: normals = new Float32Array( count * 3 )
  787. }
  788. }
  789. if (t)
  790. {
  791. geo.attributes.tangent = {
  792. itemSize: 3,
  793. array: normals = new Float32Array( count * 3 )
  794. }
  795. }
  796. if (vc)
  797. {
  798. geo.attributes.color = {
  799. itemSize: 3,
  800. array: colors = new Float32Array( count * 3 )
  801. }
  802. }
  803. var flip = this.config.flip ? -1 : 1;
  804. for (var f = 0, vt = 0, vu=0; f < count; f+=3, vt+=9, vu+=6) {
  805. // index
  806. a = index[ f ] * 3;
  807. b = index[ f + 2 ] * 3;
  808. c = index[ f + 1 ] * 3;
  809. // position
  810. position[ vt ] = v[ a ];
  811. position[ vt + 1 ] = v[ a + 1 ];
  812. position[ vt + 2 ] = v[ a + 2 ] * flip;
  813. position[ vt + 3 ] = v[ b ];
  814. position[ vt + 4 ] = v[ b + 1 ];
  815. position[ vt + 5 ] = v[ b + 2 ] * flip;
  816. position[ vt + 6 ] = v[ c ];
  817. position[ vt + 7 ] = v[ c + 1 ];
  818. position[ vt + 8 ] = v[ c + 2 ] * flip;
  819. // normal
  820. if (n)
  821. {
  822. normals[ vt ] = n[ a ];
  823. normals[ vt + 1 ] = n[ a + 1 ];
  824. normals[ vt + 2 ] = n[ a + 2 ] * flip;
  825. normals[ vt + 3 ] = n[ b ];
  826. normals[ vt + 4 ] = n[ b + 1 ];
  827. normals[ vt + 5 ] = n[ b + 2 ] * flip;
  828. normals[ vt + 6 ] = n[ c ];
  829. normals[ vt + 7 ] = n[ c + 1 ];
  830. normals[ vt + 8 ] = n[ c + 2 ] * flip;
  831. }
  832. // tangent
  833. if (t)
  834. {
  835. tangent[ vt ] = t[ a ];
  836. tangent[ vt + 1 ] = t[ a + 1 ];
  837. tangent[ vt + 2 ] = t[ a + 2 ] * flip;
  838. tangent[ vt + 3 ] = t[ b ];
  839. tangent[ vt + 4 ] = t[ b + 1 ];
  840. tangent[ vt + 5 ] = t[ b + 2 ] * flip;
  841. tangent[ vt + 6 ] = t[ c ];
  842. tangent[ vt + 7 ] = t[ c + 1 ];
  843. tangent[ vt + 8 ] = t[ c + 2 ] * flip;
  844. }
  845. // uv
  846. if (u)
  847. {
  848. a = index[ f ] * 2;
  849. b = index[ f + 2 ] * 2;
  850. c = index[ f + 1 ] * 2;
  851. uv[ vu ] = u[ a ];
  852. uv[ vu + 1 ] = u[ a + 1 ];
  853. uv[ vu + 2 ] = u[ b ];
  854. uv[ vu + 3 ] = u[ b + 1 ];
  855. uv[ vu + 4 ] = u[ c ];
  856. uv[ vu + 5 ] = u[ c + 1 ];
  857. }
  858. else
  859. {
  860. uv[ vu ] = 0;
  861. uv[ vu + 1 ] = 0;
  862. uv[ vu + 2 ] = 0;
  863. uv[ vu + 3 ] = 1;
  864. uv[ vu + 4 ] = 1;
  865. uv[ vu + 5 ] = 1;
  866. }
  867. // colors
  868. if (vc)
  869. {
  870. a = index[ f ] * 4;
  871. b = index[ f + 2 ] * 4;
  872. c = index[ f + 1 ] * 4;
  873. colors[ vt ] = vc[ a ];
  874. colors[ vt + 1 ] = vc[ a + 1 ];
  875. colors[ vt + 2 ] = vc[ a + 2 ];
  876. colors[ vt + 3 ] = vc[ b ];
  877. colors[ vt + 4 ] = vc[ b + 1 ];
  878. colors[ vt + 5 ] = vc[ b + 2 ];
  879. colors[ vt + 6 ] = vc[ c ];
  880. colors[ vt + 7 ] = vc[ c + 1 ];
  881. colors[ vt + 8 ] = vc[ c + 2 ];
  882. }
  883. // indices
  884. indices[ f ] = f;
  885. indices[ f + 1 ] = f + 1;
  886. indices[ f + 2 ] = f + 2;
  887. }
  888. if (sea.numVertex >= 0xFFFE) {
  889. console.warn( "SEA3D: Big Mesh is not compatible with geometry buffer." );
  890. //geo.computeOffsets();
  891. }
  892. if (!n)
  893. geo.computeVertexNormals();
  894. if (this.config.tangent && !sea.tangent)
  895. geo.computeTangents();
  896. if (this.config.bounding)
  897. {
  898. geo.computeBoundingBox();
  899. geo.computeBoundingSphere();
  900. }
  901. geo.name = sea.name;
  902. sea.tag = geo;
  903. }
  904. THREE.SEA3D.prototype.readGeometry = function(sea) {
  905. var i, j, k, l,
  906. geo = new THREE.Geometry(),
  907. vertex, normal, tangent, color, uv;
  908. vertex = geo.vertices = this.config.flip ? this.vectorToVector3Inv(sea.vertex) : this.vectorToVector3(sea.vertex);
  909. if (sea.normal) normal = this.config.flip ? this.vectorToVector3Inv(sea.normal) : this.vectorToVector3(sea.normal);
  910. if (sea.tangent) tangent = this.config.flip ? this.vectorToVector3Inv(sea.tangent) : this.vectorToVector3(sea.tangent);
  911. if (sea.color) color = this.vectorToColor(sea.color[0]);
  912. if (sea.uv)
  913. {
  914. uv = this.vectorToUV(sea.uv);
  915. for (k = 0; k < uv.length; k++) {
  916. geo.faceVertexUvs[k] = [];
  917. }
  918. }
  919. for (i = 0; i < sea.indexes.length; i++) {
  920. var indexes = sea.indexes[i];
  921. var num_index = indexes.length / 3;
  922. for (j = 0; j < num_index; j++) {
  923. var index = j * 3,
  924. indexX, indexY, indexZ;
  925. // invert faces order XZY
  926. indexX = indexes[index];
  927. indexZ = indexes[index+1];
  928. indexY = indexes[index+2];
  929. var face = new THREE.Face3( indexX , indexY , indexZ ,
  930. normal ? [
  931. normal[ indexX ] ,
  932. normal[ indexY ] ,
  933. normal[ indexZ ]
  934. ] : undefined,
  935. color ? [
  936. color[ indexX ] ,
  937. color[ indexY ] ,
  938. color[ indexZ ]
  939. ] : undefined,
  940. i // face index
  941. );
  942. if (tangent) {
  943. face.vertexTangents = [
  944. tangent[ indexX ] ,
  945. tangent[ indexY ] ,
  946. tangent[ indexZ ]
  947. ];
  948. }
  949. geo.faces.push(face);
  950. if (uv)
  951. {
  952. for (k = 0; k < uv.length; k++) {
  953. var _uv = [
  954. uv[k][indexX] ,
  955. uv[k][indexY] ,
  956. uv[k][indexZ]
  957. ];
  958. geo.faceVertexUvs[k].push( _uv );
  959. }
  960. }
  961. else
  962. {
  963. geo.faceVertexUvs[ 0 ].push( [
  964. new THREE.Vector2( 0, 0 ),
  965. new THREE.Vector2( 0, 1 ),
  966. new THREE.Vector2( 1, 1 )
  967. ] );
  968. }
  969. }
  970. }
  971. // for skeleton animation
  972. if (sea.joint) {
  973. var indice_buffer = [0,0,0,0];
  974. var weight_buffer = [0,0,0,0];
  975. var jointPerVertex = sea.jointPerVertex;
  976. if (jointPerVertex > 4) {
  977. console.warn( "SEA3D: Joint Per Vertex can not be greater than 4 (currently " + sea.jointPerVertex + "). Using compression for joints.\nTip: Use SEA3D Studio for automatic compression." );
  978. for (k = 0; k < sea.joint.length; k+=jointPerVertex) {
  979. var jointIndex = [0];
  980. // get indices with greater influence
  981. for (l = 1; l < jointPerVertex; l++) {
  982. var w = sea.weight[k + l],
  983. actW = sea.weight[k + jointIndex[0]];
  984. if (w > actW) jointIndex.unshift( l );
  985. else jointIndex.push( l );
  986. }
  987. // diferrence
  988. var w = (1 - ((sea.weight[k + jointIndex[0]] + sea.weight[k + jointIndex[1]] +
  989. sea.weight[k + jointIndex[2]] + sea.weight[k + jointIndex[3]]))) / 4;
  990. // compress
  991. for (l = 0; l < 4; l++) {
  992. i = jointIndex[l];
  993. indice_buffer[l] = sea.joint[k + i];
  994. weight_buffer[l] = sea.weight[k + i] + w;
  995. }
  996. geo.skinIndices.push( new THREE.Vector4( indice_buffer[0], indice_buffer[1], indice_buffer[2], indice_buffer[3] ) );
  997. geo.skinWeights.push( new THREE.Vector4( weight_buffer[0], weight_buffer[1], weight_buffer[2], weight_buffer[3] ) );
  998. }
  999. } else {
  1000. for (k = 0; k < sea.joint.length; k+=jointPerVertex) {
  1001. for (l = 0; l < jointPerVertex; l++) {
  1002. indice_buffer[l] = sea.joint[k + l];
  1003. weight_buffer[l] = sea.weight[k + l];
  1004. }
  1005. geo.skinIndices.push( new THREE.Vector4( indice_buffer[0], indice_buffer[1], indice_buffer[2], indice_buffer[3] ) );
  1006. geo.skinWeights.push( new THREE.Vector4( weight_buffer[0], weight_buffer[1], weight_buffer[2], weight_buffer[3] ) );
  1007. }
  1008. }
  1009. }
  1010. if (!sea.normal)
  1011. {
  1012. geo.computeFaceNormals();
  1013. geo.computeVertexNormals();
  1014. }
  1015. if (this.config.tangent && !sea.tangent)
  1016. geo.computeTangents();
  1017. if (this.config.bounding)
  1018. {
  1019. geo.computeBoundingBox();
  1020. geo.computeBoundingSphere();
  1021. }
  1022. geo.name = sea.name;
  1023. sea.tag = geo;
  1024. }
  1025. //
  1026. // Dummy
  1027. //
  1028. THREE.SEA3D.prototype.readDummy = function(sea) {
  1029. var geo = new THREE.BoxGeometry( sea.width, sea.height, sea.depth, 1, 1, 1 );
  1030. var mat = new THREE.MeshBasicMaterial( { wireframe: true, color: THREE.SEA3D.HELPER_COLOR } );
  1031. var dummy = new THREE.Mesh( geo, mat );
  1032. dummy.name = sea.name;
  1033. this.dummys = this.dummys || [];
  1034. this.dummys.push( this.objects["dmy/" + sea.name] = sea.tag = dummy );
  1035. this.addSceneObject( sea );
  1036. this.updateTransform(dummy, sea);
  1037. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1038. }
  1039. //
  1040. // Line
  1041. //
  1042. THREE.SEA3D.prototype.readLine = function(sea) {
  1043. var geo = new THREE.Geometry();
  1044. geo.vertices = this.config.flip ? this.vectorToVector3Inv(sea.vertex) : this.vectorToVector3(sea.vertex);
  1045. if (sea.closed)
  1046. geo.vertices.push( geo.vertices[0] );
  1047. var line = new THREE.Line( geo, new THREE.LineBasicMaterial( { color: THREE.SEA3D.HELPER_COLOR, linewidth: 3 } ) );
  1048. line.name = sea.name;
  1049. this.lines = this.lines || [];
  1050. this.lines.push( this.objects["line/" + sea.name] = sea.tag = line );
  1051. this.addSceneObject( sea );
  1052. this.updateTransform(line, sea);
  1053. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1054. }
  1055. //
  1056. // Container3D
  1057. //
  1058. THREE.SEA3D.prototype.readContainer3D = function(sea) {
  1059. var container = new THREE.Object3D();
  1060. this.containers = this.containers || [];
  1061. this.containers.push( this.objects["c3d/" + sea.name] = sea.tag = container );
  1062. this.addSceneObject( sea );
  1063. this.updateTransform(container, sea);
  1064. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1065. }
  1066. //
  1067. // Mesh2D | Sprite
  1068. //
  1069. THREE.SEA3D.prototype.readMesh2D = function(sea) {
  1070. var material;
  1071. if ( sea.material )
  1072. {
  1073. if ( !sea.material.tag.sprite )
  1074. {
  1075. material = sea.material.tag.sprite = new THREE.SpriteMaterial();
  1076. material.map = sea.material.tag.map;
  1077. material.map.flipY = true;
  1078. material.color = sea.material.tag.emissive;
  1079. material.opacity = sea.material.tag.opacity;
  1080. material.blending = sea.material.tag.blending;
  1081. }
  1082. else material = sea.material.tag.sprite;
  1083. }
  1084. var sprite = new THREE.Sprite( material );
  1085. sprite.name = sea.name;
  1086. this.sprites = this.sprites || [];
  1087. this.sprites.push( this.objects["m2d/" + sea.name] = sea.tag = sprite );
  1088. this.addSceneObject( sea );
  1089. this.updateTransform(sprite, sea);
  1090. sprite.scale.set( sea.width, sea.height, 1 );
  1091. }
  1092. //
  1093. // Mesh
  1094. //
  1095. THREE.SEA3D.prototype.readMesh = function(sea) {
  1096. var geo = sea.geometry.tag,
  1097. mesh, mat, skeleton, skeletonAnimation, morpher;
  1098. for(var i = 0, count = sea.modifiers ? sea.modifiers.length : 0; i < count; i++) {
  1099. var mod = sea.modifiers[i];
  1100. switch(mod.type)
  1101. {
  1102. case SEA3D.Skeleton.prototype.type:
  1103. skeleton = mod;
  1104. geo.bones = skeleton.tag;
  1105. break;
  1106. case SEA3D.Morph.prototype.type:
  1107. morpher = mod;
  1108. break;
  1109. }
  1110. }
  1111. for(var i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i++) {
  1112. var anm = sea.animations[i];
  1113. switch(anm.tag.type)
  1114. {
  1115. case SEA3D.SkeletonAnimation.prototype.type:
  1116. skeletonAnimation = anm.tag;
  1117. geo.animations = this.getSkeletonAnimation( skeletonAnimation, skeleton );
  1118. break;
  1119. }
  1120. }
  1121. if (sea.material) {
  1122. if (sea.material.length > 1) {
  1123. var mats = [];
  1124. for(var i = 0; i < sea.material.length; i++) {
  1125. mats[i] = sea.material[i].tag;
  1126. mats[i].skinning = skeleton != null;
  1127. mats[i].morphTargets = morpher != null;
  1128. mats[i].morphNormals = false;
  1129. mats[i].vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  1130. }
  1131. mat = new THREE.MeshFaceMaterial( mats );
  1132. } else {
  1133. mat = sea.material[0].tag;
  1134. mat.skinning = skeleton != null;
  1135. mat.morphTargets = morpher != null;
  1136. mat.morphNormals = false;
  1137. mat.vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  1138. }
  1139. }
  1140. if (morpher)
  1141. geo.morphTargets = this.getMorpher( morpher, sea.geometry );
  1142. if (skeleton) {
  1143. mesh = new THREE.SkinnedMesh( geo, mat, false );
  1144. if (skeletonAnimation) {
  1145. mesh.setAnimations( geo.animations );
  1146. if (this.config.autoPlay) mesh.play( mesh.animations[0].name );
  1147. }
  1148. } else {
  1149. mesh = new THREE.Mesh( geo, mat );
  1150. }
  1151. mesh.name = sea.name;
  1152. mesh.castShadow = sea.castShadows;
  1153. mesh.receiveShadow = sea.material ? sea.material[0].receiveShadows : true;
  1154. this.meshes = this.meshes || [];
  1155. this.meshes.push( this.objects["m3d/" + sea.name] = sea.tag = mesh );
  1156. this.addSceneObject( sea );
  1157. this.updateTransform(mesh, sea);
  1158. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1159. }
  1160. //
  1161. // Sound Point
  1162. //
  1163. THREE.SEA3D.prototype.readSoundPoint = function(sea) {
  1164. var sound3d = new THREE.Sound3D( sea.sound.tag, sea.volume, sea.distance );
  1165. if (sea.autoPlay) {
  1166. sound3d.loop = true;
  1167. sound3d.play();
  1168. }
  1169. sound3d.name = sea.name;
  1170. this.sounds3d = this.sounds3d || [];
  1171. this.sounds3d.push( this.objects["sn3d/" + sea.name] = sea.tag = sound3d );
  1172. this.addSceneObject( sea );
  1173. this.updateTransform(sound3d, sea);
  1174. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1175. }
  1176. //
  1177. // Cube Render
  1178. //
  1179. THREE.SEA3D.prototype.readCubeRender = function(sea) {
  1180. var cube = new THREE.CubeCamera( 0.1, 5000, THREE.SEA3D.TEXTURE_SIZE );
  1181. cube.renderTarget.cubeCamera = cube;
  1182. this.cubeRenderers = this.cubeRenderers || [];
  1183. this.cubeRenderers.push( this.objects["rttc/" + sea.name] = sea.tag = cube.renderTarget );
  1184. this.addSceneObject( sea );
  1185. this.updateTransform(cube, sea);
  1186. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1187. }
  1188. //
  1189. // Images (WDP, JPEG, PNG and GIF)
  1190. //
  1191. THREE.SEA3D.prototype.readImage = function(sea) {
  1192. var image = new Image(), texture = new THREE.Texture(), scope = this;
  1193. texture.name = sea.name;
  1194. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1195. texture.flipY = false;
  1196. image.onload = function () {
  1197. if (!scope.isPowerOfTwo(image.width) ||
  1198. !scope.isPowerOfTwo(image.height))
  1199. {
  1200. var width = scope.nearestPowerOfTwo( image.width ),
  1201. height = scope.nearestPowerOfTwo( image.height );
  1202. var canvas = document.createElement( "canvas" );
  1203. canvas.width = width;
  1204. canvas.height = height;
  1205. var ctx = canvas.getContext( "2d" );
  1206. ctx.drawImage( image, 0, 0, width, height );
  1207. image = canvas;
  1208. }
  1209. texture.image = image;
  1210. texture.needsUpdate = true;
  1211. }
  1212. image.src = this.bufferToTexture( sea.data.buffer );
  1213. this.textures = this.textures || [];
  1214. this.textures.push( this.objects["tex/" + sea.name] = sea.tag = texture );
  1215. }
  1216. //
  1217. // Cube Map
  1218. //
  1219. THREE.SEA3D.prototype.readCubeMap = function(sea) {
  1220. var images = [],
  1221. texture = new THREE.Texture();
  1222. // xyz(- / +) to xyz(+ / -) sequence
  1223. var faces = [];
  1224. faces[0] = sea.faces[1];
  1225. faces[1] = sea.faces[0];
  1226. faces[2] = sea.faces[3];
  1227. faces[3] = sea.faces[2];
  1228. faces[4] = sea.faces[5];
  1229. faces[5] = sea.faces[4];
  1230. images.loadedCount = 0;
  1231. texture.name = sea.name;
  1232. texture.image = images;
  1233. texture.flipY = false;
  1234. for ( var i=0, il=faces.length; i<il; ++i) {
  1235. var cubeImage = new Image();
  1236. images[i] = cubeImage;
  1237. cubeImage.onload = function () {
  1238. if (++images.loadedCount == 6)
  1239. texture.needsUpdate = true;
  1240. }
  1241. cubeImage.src = this.bufferToTexture( faces[i].buffer );
  1242. }
  1243. this.cubmaps = this.cubmaps || [];
  1244. this.cubmaps.push( this.objects["cmap/" + sea.name] = sea.tag = texture );
  1245. }
  1246. //
  1247. // Sound (MP3, OGG)
  1248. //
  1249. THREE.SEA3D.prototype.readSound = function(sea) {
  1250. var sound = this.bufferToSound( sea.data.buffer );
  1251. this.sounds = this.sounds || [];
  1252. this.sounds.push( this.objects["snd/" + sea.name] = sea.tag = sound );
  1253. }
  1254. //
  1255. // Texture URL
  1256. //
  1257. THREE.SEA3D.prototype.readTextureURL = function(sea) {
  1258. var texture = THREE.ImageUtils.loadTexture( sea.url );
  1259. texture.name = sea.name;
  1260. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1261. texture.flipY = false;
  1262. this.textures = this.textures || [];
  1263. this.textures.push( this.objects["tex/" + sea.name] = sea.tag = texture );
  1264. }
  1265. //
  1266. // JavaScript
  1267. //
  1268. SEA3D.GLOBAL = {};
  1269. SEA3D.PRINT = function() { console.log.apply(console, arguments); }
  1270. SEA3D.WATCH = function() {}
  1271. SEA3D.REF = function(ns) { return eval(ns); }
  1272. THREE.SEA3D.prototype.runJS = function(obj, script)
  1273. {
  1274. if (obj.local === undefined) obj.local = {};
  1275. var global =
  1276. {
  1277. print : SEA3D.PRINT,
  1278. watch : SEA3D.WATCH,
  1279. sea3d : this,
  1280. scene : this.container
  1281. }
  1282. try
  1283. {
  1284. this.script[script.method]
  1285. (
  1286. global,
  1287. SEA3D.REF,
  1288. SEA3D.GLOBAL,
  1289. obj.local,
  1290. obj,
  1291. script.params
  1292. )
  1293. }
  1294. catch(e)
  1295. {
  1296. console.error('SEA3D JavaScript: Error running method "' + script.method + '".');
  1297. console.error(e);
  1298. }
  1299. }
  1300. THREE.SEA3D.prototype.readJavaScript = function(sea) {
  1301. try
  1302. {
  1303. var custom =
  1304. 'var watch = $GLOBAL["watch"],\n' +
  1305. 'sea3d = $GLOBAL["sea3d"],\n' +
  1306. 'scene = $GLOBAL["scene"],\n' +
  1307. 'print = $GLOBAL["print"];';
  1308. var src = '(' + sea.src.replace(/"%CUSTOM%"/g, custom) + ')';
  1309. var func = eval(src);
  1310. this.script = func();
  1311. }
  1312. catch(e)
  1313. {
  1314. console.error('SEA3D JavaScript: Error running "' + sea.name + '".');
  1315. console.error(e);
  1316. }
  1317. }
  1318. //
  1319. // GLSL
  1320. //
  1321. THREE.SEA3D.prototype.readGLSL = function(sea) {
  1322. this.glsl = this.glsl || [];
  1323. this.glsl.push( this.objects["glsl/" + sea.name] = sea.tag = sea.src );
  1324. }
  1325. //
  1326. // Material
  1327. //
  1328. THREE.SEA3D.prototype.blendMode = {
  1329. normal:THREE.NormalBlending,
  1330. add:THREE.AdditiveBlending,
  1331. subtract:THREE.SubtractiveBlending,
  1332. multiply:THREE.MultiplyBlending,
  1333. screen:THREE.AdditiveBlending
  1334. }
  1335. THREE.SEA3D.prototype.materialTechnique =
  1336. (function(){
  1337. var techniques = {}
  1338. // DEFAULT
  1339. techniques[SEA3D.Material.DEFAULT] =
  1340. function(tech, mat) {
  1341. mat.emissive.setHex(tech.ambientColor);
  1342. mat.color.setHex(tech.diffuseColor);
  1343. mat.specular.setHex(this.scaleColor(tech.specularColor, tech.specular));
  1344. mat.shininess = tech.gloss;
  1345. }
  1346. // DIFFUSE_MAP
  1347. techniques[SEA3D.Material.DIFFUSE_MAP] =
  1348. function(tech, mat) {
  1349. mat.map = tech.texture.tag;
  1350. mat.transparent = tech.texture.transparent;
  1351. mat.color.setHex(0xFFFFFF);
  1352. }
  1353. // SPECULAR_MAP
  1354. techniques[SEA3D.Material.SPECULAR_MAP] =
  1355. function(tech, mat) {
  1356. mat.specularMap = tech.texture.tag;
  1357. }
  1358. // NORMAL_MAP
  1359. techniques[SEA3D.Material.NORMAL_MAP] =
  1360. function(tech, mat) {
  1361. mat.normalMap = tech.texture.tag;
  1362. }
  1363. // REFLECTION
  1364. techniques[SEA3D.Material.REFLECTION] =
  1365. techniques[SEA3D.Material.FRESNEL_REFLECTION] =
  1366. function(tech, mat) {
  1367. mat.envMap = tech.texture.tag;
  1368. mat.envMap.mapping = THREE.CubeReflectionMapping;
  1369. mat.combine = THREE.MixOperation;
  1370. mat.reflectivity = tech.alpha;
  1371. //if (tech.kind == SEA3D.Material.FRESNEL_REFLECTION) {
  1372. // not implemented
  1373. //}
  1374. }
  1375. // REFLECTION_SPHERICAL
  1376. techniques[SEA3D.Material.REFLECTION_SPHERICAL] =
  1377. function(tech, mat) {
  1378. mat.envMap = tech.texture.tag;
  1379. mat.envMap.mapping = THREE.SphericalReflectionMapping;
  1380. mat.combine = THREE.MixOperation;
  1381. mat.reflectivity = tech.alpha;
  1382. }
  1383. // REFRACTION
  1384. techniques[SEA3D.Material.REFRACTION_MAP] =
  1385. function(tech, mat) {
  1386. mat.envMap = tech.texture.tag;
  1387. mat.envMap.mapping = THREE.CubeRefractionMapping();
  1388. mat.refractionRatio = tech.ior;
  1389. mat.reflectivity = tech.alpha;
  1390. }
  1391. // LIGHT_MAP
  1392. techniques[SEA3D.Material.LIGHT_MAP] =
  1393. function(tech, mat) {
  1394. mat.lightMap = tech.texture.tag;
  1395. }
  1396. return techniques;
  1397. })();
  1398. THREE.SEA3D.prototype.readMaterial = function(sea) {
  1399. var mat = new THREE.MeshPhongMaterial();
  1400. mat.emissiveToAmbientColor = this.config.ambientColor;
  1401. mat.name = sea.name;
  1402. mat.side = sea.bothSides ? THREE.DoubleSide : THREE.FrontSide;
  1403. mat.shading = sea.smooth ? THREE.SmoothShading : THREE.FlatShading;
  1404. if (sea.blendMode != "normal" && this.blendMode[sea.blendMode])
  1405. mat.blending = this.blendMode[sea.blendMode];
  1406. if (sea.alpha < 1 || mat.blending > THREE.NormalBlending) {
  1407. mat.opacity = sea.alpha;
  1408. mat.transparent = true;
  1409. }
  1410. for(var i = 0; i < sea.technique.length; i++) {
  1411. var tech = sea.technique[i];
  1412. if (this.materialTechnique[tech.kind])
  1413. this.materialTechnique[tech.kind].call(this, tech, mat);
  1414. }
  1415. if (mat.transparent) {
  1416. mat.alphaTest = sea.alphaThreshold;
  1417. }
  1418. this.materials = this.materials || [];
  1419. this.materials.push( this.objects["mat/" + sea.name] = sea.tag = mat );
  1420. }
  1421. //
  1422. // Point Light
  1423. //
  1424. THREE.SEA3D.prototype.readPointLight = function(sea) {
  1425. var light = new THREE.PointLight( sea.color, sea.multiplier * this.config.multiplier );
  1426. light.name = sea.name;
  1427. if (sea.shadow)
  1428. this.setShadowMap(light, sea.shadow.opacity);
  1429. this.lights = this.lights || [];
  1430. this.lights.push( this.objects["lht/" + sea.name] = sea.tag = light );
  1431. this.addSceneObject( sea );
  1432. this.updateTransform(light, sea);
  1433. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1434. this.updateScene();
  1435. }
  1436. //
  1437. // Hemisphere Light
  1438. //
  1439. THREE.SEA3D.prototype.readHemisphereLight = function(sea) {
  1440. var light = new THREE.HemisphereLight( sea.color, sea.secondColor, sea.multiplier * this.config.multiplier );
  1441. light.name = sea.name;
  1442. this.lights = this.lights || [];
  1443. this.lights.push( this.objects["lht/" + sea.name] = sea.tag = light );
  1444. this.addSceneObject( sea );
  1445. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1446. this.updateScene();
  1447. }
  1448. //
  1449. // Directional Light
  1450. //
  1451. THREE.SEA3D.prototype.readDirectionalLight = function(sea) {
  1452. var light = new THREE.DirectionalLight( sea.color, sea.multiplier * this.config.multiplier );
  1453. light.name = sea.name;
  1454. if (sea.shadow)
  1455. this.setShadowMap(light, sea.shadow.opacity);
  1456. this.lights = this.lights || [];
  1457. this.lights.push( this.objects["lht/" + sea.name] = sea.tag = light );
  1458. this.addSceneObject( sea );
  1459. this.updateTransform(light, sea);
  1460. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1461. this.updateScene();
  1462. }
  1463. //
  1464. // Camera
  1465. //
  1466. THREE.SEA3D.prototype.readCamera = function(sea) {
  1467. var camera = new THREE.PerspectiveCamera( sea.fov );
  1468. camera.name = sea.name;
  1469. this.cameras = this.camera || [];
  1470. this.cameras.push( this.objects["cam/" + sea.name] = sea.tag = camera );
  1471. this.addSceneObject( sea );
  1472. this.updateTransform(camera, sea);
  1473. this.applyDefaultAnimation( sea, THREE.SEA3D.CameraAnimator );
  1474. }
  1475. //
  1476. // Skeleton
  1477. //
  1478. THREE.SEA3D.prototype.readSkeleton = function(sea) {
  1479. var bones = [],
  1480. mtx_inv = new THREE.Matrix4(),
  1481. mtx = new THREE.Matrix4(),
  1482. mtx_loc = new THREE.Matrix4(),
  1483. pos = new THREE.Vector3(),
  1484. quat = new THREE.Quaternion();
  1485. for (var i = 0; i < sea.joint.length; i++)
  1486. {
  1487. var bone = sea.joint[i]
  1488. mtx_inv.elements = bone.inverseBindMatrix; // get world inverse matrix
  1489. mtx.getInverse( mtx_inv ); // convert to world matrix
  1490. this.flipMatrix(mtx); // convert to three.js order
  1491. if (bone.parentIndex > -1)
  1492. {
  1493. // to world
  1494. mtx_inv.elements = sea.joint[bone.parentIndex].inverseBindMatrix;
  1495. mtx_loc.getInverse( mtx_inv );
  1496. this.flipMatrix(mtx_loc); // convert to three.js order
  1497. // to local
  1498. mtx_loc.getInverse( mtx_loc );
  1499. mtx.multiplyMatrices( mtx_loc, mtx );
  1500. }
  1501. // mtx is local matrix
  1502. pos.setFromMatrixPosition( mtx );
  1503. quat.setFromRotationMatrix( mtx );
  1504. bones[i] = {
  1505. name:bone.name,
  1506. pos:[pos.x, pos.y, pos.z],
  1507. rotq:[quat.x, quat.y, quat.z, quat.w],
  1508. parent:bone.parentIndex
  1509. }
  1510. }
  1511. sea.tag = bones;
  1512. }
  1513. //
  1514. // Skeleton Local
  1515. //
  1516. THREE.SEA3D.prototype.readSkeletonLocal = function(sea) {
  1517. var bones = [];
  1518. for (var i = 0; i < sea.joint.length; i++) {
  1519. var bone = sea.joint[i];
  1520. bones[i] = {
  1521. name:bone.name,
  1522. pos:[bone.x, bone.y, bone.z],
  1523. rotq:[bone.qx, bone.qy, bone.qz, bone.qw],
  1524. parent:bone.parentIndex
  1525. }
  1526. }
  1527. sea.tag = bones;
  1528. }
  1529. //
  1530. // Joint Object
  1531. //
  1532. THREE.SEA3D.prototype.readJointObject = function(sea) {
  1533. var mesh = sea.target.tag,
  1534. bone = mesh.skeleton.bones[sea.joint];
  1535. this.joints = this.joints || [];
  1536. this.joints.push( this.objects["jnt/" + sea.name] = sea.tag = bone );
  1537. }
  1538. //
  1539. // Skeleton Animation
  1540. //
  1541. THREE.SEA3D.prototype.getSkeletonAnimation = function(sea, skl) {
  1542. if (sea.tag) return sea.tag;
  1543. var animations = [],
  1544. delta = sea.frameRate / 1000,
  1545. scale = [1,1,1],
  1546. mtx_inv = new THREE.Matrix4();
  1547. for (var i = 0; i < sea.sequence.length; i++) {
  1548. var seq = sea.sequence[i];
  1549. var start = seq.start;
  1550. var end = start + seq.count;
  1551. var ns = sea.name + "/" + seq.name;
  1552. var animation = {
  1553. name:ns,
  1554. repeat:seq.repeat,
  1555. fps:sea.frameRate,
  1556. JIT:0,
  1557. length:delta * (seq.count - 1),
  1558. hierarchy:[]
  1559. }
  1560. var len = sea.pose[0].length;
  1561. for (var j = 0; j < len; j++) {
  1562. var bone = skl.joint[j],
  1563. node = {parent:bone.parentIndex, keys:[]},
  1564. keys = node.keys,
  1565. time = 0;
  1566. for (var t = start; t < end; t++) {
  1567. var joint = sea.pose[t][j];
  1568. var mtx_global = THREE.SEA3D.BUFFER2.makeRotationFromQuaternion(new THREE.Quaternion(joint.qx, joint.qy, joint.qz, joint.qw));
  1569. mtx_global.setPosition(new THREE.Vector3(joint.x, joint.y, joint.z));
  1570. if (bone.parentIndex > -1)
  1571. {
  1572. // to global
  1573. mtx_inv.elements = skl.joint[bone.parentIndex].inverseBindMatrix;
  1574. var mtx_rect = THREE.SEA3D.BUFFER3.getInverse( mtx_inv );
  1575. mtx_global.multiplyMatrices( mtx_rect, mtx_global );
  1576. // convert to three.js matrix
  1577. this.flipMatrix(mtx_global);
  1578. // To Local
  1579. mtx_rect.getInverse( mtx_inv );
  1580. this.flipMatrix(mtx_rect); // flip parent inverse
  1581. mtx_rect.getInverse( mtx_rect ); // reverse to normal direction
  1582. mtx_global.multiplyMatrices( mtx_rect, mtx_global );
  1583. }
  1584. else
  1585. {
  1586. this.flipMatrix(mtx_global);
  1587. }
  1588. var posQ = THREE.SEA3D.VECBUF0.setFromMatrixPosition(mtx_global);
  1589. var newQ = THREE.SEA3D.QUABUF0.setFromRotationMatrix(mtx_global);
  1590. keys.push({
  1591. time:time,
  1592. pos:[posQ.x, posQ.y, posQ.z],
  1593. rot:[newQ.x, newQ.y, newQ.z, newQ.w],
  1594. scl:scale
  1595. });
  1596. time += delta;
  1597. }
  1598. animation.hierarchy[j] = node;
  1599. }
  1600. animations.push( animation );
  1601. }
  1602. return sea.tag = animations;
  1603. }
  1604. //
  1605. // Morpher
  1606. //
  1607. THREE.SEA3D.prototype.getMorpher = function(sea, geo) {
  1608. var morphs = [],
  1609. flip = this.config.flip ? -1 : 1;
  1610. for(var i = 0; i < sea.node.length; i++) {
  1611. var node = sea.node[i],
  1612. vertex = [];
  1613. var j = 0, k = 0;
  1614. while(j < geo.vertex.length)
  1615. vertex[k++] = new THREE.Vector3(
  1616. (geo.vertex[j] + node.vertex[j++]),
  1617. geo.vertex[j] + node.vertex[j++],
  1618. (geo.vertex[j] + node.vertex[j++]) * flip
  1619. );
  1620. morphs[node.name] = i;
  1621. morphs[i] = {
  1622. name:node.name,
  1623. vertices:vertex
  1624. }
  1625. }
  1626. return morphs;
  1627. }
  1628. //
  1629. // Events
  1630. //
  1631. THREE.SEA3D.Event = {
  1632. LOAD_PROGRESS:"sea3d_progress",
  1633. DOWNLOAD_PROGRESS:"sea3d_download",
  1634. COMPLETE:"sea3d_complete",
  1635. OBJECT_COMPLETE:"sea3d_object",
  1636. ERROR:"sea3d_error"
  1637. }
  1638. THREE.SEA3D.prototype.onProgress = null;
  1639. THREE.SEA3D.prototype.onComplete = function( args ) {
  1640. args.file = this.scope; args.type = THREE.SEA3D.Event.COMPLETE;
  1641. args.file.dispatchEvent(args);
  1642. //console.log("SEA3D:", args.message);
  1643. }
  1644. THREE.SEA3D.prototype.onLoadProgress = function( args ) {
  1645. args.file = this.scope; args.type = THREE.SEA3D.Event.LOAD_PROGRESS;
  1646. args.file.dispatchEvent(args);
  1647. //console.log("SEA3D:", args.progress);
  1648. if (args.file.onProgress) args.file.onProgress( args );
  1649. }
  1650. THREE.SEA3D.prototype.onDownloadProgress = function( args ) {
  1651. args.file = this.scope; args.type = THREE.SEA3D.Event.DOWNLOAD_PROGRESS;
  1652. args.file.dispatchEvent(args);
  1653. //console.log("SEA3D:", args.progress);
  1654. if (args.file.onProgress) args.file.onProgress( args );
  1655. }
  1656. THREE.SEA3D.prototype.onCompleteObject = function( args ) {
  1657. args.file = this.scope; args.type = THREE.SEA3D.Event.OBJECT_COMPLETE;
  1658. args.file.dispatchEvent(args);
  1659. //console.log("SEA3D:", args.object.name + "." + args.object.type);
  1660. }
  1661. THREE.SEA3D.prototype.onError = function( args ) {
  1662. args.file = this.scope; args.type = THREE.SEA3D.Event.ERROR;
  1663. args.file.dispatchEvent(args);
  1664. //console.log("SEA3D:", args.message);
  1665. }
  1666. //
  1667. // Loader
  1668. //
  1669. THREE.SEA3D.prototype.load = function( url ) {
  1670. this.loadBytes();
  1671. this.file.load(url);
  1672. }
  1673. THREE.SEA3D.prototype.loadBytes = function( data ) {
  1674. this.file = new SEA3D.File( data );
  1675. this.file.scope = this;
  1676. this.file.onComplete = this.onComplete;
  1677. this.file.onProgress = this.onLoadProgress;
  1678. this.file.onCompleteObject = this.onCompleteObject;
  1679. this.file.onDownloadProgress = this.onDownloadProgress;
  1680. this.file.onError = this.onError;
  1681. // SEA3D
  1682. switch(this.config.parser)
  1683. {
  1684. case THREE.SEA3D.AUTO:
  1685. this.file.typeRead[SEA3D.Geometry.prototype.type] =
  1686. this.file.typeRead[SEA3D.GeometryDelta.prototype.type] =
  1687. this.readGeometrySwitch;
  1688. break;
  1689. case THREE.SEA3D.BUFFER:
  1690. this.file.typeRead[SEA3D.Geometry.prototype.type] =
  1691. this.file.typeRead[SEA3D.GeometryDelta.prototype.type] =
  1692. this.readGeometryBuffer;
  1693. break;
  1694. default:
  1695. this.file.typeRead[SEA3D.Geometry.prototype.type] =
  1696. this.file.typeRead[SEA3D.GeometryDelta.prototype.type] =
  1697. this.readGeometry;
  1698. break;
  1699. }
  1700. this.file.typeRead[SEA3D.Mesh.prototype.type] = this.readMesh;
  1701. this.file.typeRead[SEA3D.Mesh2D.prototype.type] = this.readMesh2D;
  1702. this.file.typeRead[SEA3D.Container3D.prototype.type] = this.readContainer3D;
  1703. this.file.typeRead[SEA3D.Dummy.prototype.type] = this.readDummy;
  1704. this.file.typeRead[SEA3D.Line.prototype.type] = this.readLine;
  1705. this.file.typeRead[SEA3D.Material.prototype.type] = this.readMaterial;
  1706. this.file.typeRead[SEA3D.PointLight.prototype.type] = this.readPointLight;
  1707. this.file.typeRead[SEA3D.DirectionalLight.prototype.type] = this.readDirectionalLight;
  1708. this.file.typeRead[SEA3D.HemisphereLight.prototype.type] = this.readHemisphereLight;
  1709. this.file.typeRead[SEA3D.Camera.prototype.type] = this.readCamera;
  1710. this.file.typeRead[SEA3D.Skeleton.prototype.type] = this.readSkeleton;
  1711. this.file.typeRead[SEA3D.SkeletonLocal.prototype.type] = this.readSkeletonLocal;
  1712. this.file.typeRead[SEA3D.JointObject.prototype.type] = this.readJointObject;
  1713. this.file.typeRead[SEA3D.CubeMap.prototype.type] = this.readCubeMap;
  1714. this.file.typeRead[SEA3D.CubeRender.prototype.type] = this.readCubeRender;
  1715. this.file.typeRead[SEA3D.Animation.prototype.type] = this.readAnimation;
  1716. this.file.typeRead[SEA3D.SoundPoint.prototype.type] = this.readSoundPoint;
  1717. this.file.typeRead[SEA3D.TextureURL.prototype.type] = this.readTextureURL;
  1718. // UNIVERSAL
  1719. this.file.typeRead[SEA3D.JPEG.prototype.type] = this.readImage;
  1720. this.file.typeRead[SEA3D.JPEG_XR.prototype.type] = this.readImage;
  1721. this.file.typeRead[SEA3D.PNG.prototype.type] = this.readImage;
  1722. this.file.typeRead[SEA3D.GIF.prototype.type] = this.readImage;
  1723. this.file.typeRead[SEA3D.MP3.prototype.type] = this.readSound;
  1724. this.file.typeRead[SEA3D.GLSL.prototype.type] = this.readGLSL;
  1725. this.file.typeRead[SEA3D.JavaScript.prototype.type] = this.readJavaScript;
  1726. if (data) this.file.read();
  1727. }