CylinderGeometry.js 14 KB

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