createRectangleGeometry.js 52 KB

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