CommonUtilities.js 3.3 KB

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