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