RectangleGeometry.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. import arrayFill from "./arrayFill.js";
  2. import BoundingSphere from "./BoundingSphere.js";
  3. import Cartesian2 from "./Cartesian2.js";
  4. import Cartesian3 from "./Cartesian3.js";
  5. import Cartographic from "./Cartographic.js";
  6. import Check from "./Check.js";
  7. import ComponentDatatype from "./ComponentDatatype.js";
  8. import defaultValue from "./defaultValue.js";
  9. import defined from "./defined.js";
  10. import DeveloperError from "./DeveloperError.js";
  11. import Ellipsoid from "./Ellipsoid.js";
  12. import Geometry from "./Geometry.js";
  13. import GeometryAttribute from "./GeometryAttribute.js";
  14. import GeometryAttributes from "./GeometryAttributes.js";
  15. import GeometryInstance from "./GeometryInstance.js";
  16. import GeometryOffsetAttribute from "./GeometryOffsetAttribute.js";
  17. import GeometryPipeline from "./GeometryPipeline.js";
  18. import IndexDatatype from "./IndexDatatype.js";
  19. import CesiumMath from "./Math.js";
  20. import Matrix2 from "./Matrix2.js";
  21. import Matrix3 from "./Matrix3.js";
  22. import PolygonPipeline from "./PolygonPipeline.js";
  23. import PrimitiveType from "./PrimitiveType.js";
  24. import Quaternion from "./Quaternion.js";
  25. import Rectangle from "./Rectangle.js";
  26. import RectangleGeometryLibrary from "./RectangleGeometryLibrary.js";
  27. import VertexFormat from "./VertexFormat.js";
  28. const positionScratch = new Cartesian3();
  29. const normalScratch = new Cartesian3();
  30. const tangentScratch = new Cartesian3();
  31. const bitangentScratch = new Cartesian3();
  32. const rectangleScratch = new Rectangle();
  33. const stScratch = new Cartesian2();
  34. const bottomBoundingSphere = new BoundingSphere();
  35. const topBoundingSphere = new BoundingSphere();
  36. function createAttributes(vertexFormat, attributes) {
  37. const geo = new Geometry({
  38. attributes: new GeometryAttributes(),
  39. primitiveType: PrimitiveType.TRIANGLES,
  40. });
  41. geo.attributes.position = new GeometryAttribute({
  42. componentDatatype: ComponentDatatype.DOUBLE,
  43. componentsPerAttribute: 3,
  44. values: attributes.positions,
  45. });
  46. if (vertexFormat.normal) {
  47. geo.attributes.normal = new GeometryAttribute({
  48. componentDatatype: ComponentDatatype.FLOAT,
  49. componentsPerAttribute: 3,
  50. values: attributes.normals,
  51. });
  52. }
  53. if (vertexFormat.tangent) {
  54. geo.attributes.tangent = new GeometryAttribute({
  55. componentDatatype: ComponentDatatype.FLOAT,
  56. componentsPerAttribute: 3,
  57. values: attributes.tangents,
  58. });
  59. }
  60. if (vertexFormat.bitangent) {
  61. geo.attributes.bitangent = new GeometryAttribute({
  62. componentDatatype: ComponentDatatype.FLOAT,
  63. componentsPerAttribute: 3,
  64. values: attributes.bitangents,
  65. });
  66. }
  67. return geo;
  68. }
  69. function calculateAttributes(
  70. positions,
  71. vertexFormat,
  72. ellipsoid,
  73. tangentRotationMatrix
  74. ) {
  75. const length = positions.length;
  76. const normals = vertexFormat.normal ? new Float32Array(length) : undefined;
  77. const tangents = vertexFormat.tangent ? new Float32Array(length) : undefined;
  78. const bitangents = vertexFormat.bitangent
  79. ? new Float32Array(length)
  80. : undefined;
  81. let attrIndex = 0;
  82. const bitangent = bitangentScratch;
  83. const tangent = tangentScratch;
  84. let normal = normalScratch;
  85. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
  86. for (let i = 0; i < length; i += 3) {
  87. const p = Cartesian3.fromArray(positions, i, positionScratch);
  88. const attrIndex1 = attrIndex + 1;
  89. const attrIndex2 = attrIndex + 2;
  90. normal = ellipsoid.geodeticSurfaceNormal(p, normal);
  91. if (vertexFormat.tangent || vertexFormat.bitangent) {
  92. Cartesian3.cross(Cartesian3.UNIT_Z, normal, tangent);
  93. Matrix3.multiplyByVector(tangentRotationMatrix, tangent, tangent);
  94. Cartesian3.normalize(tangent, tangent);
  95. if (vertexFormat.bitangent) {
  96. Cartesian3.normalize(
  97. Cartesian3.cross(normal, tangent, bitangent),
  98. bitangent
  99. );
  100. }
  101. }
  102. if (vertexFormat.normal) {
  103. normals[attrIndex] = normal.x;
  104. normals[attrIndex1] = normal.y;
  105. normals[attrIndex2] = normal.z;
  106. }
  107. if (vertexFormat.tangent) {
  108. tangents[attrIndex] = tangent.x;
  109. tangents[attrIndex1] = tangent.y;
  110. tangents[attrIndex2] = tangent.z;
  111. }
  112. if (vertexFormat.bitangent) {
  113. bitangents[attrIndex] = bitangent.x;
  114. bitangents[attrIndex1] = bitangent.y;
  115. bitangents[attrIndex2] = bitangent.z;
  116. }
  117. attrIndex += 3;
  118. }
  119. }
  120. return createAttributes(vertexFormat, {
  121. positions: positions,
  122. normals: normals,
  123. tangents: tangents,
  124. bitangents: bitangents,
  125. });
  126. }
  127. const v1Scratch = new Cartesian3();
  128. const v2Scratch = new Cartesian3();
  129. function calculateAttributesWall(positions, vertexFormat, ellipsoid) {
  130. const length = positions.length;
  131. const normals = vertexFormat.normal ? new Float32Array(length) : undefined;
  132. const tangents = vertexFormat.tangent ? new Float32Array(length) : undefined;
  133. const bitangents = vertexFormat.bitangent
  134. ? new Float32Array(length)
  135. : undefined;
  136. let normalIndex = 0;
  137. let tangentIndex = 0;
  138. let bitangentIndex = 0;
  139. let recomputeNormal = true;
  140. let bitangent = bitangentScratch;
  141. let tangent = tangentScratch;
  142. let normal = normalScratch;
  143. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
  144. for (let i = 0; i < length; i += 6) {
  145. const p = Cartesian3.fromArray(positions, i, positionScratch);
  146. const p1 = Cartesian3.fromArray(positions, (i + 6) % length, v1Scratch);
  147. if (recomputeNormal) {
  148. const p2 = Cartesian3.fromArray(positions, (i + 3) % length, v2Scratch);
  149. Cartesian3.subtract(p1, p, p1);
  150. Cartesian3.subtract(p2, p, p2);
  151. normal = Cartesian3.normalize(Cartesian3.cross(p2, p1, normal), normal);
  152. recomputeNormal = false;
  153. }
  154. if (Cartesian3.equalsEpsilon(p1, p, CesiumMath.EPSILON10)) {
  155. // if we've reached a corner
  156. recomputeNormal = true;
  157. }
  158. if (vertexFormat.tangent || vertexFormat.bitangent) {
  159. bitangent = ellipsoid.geodeticSurfaceNormal(p, bitangent);
  160. if (vertexFormat.tangent) {
  161. tangent = Cartesian3.normalize(
  162. Cartesian3.cross(bitangent, normal, tangent),
  163. tangent
  164. );
  165. }
  166. }
  167. if (vertexFormat.normal) {
  168. normals[normalIndex++] = normal.x;
  169. normals[normalIndex++] = normal.y;
  170. normals[normalIndex++] = normal.z;
  171. normals[normalIndex++] = normal.x;
  172. normals[normalIndex++] = normal.y;
  173. normals[normalIndex++] = normal.z;
  174. }
  175. if (vertexFormat.tangent) {
  176. tangents[tangentIndex++] = tangent.x;
  177. tangents[tangentIndex++] = tangent.y;
  178. tangents[tangentIndex++] = tangent.z;
  179. tangents[tangentIndex++] = tangent.x;
  180. tangents[tangentIndex++] = tangent.y;
  181. tangents[tangentIndex++] = tangent.z;
  182. }
  183. if (vertexFormat.bitangent) {
  184. bitangents[bitangentIndex++] = bitangent.x;
  185. bitangents[bitangentIndex++] = bitangent.y;
  186. bitangents[bitangentIndex++] = bitangent.z;
  187. bitangents[bitangentIndex++] = bitangent.x;
  188. bitangents[bitangentIndex++] = bitangent.y;
  189. bitangents[bitangentIndex++] = bitangent.z;
  190. }
  191. }
  192. }
  193. return createAttributes(vertexFormat, {
  194. positions: positions,
  195. normals: normals,
  196. tangents: tangents,
  197. bitangents: bitangents,
  198. });
  199. }
  200. function constructRectangle(rectangleGeometry, computedOptions) {
  201. const vertexFormat = rectangleGeometry._vertexFormat;
  202. const ellipsoid = rectangleGeometry._ellipsoid;
  203. const height = computedOptions.height;
  204. const width = computedOptions.width;
  205. const northCap = computedOptions.northCap;
  206. const southCap = computedOptions.southCap;
  207. let rowStart = 0;
  208. let rowEnd = height;
  209. let rowHeight = height;
  210. let size = 0;
  211. if (northCap) {
  212. rowStart = 1;
  213. rowHeight -= 1;
  214. size += 1;
  215. }
  216. if (southCap) {
  217. rowEnd -= 1;
  218. rowHeight -= 1;
  219. size += 1;
  220. }
  221. size += width * rowHeight;
  222. const positions = vertexFormat.position
  223. ? new Float64Array(size * 3)
  224. : undefined;
  225. const textureCoordinates = vertexFormat.st
  226. ? new Float32Array(size * 2)
  227. : undefined;
  228. let posIndex = 0;
  229. let stIndex = 0;
  230. const position = positionScratch;
  231. const st = stScratch;
  232. let minX = Number.MAX_VALUE;
  233. let minY = Number.MAX_VALUE;
  234. let maxX = -Number.MAX_VALUE;
  235. let maxY = -Number.MAX_VALUE;
  236. for (let row = rowStart; row < rowEnd; ++row) {
  237. for (let col = 0; col < width; ++col) {
  238. RectangleGeometryLibrary.computePosition(
  239. computedOptions,
  240. ellipsoid,
  241. vertexFormat.st,
  242. row,
  243. col,
  244. position,
  245. st
  246. );
  247. positions[posIndex++] = position.x;
  248. positions[posIndex++] = position.y;
  249. positions[posIndex++] = position.z;
  250. if (vertexFormat.st) {
  251. textureCoordinates[stIndex++] = st.x;
  252. textureCoordinates[stIndex++] = st.y;
  253. minX = Math.min(minX, st.x);
  254. minY = Math.min(minY, st.y);
  255. maxX = Math.max(maxX, st.x);
  256. maxY = Math.max(maxY, st.y);
  257. }
  258. }
  259. }
  260. if (northCap) {
  261. RectangleGeometryLibrary.computePosition(
  262. computedOptions,
  263. ellipsoid,
  264. vertexFormat.st,
  265. 0,
  266. 0,
  267. position,
  268. st
  269. );
  270. positions[posIndex++] = position.x;
  271. positions[posIndex++] = position.y;
  272. positions[posIndex++] = position.z;
  273. if (vertexFormat.st) {
  274. textureCoordinates[stIndex++] = st.x;
  275. textureCoordinates[stIndex++] = st.y;
  276. minX = st.x;
  277. minY = st.y;
  278. maxX = st.x;
  279. maxY = st.y;
  280. }
  281. }
  282. if (southCap) {
  283. RectangleGeometryLibrary.computePosition(
  284. computedOptions,
  285. ellipsoid,
  286. vertexFormat.st,
  287. height - 1,
  288. 0,
  289. position,
  290. st
  291. );
  292. positions[posIndex++] = position.x;
  293. positions[posIndex++] = position.y;
  294. positions[posIndex] = position.z;
  295. if (vertexFormat.st) {
  296. textureCoordinates[stIndex++] = st.x;
  297. textureCoordinates[stIndex] = st.y;
  298. minX = Math.min(minX, st.x);
  299. minY = Math.min(minY, st.y);
  300. maxX = Math.max(maxX, st.x);
  301. maxY = Math.max(maxY, st.y);
  302. }
  303. }
  304. if (
  305. vertexFormat.st &&
  306. (minX < 0.0 || minY < 0.0 || maxX > 1.0 || maxY > 1.0)
  307. ) {
  308. for (let k = 0; k < textureCoordinates.length; k += 2) {
  309. textureCoordinates[k] = (textureCoordinates[k] - minX) / (maxX - minX);
  310. textureCoordinates[k + 1] =
  311. (textureCoordinates[k + 1] - minY) / (maxY - minY);
  312. }
  313. }
  314. const geo = calculateAttributes(
  315. positions,
  316. vertexFormat,
  317. ellipsoid,
  318. computedOptions.tangentRotationMatrix
  319. );
  320. let indicesSize = 6 * (width - 1) * (rowHeight - 1);
  321. if (northCap) {
  322. indicesSize += 3 * (width - 1);
  323. }
  324. if (southCap) {
  325. indicesSize += 3 * (width - 1);
  326. }
  327. const indices = IndexDatatype.createTypedArray(size, indicesSize);
  328. let index = 0;
  329. let indicesIndex = 0;
  330. let i;
  331. for (i = 0; i < rowHeight - 1; ++i) {
  332. for (let j = 0; j < width - 1; ++j) {
  333. const upperLeft = index;
  334. const lowerLeft = upperLeft + width;
  335. const lowerRight = lowerLeft + 1;
  336. const upperRight = upperLeft + 1;
  337. indices[indicesIndex++] = upperLeft;
  338. indices[indicesIndex++] = lowerLeft;
  339. indices[indicesIndex++] = upperRight;
  340. indices[indicesIndex++] = upperRight;
  341. indices[indicesIndex++] = lowerLeft;
  342. indices[indicesIndex++] = lowerRight;
  343. ++index;
  344. }
  345. ++index;
  346. }
  347. if (northCap || southCap) {
  348. let northIndex = size - 1;
  349. const southIndex = size - 1;
  350. if (northCap && southCap) {
  351. northIndex = size - 2;
  352. }
  353. let p1;
  354. let p2;
  355. index = 0;
  356. if (northCap) {
  357. for (i = 0; i < width - 1; i++) {
  358. p1 = index;
  359. p2 = p1 + 1;
  360. indices[indicesIndex++] = northIndex;
  361. indices[indicesIndex++] = p1;
  362. indices[indicesIndex++] = p2;
  363. ++index;
  364. }
  365. }
  366. if (southCap) {
  367. index = (rowHeight - 1) * width;
  368. for (i = 0; i < width - 1; i++) {
  369. p1 = index;
  370. p2 = p1 + 1;
  371. indices[indicesIndex++] = p1;
  372. indices[indicesIndex++] = southIndex;
  373. indices[indicesIndex++] = p2;
  374. ++index;
  375. }
  376. }
  377. }
  378. geo.indices = indices;
  379. if (vertexFormat.st) {
  380. geo.attributes.st = new GeometryAttribute({
  381. componentDatatype: ComponentDatatype.FLOAT,
  382. componentsPerAttribute: 2,
  383. values: textureCoordinates,
  384. });
  385. }
  386. return geo;
  387. }
  388. function addWallPositions(
  389. wallPositions,
  390. posIndex,
  391. i,
  392. topPositions,
  393. bottomPositions
  394. ) {
  395. wallPositions[posIndex++] = topPositions[i];
  396. wallPositions[posIndex++] = topPositions[i + 1];
  397. wallPositions[posIndex++] = topPositions[i + 2];
  398. wallPositions[posIndex++] = bottomPositions[i];
  399. wallPositions[posIndex++] = bottomPositions[i + 1];
  400. wallPositions[posIndex] = bottomPositions[i + 2];
  401. return wallPositions;
  402. }
  403. function addWallTextureCoordinates(wallTextures, stIndex, i, st) {
  404. wallTextures[stIndex++] = st[i];
  405. wallTextures[stIndex++] = st[i + 1];
  406. wallTextures[stIndex++] = st[i];
  407. wallTextures[stIndex] = st[i + 1];
  408. return wallTextures;
  409. }
  410. const scratchVertexFormat = new VertexFormat();
  411. function constructExtrudedRectangle(rectangleGeometry, computedOptions) {
  412. const shadowVolume = rectangleGeometry._shadowVolume;
  413. const offsetAttributeValue = rectangleGeometry._offsetAttribute;
  414. const vertexFormat = rectangleGeometry._vertexFormat;
  415. const minHeight = rectangleGeometry._extrudedHeight;
  416. const maxHeight = rectangleGeometry._surfaceHeight;
  417. const ellipsoid = rectangleGeometry._ellipsoid;
  418. const height = computedOptions.height;
  419. const width = computedOptions.width;
  420. let i;
  421. if (shadowVolume) {
  422. const newVertexFormat = VertexFormat.clone(
  423. vertexFormat,
  424. scratchVertexFormat
  425. );
  426. newVertexFormat.normal = true;
  427. rectangleGeometry._vertexFormat = newVertexFormat;
  428. }
  429. const topBottomGeo = constructRectangle(rectangleGeometry, computedOptions);
  430. if (shadowVolume) {
  431. rectangleGeometry._vertexFormat = vertexFormat;
  432. }
  433. let topPositions = PolygonPipeline.scaleToGeodeticHeight(
  434. topBottomGeo.attributes.position.values,
  435. maxHeight,
  436. ellipsoid,
  437. false
  438. );
  439. topPositions = new Float64Array(topPositions);
  440. let length = topPositions.length;
  441. const newLength = length * 2;
  442. const positions = new Float64Array(newLength);
  443. positions.set(topPositions);
  444. const bottomPositions = PolygonPipeline.scaleToGeodeticHeight(
  445. topBottomGeo.attributes.position.values,
  446. minHeight,
  447. ellipsoid
  448. );
  449. positions.set(bottomPositions, length);
  450. topBottomGeo.attributes.position.values = positions;
  451. const normals = vertexFormat.normal ? new Float32Array(newLength) : undefined;
  452. const tangents = vertexFormat.tangent
  453. ? new Float32Array(newLength)
  454. : undefined;
  455. const bitangents = vertexFormat.bitangent
  456. ? new Float32Array(newLength)
  457. : undefined;
  458. const textures = vertexFormat.st
  459. ? new Float32Array((newLength / 3) * 2)
  460. : undefined;
  461. let topSt;
  462. let topNormals;
  463. if (vertexFormat.normal) {
  464. topNormals = topBottomGeo.attributes.normal.values;
  465. normals.set(topNormals);
  466. for (i = 0; i < length; i++) {
  467. topNormals[i] = -topNormals[i];
  468. }
  469. normals.set(topNormals, length);
  470. topBottomGeo.attributes.normal.values = normals;
  471. }
  472. if (shadowVolume) {
  473. topNormals = topBottomGeo.attributes.normal.values;
  474. if (!vertexFormat.normal) {
  475. topBottomGeo.attributes.normal = undefined;
  476. }
  477. const extrudeNormals = new Float32Array(newLength);
  478. for (i = 0; i < length; i++) {
  479. topNormals[i] = -topNormals[i];
  480. }
  481. extrudeNormals.set(topNormals, length); //only get normals for bottom layer that's going to be pushed down
  482. topBottomGeo.attributes.extrudeDirection = new GeometryAttribute({
  483. componentDatatype: ComponentDatatype.FLOAT,
  484. componentsPerAttribute: 3,
  485. values: extrudeNormals,
  486. });
  487. }
  488. let offsetValue;
  489. const hasOffsets = defined(offsetAttributeValue);
  490. if (hasOffsets) {
  491. const size = (length / 3) * 2;
  492. let offsetAttribute = new Uint8Array(size);
  493. if (offsetAttributeValue === GeometryOffsetAttribute.TOP) {
  494. offsetAttribute = arrayFill(offsetAttribute, 1, 0, size / 2);
  495. } else {
  496. offsetValue =
  497. offsetAttributeValue === GeometryOffsetAttribute.NONE ? 0 : 1;
  498. offsetAttribute = arrayFill(offsetAttribute, offsetValue);
  499. }
  500. topBottomGeo.attributes.applyOffset = new GeometryAttribute({
  501. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  502. componentsPerAttribute: 1,
  503. values: offsetAttribute,
  504. });
  505. }
  506. if (vertexFormat.tangent) {
  507. const topTangents = topBottomGeo.attributes.tangent.values;
  508. tangents.set(topTangents);
  509. for (i = 0; i < length; i++) {
  510. topTangents[i] = -topTangents[i];
  511. }
  512. tangents.set(topTangents, length);
  513. topBottomGeo.attributes.tangent.values = tangents;
  514. }
  515. if (vertexFormat.bitangent) {
  516. const topBitangents = topBottomGeo.attributes.bitangent.values;
  517. bitangents.set(topBitangents);
  518. bitangents.set(topBitangents, length);
  519. topBottomGeo.attributes.bitangent.values = bitangents;
  520. }
  521. if (vertexFormat.st) {
  522. topSt = topBottomGeo.attributes.st.values;
  523. textures.set(topSt);
  524. textures.set(topSt, (length / 3) * 2);
  525. topBottomGeo.attributes.st.values = textures;
  526. }
  527. const indices = topBottomGeo.indices;
  528. const indicesLength = indices.length;
  529. const posLength = length / 3;
  530. const newIndices = IndexDatatype.createTypedArray(
  531. newLength / 3,
  532. indicesLength * 2
  533. );
  534. newIndices.set(indices);
  535. for (i = 0; i < indicesLength; i += 3) {
  536. newIndices[i + indicesLength] = indices[i + 2] + posLength;
  537. newIndices[i + 1 + indicesLength] = indices[i + 1] + posLength;
  538. newIndices[i + 2 + indicesLength] = indices[i] + posLength;
  539. }
  540. topBottomGeo.indices = newIndices;
  541. const northCap = computedOptions.northCap;
  542. const southCap = computedOptions.southCap;
  543. let rowHeight = height;
  544. let widthMultiplier = 2;
  545. let perimeterPositions = 0;
  546. let corners = 4;
  547. let dupliateCorners = 4;
  548. if (northCap) {
  549. widthMultiplier -= 1;
  550. rowHeight -= 1;
  551. perimeterPositions += 1;
  552. corners -= 2;
  553. dupliateCorners -= 1;
  554. }
  555. if (southCap) {
  556. widthMultiplier -= 1;
  557. rowHeight -= 1;
  558. perimeterPositions += 1;
  559. corners -= 2;
  560. dupliateCorners -= 1;
  561. }
  562. perimeterPositions += widthMultiplier * width + 2 * rowHeight - corners;
  563. const wallCount = (perimeterPositions + dupliateCorners) * 2;
  564. let wallPositions = new Float64Array(wallCount * 3);
  565. const wallExtrudeNormals = shadowVolume
  566. ? new Float32Array(wallCount * 3)
  567. : undefined;
  568. let wallOffsetAttribute = hasOffsets ? new Uint8Array(wallCount) : undefined;
  569. let wallTextures = vertexFormat.st
  570. ? new Float32Array(wallCount * 2)
  571. : undefined;
  572. const computeTopOffsets =
  573. offsetAttributeValue === GeometryOffsetAttribute.TOP;
  574. if (hasOffsets && !computeTopOffsets) {
  575. offsetValue = offsetAttributeValue === GeometryOffsetAttribute.ALL ? 1 : 0;
  576. wallOffsetAttribute = arrayFill(wallOffsetAttribute, offsetValue);
  577. }
  578. let posIndex = 0;
  579. let stIndex = 0;
  580. let extrudeNormalIndex = 0;
  581. let wallOffsetIndex = 0;
  582. const area = width * rowHeight;
  583. let threeI;
  584. for (i = 0; i < area; i += width) {
  585. threeI = i * 3;
  586. wallPositions = addWallPositions(
  587. wallPositions,
  588. posIndex,
  589. threeI,
  590. topPositions,
  591. bottomPositions
  592. );
  593. posIndex += 6;
  594. if (vertexFormat.st) {
  595. wallTextures = addWallTextureCoordinates(
  596. wallTextures,
  597. stIndex,
  598. i * 2,
  599. topSt
  600. );
  601. stIndex += 4;
  602. }
  603. if (shadowVolume) {
  604. extrudeNormalIndex += 3;
  605. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  606. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  607. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  608. }
  609. if (computeTopOffsets) {
  610. wallOffsetAttribute[wallOffsetIndex++] = 1;
  611. wallOffsetIndex += 1;
  612. }
  613. }
  614. if (!southCap) {
  615. for (i = area - width; i < area; i++) {
  616. threeI = i * 3;
  617. wallPositions = addWallPositions(
  618. wallPositions,
  619. posIndex,
  620. threeI,
  621. topPositions,
  622. bottomPositions
  623. );
  624. posIndex += 6;
  625. if (vertexFormat.st) {
  626. wallTextures = addWallTextureCoordinates(
  627. wallTextures,
  628. stIndex,
  629. i * 2,
  630. topSt
  631. );
  632. stIndex += 4;
  633. }
  634. if (shadowVolume) {
  635. extrudeNormalIndex += 3;
  636. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  637. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  638. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  639. }
  640. if (computeTopOffsets) {
  641. wallOffsetAttribute[wallOffsetIndex++] = 1;
  642. wallOffsetIndex += 1;
  643. }
  644. }
  645. } else {
  646. const southIndex = northCap ? area + 1 : area;
  647. threeI = southIndex * 3;
  648. for (i = 0; i < 2; i++) {
  649. // duplicate corner points
  650. wallPositions = addWallPositions(
  651. wallPositions,
  652. posIndex,
  653. threeI,
  654. topPositions,
  655. bottomPositions
  656. );
  657. posIndex += 6;
  658. if (vertexFormat.st) {
  659. wallTextures = addWallTextureCoordinates(
  660. wallTextures,
  661. stIndex,
  662. southIndex * 2,
  663. topSt
  664. );
  665. stIndex += 4;
  666. }
  667. if (shadowVolume) {
  668. extrudeNormalIndex += 3;
  669. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  670. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  671. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  672. }
  673. if (computeTopOffsets) {
  674. wallOffsetAttribute[wallOffsetIndex++] = 1;
  675. wallOffsetIndex += 1;
  676. }
  677. }
  678. }
  679. for (i = area - 1; i > 0; i -= width) {
  680. threeI = i * 3;
  681. wallPositions = addWallPositions(
  682. wallPositions,
  683. posIndex,
  684. threeI,
  685. topPositions,
  686. bottomPositions
  687. );
  688. posIndex += 6;
  689. if (vertexFormat.st) {
  690. wallTextures = addWallTextureCoordinates(
  691. wallTextures,
  692. stIndex,
  693. i * 2,
  694. topSt
  695. );
  696. stIndex += 4;
  697. }
  698. if (shadowVolume) {
  699. extrudeNormalIndex += 3;
  700. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  701. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  702. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  703. }
  704. if (computeTopOffsets) {
  705. wallOffsetAttribute[wallOffsetIndex++] = 1;
  706. wallOffsetIndex += 1;
  707. }
  708. }
  709. if (!northCap) {
  710. for (i = width - 1; i >= 0; i--) {
  711. threeI = i * 3;
  712. wallPositions = addWallPositions(
  713. wallPositions,
  714. posIndex,
  715. threeI,
  716. topPositions,
  717. bottomPositions
  718. );
  719. posIndex += 6;
  720. if (vertexFormat.st) {
  721. wallTextures = addWallTextureCoordinates(
  722. wallTextures,
  723. stIndex,
  724. i * 2,
  725. topSt
  726. );
  727. stIndex += 4;
  728. }
  729. if (shadowVolume) {
  730. extrudeNormalIndex += 3;
  731. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  732. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  733. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  734. }
  735. if (computeTopOffsets) {
  736. wallOffsetAttribute[wallOffsetIndex++] = 1;
  737. wallOffsetIndex += 1;
  738. }
  739. }
  740. } else {
  741. const northIndex = area;
  742. threeI = northIndex * 3;
  743. for (i = 0; i < 2; i++) {
  744. // duplicate corner points
  745. wallPositions = addWallPositions(
  746. wallPositions,
  747. posIndex,
  748. threeI,
  749. topPositions,
  750. bottomPositions
  751. );
  752. posIndex += 6;
  753. if (vertexFormat.st) {
  754. wallTextures = addWallTextureCoordinates(
  755. wallTextures,
  756. stIndex,
  757. northIndex * 2,
  758. topSt
  759. );
  760. stIndex += 4;
  761. }
  762. if (shadowVolume) {
  763. extrudeNormalIndex += 3;
  764. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  765. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  766. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  767. }
  768. if (computeTopOffsets) {
  769. wallOffsetAttribute[wallOffsetIndex++] = 1;
  770. wallOffsetIndex += 1;
  771. }
  772. }
  773. }
  774. let geo = calculateAttributesWall(wallPositions, vertexFormat, ellipsoid);
  775. if (vertexFormat.st) {
  776. geo.attributes.st = new GeometryAttribute({
  777. componentDatatype: ComponentDatatype.FLOAT,
  778. componentsPerAttribute: 2,
  779. values: wallTextures,
  780. });
  781. }
  782. if (shadowVolume) {
  783. geo.attributes.extrudeDirection = new GeometryAttribute({
  784. componentDatatype: ComponentDatatype.FLOAT,
  785. componentsPerAttribute: 3,
  786. values: wallExtrudeNormals,
  787. });
  788. }
  789. if (hasOffsets) {
  790. geo.attributes.applyOffset = new GeometryAttribute({
  791. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  792. componentsPerAttribute: 1,
  793. values: wallOffsetAttribute,
  794. });
  795. }
  796. const wallIndices = IndexDatatype.createTypedArray(
  797. wallCount,
  798. perimeterPositions * 6
  799. );
  800. let upperLeft;
  801. let lowerLeft;
  802. let lowerRight;
  803. let upperRight;
  804. length = wallPositions.length / 3;
  805. let index = 0;
  806. for (i = 0; i < length - 1; i += 2) {
  807. upperLeft = i;
  808. upperRight = (upperLeft + 2) % length;
  809. const p1 = Cartesian3.fromArray(wallPositions, upperLeft * 3, v1Scratch);
  810. const p2 = Cartesian3.fromArray(wallPositions, upperRight * 3, v2Scratch);
  811. if (Cartesian3.equalsEpsilon(p1, p2, CesiumMath.EPSILON10)) {
  812. continue;
  813. }
  814. lowerLeft = (upperLeft + 1) % length;
  815. lowerRight = (lowerLeft + 2) % length;
  816. wallIndices[index++] = upperLeft;
  817. wallIndices[index++] = lowerLeft;
  818. wallIndices[index++] = upperRight;
  819. wallIndices[index++] = upperRight;
  820. wallIndices[index++] = lowerLeft;
  821. wallIndices[index++] = lowerRight;
  822. }
  823. geo.indices = wallIndices;
  824. geo = GeometryPipeline.combineInstances([
  825. new GeometryInstance({
  826. geometry: topBottomGeo,
  827. }),
  828. new GeometryInstance({
  829. geometry: geo,
  830. }),
  831. ]);
  832. return geo[0];
  833. }
  834. const scratchRectanglePoints = [
  835. new Cartesian3(),
  836. new Cartesian3(),
  837. new Cartesian3(),
  838. new Cartesian3(),
  839. ];
  840. const nwScratch = new Cartographic();
  841. const stNwScratch = new Cartographic();
  842. function computeRectangle(rectangle, granularity, rotation, ellipsoid, result) {
  843. if (rotation === 0.0) {
  844. return Rectangle.clone(rectangle, result);
  845. }
  846. const computedOptions = RectangleGeometryLibrary.computeOptions(
  847. rectangle,
  848. granularity,
  849. rotation,
  850. 0,
  851. rectangleScratch,
  852. nwScratch
  853. );
  854. const height = computedOptions.height;
  855. const width = computedOptions.width;
  856. const positions = scratchRectanglePoints;
  857. RectangleGeometryLibrary.computePosition(
  858. computedOptions,
  859. ellipsoid,
  860. false,
  861. 0,
  862. 0,
  863. positions[0]
  864. );
  865. RectangleGeometryLibrary.computePosition(
  866. computedOptions,
  867. ellipsoid,
  868. false,
  869. 0,
  870. width - 1,
  871. positions[1]
  872. );
  873. RectangleGeometryLibrary.computePosition(
  874. computedOptions,
  875. ellipsoid,
  876. false,
  877. height - 1,
  878. 0,
  879. positions[2]
  880. );
  881. RectangleGeometryLibrary.computePosition(
  882. computedOptions,
  883. ellipsoid,
  884. false,
  885. height - 1,
  886. width - 1,
  887. positions[3]
  888. );
  889. return Rectangle.fromCartesianArray(positions, ellipsoid, result);
  890. }
  891. /**
  892. * A description of a cartographic rectangle on an ellipsoid centered at the origin. Rectangle geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  893. *
  894. * @alias RectangleGeometry
  895. * @constructor
  896. *
  897. * @param {Object} options Object with the following properties:
  898. * @param {Rectangle} options.rectangle A cartographic rectangle with north, south, east and west properties in radians.
  899. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  900. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle lies.
  901. * @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.
  902. * @param {Number} [options.height=0.0] The distance in meters between the rectangle and the ellipsoid surface.
  903. * @param {Number} [options.rotation=0.0] The rotation of the rectangle, in radians. A positive rotation is counter-clockwise.
  904. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  905. * @param {Number} [options.extrudedHeight] The distance in meters between the rectangle's extruded face and the ellipsoid surface.
  906. *
  907. * @exception {DeveloperError} <code>options.rectangle.north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  908. * @exception {DeveloperError} <code>options.rectangle.south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  909. * @exception {DeveloperError} <code>options.rectangle.east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  910. * @exception {DeveloperError} <code>options.rectangle.west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  911. * @exception {DeveloperError} <code>options.rectangle.north</code> must be greater than <code>options.rectangle.south</code>.
  912. *
  913. * @see RectangleGeometry#createGeometry
  914. *
  915. * @demo {@link https://sandcastle.cesium.com/index.html?src=Rectangle.html|Cesium Sandcastle Rectangle Demo}
  916. *
  917. * @example
  918. * // 1. create a rectangle
  919. * const rectangle = new Cesium.RectangleGeometry({
  920. * ellipsoid : Cesium.Ellipsoid.WGS84,
  921. * rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
  922. * height : 10000.0
  923. * });
  924. * const geometry = Cesium.RectangleGeometry.createGeometry(rectangle);
  925. *
  926. * // 2. create an extruded rectangle without a top
  927. * const rectangle = new Cesium.RectangleGeometry({
  928. * ellipsoid : Cesium.Ellipsoid.WGS84,
  929. * rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
  930. * height : 10000.0,
  931. * extrudedHeight: 300000
  932. * });
  933. * const geometry = Cesium.RectangleGeometry.createGeometry(rectangle);
  934. */
  935. function RectangleGeometry(options) {
  936. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  937. const rectangle = options.rectangle;
  938. //>>includeStart('debug', pragmas.debug);
  939. Check.typeOf.object("rectangle", rectangle);
  940. Rectangle.validate(rectangle);
  941. if (rectangle.north < rectangle.south) {
  942. throw new DeveloperError(
  943. "options.rectangle.north must be greater than or equal to options.rectangle.south"
  944. );
  945. }
  946. //>>includeEnd('debug');
  947. const height = defaultValue(options.height, 0.0);
  948. const extrudedHeight = defaultValue(options.extrudedHeight, height);
  949. this._rectangle = Rectangle.clone(rectangle);
  950. this._granularity = defaultValue(
  951. options.granularity,
  952. CesiumMath.RADIANS_PER_DEGREE
  953. );
  954. this._ellipsoid = Ellipsoid.clone(
  955. defaultValue(options.ellipsoid, Ellipsoid.WGS84)
  956. );
  957. this._surfaceHeight = Math.max(height, extrudedHeight);
  958. this._rotation = defaultValue(options.rotation, 0.0);
  959. this._stRotation = defaultValue(options.stRotation, 0.0);
  960. this._vertexFormat = VertexFormat.clone(
  961. defaultValue(options.vertexFormat, VertexFormat.DEFAULT)
  962. );
  963. this._extrudedHeight = Math.min(height, extrudedHeight);
  964. this._shadowVolume = defaultValue(options.shadowVolume, false);
  965. this._workerName = "createRectangleGeometry";
  966. this._offsetAttribute = options.offsetAttribute;
  967. this._rotatedRectangle = undefined;
  968. this._textureCoordinateRotationPoints = undefined;
  969. }
  970. /**
  971. * The number of elements used to pack the object into an array.
  972. * @type {Number}
  973. */
  974. RectangleGeometry.packedLength =
  975. Rectangle.packedLength +
  976. Ellipsoid.packedLength +
  977. VertexFormat.packedLength +
  978. 7;
  979. /**
  980. * Stores the provided instance into the provided array.
  981. *
  982. * @param {RectangleGeometry} value The value to pack.
  983. * @param {Number[]} array The array to pack into.
  984. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  985. *
  986. * @returns {Number[]} The array that was packed into
  987. */
  988. RectangleGeometry.pack = function (value, array, startingIndex) {
  989. //>>includeStart('debug', pragmas.debug);
  990. Check.typeOf.object("value", value);
  991. Check.defined("array", array);
  992. //>>includeEnd('debug');
  993. startingIndex = defaultValue(startingIndex, 0);
  994. Rectangle.pack(value._rectangle, array, startingIndex);
  995. startingIndex += Rectangle.packedLength;
  996. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  997. startingIndex += Ellipsoid.packedLength;
  998. VertexFormat.pack(value._vertexFormat, array, startingIndex);
  999. startingIndex += VertexFormat.packedLength;
  1000. array[startingIndex++] = value._granularity;
  1001. array[startingIndex++] = value._surfaceHeight;
  1002. array[startingIndex++] = value._rotation;
  1003. array[startingIndex++] = value._stRotation;
  1004. array[startingIndex++] = value._extrudedHeight;
  1005. array[startingIndex++] = value._shadowVolume ? 1.0 : 0.0;
  1006. array[startingIndex] = defaultValue(value._offsetAttribute, -1);
  1007. return array;
  1008. };
  1009. const scratchRectangle = new Rectangle();
  1010. const scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  1011. const scratchOptions = {
  1012. rectangle: scratchRectangle,
  1013. ellipsoid: scratchEllipsoid,
  1014. vertexFormat: scratchVertexFormat,
  1015. granularity: undefined,
  1016. height: undefined,
  1017. rotation: undefined,
  1018. stRotation: undefined,
  1019. extrudedHeight: undefined,
  1020. shadowVolume: undefined,
  1021. offsetAttribute: undefined,
  1022. };
  1023. /**
  1024. * Retrieves an instance from a packed array.
  1025. *
  1026. * @param {Number[]} array The packed array.
  1027. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1028. * @param {RectangleGeometry} [result] The object into which to store the result.
  1029. * @returns {RectangleGeometry} The modified result parameter or a new RectangleGeometry instance if one was not provided.
  1030. */
  1031. RectangleGeometry.unpack = function (array, startingIndex, result) {
  1032. //>>includeStart('debug', pragmas.debug);
  1033. Check.defined("array", array);
  1034. //>>includeEnd('debug');
  1035. startingIndex = defaultValue(startingIndex, 0);
  1036. const rectangle = Rectangle.unpack(array, startingIndex, scratchRectangle);
  1037. startingIndex += Rectangle.packedLength;
  1038. const ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  1039. startingIndex += Ellipsoid.packedLength;
  1040. const vertexFormat = VertexFormat.unpack(
  1041. array,
  1042. startingIndex,
  1043. scratchVertexFormat
  1044. );
  1045. startingIndex += VertexFormat.packedLength;
  1046. const granularity = array[startingIndex++];
  1047. const surfaceHeight = array[startingIndex++];
  1048. const rotation = array[startingIndex++];
  1049. const stRotation = array[startingIndex++];
  1050. const extrudedHeight = array[startingIndex++];
  1051. const shadowVolume = array[startingIndex++] === 1.0;
  1052. const offsetAttribute = array[startingIndex];
  1053. if (!defined(result)) {
  1054. scratchOptions.granularity = granularity;
  1055. scratchOptions.height = surfaceHeight;
  1056. scratchOptions.rotation = rotation;
  1057. scratchOptions.stRotation = stRotation;
  1058. scratchOptions.extrudedHeight = extrudedHeight;
  1059. scratchOptions.shadowVolume = shadowVolume;
  1060. scratchOptions.offsetAttribute =
  1061. offsetAttribute === -1 ? undefined : offsetAttribute;
  1062. return new RectangleGeometry(scratchOptions);
  1063. }
  1064. result._rectangle = Rectangle.clone(rectangle, result._rectangle);
  1065. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  1066. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  1067. result._granularity = granularity;
  1068. result._surfaceHeight = surfaceHeight;
  1069. result._rotation = rotation;
  1070. result._stRotation = stRotation;
  1071. result._extrudedHeight = extrudedHeight;
  1072. result._shadowVolume = shadowVolume;
  1073. result._offsetAttribute =
  1074. offsetAttribute === -1 ? undefined : offsetAttribute;
  1075. return result;
  1076. };
  1077. /**
  1078. * Computes the bounding rectangle based on the provided options
  1079. *
  1080. * @param {Object} options Object with the following properties:
  1081. * @param {Rectangle} options.rectangle A cartographic rectangle with north, south, east and west properties in radians.
  1082. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle lies.
  1083. * @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.
  1084. * @param {Number} [options.rotation=0.0] The rotation of the rectangle, in radians. A positive rotation is counter-clockwise.
  1085. * @param {Rectangle} [result] An object in which to store the result.
  1086. *
  1087. * @returns {Rectangle} The result rectangle
  1088. */
  1089. RectangleGeometry.computeRectangle = function (options, result) {
  1090. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  1091. const rectangle = options.rectangle;
  1092. //>>includeStart('debug', pragmas.debug);
  1093. Check.typeOf.object("rectangle", rectangle);
  1094. Rectangle.validate(rectangle);
  1095. if (rectangle.north < rectangle.south) {
  1096. throw new DeveloperError(
  1097. "options.rectangle.north must be greater than or equal to options.rectangle.south"
  1098. );
  1099. }
  1100. //>>includeEnd('debug');
  1101. const granularity = defaultValue(
  1102. options.granularity,
  1103. CesiumMath.RADIANS_PER_DEGREE
  1104. );
  1105. const ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  1106. const rotation = defaultValue(options.rotation, 0.0);
  1107. return computeRectangle(rectangle, granularity, rotation, ellipsoid, result);
  1108. };
  1109. const tangentRotationMatrixScratch = new Matrix3();
  1110. const quaternionScratch = new Quaternion();
  1111. const centerScratch = new Cartographic();
  1112. /**
  1113. * Computes the geometric representation of a rectangle, including its vertices, indices, and a bounding sphere.
  1114. *
  1115. * @param {RectangleGeometry} rectangleGeometry A description of the rectangle.
  1116. * @returns {Geometry|undefined} The computed vertices and indices.
  1117. *
  1118. * @exception {DeveloperError} Rotated rectangle is invalid.
  1119. */
  1120. RectangleGeometry.createGeometry = function (rectangleGeometry) {
  1121. if (
  1122. CesiumMath.equalsEpsilon(
  1123. rectangleGeometry._rectangle.north,
  1124. rectangleGeometry._rectangle.south,
  1125. CesiumMath.EPSILON10
  1126. ) ||
  1127. CesiumMath.equalsEpsilon(
  1128. rectangleGeometry._rectangle.east,
  1129. rectangleGeometry._rectangle.west,
  1130. CesiumMath.EPSILON10
  1131. )
  1132. ) {
  1133. return undefined;
  1134. }
  1135. let rectangle = rectangleGeometry._rectangle;
  1136. const ellipsoid = rectangleGeometry._ellipsoid;
  1137. const rotation = rectangleGeometry._rotation;
  1138. const stRotation = rectangleGeometry._stRotation;
  1139. const vertexFormat = rectangleGeometry._vertexFormat;
  1140. const computedOptions = RectangleGeometryLibrary.computeOptions(
  1141. rectangle,
  1142. rectangleGeometry._granularity,
  1143. rotation,
  1144. stRotation,
  1145. rectangleScratch,
  1146. nwScratch,
  1147. stNwScratch
  1148. );
  1149. const tangentRotationMatrix = tangentRotationMatrixScratch;
  1150. if (stRotation !== 0 || rotation !== 0) {
  1151. const center = Rectangle.center(rectangle, centerScratch);
  1152. const axis = ellipsoid.geodeticSurfaceNormalCartographic(center, v1Scratch);
  1153. Quaternion.fromAxisAngle(axis, -stRotation, quaternionScratch);
  1154. Matrix3.fromQuaternion(quaternionScratch, tangentRotationMatrix);
  1155. } else {
  1156. Matrix3.clone(Matrix3.IDENTITY, tangentRotationMatrix);
  1157. }
  1158. const surfaceHeight = rectangleGeometry._surfaceHeight;
  1159. const extrudedHeight = rectangleGeometry._extrudedHeight;
  1160. const extrude = !CesiumMath.equalsEpsilon(
  1161. surfaceHeight,
  1162. extrudedHeight,
  1163. 0,
  1164. CesiumMath.EPSILON2
  1165. );
  1166. computedOptions.lonScalar = 1.0 / rectangleGeometry._rectangle.width;
  1167. computedOptions.latScalar = 1.0 / rectangleGeometry._rectangle.height;
  1168. computedOptions.tangentRotationMatrix = tangentRotationMatrix;
  1169. let geometry;
  1170. let boundingSphere;
  1171. rectangle = rectangleGeometry._rectangle;
  1172. if (extrude) {
  1173. geometry = constructExtrudedRectangle(rectangleGeometry, computedOptions);
  1174. const topBS = BoundingSphere.fromRectangle3D(
  1175. rectangle,
  1176. ellipsoid,
  1177. surfaceHeight,
  1178. topBoundingSphere
  1179. );
  1180. const bottomBS = BoundingSphere.fromRectangle3D(
  1181. rectangle,
  1182. ellipsoid,
  1183. extrudedHeight,
  1184. bottomBoundingSphere
  1185. );
  1186. boundingSphere = BoundingSphere.union(topBS, bottomBS);
  1187. } else {
  1188. geometry = constructRectangle(rectangleGeometry, computedOptions);
  1189. geometry.attributes.position.values = PolygonPipeline.scaleToGeodeticHeight(
  1190. geometry.attributes.position.values,
  1191. surfaceHeight,
  1192. ellipsoid,
  1193. false
  1194. );
  1195. if (defined(rectangleGeometry._offsetAttribute)) {
  1196. const length = geometry.attributes.position.values.length;
  1197. const applyOffset = new Uint8Array(length / 3);
  1198. const offsetValue =
  1199. rectangleGeometry._offsetAttribute === GeometryOffsetAttribute.NONE
  1200. ? 0
  1201. : 1;
  1202. arrayFill(applyOffset, offsetValue);
  1203. geometry.attributes.applyOffset = new GeometryAttribute({
  1204. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  1205. componentsPerAttribute: 1,
  1206. values: applyOffset,
  1207. });
  1208. }
  1209. boundingSphere = BoundingSphere.fromRectangle3D(
  1210. rectangle,
  1211. ellipsoid,
  1212. surfaceHeight
  1213. );
  1214. }
  1215. if (!vertexFormat.position) {
  1216. delete geometry.attributes.position;
  1217. }
  1218. return new Geometry({
  1219. attributes: geometry.attributes,
  1220. indices: geometry.indices,
  1221. primitiveType: geometry.primitiveType,
  1222. boundingSphere: boundingSphere,
  1223. offsetAttribute: rectangleGeometry._offsetAttribute,
  1224. });
  1225. };
  1226. /**
  1227. * @private
  1228. */
  1229. RectangleGeometry.createShadowVolume = function (
  1230. rectangleGeometry,
  1231. minHeightFunc,
  1232. maxHeightFunc
  1233. ) {
  1234. const granularity = rectangleGeometry._granularity;
  1235. const ellipsoid = rectangleGeometry._ellipsoid;
  1236. const minHeight = minHeightFunc(granularity, ellipsoid);
  1237. const maxHeight = maxHeightFunc(granularity, ellipsoid);
  1238. return new RectangleGeometry({
  1239. rectangle: rectangleGeometry._rectangle,
  1240. rotation: rectangleGeometry._rotation,
  1241. ellipsoid: ellipsoid,
  1242. stRotation: rectangleGeometry._stRotation,
  1243. granularity: granularity,
  1244. extrudedHeight: maxHeight,
  1245. height: minHeight,
  1246. vertexFormat: VertexFormat.POSITION_ONLY,
  1247. shadowVolume: true,
  1248. });
  1249. };
  1250. const unrotatedTextureRectangleScratch = new Rectangle();
  1251. const points2DScratch = [new Cartesian2(), new Cartesian2(), new Cartesian2()];
  1252. const rotation2DScratch = new Matrix2();
  1253. const rectangleCenterScratch = new Cartographic();
  1254. function textureCoordinateRotationPoints(rectangleGeometry) {
  1255. if (rectangleGeometry._stRotation === 0.0) {
  1256. return [0, 0, 0, 1, 1, 0];
  1257. }
  1258. const rectangle = Rectangle.clone(
  1259. rectangleGeometry._rectangle,
  1260. unrotatedTextureRectangleScratch
  1261. );
  1262. const granularity = rectangleGeometry._granularity;
  1263. const ellipsoid = rectangleGeometry._ellipsoid;
  1264. // Rotate to align the texture coordinates with ENU
  1265. const rotation = rectangleGeometry._rotation - rectangleGeometry._stRotation;
  1266. const unrotatedTextureRectangle = computeRectangle(
  1267. rectangle,
  1268. granularity,
  1269. rotation,
  1270. ellipsoid,
  1271. unrotatedTextureRectangleScratch
  1272. );
  1273. // Assume a computed "east-north" texture coordinate system based on spherical or planar tricks, bounded by `boundingRectangle`.
  1274. // The "desired" texture coordinate system forms an oriented rectangle (un-oriented computed) around the geometry that completely and tightly bounds it.
  1275. // We want to map from the "east-north" texture coordinate system into the "desired" system using a pair of lines (analagous planes in 2D)
  1276. // Compute 3 corners of the "desired" texture coordinate system in "east-north" texture space by the following in cartographic space:
  1277. // - rotate 3 of the corners in unrotatedTextureRectangle by stRotation around the center of the bounding rectangle
  1278. // - apply the "east-north" system's normalization formula to the rotated cartographics, even though this is likely to produce values outside [0-1].
  1279. // This gives us a set of points in the "east-north" texture coordinate system that can be used to map "east-north" texture coordinates to "desired."
  1280. const points2D = points2DScratch;
  1281. points2D[0].x = unrotatedTextureRectangle.west;
  1282. points2D[0].y = unrotatedTextureRectangle.south;
  1283. points2D[1].x = unrotatedTextureRectangle.west;
  1284. points2D[1].y = unrotatedTextureRectangle.north;
  1285. points2D[2].x = unrotatedTextureRectangle.east;
  1286. points2D[2].y = unrotatedTextureRectangle.south;
  1287. const boundingRectangle = rectangleGeometry.rectangle;
  1288. const toDesiredInComputed = Matrix2.fromRotation(
  1289. rectangleGeometry._stRotation,
  1290. rotation2DScratch
  1291. );
  1292. const boundingRectangleCenter = Rectangle.center(
  1293. boundingRectangle,
  1294. rectangleCenterScratch
  1295. );
  1296. for (let i = 0; i < 3; ++i) {
  1297. const point2D = points2D[i];
  1298. point2D.x -= boundingRectangleCenter.longitude;
  1299. point2D.y -= boundingRectangleCenter.latitude;
  1300. Matrix2.multiplyByVector(toDesiredInComputed, point2D, point2D);
  1301. point2D.x += boundingRectangleCenter.longitude;
  1302. point2D.y += boundingRectangleCenter.latitude;
  1303. // Convert point into east-north texture coordinate space
  1304. point2D.x = (point2D.x - boundingRectangle.west) / boundingRectangle.width;
  1305. point2D.y =
  1306. (point2D.y - boundingRectangle.south) / boundingRectangle.height;
  1307. }
  1308. const minXYCorner = points2D[0];
  1309. const maxYCorner = points2D[1];
  1310. const maxXCorner = points2D[2];
  1311. const result = new Array(6);
  1312. Cartesian2.pack(minXYCorner, result);
  1313. Cartesian2.pack(maxYCorner, result, 2);
  1314. Cartesian2.pack(maxXCorner, result, 4);
  1315. return result;
  1316. }
  1317. Object.defineProperties(RectangleGeometry.prototype, {
  1318. /**
  1319. * @private
  1320. */
  1321. rectangle: {
  1322. get: function () {
  1323. if (!defined(this._rotatedRectangle)) {
  1324. this._rotatedRectangle = computeRectangle(
  1325. this._rectangle,
  1326. this._granularity,
  1327. this._rotation,
  1328. this._ellipsoid
  1329. );
  1330. }
  1331. return this._rotatedRectangle;
  1332. },
  1333. },
  1334. /**
  1335. * For remapping texture coordinates when rendering RectangleGeometries as GroundPrimitives.
  1336. * This version permits skew in textures by computing offsets directly in cartographic space and
  1337. * more accurately approximates rendering RectangleGeometries with height as standard Primitives.
  1338. * @see Geometry#_textureCoordinateRotationPoints
  1339. * @private
  1340. */
  1341. textureCoordinateRotationPoints: {
  1342. get: function () {
  1343. if (!defined(this._textureCoordinateRotationPoints)) {
  1344. this._textureCoordinateRotationPoints = textureCoordinateRotationPoints(
  1345. this
  1346. );
  1347. }
  1348. return this._textureCoordinateRotationPoints;
  1349. },
  1350. },
  1351. });
  1352. export default RectangleGeometry;