CommonUtilities.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 getParams( type, customParams ) {
  12. var defaults = {};
  13. switch ( type ) {
  14. case "Box":
  15. defaults = { width: 100, height: 100, depth: 100, widthSegments: 1, heightSegments: 1, depthSegments: 1 };
  16. break;
  17. case "Sphere":
  18. defaults = { radius: 75, widthSegments: 32, heightSegments: 16, phiStart: 0, phiLength: 6.28, thetaStart: 0.00, thetaLength: 3.14 };
  19. break;
  20. default:
  21. console.error( "Type '" + type + "' is not known while creating params" );
  22. return false;
  23. }
  24. return mergeParams( defaults, customParams );
  25. }
  26. function getGeometry( type, customParams ) {
  27. var params = getParams( type, customParams );
  28. switch ( type ) {
  29. case "Box":
  30. return new THREE.BoxGeometry(
  31. params['width'],
  32. params['height'],
  33. params['depth'],
  34. params['widthSegments'],
  35. params['heightSegments'],
  36. params['depthSegments']
  37. );
  38. case "Sphere":
  39. return new THREE.SphereGeometry(
  40. params['radius'],
  41. params['widthSegments'],
  42. params['heightSegments'],
  43. params['phiStart'],
  44. params['phiLength'],
  45. params['thetaStart'],
  46. params['thetaLength']
  47. );
  48. default:
  49. console.error( "Type '" + type + "' is not known while creating geometry " );
  50. return false;
  51. }
  52. }
  53. function getObject( name, type, customParams ) {
  54. var geometry = getGeometry( type, customParams );
  55. var object = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial() );
  56. object.name = name || type + " 1";
  57. return object;
  58. }
  59. function aBox( name, customParams ) {
  60. return getObject( name, "Box", customParams );
  61. }
  62. function aSphere( name, customParams ) {
  63. return getObject( name, "Sphere", customParams );
  64. }
  65. function aPointlight( name ) {
  66. var object = new THREE.PointLight( 54321, 1.0, 0.0, 1.0 );
  67. object.name = name || "PointLight 1";
  68. return object;
  69. }
  70. function aPerspectiveCamera( name ) {
  71. var object = new THREE.PerspectiveCamera( 50.1, 0.4, 1.03, 999.05 );
  72. object.name = name || "PerspectiveCamera 1";
  73. return object;
  74. }
  75. function getScriptCount( editor ) {
  76. var scriptsKeys = Object.keys( editor.scripts );
  77. var scriptCount = 0;
  78. for ( var i = 0; i < scriptsKeys.length; i++ ) {
  79. scriptCount += editor.scripts[ scriptsKeys[i] ].length;
  80. }
  81. return scriptCount;
  82. }
粤ICP备19079148号