SphereGeometry.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * const sphere = new Cesium.SphereGeometry({
  26. * radius : 100.0,
  27. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY
  28. * });
  29. * const geometry = Cesium.SphereGeometry.createGeometry(sphere);
  30. */
  31. function SphereGeometry(options) {
  32. const radius = defaultValue(options.radius, 1.0);
  33. const radii = new Cartesian3(radius, radius, radius);
  34. const 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. const scratchEllipsoidGeometry = new EllipsoidGeometry();
  64. const 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. const ellipsoidGeometry = EllipsoidGeometry.unpack(
  81. array,
  82. startingIndex,
  83. scratchEllipsoidGeometry
  84. );
  85. scratchOptions.vertexFormat = VertexFormat.clone(
  86. ellipsoidGeometry._vertexFormat,
  87. scratchOptions.vertexFormat
  88. );
  89. scratchOptions.stackPartitions = ellipsoidGeometry._stackPartitions;
  90. scratchOptions.slicePartitions = ellipsoidGeometry._slicePartitions;
  91. if (!defined(result)) {
  92. scratchOptions.radius = ellipsoidGeometry._radii.x;
  93. return new SphereGeometry(scratchOptions);
  94. }
  95. Cartesian3.clone(ellipsoidGeometry._radii, scratchOptions.radii);
  96. result._ellipsoidGeometry = new EllipsoidGeometry(scratchOptions);
  97. return result;
  98. };
  99. /**
  100. * Computes the geometric representation of a sphere, including its vertices, indices, and a bounding sphere.
  101. *
  102. * @param {SphereGeometry} sphereGeometry A description of the sphere.
  103. * @returns {Geometry|undefined} The computed vertices and indices.
  104. */
  105. SphereGeometry.createGeometry = function (sphereGeometry) {
  106. return EllipsoidGeometry.createGeometry(sphereGeometry._ellipsoidGeometry);
  107. };
  108. export default SphereGeometry;