CylinderGeometry.js 17 KB

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