PolygonGeometry.js 49 KB

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