createRectangleGeometry.js 51 KB

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