SceneExporter.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.SceneExporter = function () {};
  5. THREE.SceneExporter.prototype = {
  6. constructor: THREE.SceneExporter,
  7. parse: function ( scene ) {
  8. var position = Vector3String( scene.position );
  9. var rotation = Vector3String( scene.rotation );
  10. var scale = Vector3String( scene.scale );
  11. // todo: extract all scene elements
  12. var nobjects = 0;
  13. var ngeometries = 0;
  14. var nmaterials = 0;
  15. var ntextures = 0;
  16. var objectsArray = [];
  17. var geometriesArray = [];
  18. var materialsArray = [];
  19. var texturesArray = [];
  20. var geometriesMap = {};
  21. var materialsMap = {};
  22. var texturesMap = {};
  23. // todo: make object creation properly recursive
  24. var checkTexture = function ( map ) {
  25. if ( ! map ) return;
  26. if ( ! ( map.id in texturesMap ) ) {
  27. texturesMap[ map.id ] = true;
  28. texturesArray.push( TextureString( map ) );
  29. ntextures += 1;
  30. }
  31. };
  32. scene.traverse( function ( node ) {
  33. if ( node instanceof THREE.Mesh ) {
  34. objectsArray.push( ObjectString( node ) );
  35. nobjects += 1;
  36. if ( ! ( node.geometry.id in geometriesMap ) ) {
  37. geometriesMap[ node.geometry.id ] = true;
  38. geometriesArray.push( GeometryString( node.geometry ) );
  39. ngeometries += 1;
  40. }
  41. if ( ! ( node.material.id in materialsMap ) ) {
  42. materialsMap[ node.material.id ] = true;
  43. materialsArray.push( MaterialString( node.material ) );
  44. nmaterials += 1;
  45. checkTexture( node.material.map );
  46. checkTexture( node.material.envMap );
  47. checkTexture( node.material.lightMap );
  48. checkTexture( node.material.specularMap );
  49. checkTexture( node.material.bumpMap );
  50. checkTexture( node.material.normalMap );
  51. }
  52. }
  53. } );
  54. var objects = generateMultiLineString( objectsArray, ",\n\n\t" );
  55. var geometries = generateMultiLineString( geometriesArray, ",\n\n\t" );
  56. var materials = generateMultiLineString( materialsArray, ",\n\n\t" );
  57. var textures = generateMultiLineString( texturesArray, ",\n\n\t" );
  58. var cameras = "";
  59. var lights = "";
  60. // todo: get somehow these from Viewport's renderer
  61. var bgcolor = ColorString( new THREE.Color( 0xaaaaaa ) );
  62. var bgalpha = 1.0;
  63. var defcamera = LabelString( "default_camera" );
  64. //
  65. function Vector2String( v ) {
  66. return "[" + v.x + "," + v.y + "]";
  67. }
  68. function Vector3String( v ) {
  69. return "[" + v.x + "," + v.y + "," + v.z + "]";
  70. }
  71. function ColorString( c ) {
  72. return "[" + c.r.toFixed( 3 ) + "," + c.g.toFixed( 3 ) + "," + c.b.toFixed( 3 ) + "]";
  73. }
  74. function LabelString( s ) {
  75. return '"' + s + '"';
  76. }
  77. function NumConstantString( c ) {
  78. var constants = [ "NearestFilter", "NearestMipMapNearestFilter" , "NearestMipMapLinearFilter",
  79. "LinearFilter", "LinearMipMapNearestFilter", "LinearMipMapLinearFilter" ];
  80. for ( var i = 0; i < constants.length; i ++ ) {
  81. if ( THREE[ constants[ i ] ] === c ) return LabelString( constants[ i ] );
  82. };
  83. return "";
  84. }
  85. //
  86. function ObjectString( o ) {
  87. var output = [
  88. '\t' + LabelString( getObjectName( o ) ) + ' : {',
  89. ' "geometry" : ' + LabelString( getGeometryName( o.geometry ) ) + ',',
  90. ' "materials": [ ' + LabelString( getMaterialName( o.material ) ) + ' ],',
  91. ' "position" : ' + Vector3String( o.position ) + ',',
  92. ' "rotation" : ' + Vector3String( o.rotation ) + ',',
  93. ' "scale" : ' + Vector3String( o.scale ) + ',',
  94. ' "visible" : ' + o.visible,
  95. '}'
  96. ];
  97. return output.join( '\n\t\t' );
  98. }
  99. function GeometryString( g ) {
  100. if ( g instanceof THREE.SphereGeometry ) {
  101. var output = [
  102. '\t' + LabelString( getGeometryName( g ) ) + ': {',
  103. ' "type" : "sphere",',
  104. ' "radius" : ' + g.radius + ',',
  105. ' "widthSegments" : ' + g.widthSegments + ',',
  106. ' "heightSegments" : ' + g.heightSegments + ',',
  107. '}',
  108. ];
  109. } else if ( g instanceof THREE.CubeGeometry ) {
  110. var output = [
  111. '\t' + LabelString( getGeometryName( g ) ) + ': {',
  112. ' "type" : "cube",',
  113. ' "width" : ' + g.width + ',',
  114. ' "height" : ' + g.height + ',',
  115. ' "depth" : ' + g.depth + ',',
  116. ' "widthSegments" : ' + g.widthSegments + ',',
  117. ' "heightSegments" : ' + g.heightSegments + ',',
  118. ' "depthSegments" : ' + g.depthSegments + ',',
  119. '}',
  120. ];
  121. } else if ( g instanceof THREE.PlaneGeometry ) {
  122. var output = [
  123. '\t' + LabelString( getGeometryName( g ) ) + ': {',
  124. ' "type" : "plane",',
  125. ' "width" : ' + g.width + ',',
  126. ' "height" : ' + g.height + ',',
  127. ' "widthSegments" : ' + g.widthSegments + ',',
  128. ' "heightSegments" : ' + g.heightSegments + ',',
  129. '}',
  130. ];
  131. } else {
  132. var output = [];
  133. }
  134. return generateMultiLineString( output, '\n\t\t' );
  135. }
  136. function MaterialString( m ) {
  137. if ( m instanceof THREE.MeshBasicMaterial ) {
  138. var output = [
  139. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  140. ' "type" : "MeshBasicMaterial",',
  141. ' "parameters" : {',
  142. ' "color" : ' + m.color.getHex() + ',',
  143. m.map ? ' "map" : ' + LabelString( getTextureName( m.map ) ) + ',' : '',
  144. m.envMap ? ' "envMap" : ' + LabelString( getTextureName( m.envMap ) ) + ',' : '',
  145. m.specularMap ? ' "specularMap" : ' + LabelString( getTextureName( m.specularMap ) ) + ',' : '',
  146. m.lightMap ? ' "lightMap" : ' + LabelString( getTextureName( m.lightMap ) ) + ',' : '',
  147. ' "reflectivity" : ' + m.reflectivity + ',',
  148. ' "transparent" : ' + m.transparent + ',',
  149. ' "opacity" : ' + m.opacity + ',',
  150. ' "wireframe" : ' + m.wireframe + ',',
  151. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  152. ' }',
  153. '}',
  154. ];
  155. } else if ( m instanceof THREE.MeshLambertMaterial ) {
  156. var output = [
  157. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  158. ' "type" : "MeshLambertMaterial",',
  159. ' "parameters" : {',
  160. ' "color" : ' + m.color.getHex() + ',',
  161. ' "ambient" : ' + m.ambient.getHex() + ',',
  162. ' "emissive" : ' + m.emissive.getHex() + ',',
  163. m.map ? ' "map" : ' + LabelString( getTextureName( m.map ) ) + ',' : '',
  164. m.envMap ? ' "envMap" : ' + LabelString( getTextureName( m.envMap ) ) + ',' : '',
  165. m.specularMap ? ' "specularMap" : ' + LabelString( getTextureName( m.specularMap ) ) + ',' : '',
  166. m.lightMap ? ' "lightMap" : ' + LabelString( getTextureName( m.lightMap ) ) + ',' : '',
  167. ' "reflectivity" : ' + m.reflectivity + ',',
  168. ' "transparent" : ' + m.transparent + ',',
  169. ' "opacity" : ' + m.opacity + ',',
  170. ' "wireframe" : ' + m.wireframe + ',',
  171. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  172. ' }',
  173. '}',
  174. ];
  175. } else if ( m instanceof THREE.MeshPhongMaterial ) {
  176. var output = [
  177. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  178. ' "type" : "MeshPhongMaterial",',
  179. ' "parameters" : {',
  180. ' "color" : ' + m.color.getHex() + ',',
  181. ' "ambient" : ' + m.ambient.getHex() + ',',
  182. ' "emissive" : ' + m.emissive.getHex() + ',',
  183. ' "specular" : ' + m.specular.getHex() + ',',
  184. ' "shininess" : ' + m.shininess + ',',
  185. m.map ? ' "map" : ' + LabelString( getTextureName( m.map ) ) + ',' : '',
  186. m.envMap ? ' "envMap" : ' + LabelString( getTextureName( m.envMap ) ) + ',' : '',
  187. m.specularMap ? ' "specularMap" : ' + LabelString( getTextureName( m.specularMap ) ) + ',' : '',
  188. m.lightMap ? ' "lightMap" : ' + LabelString( getTextureName( m.lightMap ) ) + ',' : '',
  189. m.normalMap ? ' "normalMap" : ' + LabelString( getTextureName( m.normalMap ) ) + ',' : '',
  190. m.bumpMap ? ' "bumpMap" : ' + LabelString( getTextureName( m.bumpMap ) ) + ',' : '',
  191. ' "bumpScale" : ' + m.bumpScale + ',',
  192. ' "reflectivity" : ' + m.reflectivity + ',',
  193. ' "transparent" : ' + m.transparent + ',',
  194. ' "opacity" : ' + m.opacity + ',',
  195. ' "wireframe" : ' + m.wireframe + ',',
  196. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  197. ' }',
  198. '}',
  199. ];
  200. } else if ( m instanceof THREE.MeshDepthMaterial ) {
  201. var output = [
  202. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  203. ' "type" : "MeshDepthMaterial",',
  204. ' "parameters" : {',
  205. ' "transparent" : ' + m.transparent + ',',
  206. ' "opacity" : ' + m.opacity + ',',
  207. ' "wireframe" : ' + m.wireframe + ',',
  208. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  209. ' }',
  210. '}',
  211. ];
  212. } else if ( m instanceof THREE.MeshNormalMaterial ) {
  213. var output = [
  214. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  215. ' "type" : "MeshNormalMaterial",',
  216. ' "parameters" : {',
  217. ' "transparent" : ' + m.transparent + ',',
  218. ' "opacity" : ' + m.opacity + ',',
  219. ' "wireframe" : ' + m.wireframe + ',',
  220. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  221. ' }',
  222. '}',
  223. ];
  224. } else if ( m instanceof THREE.MeshFaceMaterial ) {
  225. var output = [
  226. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  227. ' "type" : "MeshFaceMaterial",',
  228. ' "parameters" : {}',
  229. '}',
  230. ];
  231. }
  232. return generateMultiLineString( output, '\n\t\t' );
  233. }
  234. function TextureString( t ) {
  235. // here would be also an option to use data URI
  236. // with embedded image from "t.image.src"
  237. // (that's a side effect of using FileReader to load images)
  238. var output = [
  239. '\t' + LabelString( getTextureName( t ) ) + ': {',
  240. ' "url" : "' + t.sourceFile + '",',
  241. ' "repeat" : ' + Vector2String( t.repeat ) + ',',
  242. ' "offset" : ' + Vector2String( t.offset ) + ',',
  243. ' "magFilter" : ' + NumConstantString( t.magFilter ) + ',',
  244. ' "minFilter" : ' + NumConstantString( t.minFilter ) + ',',
  245. ' "anisotropy" : ' + t.anisotropy,
  246. '}',
  247. ];
  248. return generateMultiLineString( output, '\n\t\t' );
  249. }
  250. //
  251. function generateMultiLineString( lines, separator ) {
  252. var cleanLines = [];
  253. for ( var i = 0; i < lines.length; i ++ ) {
  254. if ( lines[ i ] ) cleanLines.push( lines[ i ] );
  255. }
  256. return cleanLines.join( separator );
  257. }
  258. function getObjectName( o ) {
  259. return o.name ? o.name : "Object_" + o.id;
  260. }
  261. function getGeometryName( g ) {
  262. return g.name ? g.name : "Geometry_" + g.id;
  263. }
  264. function getMaterialName( m ) {
  265. return m.name ? m.name : "Material_" + m.id;
  266. }
  267. function getTextureName( t ) {
  268. return t.name ? t.name : "Texture_" + t.id;
  269. }
  270. //
  271. var output = [
  272. '{',
  273. ' "metadata": {',
  274. ' "formatVersion" : 3.1,',
  275. ' "type" : "scene",',
  276. ' "generatedBy" : "SceneExporter",',
  277. ' "objects" : ' + nobjects + ',',
  278. ' "geometries" : ' + ngeometries + ',',
  279. ' "materials" : ' + nmaterials + ',',
  280. ' "textures" : ' + ntextures,
  281. ' },',
  282. '',
  283. ' "urlBaseType": "relativeToScene",',
  284. '',
  285. ' "objects" :',
  286. ' {',
  287. '\t' + objects,
  288. ' },',
  289. '',
  290. ' "geometries" :',
  291. ' {',
  292. '\t' + geometries,
  293. ' },',
  294. '',
  295. ' "materials" :',
  296. ' {',
  297. '\t' + materials,
  298. ' },',
  299. '',
  300. ' "textures" :',
  301. ' {',
  302. '\t' + textures,
  303. ' },',
  304. '',
  305. ' "cameras" :',
  306. ' {',
  307. '\t' + cameras,
  308. ' },',
  309. '',
  310. ' "lights" :',
  311. ' {',
  312. '\t' + lights,
  313. ' },',
  314. '',
  315. ' "transform" :',
  316. ' {',
  317. ' "position" : ' + position + ',',
  318. ' "rotation" : ' + rotation + ',',
  319. ' "scale" : ' + scale,
  320. ' },',
  321. '',
  322. ' "defaults" :',
  323. ' {',
  324. ' "bgcolor" : ' + bgcolor + ',',
  325. ' "bgalpha" : ' + bgalpha + ',',
  326. ' "camera" : ' + defcamera,
  327. ' }',
  328. '}'
  329. ].join( '\n' );
  330. return output;
  331. }
  332. }