CylinderGeometry-6fabfda1.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-775c5788', './ComponentDatatype-5862616f', './GeometryAttribute-91704ebb', './PrimitiveType-97893bc7', './GeometryAttributes-aacecde6', './IndexDatatype-9435b55f', './GeometryOffsetAttribute-ca302482', './VertexFormat-fe4db402', './CylinderGeometryLibrary-8c0fda9f'], function (exports, when, Check, _Math, Cartographic, Cartesian2, BoundingSphere, ComponentDatatype, GeometryAttribute, PrimitiveType, GeometryAttributes, IndexDatatype, GeometryOffsetAttribute, VertexFormat, CylinderGeometryLibrary) { 'use strict';
  24. var radiusScratch = new Cartesian2.Cartesian2();
  25. var normalScratch = new Cartographic.Cartesian3();
  26. var bitangentScratch = new Cartographic.Cartesian3();
  27. var tangentScratch = new Cartographic.Cartesian3();
  28. var positionScratch = new Cartographic.Cartesian3();
  29. /**
  30. * A description of a cylinder.
  31. *
  32. * @alias CylinderGeometry
  33. * @constructor
  34. *
  35. * @param {Object} options Object with the following properties:
  36. * @param {Number} options.length The length of the cylinder.
  37. * @param {Number} options.topRadius The radius of the top of the cylinder.
  38. * @param {Number} options.bottomRadius The radius of the bottom of the cylinder.
  39. * @param {Number} [options.slices=128] The number of edges around the perimeter of the cylinder.
  40. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  41. *
  42. * @exception {DeveloperError} options.slices must be greater than or equal to 3.
  43. *
  44. * @see CylinderGeometry.createGeometry
  45. *
  46. * @example
  47. * // create cylinder geometry
  48. * var cylinder = new Cesium.CylinderGeometry({
  49. * length: 200000,
  50. * topRadius: 80000,
  51. * bottomRadius: 200000,
  52. * });
  53. * var geometry = Cesium.CylinderGeometry.createGeometry(cylinder);
  54. */
  55. function CylinderGeometry(options) {
  56. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  57. var length = options.length;
  58. var topRadius = options.topRadius;
  59. var bottomRadius = options.bottomRadius;
  60. var vertexFormat = when.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  61. var slices = when.defaultValue(options.slices, 128);
  62. //>>includeStart('debug', pragmas.debug);
  63. if (!when.defined(length)) {
  64. throw new Check.DeveloperError('options.length must be defined.');
  65. }
  66. if (!when.defined(topRadius)) {
  67. throw new Check.DeveloperError('options.topRadius must be defined.');
  68. }
  69. if (!when.defined(bottomRadius)) {
  70. throw new Check.DeveloperError('options.bottomRadius must be defined.');
  71. }
  72. if (slices < 3) {
  73. throw new Check.DeveloperError('options.slices must be greater than or equal to 3.');
  74. }
  75. if (when.defined(options.offsetAttribute) && options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  76. throw new Check.DeveloperError('GeometryOffsetAttribute.TOP is not a supported options.offsetAttribute for this geometry.');
  77. }
  78. //>>includeEnd('debug');
  79. this._length = length;
  80. this._topRadius = topRadius;
  81. this._bottomRadius = bottomRadius;
  82. this._vertexFormat = 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.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 (!when.defined(value)) {
  104. throw new Check.DeveloperError('value is required');
  105. }
  106. if (!when.defined(array)) {
  107. throw new Check.DeveloperError('array is required');
  108. }
  109. //>>includeEnd('debug');
  110. startingIndex = when.defaultValue(startingIndex, 0);
  111. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  112. startingIndex += VertexFormat.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] = when.defaultValue(value._offsetAttribute, -1);
  118. return array;
  119. };
  120. var scratchVertexFormat = new VertexFormat.VertexFormat();
  121. var 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 (!when.defined(array)) {
  140. throw new Check.DeveloperError('array is required');
  141. }
  142. //>>includeEnd('debug');
  143. startingIndex = when.defaultValue(startingIndex, 0);
  144. var vertexFormat = VertexFormat.VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  145. startingIndex += VertexFormat.VertexFormat.packedLength;
  146. var length = array[startingIndex++];
  147. var topRadius = array[startingIndex++];
  148. var bottomRadius = array[startingIndex++];
  149. var slices = array[startingIndex++];
  150. var offsetAttribute = array[startingIndex];
  151. if (!when.defined(result)) {
  152. scratchOptions.length = length;
  153. scratchOptions.topRadius = topRadius;
  154. scratchOptions.bottomRadius = bottomRadius;
  155. scratchOptions.slices = slices;
  156. scratchOptions.offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  157. return new CylinderGeometry(scratchOptions);
  158. }
  159. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  160. result._length = length;
  161. result._topRadius = topRadius;
  162. result._bottomRadius = bottomRadius;
  163. result._slices = slices;
  164. result._offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  165. return result;
  166. };
  167. /**
  168. * Computes the geometric representation of a cylinder, including its vertices, indices, and a bounding sphere.
  169. *
  170. * @param {CylinderGeometry} cylinderGeometry A description of the cylinder.
  171. * @returns {Geometry|undefined} The computed vertices and indices.
  172. */
  173. CylinderGeometry.createGeometry = function(cylinderGeometry) {
  174. var length = cylinderGeometry._length;
  175. var topRadius = cylinderGeometry._topRadius;
  176. var bottomRadius = cylinderGeometry._bottomRadius;
  177. var vertexFormat = cylinderGeometry._vertexFormat;
  178. var slices = cylinderGeometry._slices;
  179. if ((length <= 0) || (topRadius < 0) || (bottomRadius < 0) || ((topRadius === 0) && (bottomRadius === 0))) {
  180. return;
  181. }
  182. var twoSlices = slices + slices;
  183. var threeSlices = slices + twoSlices;
  184. var numVertices = twoSlices + twoSlices;
  185. var positions = CylinderGeometryLibrary.CylinderGeometryLibrary.computePositions(length, topRadius, bottomRadius, slices, true);
  186. var st = (vertexFormat.st) ? new Float32Array(numVertices * 2) : undefined;
  187. var normals = (vertexFormat.normal) ? new Float32Array(numVertices * 3) : undefined;
  188. var tangents = (vertexFormat.tangent) ? new Float32Array(numVertices * 3) : undefined;
  189. var bitangents = (vertexFormat.bitangent) ? new Float32Array(numVertices * 3) : undefined;
  190. var i;
  191. var computeNormal = (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent);
  192. if (computeNormal) {
  193. var computeTangent = (vertexFormat.tangent || vertexFormat.bitangent);
  194. var normalIndex = 0;
  195. var tangentIndex = 0;
  196. var bitangentIndex = 0;
  197. var theta = Math.atan2(bottomRadius - topRadius, length);
  198. var normal = normalScratch;
  199. normal.z = Math.sin(theta);
  200. var normalScale = Math.cos(theta);
  201. var tangent = tangentScratch;
  202. var bitangent = bitangentScratch;
  203. for (i = 0; i < slices; i++) {
  204. var angle = i / slices * _Math.CesiumMath.TWO_PI;
  205. var x = normalScale * Math.cos(angle);
  206. var y = normalScale * Math.sin(angle);
  207. if (computeNormal) {
  208. normal.x = x;
  209. normal.y = y;
  210. if (computeTangent) {
  211. tangent = Cartographic.Cartesian3.normalize(Cartographic.Cartesian3.cross(Cartographic.Cartesian3.UNIT_Z, normal, tangent), tangent);
  212. }
  213. if (vertexFormat.normal) {
  214. normals[normalIndex++] = normal.x;
  215. normals[normalIndex++] = normal.y;
  216. normals[normalIndex++] = normal.z;
  217. normals[normalIndex++] = normal.x;
  218. normals[normalIndex++] = normal.y;
  219. normals[normalIndex++] = normal.z;
  220. }
  221. if (vertexFormat.tangent) {
  222. tangents[tangentIndex++] = tangent.x;
  223. tangents[tangentIndex++] = tangent.y;
  224. tangents[tangentIndex++] = tangent.z;
  225. tangents[tangentIndex++] = tangent.x;
  226. tangents[tangentIndex++] = tangent.y;
  227. tangents[tangentIndex++] = tangent.z;
  228. }
  229. if (vertexFormat.bitangent) {
  230. bitangent = Cartographic.Cartesian3.normalize(Cartographic.Cartesian3.cross(normal, tangent, bitangent), bitangent);
  231. bitangents[bitangentIndex++] = bitangent.x;
  232. bitangents[bitangentIndex++] = bitangent.y;
  233. bitangents[bitangentIndex++] = bitangent.z;
  234. bitangents[bitangentIndex++] = bitangent.x;
  235. bitangents[bitangentIndex++] = bitangent.y;
  236. bitangents[bitangentIndex++] = bitangent.z;
  237. }
  238. }
  239. }
  240. for (i = 0; i < slices; i++) {
  241. if (vertexFormat.normal) {
  242. normals[normalIndex++] = 0;
  243. normals[normalIndex++] = 0;
  244. normals[normalIndex++] = -1;
  245. }
  246. if (vertexFormat.tangent) {
  247. tangents[tangentIndex++] = 1;
  248. tangents[tangentIndex++] = 0;
  249. tangents[tangentIndex++] = 0;
  250. }
  251. if (vertexFormat.bitangent) {
  252. bitangents[bitangentIndex++] = 0;
  253. bitangents[bitangentIndex++] = -1;
  254. bitangents[bitangentIndex++] = 0;
  255. }
  256. }
  257. for (i = 0; i < slices; i++) {
  258. if (vertexFormat.normal) {
  259. normals[normalIndex++] = 0;
  260. normals[normalIndex++] = 0;
  261. normals[normalIndex++] = 1;
  262. }
  263. if (vertexFormat.tangent) {
  264. tangents[tangentIndex++] = 1;
  265. tangents[tangentIndex++] = 0;
  266. tangents[tangentIndex++] = 0;
  267. }
  268. if (vertexFormat.bitangent) {
  269. bitangents[bitangentIndex++] = 0;
  270. bitangents[bitangentIndex++] = 1;
  271. bitangents[bitangentIndex++] = 0;
  272. }
  273. }
  274. }
  275. var numIndices = 12 * slices - 12;
  276. var indices = IndexDatatype.IndexDatatype.createTypedArray(numVertices, numIndices);
  277. var index = 0;
  278. var j = 0;
  279. for (i = 0; i < slices - 1; i++) {
  280. indices[index++] = j;
  281. indices[index++] = j + 2;
  282. indices[index++] = j + 3;
  283. indices[index++] = j;
  284. indices[index++] = j + 3;
  285. indices[index++] = j + 1;
  286. j += 2;
  287. }
  288. indices[index++] = twoSlices - 2;
  289. indices[index++] = 0;
  290. indices[index++] = 1;
  291. indices[index++] = twoSlices - 2;
  292. indices[index++] = 1;
  293. indices[index++] = twoSlices - 1;
  294. for (i = 1; i < slices - 1; i++) {
  295. indices[index++] = twoSlices + i + 1;
  296. indices[index++] = twoSlices + i;
  297. indices[index++] = twoSlices;
  298. }
  299. for (i = 1; i < slices - 1; i++) {
  300. indices[index++] = threeSlices;
  301. indices[index++] = threeSlices + i;
  302. indices[index++] = threeSlices + i + 1;
  303. }
  304. var textureCoordIndex = 0;
  305. if (vertexFormat.st) {
  306. var rad = Math.max(topRadius, bottomRadius);
  307. for (i = 0; i < numVertices; i++) {
  308. var position = Cartographic.Cartesian3.fromArray(positions, i * 3, positionScratch);
  309. st[textureCoordIndex++] = (position.x + rad) / (2.0 * rad);
  310. st[textureCoordIndex++] = (position.y + rad) / (2.0 * rad);
  311. }
  312. }
  313. var attributes = new GeometryAttributes.GeometryAttributes();
  314. if (vertexFormat.position) {
  315. attributes.position = new GeometryAttribute.GeometryAttribute({
  316. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  317. componentsPerAttribute: 3,
  318. values: positions
  319. });
  320. }
  321. if (vertexFormat.normal) {
  322. attributes.normal = new GeometryAttribute.GeometryAttribute({
  323. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  324. componentsPerAttribute : 3,
  325. values : normals
  326. });
  327. }
  328. if (vertexFormat.tangent) {
  329. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  330. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  331. componentsPerAttribute : 3,
  332. values : tangents
  333. });
  334. }
  335. if (vertexFormat.bitangent) {
  336. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  337. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  338. componentsPerAttribute : 3,
  339. values : bitangents
  340. });
  341. }
  342. if (vertexFormat.st) {
  343. attributes.st = new GeometryAttribute.GeometryAttribute({
  344. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  345. componentsPerAttribute : 2,
  346. values : st
  347. });
  348. }
  349. radiusScratch.x = length * 0.5;
  350. radiusScratch.y = Math.max(bottomRadius, topRadius);
  351. var boundingSphere = new BoundingSphere.BoundingSphere(Cartographic.Cartesian3.ZERO, Cartesian2.Cartesian2.magnitude(radiusScratch));
  352. if (when.defined(cylinderGeometry._offsetAttribute)) {
  353. length = positions.length;
  354. var applyOffset = new Uint8Array(length / 3);
  355. var offsetValue = cylinderGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  356. GeometryOffsetAttribute.arrayFill(applyOffset, offsetValue);
  357. attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  358. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  359. componentsPerAttribute : 1,
  360. values: applyOffset
  361. });
  362. }
  363. return new GeometryAttribute.Geometry({
  364. attributes : attributes,
  365. indices : indices,
  366. primitiveType : PrimitiveType.PrimitiveType.TRIANGLES,
  367. boundingSphere : boundingSphere,
  368. offsetAttribute : cylinderGeometry._offsetAttribute
  369. });
  370. };
  371. var unitCylinderGeometry;
  372. /**
  373. * Returns the geometric representation of a unit cylinder, including its vertices, indices, and a bounding sphere.
  374. * @returns {Geometry} The computed vertices and indices.
  375. *
  376. * @private
  377. */
  378. CylinderGeometry.getUnitCylinder = function() {
  379. if (!when.defined(unitCylinderGeometry)) {
  380. unitCylinderGeometry = CylinderGeometry.createGeometry(new CylinderGeometry({
  381. topRadius : 1.0,
  382. bottomRadius : 1.0,
  383. length : 1.0,
  384. vertexFormat : VertexFormat.VertexFormat.POSITION_ONLY
  385. }));
  386. }
  387. return unitCylinderGeometry;
  388. };
  389. exports.CylinderGeometry = CylinderGeometry;
  390. });