SEA3DLoader.js 57 KB

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