SceneExporter.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. var nobjects = 0;
  12. var ngeometries = 0;
  13. var nmaterials = 0;
  14. var ntextures = 0;
  15. var objectsArray = [];
  16. var geometriesArray = [];
  17. var materialsArray = [];
  18. var texturesArray = [];
  19. var fogsArray = [];
  20. var geometriesMap = {};
  21. var materialsMap = {};
  22. var texturesMap = {};
  23. // extract objects, geometries, materials, textures
  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. var linesArray = [];
  33. function createObjectsList( object, pad ) {
  34. for ( var i = 0; i < object.children.length; i ++ ) {
  35. var node = object.children[ i ];
  36. if ( node instanceof THREE.Mesh ) {
  37. linesArray.push( MeshString( node, pad ) );
  38. nobjects += 1;
  39. if ( ! ( node.geometry.id in geometriesMap ) ) {
  40. geometriesMap[ node.geometry.id ] = true;
  41. geometriesArray.push( GeometryString( node.geometry ) );
  42. ngeometries += 1;
  43. }
  44. if ( ! ( node.material.id in materialsMap ) ) {
  45. materialsMap[ node.material.id ] = true;
  46. materialsArray.push( MaterialString( node.material ) );
  47. nmaterials += 1;
  48. checkTexture( node.material.map );
  49. checkTexture( node.material.envMap );
  50. checkTexture( node.material.lightMap );
  51. checkTexture( node.material.specularMap );
  52. checkTexture( node.material.bumpMap );
  53. checkTexture( node.material.normalMap );
  54. }
  55. } else if ( node instanceof THREE.Light ) {
  56. linesArray.push( LightString( node, pad ) );
  57. nobjects += 1;
  58. } else if ( node instanceof THREE.Camera ) {
  59. linesArray.push( CameraString( node, pad ) );
  60. nobjects += 1;
  61. } else if ( node instanceof THREE.Object3D ) {
  62. linesArray.push( ObjectString( node, pad ) );
  63. nobjects += 1;
  64. }
  65. if ( node.children.length > 0 ) {
  66. linesArray.push( PaddingString( pad + 1 ) + '\t\t"children" : {' );
  67. }
  68. createObjectsList( node, pad + 2 );
  69. if ( node.children.length > 0 ) {
  70. linesArray.push( PaddingString( pad + 1 ) + "\t\t}" );
  71. }
  72. linesArray.push( PaddingString( pad ) + "\t\t}" + ( i < object.children.length - 1 ? ",\n" : "" ) );
  73. }
  74. }
  75. createObjectsList( scene, 0 );
  76. var objects = linesArray.join( "\n" );
  77. // extract fog
  78. if ( scene.fog ) {
  79. fogsArray.push( FogString( scene.fog ) );
  80. }
  81. // generate sections
  82. var geometries = generateMultiLineString( geometriesArray, ",\n\n\t" );
  83. var materials = generateMultiLineString( materialsArray, ",\n\n\t" );
  84. var textures = generateMultiLineString( texturesArray, ",\n\n\t" );
  85. var fogs = generateMultiLineString( fogsArray, ",\n\n\t" );
  86. // generate defaults
  87. var activeCamera = null;
  88. scene.traverse( function ( node ) {
  89. if ( node instanceof THREE.Camera && node.userData.active ) {
  90. activeCamera = node;
  91. }
  92. } );
  93. var defcamera = LabelString( activeCamera ? getObjectName( activeCamera ) : "" );
  94. var deffog = LabelString( scene.fog ? getFogName( scene.fog ) : "" );
  95. // templates
  96. function Vector2String( v ) {
  97. return "[" + v.x + "," + v.y + "]";
  98. }
  99. function Vector3String( v ) {
  100. return "[" + v.x + "," + v.y + "," + v.z + "]";
  101. }
  102. function ColorString( c ) {
  103. return "[" + c.r.toFixed( 3 ) + "," + c.g.toFixed( 3 ) + "," + c.b.toFixed( 3 ) + "]";
  104. }
  105. function LabelString( s ) {
  106. return '"' + s + '"';
  107. }
  108. function NumConstantString( c ) {
  109. var constants = [ "NearestFilter", "NearestMipMapNearestFilter", "NearestMipMapLinearFilter",
  110. "LinearFilter", "LinearMipMapNearestFilter", "LinearMipMapLinearFilter" ];
  111. for ( var i = 0; i < constants.length; i ++ ) {
  112. if ( THREE[ constants[ i ] ] === c ) return LabelString( constants[ i ] );
  113. };
  114. return "";
  115. }
  116. function PaddingString( n ) {
  117. var output = "";
  118. for ( var i = 0; i < n; i ++ ) output += "\t";
  119. return output;
  120. }
  121. //
  122. function LightString( o, n ) {
  123. if ( o instanceof THREE.AmbientLight ) {
  124. var output = [
  125. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  126. ' "type" : "AmbientLight",',
  127. ' "color" : ' + o.color.getHex() + ( o.children.length ? ',' : '' )
  128. ];
  129. } else if ( o instanceof THREE.DirectionalLight ) {
  130. var output = [
  131. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  132. ' "type" : "DirectionalLight",',
  133. ' "color" : ' + o.color.getHex() + ',',
  134. ' "intensity" : ' + o.intensity + ',',
  135. ' "direction" : ' + Vector3String( o.position ) + ',',
  136. ' "target" : ' + LabelString( getObjectName( o.target ) ) + ( o.children.length ? ',' : '' )
  137. ];
  138. } else if ( o instanceof THREE.PointLight ) {
  139. var output = [
  140. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  141. ' "type" : "PointLight",',
  142. ' "color" : ' + o.color.getHex() + ',',
  143. ' "intensity" : ' + o.intensity + ',',
  144. ' "position" : ' + Vector3String( o.position ) + ',',
  145. ' "decay" : ' + o.decay + ',',
  146. ' "distance" : ' + o.distance + ( o.children.length ? ',' : '' )
  147. ];
  148. } else if ( o instanceof THREE.SpotLight ) {
  149. var output = [
  150. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  151. ' "type" : "SpotLight",',
  152. ' "color" : ' + o.color.getHex() + ',',
  153. ' "intensity" : ' + o.intensity + ',',
  154. ' "position" : ' + Vector3String( o.position ) + ',',
  155. ' "distance" : ' + o.distance + ',',
  156. ' "angle" : ' + o.angle + ',',
  157. ' "exponent" : ' + o.exponent + ',',
  158. ' "decay" : ' + o.decay + ',',
  159. ' "target" : ' + LabelString( getObjectName( o.target ) ) + ( o.children.length ? ',' : '' )
  160. ];
  161. } else if ( o instanceof THREE.HemisphereLight ) {
  162. var output = [
  163. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  164. ' "type" : "HemisphereLight",',
  165. ' "skyColor" : ' + o.color.getHex() + ',',
  166. ' "groundColor" : ' + o.groundColor.getHex() + ',',
  167. ' "intensity" : ' + o.intensity + ',',
  168. ' "position" : ' + Vector3String( o.position ) + ( o.children.length ? ',' : '' )
  169. ];
  170. } else {
  171. var output = [];
  172. }
  173. return generateMultiLineString( output, '\n\t\t', n );
  174. }
  175. function CameraString( o, n ) {
  176. if ( o instanceof THREE.PerspectiveCamera ) {
  177. var output = [
  178. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  179. ' "type" : "PerspectiveCamera",',
  180. ' "fov" : ' + o.fov + ',',
  181. ' "aspect" : ' + o.aspect + ',',
  182. ' "near" : ' + o.near + ',',
  183. ' "far" : ' + o.far + ',',
  184. ' "position" : ' + Vector3String( o.position ) + ( o.children.length ? ',' : '' )
  185. ];
  186. } else if ( o instanceof THREE.OrthographicCamera ) {
  187. var output = [
  188. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  189. ' "type" : "OrthographicCamera",',
  190. ' "left" : ' + o.left + ',',
  191. ' "right" : ' + o.right + ',',
  192. ' "top" : ' + o.top + ',',
  193. ' "bottom" : ' + o.bottom + ',',
  194. ' "near" : ' + o.near + ',',
  195. ' "far" : ' + o.far + ',',
  196. ' "position" : ' + Vector3String( o.position ) + ( o.children.length ? ',' : '' )
  197. ];
  198. } else {
  199. var output = [];
  200. }
  201. return generateMultiLineString( output, '\n\t\t', n );
  202. }
  203. function ObjectString( o, n ) {
  204. var output = [
  205. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  206. ' "position" : ' + Vector3String( o.position ) + ',',
  207. ' "rotation" : ' + Vector3String( o.rotation ) + ',',
  208. ' "scale" : ' + Vector3String( o.scale ) + ',',
  209. ' "visible" : ' + o.visible + ( o.children.length ? ',' : '' )
  210. ];
  211. return generateMultiLineString( output, '\n\t\t', n );
  212. }
  213. function MeshString( o, n ) {
  214. var output = [
  215. '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
  216. ' "geometry" : ' + LabelString( getGeometryName( o.geometry ) ) + ',',
  217. ' "material" : ' + LabelString( getMaterialName( o.material ) ) + ',',
  218. ' "position" : ' + Vector3String( o.position ) + ',',
  219. ' "rotation" : ' + Vector3String( o.rotation ) + ',',
  220. ' "scale" : ' + Vector3String( o.scale ) + ',',
  221. ' "visible" : ' + o.visible + ( o.children.length ? ',' : '' )
  222. ];
  223. return generateMultiLineString( output, '\n\t\t', n );
  224. }
  225. //
  226. function GeometryString( g ) {
  227. if ( g instanceof THREE.SphereGeometry ) {
  228. var output = [
  229. '\t' + LabelString( getGeometryName( g ) ) + ': {',
  230. ' "type" : "sphere",',
  231. ' "radius" : ' + g.parameters.radius + ',',
  232. ' "widthSegments" : ' + g.parameters.widthSegments + ',',
  233. ' "heightSegments" : ' + g.parameters.heightSegments,
  234. '}'
  235. ];
  236. } else if ( g instanceof THREE.BoxGeometry ) {
  237. var output = [
  238. '\t' + LabelString( getGeometryName( g ) ) + ': {',
  239. ' "type" : "cube",',
  240. ' "width" : ' + g.parameters.width + ',',
  241. ' "height" : ' + g.parameters.height + ',',
  242. ' "depth" : ' + g.parameters.depth + ',',
  243. ' "widthSegments" : ' + g.widthSegments + ',',
  244. ' "heightSegments" : ' + g.heightSegments + ',',
  245. ' "depthSegments" : ' + g.depthSegments,
  246. '}'
  247. ];
  248. } else if ( g instanceof THREE.PlaneGeometry ) {
  249. var output = [
  250. '\t' + LabelString( getGeometryName( g ) ) + ': {',
  251. ' "type" : "plane",',
  252. ' "width" : ' + g.width + ',',
  253. ' "height" : ' + g.height + ',',
  254. ' "widthSegments" : ' + g.widthSegments + ',',
  255. ' "heightSegments" : ' + g.heightSegments,
  256. '}'
  257. ];
  258. } else if ( g instanceof THREE.Geometry ) {
  259. if ( g.sourceType === "ascii" || g.sourceType === "ctm" || g.sourceType === "stl" || g.sourceType === "vtk" ) {
  260. var output = [
  261. '\t' + LabelString( getGeometryName( g ) ) + ': {',
  262. ' "type" : ' + LabelString( g.sourceType ) + ',',
  263. ' "url" : ' + LabelString( g.sourceFile ),
  264. '}'
  265. ];
  266. } else {
  267. var output = [];
  268. }
  269. } else {
  270. var output = [];
  271. }
  272. return generateMultiLineString( output, '\n\t\t' );
  273. }
  274. function MaterialString( m ) {
  275. if ( m instanceof THREE.MeshBasicMaterial ) {
  276. var output = [
  277. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  278. ' "type" : "MeshBasicMaterial",',
  279. ' "parameters" : {',
  280. ' "color" : ' + m.color.getHex() + ',',
  281. m.map ? ' "map" : ' + LabelString( getTextureName( m.map ) ) + ',' : '',
  282. m.envMap ? ' "envMap" : ' + LabelString( getTextureName( m.envMap ) ) + ',' : '',
  283. m.specularMap ? ' "specularMap" : ' + LabelString( getTextureName( m.specularMap ) ) + ',' : '',
  284. m.lightMap ? ' "lightMap" : ' + LabelString( getTextureName( m.lightMap ) ) + ',' : '',
  285. ' "reflectivity" : ' + m.reflectivity + ',',
  286. ' "transparent" : ' + m.transparent + ',',
  287. ' "opacity" : ' + m.opacity + ',',
  288. ' "wireframe" : ' + m.wireframe + ',',
  289. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  290. ' }',
  291. '}'
  292. ];
  293. } else if ( m instanceof THREE.MeshLambertMaterial ) {
  294. var output = [
  295. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  296. ' "type" : "MeshLambertMaterial",',
  297. ' "parameters" : {',
  298. ' "color" : ' + m.color.getHex() + ',',
  299. ' "emissive" : ' + m.emissive.getHex() + ',',
  300. m.map ? ' "map" : ' + LabelString( getTextureName( m.map ) ) + ',' : '',
  301. m.envMap ? ' "envMap" : ' + LabelString( getTextureName( m.envMap ) ) + ',' : '',
  302. m.specularMap ? ' "specularMap" : ' + LabelString( getTextureName( m.specularMap ) ) + ',' : '',
  303. m.lightMap ? ' "lightMap" : ' + LabelString( getTextureName( m.lightMap ) ) + ',' : '',
  304. ' "reflectivity" : ' + m.reflectivity + ',',
  305. ' "transparent" : ' + m.transparent + ',',
  306. ' "opacity" : ' + m.opacity + ',',
  307. ' "wireframe" : ' + m.wireframe + ',',
  308. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  309. ' }',
  310. '}'
  311. ];
  312. } else if ( m instanceof THREE.MeshPhongMaterial ) {
  313. var output = [
  314. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  315. ' "type" : "MeshPhongMaterial",',
  316. ' "parameters" : {',
  317. ' "color" : ' + m.color.getHex() + ',',
  318. ' "emissive" : ' + m.emissive.getHex() + ',',
  319. ' "specular" : ' + m.specular.getHex() + ',',
  320. ' "shininess" : ' + m.shininess + ',',
  321. m.map ? ' "map" : ' + LabelString( getTextureName( m.map ) ) + ',' : '',
  322. m.envMap ? ' "envMap" : ' + LabelString( getTextureName( m.envMap ) ) + ',' : '',
  323. m.specularMap ? ' "specularMap" : ' + LabelString( getTextureName( m.specularMap ) ) + ',' : '',
  324. m.lightMap ? ' "lightMap" : ' + LabelString( getTextureName( m.lightMap ) ) + ',' : '',
  325. m.normalMap ? ' "normalMap" : ' + LabelString( getTextureName( m.normalMap ) ) + ',' : '',
  326. m.bumpMap ? ' "bumpMap" : ' + LabelString( getTextureName( m.bumpMap ) ) + ',' : '',
  327. ' "bumpScale" : ' + m.bumpScale + ',',
  328. ' "reflectivity" : ' + m.reflectivity + ',',
  329. ' "transparent" : ' + m.transparent + ',',
  330. ' "opacity" : ' + m.opacity + ',',
  331. ' "wireframe" : ' + m.wireframe + ',',
  332. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  333. ' }',
  334. '}'
  335. ];
  336. } else if ( m instanceof THREE.MeshDepthMaterial ) {
  337. var output = [
  338. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  339. ' "type" : "MeshDepthMaterial",',
  340. ' "parameters" : {',
  341. ' "transparent" : ' + m.transparent + ',',
  342. ' "opacity" : ' + m.opacity + ',',
  343. ' "wireframe" : ' + m.wireframe + ',',
  344. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  345. ' }',
  346. '}'
  347. ];
  348. } else if ( m instanceof THREE.MeshNormalMaterial ) {
  349. var output = [
  350. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  351. ' "type" : "MeshNormalMaterial",',
  352. ' "parameters" : {',
  353. ' "transparent" : ' + m.transparent + ',',
  354. ' "opacity" : ' + m.opacity + ',',
  355. ' "wireframe" : ' + m.wireframe + ',',
  356. ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
  357. ' }',
  358. '}'
  359. ];
  360. } else if ( m instanceof THREE.MeshFaceMaterial ) {
  361. var output = [
  362. '\t' + LabelString( getMaterialName( m ) ) + ': {',
  363. ' "type" : "MeshFaceMaterial",',
  364. ' "parameters" : {}',
  365. '}'
  366. ];
  367. }
  368. return generateMultiLineString( output, '\n\t\t' );
  369. }
  370. function TextureString( t ) {
  371. // here would be also an option to use data URI
  372. // with embedded image from "t.image.src"
  373. // (that's a side effect of using FileReader to load images)
  374. var output = [
  375. '\t' + LabelString( getTextureName( t ) ) + ': {',
  376. ' "url" : "' + t.sourceFile + '",',
  377. ' "repeat" : ' + Vector2String( t.repeat ) + ',',
  378. ' "offset" : ' + Vector2String( t.offset ) + ',',
  379. ' "magFilter" : ' + NumConstantString( t.magFilter ) + ',',
  380. ' "minFilter" : ' + NumConstantString( t.minFilter ) + ',',
  381. ' "anisotropy" : ' + t.anisotropy,
  382. '}'
  383. ];
  384. return generateMultiLineString( output, '\n\t\t' );
  385. }
  386. //
  387. function FogString( f ) {
  388. if ( f instanceof THREE.Fog ) {
  389. var output = [
  390. '\t' + LabelString( getFogName( f ) ) + ': {',
  391. ' "type" : "linear",',
  392. ' "color" : ' + ColorString( f.color ) + ',',
  393. ' "near" : ' + f.near + ',',
  394. ' "far" : ' + f.far,
  395. '}'
  396. ];
  397. } else if ( f instanceof THREE.FogExp2 ) {
  398. var output = [
  399. '\t' + LabelString( getFogName( f ) ) + ': {',
  400. ' "type" : "exp2",',
  401. ' "color" : ' + ColorString( f.color ) + ',',
  402. ' "density" : ' + f.density,
  403. '}'
  404. ];
  405. } else {
  406. var output = [];
  407. }
  408. return generateMultiLineString( output, '\n\t\t' );
  409. }
  410. //
  411. function generateMultiLineString( lines, separator, padding ) {
  412. var cleanLines = [];
  413. for ( var i = 0; i < lines.length; i ++ ) {
  414. var line = lines[ i ];
  415. if ( line ) {
  416. if ( padding ) line = PaddingString( padding ) + line;
  417. cleanLines.push( line );
  418. }
  419. }
  420. return cleanLines.join( separator );
  421. }
  422. function getObjectName( o ) {
  423. return o.name ? o.name : "Object_" + o.id;
  424. }
  425. function getGeometryName( g ) {
  426. return g.name ? g.name : "Geometry_" + g.id;
  427. }
  428. function getMaterialName( m ) {
  429. return m.name ? m.name : "Material_" + m.id;
  430. }
  431. function getTextureName( t ) {
  432. return t.name ? t.name : "Texture_" + t.id;
  433. }
  434. function getFogName( f ) {
  435. return f.name ? f.name : "Default fog";
  436. }
  437. //
  438. var output = [
  439. '{',
  440. ' "metadata": {',
  441. ' "formatVersion" : 3.2,',
  442. ' "type" : "scene",',
  443. ' "generatedBy" : "SceneExporter",',
  444. ' "objects" : ' + nobjects + ',',
  445. ' "geometries" : ' + ngeometries + ',',
  446. ' "materials" : ' + nmaterials + ',',
  447. ' "textures" : ' + ntextures,
  448. ' },',
  449. '',
  450. ' "urlBaseType": "relativeToScene",',
  451. '',
  452. ' "objects" :',
  453. ' {',
  454. objects,
  455. ' },',
  456. '',
  457. ' "geometries" :',
  458. ' {',
  459. '\t' + geometries,
  460. ' },',
  461. '',
  462. ' "materials" :',
  463. ' {',
  464. '\t' + materials,
  465. ' },',
  466. '',
  467. ' "textures" :',
  468. ' {',
  469. '\t' + textures,
  470. ' },',
  471. '',
  472. ' "fogs" :',
  473. ' {',
  474. '\t' + fogs,
  475. ' },',
  476. '',
  477. ' "transform" :',
  478. ' {',
  479. ' "position" : ' + position + ',',
  480. ' "rotation" : ' + rotation + ',',
  481. ' "scale" : ' + scale,
  482. ' },',
  483. '',
  484. ' "defaults" :',
  485. ' {',
  486. ' "camera" : ' + defcamera + ',',
  487. ' "fog" : ' + deffog,
  488. ' }',
  489. '}'
  490. ].join( '\n' );
  491. return JSON.parse( output );
  492. }
  493. }