SphereGeometry.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import Cartesian3 from './Cartesian3.js';
  2. import Check from './Check.js';
  3. import defaultValue from './defaultValue.js';
  4. import defined from './defined.js';
  5. import EllipsoidGeometry from './EllipsoidGeometry.js';
  6. import VertexFormat from './VertexFormat.js';
  7. /**
  8. * A description of a sphere centered at the origin.
  9. *
  10. * @alias SphereGeometry
  11. * @constructor
  12. *
  13. * @param {Object} [options] Object with the following properties:
  14. * @param {Number} [options.radius=1.0] The radius of the sphere.
  15. * @param {Number} [options.stackPartitions=64] The number of times to partition the ellipsoid into stacks.
  16. * @param {Number} [options.slicePartitions=64] The number of times to partition the ellipsoid into radial slices.
  17. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  18. *
  19. * @exception {DeveloperError} options.slicePartitions cannot be less than three.
  20. * @exception {DeveloperError} options.stackPartitions cannot be less than three.
  21. *
  22. * @see SphereGeometry#createGeometry
  23. *
  24. * @example
  25. * var sphere = new Cesium.SphereGeometry({
  26. * radius : 100.0,
  27. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY
  28. * });
  29. * var geometry = Cesium.SphereGeometry.createGeometry(sphere);
  30. */
  31. function SphereGeometry(options) {
  32. var radius = defaultValue(options.radius, 1.0);
  33. var radii = new Cartesian3(radius, radius, radius);
  34. var ellipsoidOptions = {
  35. radii: radii,
  36. stackPartitions: options.stackPartitions,
  37. slicePartitions: options.slicePartitions,
  38. vertexFormat: options.vertexFormat
  39. };
  40. this._ellipsoidGeometry = new EllipsoidGeometry(ellipsoidOptions);
  41. this._workerName = 'createSphereGeometry';
  42. }
  43. /**
  44. * The number of elements used to pack the object into an array.
  45. * @type {Number}
  46. */
  47. SphereGeometry.packedLength = EllipsoidGeometry.packedLength;
  48. /**
  49. * Stores the provided instance into the provided array.
  50. *
  51. * @param {SphereGeometry} value The value to pack.
  52. * @param {Number[]} array The array to pack into.
  53. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  54. *
  55. * @returns {Number[]} The array that was packed into
  56. */
  57. SphereGeometry.pack = function(value, array, startingIndex) {
  58. //>>includeStart('debug', pragmas.debug);
  59. Check.typeOf.object('value', value);
  60. //>>includeEnd('debug');
  61. return EllipsoidGeometry.pack(value._ellipsoidGeometry, array, startingIndex);
  62. };
  63. var scratchEllipsoidGeometry = new EllipsoidGeometry();
  64. var scratchOptions = {
  65. radius : undefined,
  66. radii : new Cartesian3(),
  67. vertexFormat : new VertexFormat(),
  68. stackPartitions : undefined,
  69. slicePartitions : undefined
  70. };
  71. /**
  72. * Retrieves an instance from a packed array.
  73. *
  74. * @param {Number[]} array The packed array.
  75. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  76. * @param {SphereGeometry} [result] The object into which to store the result.
  77. * @returns {SphereGeometry} The modified result parameter or a new SphereGeometry instance if one was not provided.
  78. */
  79. SphereGeometry.unpack = function(array, startingIndex, result) {
  80. var ellipsoidGeometry = EllipsoidGeometry.unpack(array, startingIndex, scratchEllipsoidGeometry);
  81. scratchOptions.vertexFormat = VertexFormat.clone(ellipsoidGeometry._vertexFormat, scratchOptions.vertexFormat);
  82. scratchOptions.stackPartitions = ellipsoidGeometry._stackPartitions;
  83. scratchOptions.slicePartitions = ellipsoidGeometry._slicePartitions;
  84. if (!defined(result)) {
  85. scratchOptions.radius = ellipsoidGeometry._radii.x;
  86. return new SphereGeometry(scratchOptions);
  87. }
  88. Cartesian3.clone(ellipsoidGeometry._radii, scratchOptions.radii);
  89. result._ellipsoidGeometry = new EllipsoidGeometry(scratchOptions);
  90. return result;
  91. };
  92. /**
  93. * Computes the geometric representation of a sphere, including its vertices, indices, and a bounding sphere.
  94. *
  95. * @param {SphereGeometry} sphereGeometry A description of the sphere.
  96. * @returns {Geometry} The computed vertices and indices.
  97. */
  98. SphereGeometry.createGeometry = function(sphereGeometry) {
  99. return EllipsoidGeometry.createGeometry(sphereGeometry._ellipsoidGeometry);
  100. };
  101. export default SphereGeometry;