SEA3DLoader.js 58 KB

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