CommonUtilities.js 3.2 KB

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