RectangleGeometry.js 45 KB

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