CylinderGeometry-3515f379.js 19 KB

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