CommonUtilities.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. function mergeParams( defaults, customParams ) {
  2. if ( typeof customParams == "undefined" ) return defaults;
  3. var defaultKeys = Object.keys( defaults );
  4. var params = {};
  5. defaultKeys.map( function( key ) {
  6. params[ key ] = customParams[ key ] || defaultKeys[ key ];
  7. } );
  8. return params;
  9. }
  10. function getGeometryParams( type, customParams ) {
  11. if ( typeof customParams != "undefined" &&
  12. typeof customParams.geometry != "undefined" &&
  13. typeof customParams.geometry.parameters != "undefined" ) {
  14. var customGeometryParams = customParams.geometry.parameters;
  15. }
  16. var defaults = {};
  17. switch ( type ) {
  18. case "BoxGeometry":
  19. defaults = { width: 100, height: 100, depth: 100, widthSegments: 1, heightSegments: 1, depthSegments: 1 };
  20. break;
  21. case "SphereGeometry":
  22. defaults = { radius: 75, widthSegments: 32, heightSegments: 16, phiStart: 0, phiLength: 6.28, thetaStart: 0.00, thetaLength: 3.14 };
  23. break;
  24. default:
  25. console.error( "Type '" + type + "' is not known while creating params" );
  26. return false;
  27. }
  28. return mergeParams( defaults, customGeometryParams );
  29. }
  30. function getGeometry( type, customParams ) {
  31. var params = getGeometryParams( type, customParams );
  32. switch ( type ) {
  33. case "BoxGeometry":
  34. return new THREE.BoxGeometry(
  35. params[ 'width' ],
  36. params[ 'height' ],
  37. params[ 'depth' ],
  38. params[ 'widthSegments' ],
  39. params[ 'heightSegments' ],
  40. params[ 'depthSegments' ]
  41. );
  42. case "SphereGeometry":
  43. return new THREE.SphereGeometry(
  44. params[ 'radius' ],
  45. params[ 'widthSegments' ],
  46. params[ 'heightSegments' ],
  47. params[ 'phiStart' ],
  48. params[ 'phiLength' ],
  49. params[ 'thetaStart' ],
  50. params[ 'thetaLength' ]
  51. );
  52. default:
  53. console.error( "Type '" + type + "' is not known while creating geometry " );
  54. return false;
  55. }
  56. }
  57. function getObject( name, type, customParams ) {
  58. var geometry = getGeometry( type, customParams );
  59. var object = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial() );
  60. object.name = name || type + " 1";
  61. return object;
  62. }
  63. function aBox( name, customParams ) {
  64. return getObject( name, "BoxGeometry", customParams );
  65. }
  66. function aSphere( name, customParams ) {
  67. return getObject( name, "SphereGeometry", customParams );
  68. }
  69. function aPointlight( name ) {
  70. var object = new THREE.PointLight( 54321, 1.0, 0.0, 1.0 );
  71. object.name = name || "PointLight 1";
  72. return object;
  73. }
  74. function aPerspectiveCamera( name ) {
  75. var object = new THREE.PerspectiveCamera( 50.1, 0.4, 1.03, 999.05 );
  76. object.name = name || "PerspectiveCamera 1";
  77. return object;
  78. }
  79. function getScriptCount( editor ) {
  80. var scriptsKeys = Object.keys( editor.scripts );
  81. var scriptCount = 0;
  82. for ( var i = 0; i < scriptsKeys.length; i ++ ) {
  83. scriptCount += editor.scripts[ scriptsKeys[ i ] ].length;
  84. }
  85. return scriptCount;
  86. }
  87. function exportScene( editor ) {
  88. var output = editor.scene.toJSON();
  89. output = JSON.stringify( output, null, '\t' );
  90. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  91. return output;
  92. }
  93. function importScene( data ) {
  94. var json = JSON.parse( data );
  95. var loader = new THREE.ObjectLoader();
  96. var result = loader.parse( json );
  97. return result;
  98. }