createSphereGeometry.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['./defaultValue-a6eb9f34', './Matrix2-ab676047', './RuntimeError-1088cc64', './EllipsoidGeometry-ac5c767c', './VertexFormat-65fd4be5', './ComponentDatatype-e06f4e16', './WebGLConstants-d81b330d', './Transforms-c78c4637', './_commonjsHelpers-89c9b271', './combine-7cf28d88', './GeometryAttribute-4f02e2ad', './GeometryAttributes-aff51037', './GeometryOffsetAttribute-102da468', './IndexDatatype-c2232ebd'], (function (defaultValue, Matrix2, RuntimeError, EllipsoidGeometry, VertexFormat, ComponentDatatype, WebGLConstants, Transforms, _commonjsHelpers, combine, GeometryAttribute, GeometryAttributes, GeometryOffsetAttribute, IndexDatatype) { 'use strict';
  26. /**
  27. * A description of a sphere centered at the origin.
  28. *
  29. * @alias SphereGeometry
  30. * @constructor
  31. *
  32. * @param {Object} [options] Object with the following properties:
  33. * @param {Number} [options.radius=1.0] The radius of the sphere.
  34. * @param {Number} [options.stackPartitions=64] The number of times to partition the ellipsoid into stacks.
  35. * @param {Number} [options.slicePartitions=64] The number of times to partition the ellipsoid into radial slices.
  36. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  37. *
  38. * @exception {DeveloperError} options.slicePartitions cannot be less than three.
  39. * @exception {DeveloperError} options.stackPartitions cannot be less than three.
  40. *
  41. * @see SphereGeometry#createGeometry
  42. *
  43. * @example
  44. * const sphere = new Cesium.SphereGeometry({
  45. * radius : 100.0,
  46. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY
  47. * });
  48. * const geometry = Cesium.SphereGeometry.createGeometry(sphere);
  49. */
  50. function SphereGeometry(options) {
  51. const radius = defaultValue.defaultValue(options.radius, 1.0);
  52. const radii = new Matrix2.Cartesian3(radius, radius, radius);
  53. const ellipsoidOptions = {
  54. radii: radii,
  55. stackPartitions: options.stackPartitions,
  56. slicePartitions: options.slicePartitions,
  57. vertexFormat: options.vertexFormat,
  58. };
  59. this._ellipsoidGeometry = new EllipsoidGeometry.EllipsoidGeometry(ellipsoidOptions);
  60. this._workerName = "createSphereGeometry";
  61. }
  62. /**
  63. * The number of elements used to pack the object into an array.
  64. * @type {Number}
  65. */
  66. SphereGeometry.packedLength = EllipsoidGeometry.EllipsoidGeometry.packedLength;
  67. /**
  68. * Stores the provided instance into the provided array.
  69. *
  70. * @param {SphereGeometry} value The value to pack.
  71. * @param {Number[]} array The array to pack into.
  72. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  73. *
  74. * @returns {Number[]} The array that was packed into
  75. */
  76. SphereGeometry.pack = function (value, array, startingIndex) {
  77. //>>includeStart('debug', pragmas.debug);
  78. RuntimeError.Check.typeOf.object("value", value);
  79. //>>includeEnd('debug');
  80. return EllipsoidGeometry.EllipsoidGeometry.pack(value._ellipsoidGeometry, array, startingIndex);
  81. };
  82. const scratchEllipsoidGeometry = new EllipsoidGeometry.EllipsoidGeometry();
  83. const scratchOptions = {
  84. radius: undefined,
  85. radii: new Matrix2.Cartesian3(),
  86. vertexFormat: new VertexFormat.VertexFormat(),
  87. stackPartitions: undefined,
  88. slicePartitions: undefined,
  89. };
  90. /**
  91. * Retrieves an instance from a packed array.
  92. *
  93. * @param {Number[]} array The packed array.
  94. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  95. * @param {SphereGeometry} [result] The object into which to store the result.
  96. * @returns {SphereGeometry} The modified result parameter or a new SphereGeometry instance if one was not provided.
  97. */
  98. SphereGeometry.unpack = function (array, startingIndex, result) {
  99. const ellipsoidGeometry = EllipsoidGeometry.EllipsoidGeometry.unpack(
  100. array,
  101. startingIndex,
  102. scratchEllipsoidGeometry
  103. );
  104. scratchOptions.vertexFormat = VertexFormat.VertexFormat.clone(
  105. ellipsoidGeometry._vertexFormat,
  106. scratchOptions.vertexFormat
  107. );
  108. scratchOptions.stackPartitions = ellipsoidGeometry._stackPartitions;
  109. scratchOptions.slicePartitions = ellipsoidGeometry._slicePartitions;
  110. if (!defaultValue.defined(result)) {
  111. scratchOptions.radius = ellipsoidGeometry._radii.x;
  112. return new SphereGeometry(scratchOptions);
  113. }
  114. Matrix2.Cartesian3.clone(ellipsoidGeometry._radii, scratchOptions.radii);
  115. result._ellipsoidGeometry = new EllipsoidGeometry.EllipsoidGeometry(scratchOptions);
  116. return result;
  117. };
  118. /**
  119. * Computes the geometric representation of a sphere, including its vertices, indices, and a bounding sphere.
  120. *
  121. * @param {SphereGeometry} sphereGeometry A description of the sphere.
  122. * @returns {Geometry|undefined} The computed vertices and indices.
  123. */
  124. SphereGeometry.createGeometry = function (sphereGeometry) {
  125. return EllipsoidGeometry.EllipsoidGeometry.createGeometry(sphereGeometry._ellipsoidGeometry);
  126. };
  127. function createSphereGeometry(sphereGeometry, offset) {
  128. if (defaultValue.defined(offset)) {
  129. sphereGeometry = SphereGeometry.unpack(sphereGeometry, offset);
  130. }
  131. return SphereGeometry.createGeometry(sphereGeometry);
  132. }
  133. return createSphereGeometry;
  134. }));