createSphereGeometry.js 6.8 KB

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