CylinderGeometry.js 14 KB

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