SEA3DLoader.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  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 + totalAmbientLight ) * specular + totalSpecularLight + totalEmissiveLight;', // METAL
  24. 'outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + totalAmbientLight ) + totalSpecularLight + totalEmissiveLight;'
  25. ], [
  26. // Replace To
  27. 'outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + totalAmbientLight + totalEmissiveLight ) * specular + totalSpecularLight;', // METAL
  28. 'outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + totalAmbientLight + totalEmissiveLight ) + 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.boneByName = function(name) {
  149. var bones = this.bones;
  150. for(var i = 0, bl = bones.length; i < bl; i++) {
  151. if (name == bones[i].name) {
  152. return bones[i];
  153. }
  154. }
  155. }
  156. THREE.SkinnedMesh.prototype.DISPOSE = THREE.SkinnedMesh.prototype.dispose;
  157. THREE.SkinnedMesh.prototype.dispose = function () {
  158. this.stop();
  159. this.animations = null;
  160. this.DISPOSE();
  161. }
  162. THREE.SkinnedMesh.prototype.CLONE = THREE.SkinnedMesh.prototype.clone;
  163. THREE.SkinnedMesh.prototype.clone = function ( object ) {
  164. var cloned = THREE.SkinnedMesh.prototype.CLONE.call( this, object );
  165. if (cloned.animation)
  166. cloned.animation = this.animation.clone( cloned );
  167. cloned.animations = [];
  168. if (this.geometry.animations) {
  169. var refAnimations = this.geometry.animations;
  170. var nsIndex = refAnimations[0].name.indexOf("/")+1;
  171. for (var i = 0; i < refAnimations.length; i++) {
  172. var name = refAnimations[i].name.substring(nsIndex);
  173. var data = refAnimations[i];
  174. data.initialized = false;
  175. cloned.animations[i] = new THREE.Animation( cloned, data );
  176. cloned.animations[i].loop = refAnimations[i].repeat;
  177. cloned.animations[i].name = name;
  178. }
  179. }
  180. return cloned;
  181. }
  182. //
  183. // Three.JS Skeleton Animation Crossfade Extension
  184. //
  185. THREE.AnimationHandler.crossfade = [];
  186. THREE.AnimationHandler.UPDATE = THREE.AnimationHandler.update;
  187. THREE.AnimationHandler.update = function( dt ) {
  188. var i, cf = THREE.AnimationHandler.crossfade;
  189. // Crossfade
  190. i = 0;
  191. while ( i < cf.length ) {
  192. var mesh = cf[i];
  193. mesh.currentAnimation.weight += dt / mesh.crossfade;
  194. if (mesh.currentAnimation.weight > 1) {
  195. mesh.previousAnimation.weight = 0;
  196. mesh.currentAnimation.weight = 1;
  197. if (mesh.onCrossfadeComplete)
  198. mesh.onCrossfadeComplete( mesh );
  199. cf.splice( i, 1 );
  200. delete mesh.crossfade;
  201. } else
  202. ++i;
  203. mesh.previousAnimation.weight = 1 - mesh.currentAnimation.weight;
  204. }
  205. THREE.AnimationHandler.UPDATE( dt );
  206. }
  207. THREE.AnimationHandler.addCrossfade = function( mesh, crossfade ) {
  208. if (mesh.crossfade !== undefined)
  209. THREE.AnimationHandler.crossfade.splice( THREE.AnimationHandler.crossfade.indexOf( mesh ), 1 );
  210. mesh.crossfade = crossfade;
  211. THREE.AnimationHandler.crossfade.push( mesh );
  212. }
  213. //
  214. // KeyFrame
  215. //
  216. THREE.VertexAnimation = function () {
  217. }
  218. //
  219. // Animation Event Extension
  220. //
  221. THREE.Animation.prototype.STOP = THREE.Animation.prototype.stop;
  222. THREE.Animation.prototype.stop = function() {
  223. if (this.onComplete)
  224. this.onComplete( this );
  225. this.STOP();
  226. }
  227. THREE.Animation.prototype.RESET = THREE.Animation.prototype.reset;
  228. THREE.Animation.prototype.reset = function() {
  229. if (this.onReset)
  230. this.onReset( this );
  231. this.RESET();
  232. }
  233. //
  234. // Sound3D
  235. //
  236. THREE.Sound3D = function( src, volume, distance ) {
  237. THREE.Object3D.call( this );
  238. this.audio = new Audio();
  239. this.audio.src = src;
  240. this.audio.load();
  241. this.distance = distance !== undefined ? distance : 1000;
  242. this.volume = volume !== undefined ? volume : 1;
  243. this.playing = false;
  244. }
  245. THREE.Sound3D.prototype = Object.create( THREE.Object3D.prototype );
  246. THREE.Sound3D.prototype.loop = false;
  247. THREE.Sound3D.prototype.play = function(offset) {
  248. if (offset !== undefined && this.audio.duration > 0)
  249. {
  250. this.audio.currentTime = offset;
  251. }
  252. this.audio.loop = this.loop;
  253. this.audio.play();
  254. if (!this.playing)
  255. {
  256. this.index = THREE.Sound3D.sounds.length;
  257. THREE.Sound3D.sounds.push( this );
  258. this.playing = true;
  259. }
  260. }
  261. THREE.Sound3D.prototype.stop = function() {
  262. if (this.audio.duration > 0)
  263. this.audio.currentTime = 0;
  264. this.pause();
  265. }
  266. THREE.Sound3D.prototype.pause = function() {
  267. this.audio.pause();
  268. if (this.playing)
  269. {
  270. THREE.Sound3D.sounds.splice( this.index, 1 );
  271. this.playing = false;
  272. }
  273. }
  274. THREE.Sound3D.prototype.update = function( camera ) {
  275. var soundPosition = new THREE.Vector3();
  276. soundPosition.setFromMatrixPosition( this.matrixWorld );
  277. var cameraPosition = new THREE.Vector3();
  278. cameraPosition.setFromMatrixPosition( camera.matrixWorld );
  279. var distance = soundPosition.distanceTo( cameraPosition );
  280. var volume = this.volume * (1 - ( distance / (this.distance * 3) ));
  281. this.audio.volume = Math.max(0, Math.min(1, volume));
  282. }
  283. THREE.Sound3D.sounds = [];
  284. THREE.Sound3D.update = function( camera ) {
  285. var sounds = THREE.Sound3D.sounds;
  286. for(var i = 0; i < sounds.length; i++) {
  287. sounds[i].update( camera );
  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. var relative = obj3d.animation.relative;
  474. if (anmSet.flip)
  475. return;
  476. var anms = anmSet.animations;
  477. for (var i = 0; i < anms.length; i++) {
  478. var dataList = anms[i].dataList;
  479. var t_anm = [], j;
  480. for (j = 0; j < dataList.length; j++) {
  481. var data = dataList[j];
  482. var raw = dataList[j].data;
  483. var kind = data.kind;
  484. var numFrames = raw.length / data.blockLength;
  485. switch(kind) {
  486. case SEA3D.Animation.POSITION:
  487. case SEA3D.Animation.ROTATION:
  488. t_anm.push( {
  489. kind : kind,
  490. numFrames : numFrames,
  491. raw : raw
  492. } );
  493. break;
  494. }
  495. }
  496. if (t_anm.length > 0) {
  497. var numFrames = t_anm[0].numFrames,
  498. ct = THREE.SEA3D.CONTAINER,
  499. tar = relative ? obj3d : obj3d.parent;
  500. if (obj3d.animation.relative) {
  501. ct.position.set(0, 0, 0);
  502. ct.rotation.set(0, 0, 0);
  503. ct.scale.set(1, 1, 1);
  504. } else {
  505. ct.position.copy(obj3d.position);
  506. ct.rotation.copy(obj3d.rotation);
  507. ct.scale.copy(obj3d.scale);
  508. }
  509. tar.add( ct );
  510. for (var f = 0, t, c; f < numFrames; f++) {
  511. for (t = 0; t < t_anm.length; t++) {
  512. var raw = t_anm[t].raw,
  513. kind = t_anm[t].kind;
  514. switch(kind) {
  515. case SEA3D.Animation.POSITION:
  516. c = f * 3;
  517. ct.position.set(
  518. raw[c ],
  519. raw[c + 1],
  520. raw[c + 2]
  521. );
  522. break;
  523. case SEA3D.Animation.ROTATION:
  524. c = f * 4;
  525. ct.quaternion.set(
  526. raw[c ],
  527. raw[c + 1],
  528. raw[c + 2],
  529. raw[c + 3]
  530. );
  531. break;
  532. }
  533. }
  534. this.updateMatrix( ct );
  535. for (t = 0; t < t_anm.length; t++) {
  536. var raw = t_anm[t].raw,
  537. kind = t_anm[t].kind;
  538. switch(kind) {
  539. case SEA3D.Animation.POSITION:
  540. c = f * 3;
  541. raw[c ] = ct.position.x;
  542. raw[c + 1] = ct.position.y;
  543. raw[c + 2] = ct.position.z;
  544. break;
  545. case SEA3D.Animation.ROTATION:
  546. c = f * 4;
  547. raw[c ] = ct.quaternion.x;
  548. raw[c + 1] = ct.quaternion.y;
  549. raw[c + 2] = ct.quaternion.z;
  550. raw[c + 3] = ct.quaternion.w;
  551. break;
  552. }
  553. }
  554. }
  555. tar.remove( ct );
  556. }
  557. }
  558. anmSet.flip = true;
  559. }
  560. THREE.SEA3D.prototype.vectorToColor = function(list) {
  561. var n = [];
  562. var i = 0, j = 0;
  563. var r, g, b, a;
  564. while(i < list.length) {
  565. r = list[i++] * 0xFF;
  566. g = list[i++] * 0xFF;
  567. b = list[i++] * 0xFF;
  568. a = list[i++] * 0xFF;
  569. n[j++] = new THREE.Color(a << 24 | r << 16 | g << 8 | b);
  570. }
  571. return n;
  572. }
  573. THREE.SEA3D.prototype.vectorToUV = function(list) {
  574. var uvs = [];
  575. for(var ch=0;ch<list.length;ch++) {
  576. var uv_ch = uvs[ch] = [];
  577. var uv = list[ch];
  578. for(var i=0,j=0;i<uv.length;i+=2) {
  579. uv_ch[j++] = new THREE.Vector2(uv[i], uv[i+1]);
  580. }
  581. }
  582. return uvs;
  583. }
  584. THREE.SEA3D.prototype.toVector3 = function(data) {
  585. return new THREE.Vector3(data.x, data.y, data.z);
  586. }
  587. THREE.SEA3D.prototype.scaleColor = function(color, scale) {
  588. var r = (color >> 16) * scale;
  589. var g = (color >> 8 & 0xFF) * scale;
  590. var b = (color & 0xFF) * scale;
  591. return (r << 16 | g << 8 | b);
  592. }
  593. THREE.SEA3D.prototype.updateScene = function () {
  594. if (this.materials != undefined) {
  595. for(var i = 0, l = this.materials.length; i < l; ++i) {
  596. this.materials[i].needsUpdate = true;
  597. }
  598. }
  599. }
  600. THREE.SEA3D.prototype.addSceneObject = function(sea) {
  601. var obj3d = sea.tag;
  602. obj3d.props = sea.properties;
  603. if (sea.scripts)
  604. {
  605. for(var i = 0; i < sea.scripts.length; i++)
  606. {
  607. var script = sea.scripts[i];
  608. if (script.tag.type == 'js')
  609. this.runJS( obj3d, script );
  610. }
  611. }
  612. if (sea.parent)
  613. sea.parent.tag.add( obj3d );
  614. else if (this.config.container)
  615. this.config.container.add( obj3d );
  616. else
  617. this.container.add( obj3d );
  618. }
  619. THREE.SEA3D.prototype.bufferToTexture = function(raw) {
  620. return "data:image/png;base64," + SEA3D.Stream.bufferToBase64(raw);
  621. }
  622. THREE.SEA3D.prototype.bufferToSound = function(raw) {
  623. return "data:audio/mp3;base64," + SEA3D.Stream.bufferToBase64(raw);
  624. }
  625. THREE.SEA3D.prototype.applyDefaultAnimation = function(sea, ANIMATOR_CLASS) {
  626. var obj = sea.tag;
  627. for(var i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i++) {
  628. var anm = sea.animations[i];
  629. switch(anm.tag.type) {
  630. case SEA3D.Animation.prototype.type:
  631. obj.animation = new ANIMATOR_CLASS(obj, anm.tag.tag);
  632. obj.animation.setRelative( anm.relative );
  633. if (this.config.flip)
  634. this.updateAnimationSet(obj);
  635. if (this.config.autoPlay)
  636. obj.animation.play( obj.animation.getStateNameByIndex(0) );
  637. return obj.animation;
  638. break;
  639. }
  640. }
  641. }
  642. //
  643. // Animation
  644. //
  645. THREE.SEA3D.prototype.readAnimation = function(sea) {
  646. var anmSet = new SEA3D.AnimationSet();
  647. for(var i = 0; i < sea.sequence.length; i++) {
  648. var seq = sea.sequence[i],
  649. node = new SEA3D.AnimationNode(seq.name, sea.frameRate, seq.count, seq.repeat, seq.intrpl);
  650. for(var j = 0; j < sea.dataList.length; j++) {
  651. var anmData = sea.dataList[j];
  652. node.addData( new SEA3D.AnimationData(anmData.kind, anmData.type, anmData.data, seq.start * anmData.blockSize) );
  653. }
  654. anmSet.addAnimation( node );
  655. }
  656. this.animationSets = this.animationSets || [];
  657. this.animationSets.push(this.objects[sea.name + '.#anm'] = sea.tag = anmSet);
  658. }
  659. //
  660. // Object3D Animator
  661. //
  662. THREE.SEA3D.Object3DAnimator = function(object3d, animationSet) {
  663. SEA3D.AnimationHandler.call( this, animationSet );
  664. this.object3d = object3d;
  665. }
  666. THREE.SEA3D.Object3DAnimator.prototype = Object.create( SEA3D.AnimationHandler.prototype );
  667. THREE.SEA3D.Object3DAnimator.prototype.STOP = THREE.SEA3D.Object3DAnimator.prototype.stop;
  668. THREE.SEA3D.Object3DAnimator.prototype.stop = function() {
  669. if (this.relative) {
  670. this.object3d.animatePosition = new THREE.Vector3();
  671. this.object3d.animateQuaternion = new THREE.Quaternion();
  672. this.object3d.animateScale = new THREE.Vector3(1,1,1);
  673. }
  674. this.STOP();
  675. }
  676. THREE.SEA3D.Object3DAnimator.prototype.setRelative = function(val) {
  677. this.object3d.setAnimateMatrix( this.relative = val );
  678. }
  679. THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame = function(frame, kind) {
  680. if (this.relative) {
  681. switch(kind) {
  682. case SEA3D.Animation.POSITION:
  683. var v = frame.toVector();
  684. this.object3d.animatePosition.set(v.x, v.y, v.z);
  685. break;
  686. case SEA3D.Animation.ROTATION:
  687. var v = frame.toVector();
  688. this.object3d.animateQuaternion.set(v.x, v.y, v.z, v.w);
  689. break;
  690. case SEA3D.Animation.SCALE:
  691. var v = frame.toVector();
  692. this.object3d.animateScale.set(v.x, v.y, v.z);
  693. break;
  694. }
  695. this.object3d.matrixWorldNeedsUpdate = true;
  696. } else {
  697. switch(kind) {
  698. case SEA3D.Animation.POSITION:
  699. var v = frame.toVector();
  700. this.object3d.position.set(v.x, v.y, v.z);
  701. break;
  702. case SEA3D.Animation.ROTATION:
  703. var v = frame.toVector();
  704. this.object3d.quaternion.set(v.x, v.y, v.z, v.w);
  705. break;
  706. case SEA3D.Animation.SCALE:
  707. var v = frame.toVector();
  708. this.object3d.scale.set(v.x, v.y, v.z);
  709. break;
  710. }
  711. }
  712. }
  713. //
  714. // Camera Animator
  715. //
  716. THREE.SEA3D.CameraAnimator = function(object3d, animationSet) {
  717. THREE.SEA3D.Object3DAnimator.call( this, object3d, animationSet );
  718. }
  719. THREE.SEA3D.CameraAnimator.prototype = Object.create( THREE.SEA3D.Object3DAnimator.prototype );
  720. THREE.SEA3D.CameraAnimator.prototype.updateAnimationFrame = function(frame, kind) {
  721. switch(kind) {
  722. case SEA3D.Animation.FOV:
  723. this.object3d.fov = frame.getX();
  724. break;
  725. default:
  726. this.$updateAnimationFrame(frame, kind);
  727. break;
  728. }
  729. }
  730. THREE.SEA3D.CameraAnimator.prototype.$updateAnimationFrame = THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame;
  731. //
  732. // Light Animator
  733. //
  734. THREE.SEA3D.LightAnimator = function(object3d, animationSet) {
  735. THREE.SEA3D.Object3DAnimator.call( this, object3d, animationSet );
  736. }
  737. THREE.SEA3D.LightAnimator.prototype = Object.create( THREE.SEA3D.Object3DAnimator.prototype );
  738. THREE.SEA3D.LightAnimator.prototype.updateAnimationFrame = function(frame, kind) {
  739. switch(kind) {
  740. case SEA3D.Animation.COLOR:
  741. this.object3d.color.setHex( frame.getX() );
  742. break;
  743. case SEA3D.Animation.MULTIPLIER:
  744. this.object3d.intensity = frame.getX();
  745. break;
  746. default:
  747. this.$updateAnimationFrame(frame, kind);
  748. break;
  749. }
  750. }
  751. THREE.SEA3D.LightAnimator.prototype.$updateAnimationFrame = THREE.SEA3D.Object3DAnimator.prototype.updateAnimationFrame;
  752. //
  753. // Geometry
  754. //
  755. THREE.SEA3D.prototype.readGeometrySwitch = function(sea) {
  756. if (sea.indexes.length === 1)
  757. this.readGeometryBuffer(sea);
  758. else
  759. this.readGeometry(sea);
  760. }
  761. THREE.SEA3D.prototype.readGeometryBuffer = function(sea) {
  762. var index = sea.indexes[0],
  763. count = index.length,
  764. geo = new THREE.BufferGeometry();
  765. var indices, position, normals, tangents, uv, uv2, colors, skinIndex, skinWeight;
  766. indices = new Uint16Array( count );
  767. position = new Float32Array( count * 3 );
  768. var a, b, c,
  769. v = sea.vertex,
  770. n = sea.normal,
  771. t = sea.tangent,
  772. u = sea.uv ? sea.uv[0] : undefined,
  773. u2 = sea.uv && sea.uv.length > 1 ? sea.uv[1] : undefined,
  774. vc = sea.color ? sea.color[0] : undefined,
  775. sI = sea.joint,
  776. sW = sea.weight;
  777. if (n) normals = new Float32Array( count * 3 );
  778. if (t) tangents = new Float32Array( count * 3 );
  779. if (u) uv = new Float32Array( count * 2 );
  780. if (u2) uv2 = new Float32Array( count * 2 );
  781. if (vc) colors = new Float32Array( count * 3 );
  782. if (sI) {
  783. skinIndex = new Float32Array( count * 4 );
  784. skinWeight = new Float32Array( count * 4 );
  785. var jointPerVertex = Math.min( sea.jointPerVertex, 4 );
  786. var compJointPerVertex = sea.jointPerVertex.length > 4;
  787. }
  788. var flip = this.config.flip ? -1 : 1;
  789. for (var f = 0, vt = 0, vu=0, jt=0; f < count; f+=3, vt+=9, vu+=6, jt+=12) {
  790. // index
  791. a = index[ f ] * 3;
  792. b = index[ f + 2 ] * 3;
  793. c = index[ f + 1 ] * 3;
  794. // position
  795. position[ vt ] = v[ a ];
  796. position[ vt + 1 ] = v[ a + 1 ];
  797. position[ vt + 2 ] = v[ a + 2 ] * flip;
  798. position[ vt + 3 ] = v[ b ];
  799. position[ vt + 4 ] = v[ b + 1 ];
  800. position[ vt + 5 ] = v[ b + 2 ] * flip;
  801. position[ vt + 6 ] = v[ c ];
  802. position[ vt + 7 ] = v[ c + 1 ];
  803. position[ vt + 8 ] = v[ c + 2 ] * flip;
  804. // normal
  805. if (n)
  806. {
  807. normals[ vt ] = n[ a ];
  808. normals[ vt + 1 ] = n[ a + 1 ];
  809. normals[ vt + 2 ] = n[ a + 2 ] * flip;
  810. normals[ vt + 3 ] = n[ b ];
  811. normals[ vt + 4 ] = n[ b + 1 ];
  812. normals[ vt + 5 ] = n[ b + 2 ] * flip;
  813. normals[ vt + 6 ] = n[ c ];
  814. normals[ vt + 7 ] = n[ c + 1 ];
  815. normals[ vt + 8 ] = n[ c + 2 ] * flip;
  816. }
  817. // tangent
  818. if (t)
  819. {
  820. tangents[ vt ] = t[ a ];
  821. tangents[ vt + 1 ] = t[ a + 1 ];
  822. tangents[ vt + 2 ] = t[ a + 2 ] * flip;
  823. tangents[ vt + 3 ] = handleVertex(tangents, vt);
  824. tangents[ vt + 4 ] = t[ b ];
  825. tangents[ vt + 5 ] = t[ b + 1 ];
  826. tangents[ vt + 6 ] = t[ b + 2 ] * flip;
  827. tangents[ vt + 7 ] = handleVertex(tangents + 5, vt);
  828. tangents[ vt + 8 ] = t[ c ];
  829. tangents[ vt + 9 ] = t[ c + 1 ];
  830. tangents[ vt + 10 ] = t[ c + 2 ] * flip;
  831. tangents[ vt + 11 ] = handleVertex(tangents + 8, vt);
  832. }
  833. // uv
  834. if (u)
  835. {
  836. a = index[ f ] * 2;
  837. b = index[ f + 2 ] * 2;
  838. c = index[ f + 1 ] * 2;
  839. uv[ vu ] = u[ a ];
  840. uv[ vu + 1 ] = u[ a + 1 ];
  841. uv[ vu + 2 ] = u[ b ];
  842. uv[ vu + 3 ] = u[ b + 1 ];
  843. uv[ vu + 4 ] = u[ c ];
  844. uv[ vu + 5 ] = u[ c + 1 ];
  845. }
  846. else
  847. {
  848. uv[ vu ] = 0;
  849. uv[ vu + 1 ] = 0;
  850. uv[ vu + 2 ] = 0;
  851. uv[ vu + 3 ] = 1;
  852. uv[ vu + 4 ] = 1;
  853. uv[ vu + 5 ] = 1;
  854. }
  855. // uv2
  856. if (u2)
  857. {
  858. a = index[ f ] * 2;
  859. b = index[ f + 2 ] * 2;
  860. c = index[ f + 1 ] * 2;
  861. uv2[ vu ] = u2[ a ];
  862. uv2[ vu + 1 ] = u2[ a + 1 ];
  863. uv2[ vu + 2 ] = u2[ b ];
  864. uv2[ vu + 3 ] = u2[ b + 1 ];
  865. uv2[ vu + 4 ] = u2[ c ];
  866. uv2[ vu + 5 ] = u2[ c + 1 ];
  867. }
  868. // colors
  869. if (vc)
  870. {
  871. a = index[ f ] * 4;
  872. b = index[ f + 2 ] * 4;
  873. c = index[ f + 1 ] * 4;
  874. colors[ vt ] = vc[ a ];
  875. colors[ vt + 1 ] = vc[ a + 1 ];
  876. colors[ vt + 2 ] = vc[ a + 2 ];
  877. colors[ vt + 3 ] = vc[ b ];
  878. colors[ vt + 4 ] = vc[ b + 1 ];
  879. colors[ vt + 5 ] = vc[ b + 2 ];
  880. colors[ vt + 6 ] = vc[ c ];
  881. colors[ vt + 7 ] = vc[ c + 1 ];
  882. colors[ vt + 8 ] = vc[ c + 2 ];
  883. }
  884. // skin
  885. if (sI)
  886. {
  887. var i = 0, totalA = 0, totalB = 0, totalC = 0;
  888. a = index[ f ] * sea.jointPerVertex;
  889. b = index[ f + 2 ] * sea.jointPerVertex;
  890. c = index[ f + 1 ] * sea.jointPerVertex;
  891. i = 0;
  892. while ( i < jointPerVertex )
  893. {
  894. skinIndex[ jt + i ] = sI[ a + i ];
  895. totalA += skinWeight[ jt + i ] = sW[ a + i ];
  896. skinIndex[ jt + i + 4 ] = sI[ b + i ];
  897. totalB += skinWeight[ jt + i + 4 ] = sW[ b + i ];
  898. skinIndex[ jt + i + 8 ] = sI[ c + i ];
  899. totalC += skinWeight[ jt + i + 8 ] = sW[ c + i ];
  900. ++i;
  901. }
  902. skinWeight[ jt ] += 1 - totalA;
  903. skinWeight[ jt + 4 ] += 1 - totalB;
  904. skinWeight[ jt + 8 ] += 1 - totalC;
  905. }
  906. // indices
  907. indices[ f ] = f;
  908. indices[ f + 1 ] = f + 1;
  909. indices[ f + 2 ] = f + 2;
  910. }
  911. geo.addAttribute( 'position', new THREE.BufferAttribute( position, 3 ) );
  912. geo.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
  913. if (normals) geo.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  914. if (tangents) geo.addAttribute( 'tangent', new THREE.BufferAttribute( tangents, 4 ) );
  915. if (uv) geo.addAttribute( 'uv', new THREE.BufferAttribute( uv, 2 ) );
  916. if (uv2) geo.addAttribute( 'uv2', new THREE.BufferAttribute( uv2, 2 ) );
  917. if (colors) geo.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  918. if (skinIndex) {
  919. geo.addAttribute( 'skinIndex', new THREE.Float32Attribute( skinIndex, 4 ) );
  920. geo.addAttribute( 'skinWeight', new THREE.Float32Attribute( skinWeight, 4 ) );
  921. }
  922. if (sea.numVertex >= 0xFFFE)
  923. geo.computeOffsets();
  924. if (!n)
  925. geo.computeVertexNormals();
  926. if (this.config.tangent && !sea.tangent)
  927. geo.computeTangents();
  928. if (this.config.bounding)
  929. {
  930. geo.computeBoundingBox();
  931. geo.computeBoundingSphere();
  932. }
  933. geo.name = sea.name;
  934. sea.tag = geo;
  935. }
  936. THREE.SEA3D.prototype.readGeometry = function(sea) {
  937. var i, j, k, l,
  938. geo = new THREE.Geometry(),
  939. vertex, normal, tangent, color, uv;
  940. vertex = geo.vertices = this.config.flip ? this.vectorToVector3Inv(sea.vertex) : this.vectorToVector3(sea.vertex);
  941. if (sea.normal) normal = this.config.flip ? this.vectorToVector3Inv(sea.normal) : this.vectorToVector3(sea.normal);
  942. if (sea.tangent) tangent = this.config.flip ? this.vectorToVector3Inv(sea.tangent) : this.vectorToVector3(sea.tangent);
  943. if (sea.color) color = this.vectorToColor(sea.color[0]);
  944. if (sea.uv)
  945. {
  946. uv = this.vectorToUV(sea.uv);
  947. for (k = 0; k < uv.length; k++) {
  948. geo.faceVertexUvs[k] = [];
  949. }
  950. }
  951. for (i = 0; i < sea.indexes.length; i++) {
  952. var indexes = sea.indexes[i];
  953. var num_index = indexes.length / 3;
  954. for (j = 0; j < num_index; j++) {
  955. var index = j * 3,
  956. indexX, indexY, indexZ;
  957. // invert faces order XZY
  958. indexX = indexes[index];
  959. indexZ = indexes[index+1];
  960. indexY = indexes[index+2];
  961. var face = new THREE.Face3( indexX , indexY , indexZ ,
  962. normal ? [
  963. normal[ indexX ] ,
  964. normal[ indexY ] ,
  965. normal[ indexZ ]
  966. ] : undefined,
  967. color ? [
  968. color[ indexX ] ,
  969. color[ indexY ] ,
  970. color[ indexZ ]
  971. ] : undefined,
  972. i // face index
  973. );
  974. if (tangent) {
  975. face.vertexTangents = [
  976. tangent[ indexX ] ,
  977. tangent[ indexY ] ,
  978. tangent[ indexZ ]
  979. ];
  980. }
  981. geo.faces.push(face);
  982. if (uv)
  983. {
  984. for (k = 0; k < uv.length; k++) {
  985. var _uv = [
  986. uv[k][indexX] ,
  987. uv[k][indexY] ,
  988. uv[k][indexZ]
  989. ];
  990. geo.faceVertexUvs[k].push( _uv );
  991. }
  992. }
  993. else
  994. {
  995. geo.faceVertexUvs[ 0 ].push( [
  996. new THREE.Vector2( 0, 0 ),
  997. new THREE.Vector2( 0, 1 ),
  998. new THREE.Vector2( 1, 1 )
  999. ] );
  1000. }
  1001. }
  1002. }
  1003. // for skeleton animation
  1004. if (sea.joint) {
  1005. var indice_buffer = [0,0,0,0];
  1006. var weight_buffer = [0,0,0,0];
  1007. var jointPerVertex = sea.jointPerVertex;
  1008. if (jointPerVertex > 4) {
  1009. 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." );
  1010. for (k = 0; k < sea.joint.length; k+=jointPerVertex) {
  1011. var jointIndex = [0];
  1012. // get indices with greater influence
  1013. for (l = 1; l < jointPerVertex; l++) {
  1014. var w = sea.weight[k + l],
  1015. actW = sea.weight[k + jointIndex[0]];
  1016. if (w > actW) jointIndex.unshift( l );
  1017. else jointIndex.push( l );
  1018. }
  1019. // diferrence
  1020. var w = (1 - ((sea.weight[k + jointIndex[0]] + sea.weight[k + jointIndex[1]] +
  1021. sea.weight[k + jointIndex[2]] + sea.weight[k + jointIndex[3]]))) / 4;
  1022. // compress
  1023. for (l = 0; l < 4; l++) {
  1024. i = jointIndex[l];
  1025. indice_buffer[l] = sea.joint[k + i];
  1026. weight_buffer[l] = sea.weight[k + i] + w;
  1027. }
  1028. geo.skinIndices.push( new THREE.Vector4( indice_buffer[0], indice_buffer[1], indice_buffer[2], indice_buffer[3] ) );
  1029. geo.skinWeights.push( new THREE.Vector4( weight_buffer[0], weight_buffer[1], weight_buffer[2], weight_buffer[3] ) );
  1030. }
  1031. } else {
  1032. for (k = 0; k < sea.joint.length; k+=jointPerVertex) {
  1033. for (l = 0; l < jointPerVertex; l++) {
  1034. indice_buffer[l] = sea.joint[k + l];
  1035. weight_buffer[l] = sea.weight[k + l];
  1036. }
  1037. geo.skinIndices.push( new THREE.Vector4( indice_buffer[0], indice_buffer[1], indice_buffer[2], indice_buffer[3] ) );
  1038. geo.skinWeights.push( new THREE.Vector4( weight_buffer[0], weight_buffer[1], weight_buffer[2], weight_buffer[3] ) );
  1039. }
  1040. }
  1041. }
  1042. if (!sea.normal)
  1043. {
  1044. geo.computeFaceNormals();
  1045. geo.computeVertexNormals();
  1046. }
  1047. if (this.config.tangent && !sea.tangent)
  1048. geo.computeTangents();
  1049. if (this.config.bounding)
  1050. {
  1051. geo.computeBoundingBox();
  1052. geo.computeBoundingSphere();
  1053. }
  1054. geo.name = sea.name;
  1055. sea.tag = geo;
  1056. }
  1057. //
  1058. // Dummy
  1059. //
  1060. THREE.SEA3D.prototype.readDummy = function(sea) {
  1061. var geo = new THREE.BoxGeometry( sea.width, sea.height, sea.depth, 1, 1, 1 );
  1062. var mat = new THREE.MeshBasicMaterial( { wireframe: true, color: THREE.SEA3D.HELPER_COLOR } );
  1063. var dummy = new THREE.Mesh( geo, mat );
  1064. dummy.name = sea.name;
  1065. this.dummys = this.dummys || [];
  1066. this.dummys.push( this.objects["dmy/" + sea.name] = sea.tag = dummy );
  1067. this.addSceneObject( sea );
  1068. this.updateTransform(dummy, sea);
  1069. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1070. }
  1071. //
  1072. // Line
  1073. //
  1074. THREE.SEA3D.prototype.readLine = function(sea) {
  1075. var geo = new THREE.Geometry();
  1076. geo.vertices = this.config.flip ? this.vectorToVector3Inv(sea.vertex) : this.vectorToVector3(sea.vertex);
  1077. if (sea.closed)
  1078. geo.vertices.push( geo.vertices[0] );
  1079. var line = new THREE.Line( geo, new THREE.LineBasicMaterial( { color: THREE.SEA3D.HELPER_COLOR, linewidth: 3 } ) );
  1080. line.name = sea.name;
  1081. this.lines = this.lines || [];
  1082. this.lines.push( this.objects["line/" + sea.name] = sea.tag = line );
  1083. this.addSceneObject( sea );
  1084. this.updateTransform(line, sea);
  1085. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1086. }
  1087. //
  1088. // Container3D
  1089. //
  1090. THREE.SEA3D.prototype.readContainer3D = function(sea) {
  1091. var container = new THREE.Object3D();
  1092. this.containers = this.containers || [];
  1093. this.containers.push( this.objects["c3d/" + sea.name] = sea.tag = container );
  1094. this.addSceneObject( sea );
  1095. this.updateTransform(container, sea);
  1096. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1097. }
  1098. //
  1099. // Mesh2D | Sprite
  1100. //
  1101. THREE.SEA3D.prototype.readMesh2D = function(sea) {
  1102. var material;
  1103. if ( sea.material )
  1104. {
  1105. if ( !sea.material.tag.sprite )
  1106. {
  1107. material = sea.material.tag.sprite = new THREE.SpriteMaterial();
  1108. material.map = sea.material.tag.map;
  1109. material.map.flipY = true;
  1110. material.color = sea.material.tag.emissive;
  1111. material.opacity = sea.material.tag.opacity;
  1112. material.blending = sea.material.tag.blending;
  1113. }
  1114. else material = sea.material.tag.sprite;
  1115. }
  1116. var sprite = new THREE.Sprite( material );
  1117. sprite.name = sea.name;
  1118. this.sprites = this.sprites || [];
  1119. this.sprites.push( this.objects["m2d/" + sea.name] = sea.tag = sprite );
  1120. this.addSceneObject( sea );
  1121. this.updateTransform(sprite, sea);
  1122. sprite.scale.set( sea.width, sea.height, 1 );
  1123. }
  1124. //
  1125. // Mesh
  1126. //
  1127. THREE.SEA3D.prototype.readMesh = function(sea) {
  1128. var geo = sea.geometry.tag,
  1129. mesh, mat, skeleton, skeletonAnimation, morpher, vtxAnm;
  1130. for(var i = 0, count = sea.modifiers ? sea.modifiers.length : 0; i < count; i++) {
  1131. var mod = sea.modifiers[i];
  1132. switch(mod.type)
  1133. {
  1134. case SEA3D.Skeleton.prototype.type:
  1135. skeleton = mod;
  1136. geo.bones = skeleton.tag;
  1137. break;
  1138. case SEA3D.Morph.prototype.type:
  1139. morpher = mod;
  1140. break;
  1141. }
  1142. }
  1143. for(var i = 0, count = sea.animations ? sea.animations.length : 0; i < count; i++) {
  1144. var anm = sea.animations[i];
  1145. switch(anm.tag.type)
  1146. {
  1147. case SEA3D.SkeletonAnimation.prototype.type:
  1148. skeletonAnimation = anm.tag;
  1149. geo.animations = this.getSkeletonAnimation( skeletonAnimation, skeleton );
  1150. break;
  1151. case SEA3D.VertexAnimation.prototype.type:
  1152. vtxAnm = anm.tag;
  1153. break;
  1154. }
  1155. }
  1156. if (sea.material) {
  1157. if (sea.material.length > 1) {
  1158. var mats = [];
  1159. for(var i = 0; i < sea.material.length; i++) {
  1160. mats[i] = sea.material[i].tag;
  1161. mats[i].skinning = skeleton != null;
  1162. mats[i].morphTargets = morpher != null;
  1163. mats[i].morphNormals = false;
  1164. mats[i].vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  1165. }
  1166. mat = new THREE.MeshFaceMaterial( mats );
  1167. } else {
  1168. mat = sea.material[0].tag;
  1169. mat.skinning = skeleton != null;
  1170. mat.morphTargets = morpher != null;
  1171. mat.morphNormals = false;
  1172. mat.vertexColors = sea.geometry.color ? THREE.VertexColors : THREE.NoColors;
  1173. }
  1174. }
  1175. if (morpher)
  1176. geo.morphTargets = this.getMorpher( morpher, sea.geometry );
  1177. if (skeleton) {
  1178. mesh = new THREE.SkinnedMesh( geo, mat, false );
  1179. if (skeletonAnimation) {
  1180. mesh.setAnimations( geo.animations );
  1181. if (this.config.autoPlay) mesh.play( mesh.animations[0].name );
  1182. }
  1183. } else {
  1184. mesh = new THREE.Mesh( geo, mat );
  1185. if (vtxAnm)
  1186. geo.morphTargets = this.getVertexAnimation( vtxAnm );
  1187. }
  1188. mesh.name = sea.name;
  1189. mesh.castShadow = sea.castShadows;
  1190. mesh.receiveShadow = sea.material ? sea.material[0].receiveShadows : true;
  1191. this.meshes = this.meshes || [];
  1192. this.meshes.push( this.objects["m3d/" + sea.name] = sea.tag = mesh );
  1193. this.addSceneObject( sea );
  1194. this.updateTransform(mesh, sea);
  1195. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1196. }
  1197. //
  1198. // Sound Point
  1199. //
  1200. THREE.SEA3D.prototype.readSoundPoint = function(sea) {
  1201. var sound3d = new THREE.Sound3D( sea.sound.tag, sea.volume, sea.distance );
  1202. if (sea.autoPlay) {
  1203. sound3d.loop = true;
  1204. sound3d.play();
  1205. }
  1206. sound3d.name = sea.name;
  1207. this.sounds3d = this.sounds3d || [];
  1208. this.sounds3d.push( this.objects["sn3d/" + sea.name] = sea.tag = sound3d );
  1209. this.addSceneObject( sea );
  1210. this.updateTransform(sound3d, sea);
  1211. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1212. }
  1213. //
  1214. // Cube Render
  1215. //
  1216. THREE.SEA3D.prototype.readCubeRender = function(sea) {
  1217. var cube = new THREE.CubeCamera( 0.1, 5000, THREE.SEA3D.TEXTURE_SIZE );
  1218. cube.renderTarget.cubeCamera = cube;
  1219. this.cubeRenderers = this.cubeRenderers || [];
  1220. this.cubeRenderers.push( this.objects["rttc/" + sea.name] = sea.tag = cube.renderTarget );
  1221. this.addSceneObject( sea );
  1222. this.updateTransform(cube, sea);
  1223. this.applyDefaultAnimation( sea, THREE.SEA3D.Object3DAnimator );
  1224. }
  1225. //
  1226. // Images (WDP, JPEG, PNG and GIF)
  1227. //
  1228. THREE.SEA3D.prototype.readImage = function(sea) {
  1229. var image = new Image(), texture = new THREE.Texture(), scope = this;
  1230. texture.name = sea.name;
  1231. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1232. texture.flipY = false;
  1233. image.onload = function () {
  1234. if (!scope.isPowerOfTwo(image.width) ||
  1235. !scope.isPowerOfTwo(image.height))
  1236. {
  1237. var width = scope.nearestPowerOfTwo( image.width ),
  1238. height = scope.nearestPowerOfTwo( image.height );
  1239. var canvas = document.createElement( "canvas" );
  1240. canvas.width = width;
  1241. canvas.height = height;
  1242. var ctx = canvas.getContext( "2d" );
  1243. ctx.drawImage( image, 0, 0, width, height );
  1244. image = canvas;
  1245. }
  1246. texture.image = image;
  1247. texture.needsUpdate = true;
  1248. }
  1249. image.src = this.bufferToTexture( sea.data.buffer );
  1250. this.textures = this.textures || [];
  1251. this.textures.push( this.objects["tex/" + sea.name] = sea.tag = texture );
  1252. }
  1253. //
  1254. // Cube Map
  1255. //
  1256. THREE.SEA3D.prototype.readCubeMap = function(sea) {
  1257. var images = [],
  1258. texture = new THREE.Texture();
  1259. // xyz(- / +) to xyz(+ / -) sequence
  1260. var faces = [];
  1261. faces[0] = sea.faces[1];
  1262. faces[1] = sea.faces[0];
  1263. faces[2] = sea.faces[3];
  1264. faces[3] = sea.faces[2];
  1265. faces[4] = sea.faces[5];
  1266. faces[5] = sea.faces[4];
  1267. images.loadedCount = 0;
  1268. texture.name = sea.name;
  1269. texture.image = images;
  1270. texture.flipY = false;
  1271. for ( var i=0, il=faces.length; i<il; ++i) {
  1272. var cubeImage = new Image();
  1273. images[i] = cubeImage;
  1274. cubeImage.onload = function () {
  1275. if (++images.loadedCount == 6)
  1276. texture.needsUpdate = true;
  1277. }
  1278. cubeImage.src = this.bufferToTexture( faces[i].buffer );
  1279. }
  1280. this.cubmaps = this.cubmaps || [];
  1281. this.cubmaps.push( this.objects["cmap/" + sea.name] = sea.tag = texture );
  1282. }
  1283. //
  1284. // Sound (MP3, OGG)
  1285. //
  1286. THREE.SEA3D.prototype.readSound = function(sea) {
  1287. var sound = this.bufferToSound( sea.data.buffer );
  1288. this.sounds = this.sounds || [];
  1289. this.sounds.push( this.objects["snd/" + sea.name] = sea.tag = sound );
  1290. }
  1291. //
  1292. // Texture URL
  1293. //
  1294. THREE.SEA3D.prototype.readTextureURL = function(sea) {
  1295. var texture = THREE.ImageUtils.loadTexture( sea.url );
  1296. texture.name = sea.name;
  1297. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  1298. texture.flipY = false;
  1299. this.textures = this.textures || [];
  1300. this.textures.push( this.objects["tex/" + sea.name] = sea.tag = texture );
  1301. }
  1302. //
  1303. // JavaScript
  1304. //
  1305. SEA3D.GLOBAL = {};
  1306. SEA3D.PRINT = function() { console.log.apply(console, arguments); }
  1307. SEA3D.WATCH = function() {}
  1308. SEA3D.REF = function(ns) { return eval(ns); }
  1309. THREE.SEA3D.prototype.runJS = function(obj, script)
  1310. {
  1311. if (obj.local === undefined) obj.local = {};
  1312. var global =
  1313. {
  1314. print : SEA3D.PRINT,
  1315. watch : SEA3D.WATCH,
  1316. sea3d : this,
  1317. scene : this.container
  1318. }
  1319. try
  1320. {
  1321. this.script[script.method]
  1322. (
  1323. global,
  1324. SEA3D.REF,
  1325. SEA3D.GLOBAL,
  1326. obj.local,
  1327. obj,
  1328. script.params
  1329. )
  1330. }
  1331. catch(e)
  1332. {
  1333. console.error('SEA3D JavaScript: Error running method "' + script.method + '".');
  1334. console.error(e);
  1335. }
  1336. }
  1337. THREE.SEA3D.prototype.readJavaScript = function(sea) {
  1338. try
  1339. {
  1340. var custom =
  1341. 'var watch = $GLOBAL["watch"],\n' +
  1342. 'sea3d = $GLOBAL["sea3d"],\n' +
  1343. 'scene = $GLOBAL["scene"],\n' +
  1344. 'print = $GLOBAL["print"];';
  1345. var src = '(' + sea.src.replace(/"%CUSTOM%"/g, custom) + ')';
  1346. var func = eval(src);
  1347. this.script = func();
  1348. }
  1349. catch(e)
  1350. {
  1351. console.error('SEA3D JavaScript: Error running "' + sea.name + '".');
  1352. console.error(e);
  1353. }
  1354. }
  1355. //
  1356. // GLSL
  1357. //
  1358. THREE.SEA3D.prototype.readGLSL = function(sea) {
  1359. this.glsl = this.glsl || [];
  1360. this.glsl.push( this.objects["glsl/" + sea.name] = sea.tag = sea.src );
  1361. }
  1362. //
  1363. // Material
  1364. //
  1365. THREE.SEA3D.prototype.blendMode = {
  1366. normal:THREE.NormalBlending,
  1367. add:THREE.AdditiveBlending,
  1368. subtract:THREE.SubtractiveBlending,
  1369. multiply:THREE.MultiplyBlending,
  1370. screen:THREE.AdditiveBlending
  1371. }
  1372. THREE.SEA3D.prototype.materialTechnique =
  1373. (function(){
  1374. var techniques = {}
  1375. // DEFAULT
  1376. techniques[SEA3D.Material.DEFAULT] =
  1377. function(tech, mat) {
  1378. mat.emissive.setHex(tech.ambientColor);
  1379. mat.color.setHex(tech.diffuseColor);
  1380. mat.specular.setHex(this.scaleColor(tech.specularColor, tech.specular));
  1381. mat.shininess = tech.gloss;
  1382. }
  1383. // DIFFUSE_MAP
  1384. techniques[SEA3D.Material.DIFFUSE_MAP] =
  1385. function(tech, mat) {
  1386. mat.map = tech.texture.tag;
  1387. mat.transparent = tech.texture.transparent;
  1388. mat.color.setHex(0xFFFFFF);
  1389. }
  1390. // SPECULAR_MAP
  1391. techniques[SEA3D.Material.SPECULAR_MAP] =
  1392. function(tech, mat) {
  1393. mat.specularMap = tech.texture.tag;
  1394. }
  1395. // NORMAL_MAP
  1396. techniques[SEA3D.Material.NORMAL_MAP] =
  1397. function(tech, mat) {
  1398. mat.normalMap = tech.texture.tag;
  1399. }
  1400. // REFLECTION
  1401. techniques[SEA3D.Material.REFLECTION] =
  1402. techniques[SEA3D.Material.FRESNEL_REFLECTION] =
  1403. function(tech, mat) {
  1404. mat.envMap = tech.texture.tag;
  1405. mat.envMap.mapping = THREE.CubeReflectionMapping;
  1406. mat.combine = THREE.MixOperation;
  1407. mat.reflectivity = tech.alpha;
  1408. //if (tech.kind == SEA3D.Material.FRESNEL_REFLECTION) {
  1409. // not implemented
  1410. //}
  1411. }
  1412. // REFLECTION_SPHERICAL
  1413. techniques[SEA3D.Material.REFLECTION_SPHERICAL] =
  1414. function(tech, mat) {
  1415. mat.envMap = tech.texture.tag;
  1416. mat.envMap.mapping = THREE.SphericalReflectionMapping;
  1417. mat.combine = THREE.MixOperation;
  1418. mat.reflectivity = tech.alpha;
  1419. }
  1420. // REFRACTION
  1421. techniques[SEA3D.Material.REFRACTION_MAP] =
  1422. function(tech, mat) {
  1423. mat.envMap = tech.texture.tag;
  1424. mat.envMap.mapping = THREE.CubeRefractionMapping();
  1425. mat.refractionRatio = tech.ior;
  1426. mat.reflectivity = tech.alpha;
  1427. }
  1428. // LIGHT_MAP
  1429. techniques[SEA3D.Material.LIGHT_MAP] =
  1430. function(tech, mat) {
  1431. mat.lightMap = tech.texture.tag;
  1432. }
  1433. return techniques;
  1434. })();
  1435. THREE.SEA3D.prototype.readMaterial = function(sea) {
  1436. var mat = new THREE.MeshPhongMaterial();
  1437. mat.emissiveToAmbientColor = this.config.ambientColor;
  1438. mat.name = sea.name;
  1439. mat.side = sea.bothSides ? THREE.DoubleSide : THREE.FrontSide;
  1440. mat.shading = sea.smooth ? THREE.SmoothShading : THREE.FlatShading;
  1441. if (sea.blendMode != "normal" && this.blendMode[sea.blendMode])
  1442. mat.blending = this.blendMode[sea.blendMode];
  1443. if (sea.alpha < 1 || mat.blending > THREE.NormalBlending) {
  1444. mat.opacity = sea.alpha;
  1445. mat.transparent = true;
  1446. }
  1447. for(var i = 0; i < sea.technique.length; i++) {
  1448. var tech = sea.technique[i];
  1449. if (this.materialTechnique[tech.kind])
  1450. this.materialTechnique[tech.kind].call(this, tech, mat);
  1451. }
  1452. if (mat.transparent) {
  1453. mat.alphaTest = sea.alphaThreshold;
  1454. }
  1455. this.materials = this.materials || [];
  1456. this.materials.push( this.objects["mat/" + sea.name] = sea.tag = mat );
  1457. }
  1458. //
  1459. // Point Light
  1460. //
  1461. THREE.SEA3D.prototype.readPointLight = function(sea) {
  1462. var light = new THREE.PointLight( sea.color, sea.multiplier * this.config.multiplier );
  1463. light.name = sea.name;
  1464. if (sea.attenuation) {
  1465. light.distance = sea.attenuation.end;
  1466. }
  1467. if (sea.shadow)
  1468. this.setShadowMap(light, sea.shadow.opacity);
  1469. this.lights = this.lights || [];
  1470. this.lights.push( this.objects["lht/" + sea.name] = sea.tag = light );
  1471. this.addSceneObject( sea );
  1472. this.updateTransform(light, sea);
  1473. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1474. this.updateScene();
  1475. }
  1476. //
  1477. // Hemisphere Light
  1478. //
  1479. THREE.SEA3D.prototype.readHemisphereLight = function(sea) {
  1480. var light = new THREE.HemisphereLight( sea.color, sea.secondColor, sea.multiplier * this.config.multiplier );
  1481. light.name = sea.name;
  1482. this.lights = this.lights || [];
  1483. this.lights.push( this.objects["lht/" + sea.name] = sea.tag = light );
  1484. this.addSceneObject( sea );
  1485. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1486. this.updateScene();
  1487. }
  1488. //
  1489. // Directional Light
  1490. //
  1491. THREE.SEA3D.prototype.readDirectionalLight = function(sea) {
  1492. var light = new THREE.DirectionalLight( sea.color, sea.multiplier * this.config.multiplier );
  1493. light.name = sea.name;
  1494. if (sea.shadow)
  1495. this.setShadowMap(light, sea.shadow.opacity);
  1496. this.lights = this.lights || [];
  1497. this.lights.push( this.objects["lht/" + sea.name] = sea.tag = light );
  1498. this.addSceneObject( sea );
  1499. this.updateTransform(light, sea);
  1500. this.applyDefaultAnimation( sea, THREE.SEA3D.LightAnimator );
  1501. this.updateScene();
  1502. }
  1503. //
  1504. // Camera
  1505. //
  1506. THREE.SEA3D.prototype.readCamera = function(sea) {
  1507. var camera = new THREE.PerspectiveCamera( sea.fov );
  1508. camera.name = sea.name;
  1509. this.cameras = this.camera || [];
  1510. this.cameras.push( this.objects["cam/" + sea.name] = sea.tag = camera );
  1511. this.addSceneObject( sea );
  1512. this.updateTransform(camera, sea);
  1513. this.applyDefaultAnimation( sea, THREE.SEA3D.CameraAnimator );
  1514. }
  1515. //
  1516. // Skeleton
  1517. //
  1518. THREE.SEA3D.prototype.readSkeleton = function(sea) {
  1519. var bones = [],
  1520. mtx_inv = new THREE.Matrix4(),
  1521. mtx = new THREE.Matrix4(),
  1522. mtx_loc = new THREE.Matrix4(),
  1523. pos = new THREE.Vector3(),
  1524. quat = new THREE.Quaternion();
  1525. for (var i = 0; i < sea.joint.length; i++)
  1526. {
  1527. var bone = sea.joint[i]
  1528. mtx_inv.elements = bone.inverseBindMatrix; // get world inverse matrix
  1529. mtx.getInverse( mtx_inv ); // convert to world matrix
  1530. this.flipMatrix(mtx); // convert to three.js order
  1531. if (bone.parentIndex > -1)
  1532. {
  1533. // to world
  1534. mtx_inv.elements = sea.joint[bone.parentIndex].inverseBindMatrix;
  1535. mtx_loc.getInverse( mtx_inv );
  1536. this.flipMatrix(mtx_loc); // convert to three.js order
  1537. // to local
  1538. mtx_loc.getInverse( mtx_loc );
  1539. mtx.multiplyMatrices( mtx_loc, mtx );
  1540. }
  1541. // mtx is local matrix
  1542. pos.setFromMatrixPosition( mtx );
  1543. quat.setFromRotationMatrix( mtx );
  1544. bones[i] = {
  1545. name:bone.name,
  1546. pos:[pos.x, pos.y, pos.z],
  1547. rotq:[quat.x, quat.y, quat.z, quat.w],
  1548. parent:bone.parentIndex
  1549. }
  1550. }
  1551. sea.tag = bones;
  1552. }
  1553. //
  1554. // Skeleton Local
  1555. //
  1556. THREE.SEA3D.prototype.readSkeletonLocal = function(sea) {
  1557. var bones = [];
  1558. for (var i = 0; i < sea.joint.length; i++) {
  1559. var bone = sea.joint[i];
  1560. bones[i] = {
  1561. name:bone.name,
  1562. pos:[bone.x, bone.y, bone.z],
  1563. rotq:[bone.qx, bone.qy, bone.qz, bone.qw],
  1564. parent:bone.parentIndex
  1565. }
  1566. }
  1567. sea.tag = bones;
  1568. }
  1569. //
  1570. // Joint Object
  1571. //
  1572. THREE.SEA3D.prototype.readJointObject = function(sea) {
  1573. var mesh = sea.target.tag,
  1574. bone = mesh.skeleton.bones[sea.joint];
  1575. this.joints = this.joints || [];
  1576. this.joints.push( this.objects["jnt/" + sea.name] = sea.tag = bone );
  1577. }
  1578. //
  1579. // Skeleton Animation
  1580. //
  1581. THREE.SEA3D.prototype.getSkeletonAnimation = function(sea, skl) {
  1582. if (sea.tag) return sea.tag;
  1583. var animations = [],
  1584. delta = sea.frameRate / 1000,
  1585. scale = [1,1,1],
  1586. mtx_inv = new THREE.Matrix4();
  1587. for (var i = 0; i < sea.sequence.length; i++) {
  1588. var seq = sea.sequence[i];
  1589. var start = seq.start;
  1590. var end = start + seq.count;
  1591. var ns = sea.name + "/" + seq.name;
  1592. var animation = {
  1593. name:ns,
  1594. repeat:seq.repeat,
  1595. fps:sea.frameRate,
  1596. JIT:0,
  1597. length:delta * (seq.count - 1),
  1598. hierarchy:[]
  1599. }
  1600. var len = sea.pose[0].length;
  1601. for (var j = 0; j < len; j++) {
  1602. var bone = skl.joint[j],
  1603. node = {parent:bone.parentIndex, keys:[]},
  1604. keys = node.keys,
  1605. time = 0;
  1606. for (var t = start; t < end; t++) {
  1607. var joint = sea.pose[t][j];
  1608. var mtx_global = THREE.SEA3D.BUFFER2.makeRotationFromQuaternion(new THREE.Quaternion(joint.qx, joint.qy, joint.qz, joint.qw));
  1609. mtx_global.setPosition(new THREE.Vector3(joint.x, joint.y, joint.z));
  1610. if (bone.parentIndex > -1)
  1611. {
  1612. // to global
  1613. mtx_inv.elements = skl.joint[bone.parentIndex].inverseBindMatrix;
  1614. var mtx_rect = THREE.SEA3D.BUFFER3.getInverse( mtx_inv );
  1615. mtx_global.multiplyMatrices( mtx_rect, mtx_global );
  1616. // convert to three.js matrix
  1617. this.flipMatrix(mtx_global);
  1618. // To Local
  1619. mtx_rect.getInverse( mtx_inv );
  1620. this.flipMatrix(mtx_rect); // flip parent inverse
  1621. mtx_rect.getInverse( mtx_rect ); // reverse to normal direction
  1622. mtx_global.multiplyMatrices( mtx_rect, mtx_global );
  1623. }
  1624. else
  1625. {
  1626. this.flipMatrix(mtx_global);
  1627. }
  1628. var posQ = THREE.SEA3D.VECBUF0.setFromMatrixPosition(mtx_global);
  1629. var newQ = THREE.SEA3D.QUABUF0.setFromRotationMatrix(mtx_global);
  1630. keys.push({
  1631. time:time,
  1632. pos:[posQ.x, posQ.y, posQ.z],
  1633. rot:[newQ.x, newQ.y, newQ.z, newQ.w],
  1634. scl:scale
  1635. });
  1636. time += delta;
  1637. }
  1638. animation.hierarchy[j] = node;
  1639. }
  1640. animations.push( animation );
  1641. }
  1642. return sea.tag = animations;
  1643. }
  1644. //
  1645. // Morpher
  1646. //
  1647. THREE.SEA3D.prototype.getMorpher = function(sea, geo) {
  1648. var morphs = [],
  1649. flip = this.config.flip ? -1 : 1;
  1650. for(var i = 0; i < sea.node.length; i++) {
  1651. var node = sea.node[i],
  1652. vertex = [];
  1653. var j = 0, k = 0;
  1654. while(j < geo.vertex.length)
  1655. vertex[k++] = new THREE.Vector3(
  1656. (geo.vertex[j] + node.vertex[j++]),
  1657. geo.vertex[j] + node.vertex[j++],
  1658. (geo.vertex[j] + node.vertex[j++]) * flip
  1659. );
  1660. morphs[node.name] = i;
  1661. morphs[i] = {
  1662. name:node.name,
  1663. vertices:vertex
  1664. }
  1665. }
  1666. return morphs;
  1667. }
  1668. //
  1669. // Vertex Animation
  1670. //
  1671. THREE.SEA3D.prototype.getVertexAnimation = function(sea) {
  1672. var vtxAnms = [],
  1673. flip = this.config.flip ? -1 : 1;
  1674. for(var i = 0; i < sea.frame.length; i++) {
  1675. var frame = sea.frame[i],
  1676. vertex = [];
  1677. var j = 0, k = 0;
  1678. while(j < frame.vertex.length)
  1679. vertex[k++] = new THREE.Vector3(
  1680. frame.vertex[j++],
  1681. frame.vertex[j++],
  1682. frame.vertex[j++] * flip
  1683. );
  1684. vtxAnms[i] = {
  1685. vertices:vertex
  1686. }
  1687. }
  1688. return vtxAnms;
  1689. }
  1690. //
  1691. // Events
  1692. //
  1693. THREE.SEA3D.Event = {
  1694. LOAD_PROGRESS:"sea3d_progress",
  1695. DOWNLOAD_PROGRESS:"sea3d_download",
  1696. COMPLETE:"sea3d_complete",
  1697. OBJECT_COMPLETE:"sea3d_object",
  1698. ERROR:"sea3d_error"
  1699. }
  1700. THREE.SEA3D.prototype.onProgress = null;
  1701. THREE.SEA3D.prototype.onComplete = function( args ) {
  1702. args.file = this.scope; args.type = THREE.SEA3D.Event.COMPLETE;
  1703. args.file.dispatchEvent(args);
  1704. //console.log("SEA3D:", args.message);
  1705. }
  1706. THREE.SEA3D.prototype.onLoadProgress = function( args ) {
  1707. args.file = this.scope; args.type = THREE.SEA3D.Event.LOAD_PROGRESS;
  1708. args.file.dispatchEvent(args);
  1709. //console.log("SEA3D:", args.progress);
  1710. if (args.file.onProgress) args.file.onProgress( args );
  1711. }
  1712. THREE.SEA3D.prototype.onDownloadProgress = function( args ) {
  1713. args.file = this.scope; args.type = THREE.SEA3D.Event.DOWNLOAD_PROGRESS;
  1714. args.file.dispatchEvent(args);
  1715. //console.log("SEA3D:", args.progress);
  1716. if (args.file.onProgress) args.file.onProgress( args );
  1717. }
  1718. THREE.SEA3D.prototype.onCompleteObject = function( args ) {
  1719. args.file = this.scope; args.type = THREE.SEA3D.Event.OBJECT_COMPLETE;
  1720. args.file.dispatchEvent(args);
  1721. //console.log("SEA3D:", args.object.name + "." + args.object.type);
  1722. }
  1723. THREE.SEA3D.prototype.onError = function( args ) {
  1724. args.file = this.scope; args.type = THREE.SEA3D.Event.ERROR;
  1725. args.file.dispatchEvent(args);
  1726. //console.log("SEA3D:", args.message);
  1727. }
  1728. //
  1729. // Loader
  1730. //
  1731. THREE.SEA3D.prototype.load = function( url ) {
  1732. this.loadBytes();
  1733. this.file.load(url);
  1734. }
  1735. THREE.SEA3D.prototype.loadBytes = function( data ) {
  1736. this.file = new SEA3D.File( data );
  1737. this.file.scope = this;
  1738. this.file.onComplete = this.onComplete;
  1739. this.file.onProgress = this.onLoadProgress;
  1740. this.file.onCompleteObject = this.onCompleteObject;
  1741. this.file.onDownloadProgress = this.onDownloadProgress;
  1742. this.file.onError = this.onError;
  1743. // SEA3D
  1744. switch(this.config.parser)
  1745. {
  1746. case THREE.SEA3D.AUTO:
  1747. this.file.typeRead[SEA3D.Geometry.prototype.type] =
  1748. this.file.typeRead[SEA3D.GeometryDelta.prototype.type] =
  1749. this.readGeometrySwitch;
  1750. break;
  1751. case THREE.SEA3D.BUFFER:
  1752. this.file.typeRead[SEA3D.Geometry.prototype.type] =
  1753. this.file.typeRead[SEA3D.GeometryDelta.prototype.type] =
  1754. this.readGeometryBuffer;
  1755. break;
  1756. default:
  1757. this.file.typeRead[SEA3D.Geometry.prototype.type] =
  1758. this.file.typeRead[SEA3D.GeometryDelta.prototype.type] =
  1759. this.readGeometry;
  1760. break;
  1761. }
  1762. this.file.typeRead[SEA3D.Mesh.prototype.type] = this.readMesh;
  1763. this.file.typeRead[SEA3D.Mesh2D.prototype.type] = this.readMesh2D;
  1764. this.file.typeRead[SEA3D.Container3D.prototype.type] = this.readContainer3D;
  1765. this.file.typeRead[SEA3D.Dummy.prototype.type] = this.readDummy;
  1766. this.file.typeRead[SEA3D.Line.prototype.type] = this.readLine;
  1767. this.file.typeRead[SEA3D.Material.prototype.type] = this.readMaterial;
  1768. this.file.typeRead[SEA3D.PointLight.prototype.type] = this.readPointLight;
  1769. this.file.typeRead[SEA3D.DirectionalLight.prototype.type] = this.readDirectionalLight;
  1770. this.file.typeRead[SEA3D.HemisphereLight.prototype.type] = this.readHemisphereLight;
  1771. this.file.typeRead[SEA3D.Camera.prototype.type] = this.readCamera;
  1772. this.file.typeRead[SEA3D.Skeleton.prototype.type] = this.readSkeleton;
  1773. this.file.typeRead[SEA3D.SkeletonLocal.prototype.type] = this.readSkeletonLocal;
  1774. this.file.typeRead[SEA3D.JointObject.prototype.type] = this.readJointObject;
  1775. this.file.typeRead[SEA3D.CubeMap.prototype.type] = this.readCubeMap;
  1776. this.file.typeRead[SEA3D.CubeRender.prototype.type] = this.readCubeRender;
  1777. this.file.typeRead[SEA3D.Animation.prototype.type] = this.readAnimation;
  1778. this.file.typeRead[SEA3D.SoundPoint.prototype.type] = this.readSoundPoint;
  1779. this.file.typeRead[SEA3D.TextureURL.prototype.type] = this.readTextureURL;
  1780. // UNIVERSAL
  1781. this.file.typeRead[SEA3D.JPEG.prototype.type] = this.readImage;
  1782. this.file.typeRead[SEA3D.JPEG_XR.prototype.type] = this.readImage;
  1783. this.file.typeRead[SEA3D.PNG.prototype.type] = this.readImage;
  1784. this.file.typeRead[SEA3D.GIF.prototype.type] = this.readImage;
  1785. this.file.typeRead[SEA3D.MP3.prototype.type] = this.readSound;
  1786. this.file.typeRead[SEA3D.GLSL.prototype.type] = this.readGLSL;
  1787. this.file.typeRead[SEA3D.JavaScript.prototype.type] = this.readJavaScript;
  1788. if (data) this.file.read();
  1789. }