createCoplanarPolygonGeometry.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['./arrayRemoveDuplicates-63722a6f', './BoundingRectangle-66558952', './Transforms-c78c4637', './Matrix2-ab676047', './RuntimeError-1088cc64', './ComponentDatatype-e06f4e16', './CoplanarPolygonGeometryLibrary-a1408391', './defaultValue-a6eb9f34', './GeometryAttribute-4f02e2ad', './GeometryAttributes-aff51037', './GeometryInstance-6f541616', './GeometryPipeline-5a61c463', './IndexDatatype-c2232ebd', './PolygonGeometryLibrary-5dd88675', './PolygonPipeline-dd4a5392', './VertexFormat-65fd4be5', './_commonjsHelpers-89c9b271', './combine-7cf28d88', './WebGLConstants-d81b330d', './OrientedBoundingBox-28f77441', './EllipsoidTangentPlane-6691e012', './AxisAlignedBoundingBox-51d5a498', './IntersectionTests-f96cd46d', './Plane-c985a1d2', './AttributeCompression-8033f934', './EncodedCartesian3-7959a913', './ArcType-b714639b', './EllipsoidRhumbLine-34574f75'], (function (arrayRemoveDuplicates, BoundingRectangle, Transforms, Matrix2, RuntimeError, ComponentDatatype, CoplanarPolygonGeometryLibrary, defaultValue, GeometryAttribute, GeometryAttributes, GeometryInstance, GeometryPipeline, IndexDatatype, PolygonGeometryLibrary, PolygonPipeline, VertexFormat, _commonjsHelpers, combine, WebGLConstants, OrientedBoundingBox, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane, AttributeCompression, EncodedCartesian3, ArcType, EllipsoidRhumbLine) { 'use strict';
  26. const scratchPosition = new Matrix2.Cartesian3();
  27. const scratchBR = new BoundingRectangle.BoundingRectangle();
  28. const stScratch = new Matrix2.Cartesian2();
  29. const textureCoordinatesOrigin = new Matrix2.Cartesian2();
  30. const scratchNormal = new Matrix2.Cartesian3();
  31. const scratchTangent = new Matrix2.Cartesian3();
  32. const scratchBitangent = new Matrix2.Cartesian3();
  33. const centerScratch = new Matrix2.Cartesian3();
  34. const axis1Scratch = new Matrix2.Cartesian3();
  35. const axis2Scratch = new Matrix2.Cartesian3();
  36. const quaternionScratch = new Transforms.Quaternion();
  37. const textureMatrixScratch = new Matrix2.Matrix3();
  38. const tangentRotationScratch = new Matrix2.Matrix3();
  39. const surfaceNormalScratch = new Matrix2.Cartesian3();
  40. function createGeometryFromPolygon(
  41. polygon,
  42. vertexFormat,
  43. boundingRectangle,
  44. stRotation,
  45. hardcodedTextureCoordinates,
  46. projectPointTo2D,
  47. normal,
  48. tangent,
  49. bitangent
  50. ) {
  51. const positions = polygon.positions;
  52. let indices = PolygonPipeline.PolygonPipeline.triangulate(polygon.positions2D, polygon.holes);
  53. /* If polygon is completely unrenderable, just use the first three vertices */
  54. if (indices.length < 3) {
  55. indices = [0, 1, 2];
  56. }
  57. const newIndices = IndexDatatype.IndexDatatype.createTypedArray(
  58. positions.length,
  59. indices.length
  60. );
  61. newIndices.set(indices);
  62. let textureMatrix = textureMatrixScratch;
  63. if (stRotation !== 0.0) {
  64. let rotation = Transforms.Quaternion.fromAxisAngle(
  65. normal,
  66. stRotation,
  67. quaternionScratch
  68. );
  69. textureMatrix = Matrix2.Matrix3.fromQuaternion(rotation, textureMatrix);
  70. if (vertexFormat.tangent || vertexFormat.bitangent) {
  71. rotation = Transforms.Quaternion.fromAxisAngle(
  72. normal,
  73. -stRotation,
  74. quaternionScratch
  75. );
  76. const tangentRotation = Matrix2.Matrix3.fromQuaternion(
  77. rotation,
  78. tangentRotationScratch
  79. );
  80. tangent = Matrix2.Cartesian3.normalize(
  81. Matrix2.Matrix3.multiplyByVector(tangentRotation, tangent, tangent),
  82. tangent
  83. );
  84. if (vertexFormat.bitangent) {
  85. bitangent = Matrix2.Cartesian3.normalize(
  86. Matrix2.Cartesian3.cross(normal, tangent, bitangent),
  87. bitangent
  88. );
  89. }
  90. }
  91. } else {
  92. textureMatrix = Matrix2.Matrix3.clone(Matrix2.Matrix3.IDENTITY, textureMatrix);
  93. }
  94. const stOrigin = textureCoordinatesOrigin;
  95. if (vertexFormat.st) {
  96. stOrigin.x = boundingRectangle.x;
  97. stOrigin.y = boundingRectangle.y;
  98. }
  99. const length = positions.length;
  100. const size = length * 3;
  101. const flatPositions = new Float64Array(size);
  102. const normals = vertexFormat.normal ? new Float32Array(size) : undefined;
  103. const tangents = vertexFormat.tangent ? new Float32Array(size) : undefined;
  104. const bitangents = vertexFormat.bitangent
  105. ? new Float32Array(size)
  106. : undefined;
  107. const textureCoordinates = vertexFormat.st
  108. ? new Float32Array(length * 2)
  109. : undefined;
  110. let positionIndex = 0;
  111. let normalIndex = 0;
  112. let bitangentIndex = 0;
  113. let tangentIndex = 0;
  114. let stIndex = 0;
  115. for (let i = 0; i < length; i++) {
  116. const position = positions[i];
  117. flatPositions[positionIndex++] = position.x;
  118. flatPositions[positionIndex++] = position.y;
  119. flatPositions[positionIndex++] = position.z;
  120. if (vertexFormat.st) {
  121. if (
  122. defaultValue.defined(hardcodedTextureCoordinates) &&
  123. hardcodedTextureCoordinates.positions.length === length
  124. ) {
  125. textureCoordinates[stIndex++] =
  126. hardcodedTextureCoordinates.positions[i].x;
  127. textureCoordinates[stIndex++] =
  128. hardcodedTextureCoordinates.positions[i].y;
  129. } else {
  130. const p = Matrix2.Matrix3.multiplyByVector(
  131. textureMatrix,
  132. position,
  133. scratchPosition
  134. );
  135. const st = projectPointTo2D(p, stScratch);
  136. Matrix2.Cartesian2.subtract(st, stOrigin, st);
  137. const stx = ComponentDatatype.CesiumMath.clamp(st.x / boundingRectangle.width, 0, 1);
  138. const sty = ComponentDatatype.CesiumMath.clamp(st.y / boundingRectangle.height, 0, 1);
  139. textureCoordinates[stIndex++] = stx;
  140. textureCoordinates[stIndex++] = sty;
  141. }
  142. }
  143. if (vertexFormat.normal) {
  144. normals[normalIndex++] = normal.x;
  145. normals[normalIndex++] = normal.y;
  146. normals[normalIndex++] = normal.z;
  147. }
  148. if (vertexFormat.tangent) {
  149. tangents[tangentIndex++] = tangent.x;
  150. tangents[tangentIndex++] = tangent.y;
  151. tangents[tangentIndex++] = tangent.z;
  152. }
  153. if (vertexFormat.bitangent) {
  154. bitangents[bitangentIndex++] = bitangent.x;
  155. bitangents[bitangentIndex++] = bitangent.y;
  156. bitangents[bitangentIndex++] = bitangent.z;
  157. }
  158. }
  159. const attributes = new GeometryAttributes.GeometryAttributes();
  160. if (vertexFormat.position) {
  161. attributes.position = new GeometryAttribute.GeometryAttribute({
  162. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  163. componentsPerAttribute: 3,
  164. values: flatPositions,
  165. });
  166. }
  167. if (vertexFormat.normal) {
  168. attributes.normal = new GeometryAttribute.GeometryAttribute({
  169. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  170. componentsPerAttribute: 3,
  171. values: normals,
  172. });
  173. }
  174. if (vertexFormat.tangent) {
  175. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  176. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  177. componentsPerAttribute: 3,
  178. values: tangents,
  179. });
  180. }
  181. if (vertexFormat.bitangent) {
  182. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  183. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  184. componentsPerAttribute: 3,
  185. values: bitangents,
  186. });
  187. }
  188. if (vertexFormat.st) {
  189. attributes.st = new GeometryAttribute.GeometryAttribute({
  190. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  191. componentsPerAttribute: 2,
  192. values: textureCoordinates,
  193. });
  194. }
  195. return new GeometryAttribute.Geometry({
  196. attributes: attributes,
  197. indices: newIndices,
  198. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  199. });
  200. }
  201. /**
  202. * A description of a polygon composed of arbitrary coplanar positions.
  203. *
  204. * @alias CoplanarPolygonGeometry
  205. * @constructor
  206. *
  207. * @param {Object} options Object with the following properties:
  208. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  209. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  210. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  211. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  212. * @param {PolygonHierarchy} [options.textureCoordinates] Texture coordinates as a {@link PolygonHierarchy} of {@link Cartesian2} points.
  213. *
  214. * @example
  215. * const polygonGeometry = new Cesium.CoplanarPolygonGeometry({
  216. * polygonHierarchy: new Cesium.PolygonHierarchy(
  217. * Cesium.Cartesian3.fromDegreesArrayHeights([
  218. * -90.0, 30.0, 0.0,
  219. * -90.0, 30.0, 300000.0,
  220. * -80.0, 30.0, 300000.0,
  221. * -80.0, 30.0, 0.0
  222. * ]))
  223. * });
  224. *
  225. */
  226. function CoplanarPolygonGeometry(options) {
  227. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  228. const polygonHierarchy = options.polygonHierarchy;
  229. const textureCoordinates = options.textureCoordinates;
  230. //>>includeStart('debug', pragmas.debug);
  231. RuntimeError.Check.defined("options.polygonHierarchy", polygonHierarchy);
  232. //>>includeEnd('debug');
  233. const vertexFormat = defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  234. this._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat);
  235. this._polygonHierarchy = polygonHierarchy;
  236. this._stRotation = defaultValue.defaultValue(options.stRotation, 0.0);
  237. this._ellipsoid = Matrix2.Ellipsoid.clone(
  238. defaultValue.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84)
  239. );
  240. this._workerName = "createCoplanarPolygonGeometry";
  241. this._textureCoordinates = textureCoordinates;
  242. /**
  243. * The number of elements used to pack the object into an array.
  244. * @type {Number}
  245. */
  246. this.packedLength =
  247. PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(
  248. polygonHierarchy,
  249. Matrix2.Cartesian3
  250. ) +
  251. VertexFormat.VertexFormat.packedLength +
  252. Matrix2.Ellipsoid.packedLength +
  253. (defaultValue.defined(textureCoordinates)
  254. ? PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(
  255. textureCoordinates,
  256. Matrix2.Cartesian2
  257. )
  258. : 1) +
  259. 2;
  260. }
  261. /**
  262. * A description of a coplanar polygon from an array of positions.
  263. *
  264. * @param {Object} options Object with the following properties:
  265. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  266. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  267. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  268. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  269. * @param {PolygonHierarchy} [options.textureCoordinates] Texture coordinates as a {@link PolygonHierarchy} of {@link Cartesian2} points.
  270. * @returns {CoplanarPolygonGeometry}
  271. *
  272. * @example
  273. * // create a polygon from points
  274. * const polygon = Cesium.CoplanarPolygonGeometry.fromPositions({
  275. * positions : Cesium.Cartesian3.fromDegreesArray([
  276. * -72.0, 40.0,
  277. * -70.0, 35.0,
  278. * -75.0, 30.0,
  279. * -70.0, 30.0,
  280. * -68.0, 40.0
  281. * ])
  282. * });
  283. * const geometry = Cesium.PolygonGeometry.createGeometry(polygon);
  284. *
  285. * @see PolygonGeometry#createGeometry
  286. */
  287. CoplanarPolygonGeometry.fromPositions = function (options) {
  288. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  289. //>>includeStart('debug', pragmas.debug);
  290. RuntimeError.Check.defined("options.positions", options.positions);
  291. //>>includeEnd('debug');
  292. const newOptions = {
  293. polygonHierarchy: {
  294. positions: options.positions,
  295. },
  296. vertexFormat: options.vertexFormat,
  297. stRotation: options.stRotation,
  298. ellipsoid: options.ellipsoid,
  299. textureCoordinates: options.textureCoordinates,
  300. };
  301. return new CoplanarPolygonGeometry(newOptions);
  302. };
  303. /**
  304. * Stores the provided instance into the provided array.
  305. *
  306. * @param {CoplanarPolygonGeometry} value The value to pack.
  307. * @param {Number[]} array The array to pack into.
  308. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  309. *
  310. * @returns {Number[]} The array that was packed into
  311. */
  312. CoplanarPolygonGeometry.pack = function (value, array, startingIndex) {
  313. //>>includeStart('debug', pragmas.debug);
  314. RuntimeError.Check.typeOf.object("value", value);
  315. RuntimeError.Check.defined("array", array);
  316. //>>includeEnd('debug');
  317. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  318. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
  319. value._polygonHierarchy,
  320. array,
  321. startingIndex,
  322. Matrix2.Cartesian3
  323. );
  324. Matrix2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  325. startingIndex += Matrix2.Ellipsoid.packedLength;
  326. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  327. startingIndex += VertexFormat.VertexFormat.packedLength;
  328. array[startingIndex++] = value._stRotation;
  329. if (defaultValue.defined(value._textureCoordinates)) {
  330. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
  331. value._textureCoordinates,
  332. array,
  333. startingIndex,
  334. Matrix2.Cartesian2
  335. );
  336. } else {
  337. array[startingIndex++] = -1.0;
  338. }
  339. array[startingIndex++] = value.packedLength;
  340. return array;
  341. };
  342. const scratchEllipsoid = Matrix2.Ellipsoid.clone(Matrix2.Ellipsoid.UNIT_SPHERE);
  343. const scratchVertexFormat = new VertexFormat.VertexFormat();
  344. const scratchOptions = {
  345. polygonHierarchy: {},
  346. };
  347. /**
  348. * Retrieves an instance from a packed array.
  349. *
  350. * @param {Number[]} array The packed array.
  351. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  352. * @param {CoplanarPolygonGeometry} [result] The object into which to store the result.
  353. * @returns {CoplanarPolygonGeometry} The modified result parameter or a new CoplanarPolygonGeometry instance if one was not provided.
  354. */
  355. CoplanarPolygonGeometry.unpack = function (array, startingIndex, result) {
  356. //>>includeStart('debug', pragmas.debug);
  357. RuntimeError.Check.defined("array", array);
  358. //>>includeEnd('debug');
  359. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  360. const polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
  361. array,
  362. startingIndex,
  363. Matrix2.Cartesian3
  364. );
  365. startingIndex = polygonHierarchy.startingIndex;
  366. delete polygonHierarchy.startingIndex;
  367. const ellipsoid = Matrix2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  368. startingIndex += Matrix2.Ellipsoid.packedLength;
  369. const vertexFormat = VertexFormat.VertexFormat.unpack(
  370. array,
  371. startingIndex,
  372. scratchVertexFormat
  373. );
  374. startingIndex += VertexFormat.VertexFormat.packedLength;
  375. const stRotation = array[startingIndex++];
  376. const textureCoordinates =
  377. array[startingIndex] === -1.0
  378. ? undefined
  379. : PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
  380. array,
  381. startingIndex,
  382. Matrix2.Cartesian2
  383. );
  384. if (defaultValue.defined(textureCoordinates)) {
  385. startingIndex = textureCoordinates.startingIndex;
  386. delete textureCoordinates.startingIndex;
  387. } else {
  388. startingIndex++;
  389. }
  390. const packedLength = array[startingIndex++];
  391. if (!defaultValue.defined(result)) {
  392. result = new CoplanarPolygonGeometry(scratchOptions);
  393. }
  394. result._polygonHierarchy = polygonHierarchy;
  395. result._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  396. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  397. result._stRotation = stRotation;
  398. result._textureCoordinates = textureCoordinates;
  399. result.packedLength = packedLength;
  400. return result;
  401. };
  402. /**
  403. * Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
  404. *
  405. * @param {CoplanarPolygonGeometry} polygonGeometry A description of the polygon.
  406. * @returns {Geometry|undefined} The computed vertices and indices.
  407. */
  408. CoplanarPolygonGeometry.createGeometry = function (polygonGeometry) {
  409. const vertexFormat = polygonGeometry._vertexFormat;
  410. const polygonHierarchy = polygonGeometry._polygonHierarchy;
  411. const stRotation = polygonGeometry._stRotation;
  412. const textureCoordinates = polygonGeometry._textureCoordinates;
  413. const hasTextureCoordinates = defaultValue.defined(textureCoordinates);
  414. let outerPositions = polygonHierarchy.positions;
  415. outerPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  416. outerPositions,
  417. Matrix2.Cartesian3.equalsEpsilon,
  418. true
  419. );
  420. if (outerPositions.length < 3) {
  421. return;
  422. }
  423. let normal = scratchNormal;
  424. let tangent = scratchTangent;
  425. let bitangent = scratchBitangent;
  426. let axis1 = axis1Scratch;
  427. const axis2 = axis2Scratch;
  428. const validGeometry = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments(
  429. outerPositions,
  430. centerScratch,
  431. axis1,
  432. axis2
  433. );
  434. if (!validGeometry) {
  435. return undefined;
  436. }
  437. normal = Matrix2.Cartesian3.cross(axis1, axis2, normal);
  438. normal = Matrix2.Cartesian3.normalize(normal, normal);
  439. if (
  440. !Matrix2.Cartesian3.equalsEpsilon(
  441. centerScratch,
  442. Matrix2.Cartesian3.ZERO,
  443. ComponentDatatype.CesiumMath.EPSILON6
  444. )
  445. ) {
  446. const surfaceNormal = polygonGeometry._ellipsoid.geodeticSurfaceNormal(
  447. centerScratch,
  448. surfaceNormalScratch
  449. );
  450. if (Matrix2.Cartesian3.dot(normal, surfaceNormal) < 0) {
  451. normal = Matrix2.Cartesian3.negate(normal, normal);
  452. axis1 = Matrix2.Cartesian3.negate(axis1, axis1);
  453. }
  454. }
  455. const projectPoints = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction(
  456. centerScratch,
  457. axis1,
  458. axis2
  459. );
  460. const projectPoint = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction(
  461. centerScratch,
  462. axis1,
  463. axis2
  464. );
  465. if (vertexFormat.tangent) {
  466. tangent = Matrix2.Cartesian3.clone(axis1, tangent);
  467. }
  468. if (vertexFormat.bitangent) {
  469. bitangent = Matrix2.Cartesian3.clone(axis2, bitangent);
  470. }
  471. const results = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonsFromHierarchy(
  472. polygonHierarchy,
  473. hasTextureCoordinates,
  474. projectPoints,
  475. false
  476. );
  477. const hierarchy = results.hierarchy;
  478. const polygons = results.polygons;
  479. const dummyFunction = function (identity) {
  480. return identity;
  481. };
  482. const textureCoordinatePolygons = hasTextureCoordinates
  483. ? PolygonGeometryLibrary.PolygonGeometryLibrary.polygonsFromHierarchy(
  484. textureCoordinates,
  485. true,
  486. dummyFunction,
  487. false
  488. ).polygons
  489. : undefined;
  490. if (hierarchy.length === 0) {
  491. return;
  492. }
  493. outerPositions = hierarchy[0].outerRing;
  494. const boundingSphere = Transforms.BoundingSphere.fromPoints(outerPositions);
  495. const boundingRectangle = PolygonGeometryLibrary.PolygonGeometryLibrary.computeBoundingRectangle(
  496. normal,
  497. projectPoint,
  498. outerPositions,
  499. stRotation,
  500. scratchBR
  501. );
  502. const geometries = [];
  503. for (let i = 0; i < polygons.length; i++) {
  504. const geometryInstance = new GeometryInstance.GeometryInstance({
  505. geometry: createGeometryFromPolygon(
  506. polygons[i],
  507. vertexFormat,
  508. boundingRectangle,
  509. stRotation,
  510. hasTextureCoordinates ? textureCoordinatePolygons[i] : undefined,
  511. projectPoint,
  512. normal,
  513. tangent,
  514. bitangent
  515. ),
  516. });
  517. geometries.push(geometryInstance);
  518. }
  519. const geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  520. geometry.attributes.position.values = new Float64Array(
  521. geometry.attributes.position.values
  522. );
  523. geometry.indices = IndexDatatype.IndexDatatype.createTypedArray(
  524. geometry.attributes.position.values.length / 3,
  525. geometry.indices
  526. );
  527. const attributes = geometry.attributes;
  528. if (!vertexFormat.position) {
  529. delete attributes.position;
  530. }
  531. return new GeometryAttribute.Geometry({
  532. attributes: attributes,
  533. indices: geometry.indices,
  534. primitiveType: geometry.primitiveType,
  535. boundingSphere: boundingSphere,
  536. });
  537. };
  538. function createCoplanarPolygonGeometry(polygonGeometry, offset) {
  539. if (defaultValue.defined(offset)) {
  540. polygonGeometry = CoplanarPolygonGeometry.unpack(polygonGeometry, offset);
  541. }
  542. return CoplanarPolygonGeometry.createGeometry(polygonGeometry);
  543. }
  544. return createCoplanarPolygonGeometry;
  545. }));