CylinderGeometry-23c67a6c.js 16 KB

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