CylinderGeometry-3675fb23.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  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(['exports', './Transforms-273eeb44', './Matrix2-9e1c22e2', './ComponentDatatype-4eeb6d9b', './CylinderGeometryLibrary-365438d7', './defaultValue-97284df2', './RuntimeError-4f8ec8a2', './GeometryAttribute-9be2d2e5', './GeometryAttributes-734a3446', './GeometryOffsetAttribute-59b14f45', './IndexDatatype-f228f5fd', './VertexFormat-563ab2cc'], (function (exports, Transforms, Matrix2, ComponentDatatype, CylinderGeometryLibrary, defaultValue, RuntimeError, GeometryAttribute, GeometryAttributes, GeometryOffsetAttribute, IndexDatatype, VertexFormat) { 'use strict';
  26. const radiusScratch = new Matrix2.Cartesian2();
  27. const normalScratch = new Matrix2.Cartesian3();
  28. const bitangentScratch = new Matrix2.Cartesian3();
  29. const tangentScratch = new Matrix2.Cartesian3();
  30. const positionScratch = new Matrix2.Cartesian3();
  31. /**
  32. * A description of a cylinder.
  33. *
  34. * @alias CylinderGeometry
  35. * @constructor
  36. *
  37. * @param {Object} options Object with the following properties:
  38. * @param {Number} options.length The length of the cylinder.
  39. * @param {Number} options.topRadius The radius of the top of the cylinder.
  40. * @param {Number} options.bottomRadius The radius of the bottom of the cylinder.
  41. * @param {Number} [options.slices=128] The number of edges around the perimeter of the cylinder.
  42. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  43. *
  44. * @exception {DeveloperError} options.slices must be greater than or equal to 3.
  45. *
  46. * @see CylinderGeometry.createGeometry
  47. *
  48. * @example
  49. * // create cylinder geometry
  50. * const cylinder = new Cesium.CylinderGeometry({
  51. * length: 200000,
  52. * topRadius: 80000,
  53. * bottomRadius: 200000,
  54. * });
  55. * const geometry = Cesium.CylinderGeometry.createGeometry(cylinder);
  56. */
  57. function CylinderGeometry(options) {
  58. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  59. const length = options.length;
  60. const topRadius = options.topRadius;
  61. const bottomRadius = options.bottomRadius;
  62. const vertexFormat = defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  63. const slices = defaultValue.defaultValue(options.slices, 128);
  64. //>>includeStart('debug', pragmas.debug);
  65. if (!defaultValue.defined(length)) {
  66. throw new RuntimeError.DeveloperError("options.length must be defined.");
  67. }
  68. if (!defaultValue.defined(topRadius)) {
  69. throw new RuntimeError.DeveloperError("options.topRadius must be defined.");
  70. }
  71. if (!defaultValue.defined(bottomRadius)) {
  72. throw new RuntimeError.DeveloperError("options.bottomRadius must be defined.");
  73. }
  74. if (slices < 3) {
  75. throw new RuntimeError.DeveloperError(
  76. "options.slices must be greater than or equal to 3."
  77. );
  78. }
  79. if (
  80. defaultValue.defined(options.offsetAttribute) &&
  81. options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP
  82. ) {
  83. throw new RuntimeError.DeveloperError(
  84. "GeometryOffsetAttribute.TOP is not a supported options.offsetAttribute for this geometry."
  85. );
  86. }
  87. //>>includeEnd('debug');
  88. this._length = length;
  89. this._topRadius = topRadius;
  90. this._bottomRadius = bottomRadius;
  91. this._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat);
  92. this._slices = slices;
  93. this._offsetAttribute = options.offsetAttribute;
  94. this._workerName = "createCylinderGeometry";
  95. }
  96. /**
  97. * The number of elements used to pack the object into an array.
  98. * @type {Number}
  99. */
  100. CylinderGeometry.packedLength = VertexFormat.VertexFormat.packedLength + 5;
  101. /**
  102. * Stores the provided instance into the provided array.
  103. *
  104. * @param {CylinderGeometry} value The value to pack.
  105. * @param {Number[]} array The array to pack into.
  106. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  107. *
  108. * @returns {Number[]} The array that was packed into
  109. */
  110. CylinderGeometry.pack = function (value, array, startingIndex) {
  111. //>>includeStart('debug', pragmas.debug);
  112. if (!defaultValue.defined(value)) {
  113. throw new RuntimeError.DeveloperError("value is required");
  114. }
  115. if (!defaultValue.defined(array)) {
  116. throw new RuntimeError.DeveloperError("array is required");
  117. }
  118. //>>includeEnd('debug');
  119. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  120. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  121. startingIndex += VertexFormat.VertexFormat.packedLength;
  122. array[startingIndex++] = value._length;
  123. array[startingIndex++] = value._topRadius;
  124. array[startingIndex++] = value._bottomRadius;
  125. array[startingIndex++] = value._slices;
  126. array[startingIndex] = defaultValue.defaultValue(value._offsetAttribute, -1);
  127. return array;
  128. };
  129. const scratchVertexFormat = new VertexFormat.VertexFormat();
  130. const scratchOptions = {
  131. vertexFormat: scratchVertexFormat,
  132. length: undefined,
  133. topRadius: undefined,
  134. bottomRadius: undefined,
  135. slices: undefined,
  136. offsetAttribute: undefined,
  137. };
  138. /**
  139. * Retrieves an instance from a packed array.
  140. *
  141. * @param {Number[]} array The packed array.
  142. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  143. * @param {CylinderGeometry} [result] The object into which to store the result.
  144. * @returns {CylinderGeometry} The modified result parameter or a new CylinderGeometry instance if one was not provided.
  145. */
  146. CylinderGeometry.unpack = function (array, startingIndex, result) {
  147. //>>includeStart('debug', pragmas.debug);
  148. if (!defaultValue.defined(array)) {
  149. throw new RuntimeError.DeveloperError("array is required");
  150. }
  151. //>>includeEnd('debug');
  152. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  153. const vertexFormat = VertexFormat.VertexFormat.unpack(
  154. array,
  155. startingIndex,
  156. scratchVertexFormat
  157. );
  158. startingIndex += VertexFormat.VertexFormat.packedLength;
  159. const length = array[startingIndex++];
  160. const topRadius = array[startingIndex++];
  161. const bottomRadius = array[startingIndex++];
  162. const slices = array[startingIndex++];
  163. const offsetAttribute = array[startingIndex];
  164. if (!defaultValue.defined(result)) {
  165. scratchOptions.length = length;
  166. scratchOptions.topRadius = topRadius;
  167. scratchOptions.bottomRadius = bottomRadius;
  168. scratchOptions.slices = slices;
  169. scratchOptions.offsetAttribute =
  170. offsetAttribute === -1 ? undefined : offsetAttribute;
  171. return new CylinderGeometry(scratchOptions);
  172. }
  173. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  174. result._length = length;
  175. result._topRadius = topRadius;
  176. result._bottomRadius = bottomRadius;
  177. result._slices = slices;
  178. result._offsetAttribute =
  179. offsetAttribute === -1 ? undefined : offsetAttribute;
  180. return result;
  181. };
  182. /**
  183. * Computes the geometric representation of a cylinder, including its vertices, indices, and a bounding sphere.
  184. *
  185. * @param {CylinderGeometry} cylinderGeometry A description of the cylinder.
  186. * @returns {Geometry|undefined} The computed vertices and indices.
  187. */
  188. CylinderGeometry.createGeometry = function (cylinderGeometry) {
  189. let length = cylinderGeometry._length;
  190. const topRadius = cylinderGeometry._topRadius;
  191. const bottomRadius = cylinderGeometry._bottomRadius;
  192. const vertexFormat = cylinderGeometry._vertexFormat;
  193. const slices = cylinderGeometry._slices;
  194. if (
  195. length <= 0 ||
  196. topRadius < 0 ||
  197. bottomRadius < 0 ||
  198. (topRadius === 0 && bottomRadius === 0)
  199. ) {
  200. return;
  201. }
  202. const twoSlices = slices + slices;
  203. const threeSlices = slices + twoSlices;
  204. const numVertices = twoSlices + twoSlices;
  205. const positions = CylinderGeometryLibrary.CylinderGeometryLibrary.computePositions(
  206. length,
  207. topRadius,
  208. bottomRadius,
  209. slices,
  210. true
  211. );
  212. const st = vertexFormat.st ? new Float32Array(numVertices * 2) : undefined;
  213. const normals = vertexFormat.normal
  214. ? new Float32Array(numVertices * 3)
  215. : undefined;
  216. const tangents = vertexFormat.tangent
  217. ? new Float32Array(numVertices * 3)
  218. : undefined;
  219. const bitangents = vertexFormat.bitangent
  220. ? new Float32Array(numVertices * 3)
  221. : undefined;
  222. let i;
  223. const computeNormal =
  224. vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent;
  225. if (computeNormal) {
  226. const computeTangent = vertexFormat.tangent || vertexFormat.bitangent;
  227. let normalIndex = 0;
  228. let tangentIndex = 0;
  229. let bitangentIndex = 0;
  230. const theta = Math.atan2(bottomRadius - topRadius, length);
  231. const normal = normalScratch;
  232. normal.z = Math.sin(theta);
  233. const normalScale = Math.cos(theta);
  234. let tangent = tangentScratch;
  235. let bitangent = bitangentScratch;
  236. for (i = 0; i < slices; i++) {
  237. const angle = (i / slices) * ComponentDatatype.CesiumMath.TWO_PI;
  238. const x = normalScale * Math.cos(angle);
  239. const y = normalScale * Math.sin(angle);
  240. if (computeNormal) {
  241. normal.x = x;
  242. normal.y = y;
  243. if (computeTangent) {
  244. tangent = Matrix2.Cartesian3.normalize(
  245. Matrix2.Cartesian3.cross(Matrix2.Cartesian3.UNIT_Z, normal, tangent),
  246. tangent
  247. );
  248. }
  249. if (vertexFormat.normal) {
  250. normals[normalIndex++] = normal.x;
  251. normals[normalIndex++] = normal.y;
  252. normals[normalIndex++] = normal.z;
  253. normals[normalIndex++] = normal.x;
  254. normals[normalIndex++] = normal.y;
  255. normals[normalIndex++] = normal.z;
  256. }
  257. if (vertexFormat.tangent) {
  258. tangents[tangentIndex++] = tangent.x;
  259. tangents[tangentIndex++] = tangent.y;
  260. tangents[tangentIndex++] = tangent.z;
  261. tangents[tangentIndex++] = tangent.x;
  262. tangents[tangentIndex++] = tangent.y;
  263. tangents[tangentIndex++] = tangent.z;
  264. }
  265. if (vertexFormat.bitangent) {
  266. bitangent = Matrix2.Cartesian3.normalize(
  267. Matrix2.Cartesian3.cross(normal, tangent, bitangent),
  268. bitangent
  269. );
  270. bitangents[bitangentIndex++] = bitangent.x;
  271. bitangents[bitangentIndex++] = bitangent.y;
  272. bitangents[bitangentIndex++] = bitangent.z;
  273. bitangents[bitangentIndex++] = bitangent.x;
  274. bitangents[bitangentIndex++] = bitangent.y;
  275. bitangents[bitangentIndex++] = bitangent.z;
  276. }
  277. }
  278. }
  279. for (i = 0; i < slices; i++) {
  280. if (vertexFormat.normal) {
  281. normals[normalIndex++] = 0;
  282. normals[normalIndex++] = 0;
  283. normals[normalIndex++] = -1;
  284. }
  285. if (vertexFormat.tangent) {
  286. tangents[tangentIndex++] = 1;
  287. tangents[tangentIndex++] = 0;
  288. tangents[tangentIndex++] = 0;
  289. }
  290. if (vertexFormat.bitangent) {
  291. bitangents[bitangentIndex++] = 0;
  292. bitangents[bitangentIndex++] = -1;
  293. bitangents[bitangentIndex++] = 0;
  294. }
  295. }
  296. for (i = 0; i < slices; i++) {
  297. if (vertexFormat.normal) {
  298. normals[normalIndex++] = 0;
  299. normals[normalIndex++] = 0;
  300. normals[normalIndex++] = 1;
  301. }
  302. if (vertexFormat.tangent) {
  303. tangents[tangentIndex++] = 1;
  304. tangents[tangentIndex++] = 0;
  305. tangents[tangentIndex++] = 0;
  306. }
  307. if (vertexFormat.bitangent) {
  308. bitangents[bitangentIndex++] = 0;
  309. bitangents[bitangentIndex++] = 1;
  310. bitangents[bitangentIndex++] = 0;
  311. }
  312. }
  313. }
  314. const numIndices = 12 * slices - 12;
  315. const indices = IndexDatatype.IndexDatatype.createTypedArray(numVertices, numIndices);
  316. let index = 0;
  317. let j = 0;
  318. for (i = 0; i < slices - 1; i++) {
  319. indices[index++] = j;
  320. indices[index++] = j + 2;
  321. indices[index++] = j + 3;
  322. indices[index++] = j;
  323. indices[index++] = j + 3;
  324. indices[index++] = j + 1;
  325. j += 2;
  326. }
  327. indices[index++] = twoSlices - 2;
  328. indices[index++] = 0;
  329. indices[index++] = 1;
  330. indices[index++] = twoSlices - 2;
  331. indices[index++] = 1;
  332. indices[index++] = twoSlices - 1;
  333. for (i = 1; i < slices - 1; i++) {
  334. indices[index++] = twoSlices + i + 1;
  335. indices[index++] = twoSlices + i;
  336. indices[index++] = twoSlices;
  337. }
  338. for (i = 1; i < slices - 1; i++) {
  339. indices[index++] = threeSlices;
  340. indices[index++] = threeSlices + i;
  341. indices[index++] = threeSlices + i + 1;
  342. }
  343. let textureCoordIndex = 0;
  344. if (vertexFormat.st) {
  345. const rad = Math.max(topRadius, bottomRadius);
  346. for (i = 0; i < numVertices; i++) {
  347. const position = Matrix2.Cartesian3.fromArray(positions, i * 3, positionScratch);
  348. st[textureCoordIndex++] = (position.x + rad) / (2.0 * rad);
  349. st[textureCoordIndex++] = (position.y + rad) / (2.0 * rad);
  350. }
  351. }
  352. const attributes = new GeometryAttributes.GeometryAttributes();
  353. if (vertexFormat.position) {
  354. attributes.position = new GeometryAttribute.GeometryAttribute({
  355. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  356. componentsPerAttribute: 3,
  357. values: positions,
  358. });
  359. }
  360. if (vertexFormat.normal) {
  361. attributes.normal = new GeometryAttribute.GeometryAttribute({
  362. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  363. componentsPerAttribute: 3,
  364. values: normals,
  365. });
  366. }
  367. if (vertexFormat.tangent) {
  368. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  369. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  370. componentsPerAttribute: 3,
  371. values: tangents,
  372. });
  373. }
  374. if (vertexFormat.bitangent) {
  375. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  376. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  377. componentsPerAttribute: 3,
  378. values: bitangents,
  379. });
  380. }
  381. if (vertexFormat.st) {
  382. attributes.st = new GeometryAttribute.GeometryAttribute({
  383. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  384. componentsPerAttribute: 2,
  385. values: st,
  386. });
  387. }
  388. radiusScratch.x = length * 0.5;
  389. radiusScratch.y = Math.max(bottomRadius, topRadius);
  390. const boundingSphere = new Transforms.BoundingSphere(
  391. Matrix2.Cartesian3.ZERO,
  392. Matrix2.Cartesian2.magnitude(radiusScratch)
  393. );
  394. if (defaultValue.defined(cylinderGeometry._offsetAttribute)) {
  395. length = positions.length;
  396. const offsetValue =
  397. cylinderGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  398. ? 0
  399. : 1;
  400. const applyOffset = new Uint8Array(length / 3).fill(offsetValue);
  401. attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  402. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  403. componentsPerAttribute: 1,
  404. values: applyOffset,
  405. });
  406. }
  407. return new GeometryAttribute.Geometry({
  408. attributes: attributes,
  409. indices: indices,
  410. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  411. boundingSphere: boundingSphere,
  412. offsetAttribute: cylinderGeometry._offsetAttribute,
  413. });
  414. };
  415. let unitCylinderGeometry;
  416. /**
  417. * Returns the geometric representation of a unit cylinder, including its vertices, indices, and a bounding sphere.
  418. * @returns {Geometry} The computed vertices and indices.
  419. *
  420. * @private
  421. */
  422. CylinderGeometry.getUnitCylinder = function () {
  423. if (!defaultValue.defined(unitCylinderGeometry)) {
  424. unitCylinderGeometry = CylinderGeometry.createGeometry(
  425. new CylinderGeometry({
  426. topRadius: 1.0,
  427. bottomRadius: 1.0,
  428. length: 1.0,
  429. vertexFormat: VertexFormat.VertexFormat.POSITION_ONLY,
  430. })
  431. );
  432. }
  433. return unitCylinderGeometry;
  434. };
  435. exports.CylinderGeometry = CylinderGeometry;
  436. }));
  437. //# sourceMappingURL=CylinderGeometry-3675fb23.js.map