createPolygonGeometry.js 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defined-26bd4a03', './Check-da037458', './freezeObject-2d83f591', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './defineProperties-6f7a50f2', './Transforms-65aba0a4', './RuntimeError-ad75c885', './WebGLConstants-497deb20', './ComponentDatatype-69643096', './GeometryAttribute-ed359d71', './when-ee12a2cb', './GeometryAttributes-eecc9f43', './AttributeCompression-87682214', './GeometryPipeline-f0b16df6', './EncodedCartesian3-8b2b90d0', './IndexDatatype-3de60176', './IntersectionTests-c2360ffa', './Plane-a1a3fd52', './GeometryOffsetAttribute-cb30cd97', './VertexFormat-fbb91dc7', './GeometryInstance-72fd4e35', './arrayRemoveDuplicates-dd708d81', './BoundingRectangle-36e6acca', './EllipsoidTangentPlane-10c6053a', './ArcType-d521909b', './EllipsoidRhumbLine-c6cdbfd3', './PolygonPipeline-e486c11c', './PolygonGeometryLibrary-7f7b74b5', './EllipsoidGeodesic-53e988a6'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, when, GeometryAttributes, AttributeCompression, GeometryPipeline, EncodedCartesian3, IndexDatatype, IntersectionTests, Plane, GeometryOffsetAttribute, VertexFormat, GeometryInstance, arrayRemoveDuplicates, BoundingRectangle, EllipsoidTangentPlane, ArcType, EllipsoidRhumbLine, PolygonPipeline, PolygonGeometryLibrary, EllipsoidGeodesic) { 'use strict';
  3. var scratchCarto1 = new Cartesian2.Cartographic();
  4. var scratchCarto2 = new Cartesian2.Cartographic();
  5. function adjustPosHeightsForNormal(position, p1, p2, ellipsoid) {
  6. var carto1 = ellipsoid.cartesianToCartographic(position, scratchCarto1);
  7. var height = carto1.height;
  8. var p1Carto = ellipsoid.cartesianToCartographic(p1, scratchCarto2);
  9. p1Carto.height = height;
  10. ellipsoid.cartographicToCartesian(p1Carto, p1);
  11. var p2Carto = ellipsoid.cartesianToCartographic(p2, scratchCarto2);
  12. p2Carto.height = height - 100;
  13. ellipsoid.cartographicToCartesian(p2Carto, p2);
  14. }
  15. var scratchBoundingRectangle = new BoundingRectangle.BoundingRectangle();
  16. var scratchPosition = new Cartesian2.Cartesian3();
  17. var scratchNormal = new Cartesian2.Cartesian3();
  18. var scratchTangent = new Cartesian2.Cartesian3();
  19. var scratchBitangent = new Cartesian2.Cartesian3();
  20. var p1Scratch = new Cartesian2.Cartesian3();
  21. var p2Scratch = new Cartesian2.Cartesian3();
  22. var scratchPerPosNormal = new Cartesian2.Cartesian3();
  23. var scratchPerPosTangent = new Cartesian2.Cartesian3();
  24. var scratchPerPosBitangent = new Cartesian2.Cartesian3();
  25. var appendTextureCoordinatesOrigin = new Cartesian2.Cartesian2();
  26. var appendTextureCoordinatesCartesian2 = new Cartesian2.Cartesian2();
  27. var appendTextureCoordinatesCartesian3 = new Cartesian2.Cartesian3();
  28. var appendTextureCoordinatesQuaternion = new Transforms.Quaternion();
  29. var appendTextureCoordinatesMatrix3 = new Transforms.Matrix3();
  30. var tangentMatrixScratch = new Transforms.Matrix3();
  31. function computeAttributes(options) {
  32. var vertexFormat = options.vertexFormat;
  33. var geometry = options.geometry;
  34. var shadowVolume = options.shadowVolume;
  35. var flatPositions = geometry.attributes.position.values;
  36. var length = flatPositions.length;
  37. var wall = options.wall;
  38. var top = options.top || wall;
  39. var bottom = options.bottom || wall;
  40. if (vertexFormat.st || vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent || shadowVolume) {
  41. // PERFORMANCE_IDEA: Compute before subdivision, then just interpolate during subdivision.
  42. // PERFORMANCE_IDEA: Compute with createGeometryFromPositions() for fast path when there's no holes.
  43. var boundingRectangle = options.boundingRectangle;
  44. var tangentPlane = options.tangentPlane;
  45. var ellipsoid = options.ellipsoid;
  46. var stRotation = options.stRotation;
  47. var perPositionHeight = options.perPositionHeight;
  48. var origin = appendTextureCoordinatesOrigin;
  49. origin.x = boundingRectangle.x;
  50. origin.y = boundingRectangle.y;
  51. var textureCoordinates = vertexFormat.st ? new Float32Array(2 * (length / 3)) : undefined;
  52. var normals;
  53. if (vertexFormat.normal) {
  54. if (perPositionHeight && top && !wall) {
  55. normals = geometry.attributes.normal.values;
  56. } else {
  57. normals = new Float32Array(length);
  58. }
  59. }
  60. var tangents = vertexFormat.tangent ? new Float32Array(length) : undefined;
  61. var bitangents = vertexFormat.bitangent ? new Float32Array(length) : undefined;
  62. var extrudeNormals = shadowVolume ? new Float32Array(length) : undefined;
  63. var textureCoordIndex = 0;
  64. var attrIndex = 0;
  65. var normal = scratchNormal;
  66. var tangent = scratchTangent;
  67. var bitangent = scratchBitangent;
  68. var recomputeNormal = true;
  69. var textureMatrix = appendTextureCoordinatesMatrix3;
  70. var tangentRotationMatrix = tangentMatrixScratch;
  71. if (stRotation !== 0.0) {
  72. var rotation = Transforms.Quaternion.fromAxisAngle(tangentPlane._plane.normal, stRotation, appendTextureCoordinatesQuaternion);
  73. textureMatrix = Transforms.Matrix3.fromQuaternion(rotation, textureMatrix);
  74. rotation = Transforms.Quaternion.fromAxisAngle(tangentPlane._plane.normal, -stRotation, appendTextureCoordinatesQuaternion);
  75. tangentRotationMatrix = Transforms.Matrix3.fromQuaternion(rotation, tangentRotationMatrix);
  76. } else {
  77. textureMatrix = Transforms.Matrix3.clone(Transforms.Matrix3.IDENTITY, textureMatrix);
  78. tangentRotationMatrix = Transforms.Matrix3.clone(Transforms.Matrix3.IDENTITY, tangentRotationMatrix);
  79. }
  80. var bottomOffset = 0;
  81. var bottomOffset2 = 0;
  82. if (top && bottom) {
  83. bottomOffset = length / 2;
  84. bottomOffset2 = length / 3;
  85. length /= 2;
  86. }
  87. for ( var i = 0; i < length; i += 3) {
  88. var position = Cartesian2.Cartesian3.fromArray(flatPositions, i, appendTextureCoordinatesCartesian3);
  89. if (vertexFormat.st) {
  90. var p = Transforms.Matrix3.multiplyByVector(textureMatrix, position, scratchPosition);
  91. p = ellipsoid.scaleToGeodeticSurface(p,p);
  92. var st = tangentPlane.projectPointOntoPlane(p, appendTextureCoordinatesCartesian2);
  93. Cartesian2.Cartesian2.subtract(st, origin, st);
  94. var stx = _Math.CesiumMath.clamp(st.x / boundingRectangle.width, 0, 1);
  95. var sty = _Math.CesiumMath.clamp(st.y / boundingRectangle.height, 0, 1);
  96. if (bottom) {
  97. textureCoordinates[textureCoordIndex + bottomOffset2] = stx;
  98. textureCoordinates[textureCoordIndex + 1 + bottomOffset2] = sty;
  99. }
  100. if (top) {
  101. textureCoordinates[textureCoordIndex] = stx;
  102. textureCoordinates[textureCoordIndex + 1] = sty;
  103. }
  104. textureCoordIndex += 2;
  105. }
  106. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent || shadowVolume) {
  107. var attrIndex1 = attrIndex + 1;
  108. var attrIndex2 = attrIndex + 2;
  109. if (wall) {
  110. if (i + 3 < length) {
  111. var p1 = Cartesian2.Cartesian3.fromArray(flatPositions, i + 3, p1Scratch);
  112. if (recomputeNormal) {
  113. var p2 = Cartesian2.Cartesian3.fromArray(flatPositions, i + length, p2Scratch);
  114. if (perPositionHeight) {
  115. adjustPosHeightsForNormal(position, p1, p2, ellipsoid);
  116. }
  117. Cartesian2.Cartesian3.subtract(p1, position, p1);
  118. Cartesian2.Cartesian3.subtract(p2, position, p2);
  119. normal = Cartesian2.Cartesian3.normalize(Cartesian2.Cartesian3.cross(p2, p1, normal), normal);
  120. recomputeNormal = false;
  121. }
  122. if (Cartesian2.Cartesian3.equalsEpsilon(p1, position, _Math.CesiumMath.EPSILON10)) { // if we've reached a corner
  123. recomputeNormal = true;
  124. }
  125. }
  126. if (vertexFormat.tangent || vertexFormat.bitangent) {
  127. bitangent = ellipsoid.geodeticSurfaceNormal(position, bitangent);
  128. if (vertexFormat.tangent) {
  129. tangent = Cartesian2.Cartesian3.normalize(Cartesian2.Cartesian3.cross(bitangent, normal, tangent), tangent);
  130. }
  131. }
  132. } else {
  133. normal = ellipsoid.geodeticSurfaceNormal(position, normal);
  134. if (vertexFormat.tangent || vertexFormat.bitangent) {
  135. if (perPositionHeight) {
  136. scratchPerPosNormal = Cartesian2.Cartesian3.fromArray(normals, attrIndex, scratchPerPosNormal);
  137. scratchPerPosTangent = Cartesian2.Cartesian3.cross(Cartesian2.Cartesian3.UNIT_Z, scratchPerPosNormal, scratchPerPosTangent);
  138. scratchPerPosTangent = Cartesian2.Cartesian3.normalize(Transforms.Matrix3.multiplyByVector(tangentRotationMatrix, scratchPerPosTangent, scratchPerPosTangent), scratchPerPosTangent);
  139. if (vertexFormat.bitangent) {
  140. scratchPerPosBitangent = Cartesian2.Cartesian3.normalize(Cartesian2.Cartesian3.cross(scratchPerPosNormal, scratchPerPosTangent, scratchPerPosBitangent), scratchPerPosBitangent);
  141. }
  142. }
  143. tangent = Cartesian2.Cartesian3.cross(Cartesian2.Cartesian3.UNIT_Z, normal, tangent);
  144. tangent = Cartesian2.Cartesian3.normalize(Transforms.Matrix3.multiplyByVector(tangentRotationMatrix, tangent, tangent), tangent);
  145. if (vertexFormat.bitangent) {
  146. bitangent = Cartesian2.Cartesian3.normalize(Cartesian2.Cartesian3.cross(normal, tangent, bitangent), bitangent);
  147. }
  148. }
  149. }
  150. if (vertexFormat.normal) {
  151. if (options.wall) {
  152. normals[attrIndex + bottomOffset] = normal.x;
  153. normals[attrIndex1 + bottomOffset] = normal.y;
  154. normals[attrIndex2 + bottomOffset] = normal.z;
  155. } else if (bottom){
  156. normals[attrIndex + bottomOffset] = -normal.x;
  157. normals[attrIndex1 + bottomOffset] = -normal.y;
  158. normals[attrIndex2 + bottomOffset] = -normal.z;
  159. }
  160. if ((top && !perPositionHeight) || wall) {
  161. normals[attrIndex] = normal.x;
  162. normals[attrIndex1] = normal.y;
  163. normals[attrIndex2] = normal.z;
  164. }
  165. }
  166. if (shadowVolume) {
  167. if (wall) {
  168. normal = ellipsoid.geodeticSurfaceNormal(position, normal);
  169. }
  170. extrudeNormals[attrIndex + bottomOffset] = -normal.x;
  171. extrudeNormals[attrIndex1 + bottomOffset] = -normal.y;
  172. extrudeNormals[attrIndex2 + bottomOffset] = -normal.z;
  173. }
  174. if (vertexFormat.tangent) {
  175. if (options.wall) {
  176. tangents[attrIndex + bottomOffset] = tangent.x;
  177. tangents[attrIndex1 + bottomOffset] = tangent.y;
  178. tangents[attrIndex2 + bottomOffset] = tangent.z;
  179. } else if (bottom) {
  180. tangents[attrIndex + bottomOffset] = -tangent.x;
  181. tangents[attrIndex1 + bottomOffset] = -tangent.y;
  182. tangents[attrIndex2 + bottomOffset] = -tangent.z;
  183. }
  184. if(top) {
  185. if (perPositionHeight) {
  186. tangents[attrIndex] = scratchPerPosTangent.x;
  187. tangents[attrIndex1] = scratchPerPosTangent.y;
  188. tangents[attrIndex2] = scratchPerPosTangent.z;
  189. } else {
  190. tangents[attrIndex] = tangent.x;
  191. tangents[attrIndex1] = tangent.y;
  192. tangents[attrIndex2] = tangent.z;
  193. }
  194. }
  195. }
  196. if (vertexFormat.bitangent) {
  197. if (bottom) {
  198. bitangents[attrIndex + bottomOffset] = bitangent.x;
  199. bitangents[attrIndex1 + bottomOffset] = bitangent.y;
  200. bitangents[attrIndex2 + bottomOffset] = bitangent.z;
  201. }
  202. if (top) {
  203. if (perPositionHeight) {
  204. bitangents[attrIndex] = scratchPerPosBitangent.x;
  205. bitangents[attrIndex1] = scratchPerPosBitangent.y;
  206. bitangents[attrIndex2] = scratchPerPosBitangent.z;
  207. } else {
  208. bitangents[attrIndex] = bitangent.x;
  209. bitangents[attrIndex1] = bitangent.y;
  210. bitangents[attrIndex2] = bitangent.z;
  211. }
  212. }
  213. }
  214. attrIndex += 3;
  215. }
  216. }
  217. if (vertexFormat.st) {
  218. geometry.attributes.st = new GeometryAttribute.GeometryAttribute({
  219. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  220. componentsPerAttribute : 2,
  221. values : textureCoordinates
  222. });
  223. }
  224. if (vertexFormat.normal) {
  225. geometry.attributes.normal = new GeometryAttribute.GeometryAttribute({
  226. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  227. componentsPerAttribute : 3,
  228. values : normals
  229. });
  230. }
  231. if (vertexFormat.tangent) {
  232. geometry.attributes.tangent = new GeometryAttribute.GeometryAttribute({
  233. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  234. componentsPerAttribute : 3,
  235. values : tangents
  236. });
  237. }
  238. if (vertexFormat.bitangent) {
  239. geometry.attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  240. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  241. componentsPerAttribute : 3,
  242. values : bitangents
  243. });
  244. }
  245. if (shadowVolume) {
  246. geometry.attributes.extrudeDirection = new GeometryAttribute.GeometryAttribute({
  247. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  248. componentsPerAttribute : 3,
  249. values : extrudeNormals
  250. });
  251. }
  252. }
  253. if (options.extrude && defined.defined(options.offsetAttribute)) {
  254. var size = flatPositions.length / 3;
  255. var offsetAttribute = new Uint8Array(size);
  256. if (options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  257. if ((top && bottom) || wall) {
  258. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, 1, 0, size / 2);
  259. } else if (top) {
  260. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, 1);
  261. }
  262. } else {
  263. var offsetValue = options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  264. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, offsetValue);
  265. }
  266. geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  267. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  268. componentsPerAttribute : 1,
  269. values : offsetAttribute
  270. });
  271. }
  272. return geometry;
  273. }
  274. var startCartographicScratch = new Cartesian2.Cartographic();
  275. var endCartographicScratch = new Cartesian2.Cartographic();
  276. var idlCross = {
  277. westOverIDL : 0.0,
  278. eastOverIDL : 0.0
  279. };
  280. var ellipsoidGeodesic = new EllipsoidGeodesic.EllipsoidGeodesic();
  281. function computeRectangle(positions, ellipsoid, arcType, granularity, result) {
  282. result = defaultValue.defaultValue(result, new Cartesian2.Rectangle());
  283. if (!defined.defined(positions) || positions.length < 3) {
  284. result.west = 0.0;
  285. result.north = 0.0;
  286. result.south = 0.0;
  287. result.east = 0.0;
  288. return result;
  289. }
  290. if (arcType === ArcType.ArcType.RHUMB) {
  291. return Cartesian2.Rectangle.fromCartesianArray(positions, ellipsoid, result);
  292. }
  293. if (!ellipsoidGeodesic.ellipsoid.equals(ellipsoid)) {
  294. ellipsoidGeodesic = new EllipsoidGeodesic.EllipsoidGeodesic(undefined, undefined, ellipsoid);
  295. }
  296. result.west = Number.POSITIVE_INFINITY;
  297. result.east = Number.NEGATIVE_INFINITY;
  298. result.south = Number.POSITIVE_INFINITY;
  299. result.north = Number.NEGATIVE_INFINITY;
  300. idlCross.westOverIDL = Number.POSITIVE_INFINITY;
  301. idlCross.eastOverIDL = Number.NEGATIVE_INFINITY;
  302. var inverseChordLength = 1.0 / _Math.CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  303. var positionsLength = positions.length;
  304. var endCartographic = ellipsoid.cartesianToCartographic(positions[0], endCartographicScratch);
  305. var startCartographic = startCartographicScratch;
  306. var swap;
  307. for (var i = 1; i < positionsLength; i++) {
  308. swap = startCartographic;
  309. startCartographic = endCartographic;
  310. endCartographic = ellipsoid.cartesianToCartographic(positions[i], swap);
  311. ellipsoidGeodesic.setEndPoints(startCartographic, endCartographic);
  312. interpolateAndGrowRectangle(ellipsoidGeodesic, inverseChordLength, result, idlCross);
  313. }
  314. swap = startCartographic;
  315. startCartographic = endCartographic;
  316. endCartographic = ellipsoid.cartesianToCartographic(positions[0], swap);
  317. ellipsoidGeodesic.setEndPoints(startCartographic, endCartographic);
  318. interpolateAndGrowRectangle(ellipsoidGeodesic, inverseChordLength, result, idlCross);
  319. if (result.east - result.west > idlCross.eastOverIDL - idlCross.westOverIDL) {
  320. result.west = idlCross.westOverIDL;
  321. result.east = idlCross.eastOverIDL;
  322. if (result.east > _Math.CesiumMath.PI) {
  323. result.east = result.east - _Math.CesiumMath.TWO_PI;
  324. }
  325. if (result.west > _Math.CesiumMath.PI) {
  326. result.west = result.west - _Math.CesiumMath.TWO_PI;
  327. }
  328. }
  329. return result;
  330. }
  331. var interpolatedCartographicScratch = new Cartesian2.Cartographic();
  332. function interpolateAndGrowRectangle(ellipsoidGeodesic, inverseChordLength, result, idlCross) {
  333. var segmentLength = ellipsoidGeodesic.surfaceDistance;
  334. var numPoints = Math.ceil(segmentLength * inverseChordLength);
  335. var subsegmentDistance = numPoints > 0 ? segmentLength / (numPoints - 1) : Number.POSITIVE_INFINITY;
  336. var interpolationDistance = 0.0;
  337. for (var i = 0; i < numPoints; i++) {
  338. var interpolatedCartographic = ellipsoidGeodesic.interpolateUsingSurfaceDistance(interpolationDistance, interpolatedCartographicScratch);
  339. interpolationDistance += subsegmentDistance;
  340. var longitude = interpolatedCartographic.longitude;
  341. var latitude = interpolatedCartographic.latitude;
  342. result.west = Math.min(result.west, longitude);
  343. result.east = Math.max(result.east, longitude);
  344. result.south = Math.min(result.south, latitude);
  345. result.north = Math.max(result.north, latitude);
  346. var lonAdjusted = longitude >= 0 ? longitude : longitude + _Math.CesiumMath.TWO_PI;
  347. idlCross.westOverIDL = Math.min(idlCross.westOverIDL, lonAdjusted);
  348. idlCross.eastOverIDL = Math.max(idlCross.eastOverIDL, lonAdjusted);
  349. }
  350. }
  351. var createGeometryFromPositionsExtrudedPositions = [];
  352. function createGeometryFromPositionsExtruded(ellipsoid, polygon, granularity, hierarchy, perPositionHeight, closeTop, closeBottom, vertexFormat, arcType) {
  353. var geos = {
  354. walls : []
  355. };
  356. var i;
  357. if (closeTop || closeBottom) {
  358. var topGeo = PolygonGeometryLibrary.PolygonGeometryLibrary.createGeometryFromPositions(ellipsoid, polygon, granularity, perPositionHeight, vertexFormat, arcType);
  359. var edgePoints = topGeo.attributes.position.values;
  360. var indices = topGeo.indices;
  361. var numPositions;
  362. var newIndices;
  363. if (closeTop && closeBottom) {
  364. var topBottomPositions = edgePoints.concat(edgePoints);
  365. numPositions = topBottomPositions.length / 3;
  366. newIndices = IndexDatatype.IndexDatatype.createTypedArray(numPositions, indices.length * 2);
  367. newIndices.set(indices);
  368. var ilength = indices.length;
  369. var length = numPositions / 2;
  370. for (i = 0; i < ilength; i += 3) {
  371. var i0 = newIndices[i] + length;
  372. var i1 = newIndices[i + 1] + length;
  373. var i2 = newIndices[i + 2] + length;
  374. newIndices[i + ilength] = i2;
  375. newIndices[i + 1 + ilength] = i1;
  376. newIndices[i + 2 + ilength] = i0;
  377. }
  378. topGeo.attributes.position.values = topBottomPositions;
  379. if (perPositionHeight && vertexFormat.normal) {
  380. var normals = topGeo.attributes.normal.values;
  381. topGeo.attributes.normal.values = new Float32Array(topBottomPositions.length);
  382. topGeo.attributes.normal.values.set(normals);
  383. }
  384. topGeo.indices = newIndices;
  385. } else if (closeBottom) {
  386. numPositions = edgePoints.length / 3;
  387. newIndices = IndexDatatype.IndexDatatype.createTypedArray(numPositions, indices.length);
  388. for (i = 0; i < indices.length; i += 3) {
  389. newIndices[i] = indices[i + 2];
  390. newIndices[i + 1] = indices[i + 1];
  391. newIndices[i + 2] = indices[i];
  392. }
  393. topGeo.indices = newIndices;
  394. }
  395. geos.topAndBottom = new GeometryInstance.GeometryInstance({
  396. geometry : topGeo
  397. });
  398. }
  399. var outerRing = hierarchy.outerRing;
  400. var tangentPlane = EllipsoidTangentPlane.EllipsoidTangentPlane.fromPoints(outerRing, ellipsoid);
  401. var positions2D = tangentPlane.projectPointsOntoPlane(outerRing, createGeometryFromPositionsExtrudedPositions);
  402. var windingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(positions2D);
  403. if (windingOrder === PolygonPipeline.WindingOrder.CLOCKWISE) {
  404. outerRing = outerRing.slice().reverse();
  405. }
  406. var wallGeo = PolygonGeometryLibrary.PolygonGeometryLibrary.computeWallGeometry(outerRing, ellipsoid, granularity, perPositionHeight, arcType);
  407. geos.walls.push(new GeometryInstance.GeometryInstance({
  408. geometry : wallGeo
  409. }));
  410. var holes = hierarchy.holes;
  411. for (i = 0; i < holes.length; i++) {
  412. var hole = holes[i];
  413. tangentPlane = EllipsoidTangentPlane.EllipsoidTangentPlane.fromPoints(hole, ellipsoid);
  414. positions2D = tangentPlane.projectPointsOntoPlane(hole, createGeometryFromPositionsExtrudedPositions);
  415. windingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(positions2D);
  416. if (windingOrder === PolygonPipeline.WindingOrder.COUNTER_CLOCKWISE) {
  417. hole = hole.slice().reverse();
  418. }
  419. wallGeo = PolygonGeometryLibrary.PolygonGeometryLibrary.computeWallGeometry(hole, ellipsoid, granularity, perPositionHeight, arcType);
  420. geos.walls.push(new GeometryInstance.GeometryInstance({
  421. geometry : wallGeo
  422. }));
  423. }
  424. return geos;
  425. }
  426. /**
  427. * A description of a polygon on the ellipsoid. The polygon is defined by a polygon hierarchy. Polygon geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  428. *
  429. * @alias PolygonGeometry
  430. * @constructor
  431. *
  432. * @param {Object} options Object with the following properties:
  433. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  434. * @param {Number} [options.height=0.0] The distance in meters between the polygon and the ellipsoid surface.
  435. * @param {Number} [options.extrudedHeight] The distance in meters between the polygon's extruded face and the ellipsoid surface.
  436. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  437. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  438. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  439. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  440. * @param {Boolean} [options.perPositionHeight=false] Use the height of options.positions for each position instead of using options.height to determine the height.
  441. * @param {Boolean} [options.closeTop=true] When false, leaves off the top of an extruded polygon open.
  442. * @param {Boolean} [options.closeBottom=true] When false, leaves off the bottom of an extruded polygon open.
  443. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polygon edges must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
  444. *
  445. * @see PolygonGeometry#createGeometry
  446. * @see PolygonGeometry#fromPositions
  447. *
  448. * @demo {@link https://sandcastle.cesium.com/index.html?src=Polygon.html|Cesium Sandcastle Polygon Demo}
  449. *
  450. * @example
  451. * // 1. create a polygon from points
  452. * var polygon = new Cesium.PolygonGeometry({
  453. * polygonHierarchy : new Cesium.PolygonHierarchy(
  454. * Cesium.Cartesian3.fromDegreesArray([
  455. * -72.0, 40.0,
  456. * -70.0, 35.0,
  457. * -75.0, 30.0,
  458. * -70.0, 30.0,
  459. * -68.0, 40.0
  460. * ])
  461. * )
  462. * });
  463. * var geometry = Cesium.PolygonGeometry.createGeometry(polygon);
  464. *
  465. * // 2. create a nested polygon with holes
  466. * var polygonWithHole = new Cesium.PolygonGeometry({
  467. * polygonHierarchy : new Cesium.PolygonHierarchy(
  468. * Cesium.Cartesian3.fromDegreesArray([
  469. * -109.0, 30.0,
  470. * -95.0, 30.0,
  471. * -95.0, 40.0,
  472. * -109.0, 40.0
  473. * ]),
  474. * [new Cesium.PolygonHierarchy(
  475. * Cesium.Cartesian3.fromDegreesArray([
  476. * -107.0, 31.0,
  477. * -107.0, 39.0,
  478. * -97.0, 39.0,
  479. * -97.0, 31.0
  480. * ]),
  481. * [new Cesium.PolygonHierarchy(
  482. * Cesium.Cartesian3.fromDegreesArray([
  483. * -105.0, 33.0,
  484. * -99.0, 33.0,
  485. * -99.0, 37.0,
  486. * -105.0, 37.0
  487. * ]),
  488. * [new Cesium.PolygonHierarchy(
  489. * Cesium.Cartesian3.fromDegreesArray([
  490. * -103.0, 34.0,
  491. * -101.0, 34.0,
  492. * -101.0, 36.0,
  493. * -103.0, 36.0
  494. * ])
  495. * )]
  496. * )]
  497. * )]
  498. * )
  499. * });
  500. * var geometry = Cesium.PolygonGeometry.createGeometry(polygonWithHole);
  501. *
  502. * // 3. create extruded polygon
  503. * var extrudedPolygon = new Cesium.PolygonGeometry({
  504. * polygonHierarchy : new Cesium.PolygonHierarchy(
  505. * Cesium.Cartesian3.fromDegreesArray([
  506. * -72.0, 40.0,
  507. * -70.0, 35.0,
  508. * -75.0, 30.0,
  509. * -70.0, 30.0,
  510. * -68.0, 40.0
  511. * ])
  512. * ),
  513. * extrudedHeight: 300000
  514. * });
  515. * var geometry = Cesium.PolygonGeometry.createGeometry(extrudedPolygon);
  516. */
  517. function PolygonGeometry(options) {
  518. //>>includeStart('debug', pragmas.debug);
  519. Check.Check.typeOf.object('options', options);
  520. Check.Check.typeOf.object('options.polygonHierarchy', options.polygonHierarchy);
  521. if (defined.defined(options.perPositionHeight) && options.perPositionHeight && defined.defined(options.height)) {
  522. throw new Check.DeveloperError('Cannot use both options.perPositionHeight and options.height');
  523. }
  524. if (defined.defined(options.arcType) && options.arcType !== ArcType.ArcType.GEODESIC && options.arcType !== ArcType.ArcType.RHUMB) {
  525. throw new Check.DeveloperError('Invalid arcType. Valid options are ArcType.GEODESIC and ArcType.RHUMB.');
  526. }
  527. //>>includeEnd('debug');
  528. var polygonHierarchy = options.polygonHierarchy;
  529. var vertexFormat = defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  530. var ellipsoid = defaultValue.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84);
  531. var granularity = defaultValue.defaultValue(options.granularity, _Math.CesiumMath.RADIANS_PER_DEGREE);
  532. var stRotation = defaultValue.defaultValue(options.stRotation, 0.0);
  533. var perPositionHeight = defaultValue.defaultValue(options.perPositionHeight, false);
  534. var perPositionHeightExtrude = perPositionHeight && defined.defined(options.extrudedHeight);
  535. var height = defaultValue.defaultValue(options.height, 0.0);
  536. var extrudedHeight = defaultValue.defaultValue(options.extrudedHeight, height);
  537. if (!perPositionHeightExtrude) {
  538. var h = Math.max(height, extrudedHeight);
  539. extrudedHeight = Math.min(height, extrudedHeight);
  540. height = h;
  541. }
  542. this._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat);
  543. this._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid);
  544. this._granularity = granularity;
  545. this._stRotation = stRotation;
  546. this._height = height;
  547. this._extrudedHeight = extrudedHeight;
  548. this._closeTop = defaultValue.defaultValue(options.closeTop, true);
  549. this._closeBottom = defaultValue.defaultValue(options.closeBottom, true);
  550. this._polygonHierarchy = polygonHierarchy;
  551. this._perPositionHeight = perPositionHeight;
  552. this._perPositionHeightExtrude = perPositionHeightExtrude;
  553. this._shadowVolume = defaultValue.defaultValue(options.shadowVolume, false);
  554. this._workerName = 'createPolygonGeometry';
  555. this._offsetAttribute = options.offsetAttribute;
  556. this._arcType = defaultValue.defaultValue(options.arcType, ArcType.ArcType.GEODESIC);
  557. this._rectangle = undefined;
  558. this._textureCoordinateRotationPoints = undefined;
  559. /**
  560. * The number of elements used to pack the object into an array.
  561. * @type {Number}
  562. */
  563. this.packedLength = PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) + Cartesian2.Ellipsoid.packedLength + VertexFormat.VertexFormat.packedLength + 12;
  564. }
  565. /**
  566. * A description of a polygon from an array of positions. Polygon geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  567. *
  568. * @param {Object} options Object with the following properties:
  569. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  570. * @param {Number} [options.height=0.0] The height of the polygon.
  571. * @param {Number} [options.extrudedHeight] The height of the polygon extrusion.
  572. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  573. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  574. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  575. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  576. * @param {Boolean} [options.perPositionHeight=false] Use the height of options.positions for each position instead of using options.height to determine the height.
  577. * @param {Boolean} [options.closeTop=true] When false, leaves off the top of an extruded polygon open.
  578. * @param {Boolean} [options.closeBottom=true] When false, leaves off the bottom of an extruded polygon open.
  579. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polygon edges must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
  580. * @returns {PolygonGeometry}
  581. *
  582. *
  583. * @example
  584. * // create a polygon from points
  585. * var polygon = Cesium.PolygonGeometry.fromPositions({
  586. * positions : Cesium.Cartesian3.fromDegreesArray([
  587. * -72.0, 40.0,
  588. * -70.0, 35.0,
  589. * -75.0, 30.0,
  590. * -70.0, 30.0,
  591. * -68.0, 40.0
  592. * ])
  593. * });
  594. * var geometry = Cesium.PolygonGeometry.createGeometry(polygon);
  595. *
  596. * @see PolygonGeometry#createGeometry
  597. */
  598. PolygonGeometry.fromPositions = function(options) {
  599. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  600. //>>includeStart('debug', pragmas.debug);
  601. Check.Check.defined('options.positions', options.positions);
  602. //>>includeEnd('debug');
  603. var newOptions = {
  604. polygonHierarchy : {
  605. positions : options.positions
  606. },
  607. height : options.height,
  608. extrudedHeight : options.extrudedHeight,
  609. vertexFormat : options.vertexFormat,
  610. stRotation : options.stRotation,
  611. ellipsoid : options.ellipsoid,
  612. granularity : options.granularity,
  613. perPositionHeight : options.perPositionHeight,
  614. closeTop : options.closeTop,
  615. closeBottom : options.closeBottom,
  616. offsetAttribute : options.offsetAttribute,
  617. arcType : options.arcType
  618. };
  619. return new PolygonGeometry(newOptions);
  620. };
  621. /**
  622. * Stores the provided instance into the provided array.
  623. *
  624. * @param {PolygonGeometry} value The value to pack.
  625. * @param {Number[]} array The array to pack into.
  626. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  627. *
  628. * @returns {Number[]} The array that was packed into
  629. */
  630. PolygonGeometry.pack = function(value, array, startingIndex) {
  631. //>>includeStart('debug', pragmas.debug);
  632. Check.Check.typeOf.object('value', value);
  633. Check.Check.defined('array', array);
  634. //>>includeEnd('debug');
  635. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  636. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(value._polygonHierarchy, array, startingIndex);
  637. Cartesian2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  638. startingIndex += Cartesian2.Ellipsoid.packedLength;
  639. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  640. startingIndex += VertexFormat.VertexFormat.packedLength;
  641. array[startingIndex++] = value._height;
  642. array[startingIndex++] = value._extrudedHeight;
  643. array[startingIndex++] = value._granularity;
  644. array[startingIndex++] = value._stRotation;
  645. array[startingIndex++] = value._perPositionHeightExtrude ? 1.0 : 0.0;
  646. array[startingIndex++] = value._perPositionHeight ? 1.0 : 0.0;
  647. array[startingIndex++] = value._closeTop ? 1.0 : 0.0;
  648. array[startingIndex++] = value._closeBottom ? 1.0 : 0.0;
  649. array[startingIndex++] = value._shadowVolume ? 1.0 : 0.0;
  650. array[startingIndex++] = defaultValue.defaultValue(value._offsetAttribute, -1);
  651. array[startingIndex++] = value._arcType;
  652. array[startingIndex] = value.packedLength;
  653. return array;
  654. };
  655. var scratchEllipsoid = Cartesian2.Ellipsoid.clone(Cartesian2.Ellipsoid.UNIT_SPHERE);
  656. var scratchVertexFormat = new VertexFormat.VertexFormat();
  657. //Only used to avoid inability to default construct.
  658. var dummyOptions = {
  659. polygonHierarchy : {}
  660. };
  661. /**
  662. * Retrieves an instance from a packed array.
  663. *
  664. * @param {Number[]} array The packed array.
  665. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  666. * @param {PolygonGeometry} [result] The object into which to store the result.
  667. */
  668. PolygonGeometry.unpack = function(array, startingIndex, result) {
  669. //>>includeStart('debug', pragmas.debug);
  670. Check.Check.defined('array', array);
  671. //>>includeEnd('debug');
  672. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  673. var polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(array, startingIndex);
  674. startingIndex = polygonHierarchy.startingIndex;
  675. delete polygonHierarchy.startingIndex;
  676. var ellipsoid = Cartesian2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  677. startingIndex += Cartesian2.Ellipsoid.packedLength;
  678. var vertexFormat = VertexFormat.VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  679. startingIndex += VertexFormat.VertexFormat.packedLength;
  680. var height = array[startingIndex++];
  681. var extrudedHeight = array[startingIndex++];
  682. var granularity = array[startingIndex++];
  683. var stRotation = array[startingIndex++];
  684. var perPositionHeightExtrude = array[startingIndex++] === 1.0;
  685. var perPositionHeight = array[startingIndex++] === 1.0;
  686. var closeTop = array[startingIndex++] === 1.0;
  687. var closeBottom = array[startingIndex++] === 1.0;
  688. var shadowVolume = array[startingIndex++] === 1.0;
  689. var offsetAttribute = array[startingIndex++];
  690. var arcType = array[startingIndex++];
  691. var packedLength = array[startingIndex];
  692. if (!defined.defined(result)) {
  693. result = new PolygonGeometry(dummyOptions);
  694. }
  695. result._polygonHierarchy = polygonHierarchy;
  696. result._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  697. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  698. result._height = height;
  699. result._extrudedHeight = extrudedHeight;
  700. result._granularity = granularity;
  701. result._stRotation = stRotation;
  702. result._perPositionHeightExtrude = perPositionHeightExtrude;
  703. result._perPositionHeight = perPositionHeight;
  704. result._closeTop = closeTop;
  705. result._closeBottom = closeBottom;
  706. result._shadowVolume = shadowVolume;
  707. result._offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  708. result._arcType = arcType;
  709. result.packedLength = packedLength;
  710. return result;
  711. };
  712. /**
  713. * Returns the bounding rectangle given the provided options
  714. *
  715. * @param {Object} options Object with the following properties:
  716. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  717. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions sampled.
  718. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polygon edges must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
  719. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  720. * @param {Rectangle} [result] An object in which to store the result.
  721. *
  722. * @returns {Rectangle} The result rectangle
  723. */
  724. PolygonGeometry.computeRectangle = function(options, result) {
  725. //>>includeStart('debug', pragmas.debug);
  726. Check.Check.typeOf.object('options', options);
  727. Check.Check.typeOf.object('options.polygonHierarchy', options.polygonHierarchy);
  728. //>>includeEnd('debug');
  729. var granularity = defaultValue.defaultValue(options.granularity, _Math.CesiumMath.RADIANS_PER_DEGREE);
  730. var arcType = defaultValue.defaultValue(options.arcType, ArcType.ArcType.GEODESIC);
  731. //>>includeStart('debug', pragmas.debug);
  732. if (arcType !== ArcType.ArcType.GEODESIC && arcType !== ArcType.ArcType.RHUMB) {
  733. throw new Check.DeveloperError('Invalid arcType. Valid options are ArcType.GEODESIC and ArcType.RHUMB.');
  734. }
  735. //>>includeEnd('debug');
  736. var polygonHierarchy = options.polygonHierarchy;
  737. var ellipsoid = defaultValue.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84);
  738. return computeRectangle(polygonHierarchy.positions, ellipsoid, arcType, granularity, result);
  739. };
  740. /**
  741. * Computes the geometric representation of a polygon, including its vertices, indices, and a bounding sphere.
  742. *
  743. * @param {PolygonGeometry} polygonGeometry A description of the polygon.
  744. * @returns {Geometry|undefined} The computed vertices and indices.
  745. */
  746. PolygonGeometry.createGeometry = function(polygonGeometry) {
  747. var vertexFormat = polygonGeometry._vertexFormat;
  748. var ellipsoid = polygonGeometry._ellipsoid;
  749. var granularity = polygonGeometry._granularity;
  750. var stRotation = polygonGeometry._stRotation;
  751. var polygonHierarchy = polygonGeometry._polygonHierarchy;
  752. var perPositionHeight = polygonGeometry._perPositionHeight;
  753. var closeTop = polygonGeometry._closeTop;
  754. var closeBottom = polygonGeometry._closeBottom;
  755. var arcType = polygonGeometry._arcType;
  756. var outerPositions = polygonHierarchy.positions;
  757. if (outerPositions.length < 3) {
  758. return;
  759. }
  760. var tangentPlane = EllipsoidTangentPlane.EllipsoidTangentPlane.fromPoints(outerPositions, ellipsoid);
  761. var results = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonsFromHierarchy(polygonHierarchy, tangentPlane.projectPointsOntoPlane.bind(tangentPlane), !perPositionHeight, ellipsoid);
  762. var hierarchy = results.hierarchy;
  763. var polygons = results.polygons;
  764. if (hierarchy.length === 0) {
  765. return;
  766. }
  767. outerPositions = hierarchy[0].outerRing;
  768. var boundingRectangle = PolygonGeometryLibrary.PolygonGeometryLibrary.computeBoundingRectangle(tangentPlane.plane.normal, tangentPlane.projectPointOntoPlane.bind(tangentPlane), outerPositions, stRotation, scratchBoundingRectangle);
  769. var geometries = [];
  770. var height = polygonGeometry._height;
  771. var extrudedHeight = polygonGeometry._extrudedHeight;
  772. var extrude = polygonGeometry._perPositionHeightExtrude || !_Math.CesiumMath.equalsEpsilon(height, extrudedHeight, 0, _Math.CesiumMath.EPSILON2);
  773. var options = {
  774. perPositionHeight: perPositionHeight,
  775. vertexFormat: vertexFormat,
  776. geometry: undefined,
  777. tangentPlane: tangentPlane,
  778. boundingRectangle: boundingRectangle,
  779. ellipsoid: ellipsoid,
  780. stRotation: stRotation,
  781. bottom: false,
  782. top: true,
  783. wall: false,
  784. extrude: false,
  785. arcType: arcType
  786. };
  787. var i;
  788. if (extrude) {
  789. options.extrude = true;
  790. options.top = closeTop;
  791. options.bottom = closeBottom;
  792. options.shadowVolume = polygonGeometry._shadowVolume;
  793. options.offsetAttribute = polygonGeometry._offsetAttribute;
  794. for (i = 0; i < polygons.length; i++) {
  795. var splitGeometry = createGeometryFromPositionsExtruded(ellipsoid, polygons[i], granularity, hierarchy[i], perPositionHeight, closeTop, closeBottom, vertexFormat, arcType);
  796. var topAndBottom;
  797. if (closeTop && closeBottom) {
  798. topAndBottom = splitGeometry.topAndBottom;
  799. options.geometry = PolygonGeometryLibrary.PolygonGeometryLibrary.scaleToGeodeticHeightExtruded(topAndBottom.geometry, height, extrudedHeight, ellipsoid, perPositionHeight);
  800. } else if (closeTop) {
  801. topAndBottom = splitGeometry.topAndBottom;
  802. topAndBottom.geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(topAndBottom.geometry.attributes.position.values, height, ellipsoid, !perPositionHeight);
  803. options.geometry = topAndBottom.geometry;
  804. } else if (closeBottom) {
  805. topAndBottom = splitGeometry.topAndBottom;
  806. topAndBottom.geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(topAndBottom.geometry.attributes.position.values, extrudedHeight, ellipsoid, true);
  807. options.geometry = topAndBottom.geometry;
  808. }
  809. if (closeTop || closeBottom) {
  810. options.wall = false;
  811. topAndBottom.geometry = computeAttributes(options);
  812. geometries.push(topAndBottom);
  813. }
  814. var walls = splitGeometry.walls;
  815. options.wall = true;
  816. for ( var k = 0; k < walls.length; k++) {
  817. var wall = walls[k];
  818. options.geometry = PolygonGeometryLibrary.PolygonGeometryLibrary.scaleToGeodeticHeightExtruded(wall.geometry, height, extrudedHeight, ellipsoid, perPositionHeight);
  819. wall.geometry = computeAttributes(options);
  820. geometries.push(wall);
  821. }
  822. }
  823. } else {
  824. for (i = 0; i < polygons.length; i++) {
  825. var geometryInstance = new GeometryInstance.GeometryInstance({
  826. geometry : PolygonGeometryLibrary.PolygonGeometryLibrary.createGeometryFromPositions(ellipsoid, polygons[i], granularity, perPositionHeight, vertexFormat, arcType)
  827. });
  828. geometryInstance.geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(geometryInstance.geometry.attributes.position.values, height, ellipsoid, !perPositionHeight);
  829. options.geometry = geometryInstance.geometry;
  830. geometryInstance.geometry = computeAttributes(options);
  831. if (defined.defined(polygonGeometry._offsetAttribute)) {
  832. var length = geometryInstance.geometry.attributes.position.values.length;
  833. var applyOffset = new Uint8Array(length / 3);
  834. var offsetValue = polygonGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  835. GeometryOffsetAttribute.arrayFill(applyOffset, offsetValue);
  836. geometryInstance.geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  837. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  838. componentsPerAttribute : 1,
  839. values: applyOffset
  840. });
  841. }
  842. geometries.push(geometryInstance);
  843. }
  844. }
  845. var geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  846. geometry.attributes.position.values = new Float64Array(geometry.attributes.position.values);
  847. geometry.indices = IndexDatatype.IndexDatatype.createTypedArray(geometry.attributes.position.values.length / 3, geometry.indices);
  848. var attributes = geometry.attributes;
  849. var boundingSphere = Transforms.BoundingSphere.fromVertices(attributes.position.values);
  850. if (!vertexFormat.position) {
  851. delete attributes.position;
  852. }
  853. return new GeometryAttribute.Geometry({
  854. attributes : attributes,
  855. indices : geometry.indices,
  856. primitiveType : geometry.primitiveType,
  857. boundingSphere : boundingSphere,
  858. offsetAttribute : polygonGeometry._offsetAttribute
  859. });
  860. };
  861. /**
  862. * @private
  863. */
  864. PolygonGeometry.createShadowVolume = function(polygonGeometry, minHeightFunc, maxHeightFunc) {
  865. var granularity = polygonGeometry._granularity;
  866. var ellipsoid = polygonGeometry._ellipsoid;
  867. var minHeight = minHeightFunc(granularity, ellipsoid);
  868. var maxHeight = maxHeightFunc(granularity, ellipsoid);
  869. return new PolygonGeometry({
  870. polygonHierarchy : polygonGeometry._polygonHierarchy,
  871. ellipsoid : ellipsoid,
  872. stRotation : polygonGeometry._stRotation,
  873. granularity : granularity,
  874. perPositionHeight : false,
  875. extrudedHeight : minHeight,
  876. height : maxHeight,
  877. vertexFormat : VertexFormat.VertexFormat.POSITION_ONLY,
  878. shadowVolume: true,
  879. arcType : polygonGeometry._arcType
  880. });
  881. };
  882. function textureCoordinateRotationPoints(polygonGeometry) {
  883. var stRotation = -polygonGeometry._stRotation;
  884. if (stRotation === 0.0) {
  885. return [0, 0, 0, 1, 1, 0];
  886. }
  887. var ellipsoid = polygonGeometry._ellipsoid;
  888. var positions = polygonGeometry._polygonHierarchy.positions;
  889. var boundingRectangle = polygonGeometry.rectangle;
  890. return GeometryAttribute.Geometry._textureCoordinateRotationPoints(positions, stRotation, ellipsoid, boundingRectangle);
  891. }
  892. defineProperties.defineProperties(PolygonGeometry.prototype, {
  893. /**
  894. * @private
  895. */
  896. rectangle : {
  897. get : function() {
  898. if (!defined.defined(this._rectangle)) {
  899. var positions = this._polygonHierarchy.positions;
  900. this._rectangle = computeRectangle(positions, this._ellipsoid, this._arcType, this._granularity);
  901. }
  902. return this._rectangle;
  903. }
  904. },
  905. /**
  906. * For remapping texture coordinates when rendering PolygonGeometries as GroundPrimitives.
  907. * @private
  908. */
  909. textureCoordinateRotationPoints : {
  910. get : function() {
  911. if (!defined.defined(this._textureCoordinateRotationPoints)) {
  912. this._textureCoordinateRotationPoints = textureCoordinateRotationPoints(this);
  913. }
  914. return this._textureCoordinateRotationPoints;
  915. }
  916. }
  917. });
  918. function createPolygonGeometry(polygonGeometry, offset) {
  919. if (defined.defined(offset)) {
  920. polygonGeometry = PolygonGeometry.unpack(polygonGeometry, offset);
  921. }
  922. polygonGeometry._ellipsoid = Cartesian2.Ellipsoid.clone(polygonGeometry._ellipsoid);
  923. return PolygonGeometry.createGeometry(polygonGeometry);
  924. }
  925. return createPolygonGeometry;
  926. });