SceneExporter2.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.SceneExporter2 = function () {};
  5. THREE.SceneExporter2.prototype = {
  6. constructor: THREE.SceneExporter2,
  7. parse: function ( scene ) {
  8. var output = {
  9. metadata: {
  10. formatVersion : 4.0,
  11. type : "scene",
  12. generatedBy : "SceneExporter2"
  13. }
  14. };
  15. console.log( scene );
  16. var parseObject = function ( object ) {
  17. var data = {
  18. name: object.name
  19. };
  20. if ( object instanceof THREE.PerspectiveCamera ) {
  21. data.type = 'PerspectiveCamera';
  22. data.position = object.position.toArray();
  23. data.rotation = object.rotation.toArray();
  24. } else if ( object instanceof THREE.OrthographicCamera ) {
  25. data.type = 'OrthographicCamera';
  26. data.position = object.position.toArray();
  27. data.rotation = object.rotation.toArray();
  28. } else if ( object instanceof THREE.AmbientLight ) {
  29. data.type = 'AmbientLight';
  30. } else if ( object instanceof THREE.DirectionalLight ) {
  31. data.type = 'DirectionalLight';
  32. data.position = object.position.toArray();
  33. } else if ( object instanceof THREE.PointLight ) {
  34. data.type = 'PointLight';
  35. data.position = object.position.toArray();
  36. } else if ( object instanceof THREE.SpotLight ) {
  37. data.type = 'SpotLight';
  38. data.position = object.position.toArray();
  39. } else if ( object instanceof THREE.HemisphereLight ) {
  40. data.type = 'HemisphereLight';
  41. } else if ( object instanceof THREE.Mesh ) {
  42. data.type = 'Mesh';
  43. data.position = object.position.toArray();
  44. data.rotation = object.rotation.toArray();
  45. data.scale = object.scale.toArray();
  46. } else {
  47. data.type = 'Object3D';
  48. data.position = object.position.toArray();
  49. data.rotation = object.rotation.toArray();
  50. data.scale = object.scale.toArray();
  51. }
  52. // parse children
  53. if ( object.children.length > 0 ) {
  54. data.children = [];
  55. for ( var i = 0; i < object.children.length; i ++ ) {
  56. data.children.push( parseObject( object.children[ i ] ) );
  57. }
  58. }
  59. return data;
  60. }
  61. output.scene = parseObject( scene ).children;
  62. return JSON.stringify( output, null, '\t' );
  63. }
  64. }