createSphereGeometry.js 5.2 KB

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