CylinderGeometry-9e8f5a16.js 16 KB

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