createRectangleGeometry.js 51 KB

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