createRectangleGeometry.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defined-26bd4a03', './Check-da037458', './freezeObject-2d83f591', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './defineProperties-6f7a50f2', './Transforms-65aba0a4', './RuntimeError-ad75c885', './WebGLConstants-497deb20', './ComponentDatatype-69643096', './GeometryAttribute-ed359d71', './when-ee12a2cb', './GeometryAttributes-eecc9f43', './AttributeCompression-87682214', './GeometryPipeline-f0b16df6', './EncodedCartesian3-8b2b90d0', './IndexDatatype-3de60176', './IntersectionTests-c2360ffa', './Plane-a1a3fd52', './GeometryOffsetAttribute-cb30cd97', './VertexFormat-fbb91dc7', './GeometryInstance-72fd4e35', './EllipsoidRhumbLine-c6cdbfd3', './PolygonPipeline-e486c11c', './RectangleGeometryLibrary-39950242'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, when, 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(positions, vertexFormat, ellipsoid, tangentRotationMatrix) {
  45. var length = positions.length;
  46. var normals = (vertexFormat.normal) ? new Float32Array(length) : undefined;
  47. var tangents = (vertexFormat.tangent) ? new Float32Array(length) : undefined;
  48. var bitangents = (vertexFormat.bitangent) ? new Float32Array(length) : undefined;
  49. var attrIndex = 0;
  50. var bitangent = bitangentScratch;
  51. var tangent = tangentScratch;
  52. var normal = normalScratch;
  53. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
  54. for (var i = 0; i < length; i += 3) {
  55. var p = Cartesian2.Cartesian3.fromArray(positions, i, positionScratch);
  56. var attrIndex1 = attrIndex + 1;
  57. var attrIndex2 = attrIndex + 2;
  58. normal = ellipsoid.geodeticSurfaceNormal(p, normal);
  59. if (vertexFormat.tangent || vertexFormat.bitangent) {
  60. Cartesian2.Cartesian3.cross(Cartesian2.Cartesian3.UNIT_Z, normal, tangent);
  61. Transforms.Matrix3.multiplyByVector(tangentRotationMatrix, tangent, tangent);
  62. Cartesian2.Cartesian3.normalize(tangent, tangent);
  63. if (vertexFormat.bitangent) {
  64. Cartesian2.Cartesian3.normalize(Cartesian2.Cartesian3.cross(normal, tangent, bitangent), bitangent);
  65. }
  66. }
  67. if (vertexFormat.normal) {
  68. normals[attrIndex] = normal.x;
  69. normals[attrIndex1] = normal.y;
  70. normals[attrIndex2] = normal.z;
  71. }
  72. if (vertexFormat.tangent) {
  73. tangents[attrIndex] = tangent.x;
  74. tangents[attrIndex1] = tangent.y;
  75. tangents[attrIndex2] = tangent.z;
  76. }
  77. if (vertexFormat.bitangent) {
  78. bitangents[attrIndex] = bitangent.x;
  79. bitangents[attrIndex1] = bitangent.y;
  80. bitangents[attrIndex2] = bitangent.z;
  81. }
  82. attrIndex += 3;
  83. }
  84. }
  85. return createAttributes(vertexFormat, {
  86. positions : positions,
  87. normals : normals,
  88. tangents : tangents,
  89. bitangents : bitangents
  90. });
  91. }
  92. var v1Scratch = new Cartesian2.Cartesian3();
  93. var v2Scratch = new Cartesian2.Cartesian3();
  94. function calculateAttributesWall(positions, vertexFormat, ellipsoid) {
  95. var length = positions.length;
  96. var normals = (vertexFormat.normal) ? new Float32Array(length) : undefined;
  97. var tangents = (vertexFormat.tangent) ? new Float32Array(length) : undefined;
  98. var bitangents = (vertexFormat.bitangent) ? new Float32Array(length) : undefined;
  99. var normalIndex = 0;
  100. var tangentIndex = 0;
  101. var bitangentIndex = 0;
  102. var recomputeNormal = true;
  103. var bitangent = bitangentScratch;
  104. var tangent = tangentScratch;
  105. var normal = normalScratch;
  106. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
  107. for (var i = 0; i < length; i += 6) {
  108. var p = Cartesian2.Cartesian3.fromArray(positions, i, positionScratch);
  109. var p1 = Cartesian2.Cartesian3.fromArray(positions, (i + 6) % length, v1Scratch);
  110. if (recomputeNormal) {
  111. var p2 = Cartesian2.Cartesian3.fromArray(positions, (i + 3) % length, v2Scratch);
  112. Cartesian2.Cartesian3.subtract(p1, p, p1);
  113. Cartesian2.Cartesian3.subtract(p2, p, p2);
  114. normal = Cartesian2.Cartesian3.normalize(Cartesian2.Cartesian3.cross(p2, p1, normal), normal);
  115. recomputeNormal = false;
  116. }
  117. if (Cartesian2.Cartesian3.equalsEpsilon(p1, p, _Math.CesiumMath.EPSILON10)) { // if we've reached a corner
  118. recomputeNormal = true;
  119. }
  120. if (vertexFormat.tangent || vertexFormat.bitangent) {
  121. bitangent = ellipsoid.geodeticSurfaceNormal(p, bitangent);
  122. if (vertexFormat.tangent) {
  123. tangent = Cartesian2.Cartesian3.normalize(Cartesian2.Cartesian3.cross(bitangent, normal, tangent), tangent);
  124. }
  125. }
  126. if (vertexFormat.normal) {
  127. normals[normalIndex++] = normal.x;
  128. normals[normalIndex++] = normal.y;
  129. normals[normalIndex++] = normal.z;
  130. normals[normalIndex++] = normal.x;
  131. normals[normalIndex++] = normal.y;
  132. normals[normalIndex++] = normal.z;
  133. }
  134. if (vertexFormat.tangent) {
  135. tangents[tangentIndex++] = tangent.x;
  136. tangents[tangentIndex++] = tangent.y;
  137. tangents[tangentIndex++] = tangent.z;
  138. tangents[tangentIndex++] = tangent.x;
  139. tangents[tangentIndex++] = tangent.y;
  140. tangents[tangentIndex++] = tangent.z;
  141. }
  142. if (vertexFormat.bitangent) {
  143. bitangents[bitangentIndex++] = bitangent.x;
  144. bitangents[bitangentIndex++] = bitangent.y;
  145. bitangents[bitangentIndex++] = bitangent.z;
  146. bitangents[bitangentIndex++] = bitangent.x;
  147. bitangents[bitangentIndex++] = bitangent.y;
  148. bitangents[bitangentIndex++] = bitangent.z;
  149. }
  150. }
  151. }
  152. return createAttributes(vertexFormat, {
  153. positions : positions,
  154. normals : normals,
  155. tangents : tangents,
  156. bitangents : bitangents
  157. });
  158. }
  159. function constructRectangle(rectangleGeometry, computedOptions) {
  160. var vertexFormat = rectangleGeometry._vertexFormat;
  161. var ellipsoid = rectangleGeometry._ellipsoid;
  162. var height = computedOptions.height;
  163. var width = computedOptions.width;
  164. var northCap = computedOptions.northCap;
  165. var southCap = computedOptions.southCap;
  166. var rowStart = 0;
  167. var rowEnd = height;
  168. var rowHeight = height;
  169. var size = 0;
  170. if (northCap) {
  171. rowStart = 1;
  172. rowHeight -= 1;
  173. size += 1;
  174. }
  175. if (southCap) {
  176. rowEnd -= 1;
  177. rowHeight -= 1;
  178. size += 1;
  179. }
  180. size += (width * rowHeight);
  181. var positions = (vertexFormat.position) ? new Float64Array(size * 3) : undefined;
  182. var textureCoordinates = (vertexFormat.st) ? new Float32Array(size * 2) : undefined;
  183. var posIndex = 0;
  184. var stIndex = 0;
  185. var position = positionScratch;
  186. var st = stScratch;
  187. var minX = Number.MAX_VALUE;
  188. var minY = Number.MAX_VALUE;
  189. var maxX = -Number.MAX_VALUE;
  190. var maxY = -Number.MAX_VALUE;
  191. for (var row = rowStart; row < rowEnd; ++row) {
  192. for (var col = 0; col < width; ++col) {
  193. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(computedOptions, ellipsoid, vertexFormat.st, row, col, position, st);
  194. positions[posIndex++] = position.x;
  195. positions[posIndex++] = position.y;
  196. positions[posIndex++] = position.z;
  197. if (vertexFormat.st) {
  198. textureCoordinates[stIndex++] = st.x;
  199. textureCoordinates[stIndex++] = st.y;
  200. minX = Math.min(minX, st.x);
  201. minY = Math.min(minY, st.y);
  202. maxX = Math.max(maxX, st.x);
  203. maxY = Math.max(maxY, st.y);
  204. }
  205. }
  206. }
  207. if (northCap) {
  208. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(computedOptions, ellipsoid, vertexFormat.st, 0, 0, position, st);
  209. positions[posIndex++] = position.x;
  210. positions[posIndex++] = position.y;
  211. positions[posIndex++] = position.z;
  212. if (vertexFormat.st) {
  213. textureCoordinates[stIndex++] = st.x;
  214. textureCoordinates[stIndex++] = st.y;
  215. minX = st.x;
  216. minY = st.y;
  217. maxX = st.x;
  218. maxY = st.y;
  219. }
  220. }
  221. if (southCap) {
  222. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(computedOptions, ellipsoid, vertexFormat.st, height - 1, 0, position, st);
  223. positions[posIndex++] = position.x;
  224. positions[posIndex++] = position.y;
  225. positions[posIndex] = position.z;
  226. if (vertexFormat.st) {
  227. textureCoordinates[stIndex++] = st.x;
  228. textureCoordinates[stIndex] = st.y;
  229. minX = Math.min(minX, st.x);
  230. minY = Math.min(minY, st.y);
  231. maxX = Math.max(maxX, st.x);
  232. maxY = Math.max(maxY, st.y);
  233. }
  234. }
  235. if (vertexFormat.st && (minX < 0.0 || minY < 0.0 || maxX > 1.0 || maxY > 1.0)) {
  236. for (var k = 0; k < textureCoordinates.length; k += 2) {
  237. textureCoordinates[k] = (textureCoordinates[k] - minX) / (maxX - minX);
  238. textureCoordinates[k + 1] = (textureCoordinates[k + 1] - minY) / (maxY - minY);
  239. }
  240. }
  241. var geo = calculateAttributes(positions, vertexFormat, ellipsoid, computedOptions.tangentRotationMatrix);
  242. var indicesSize = 6 * (width - 1) * (rowHeight - 1);
  243. if (northCap) {
  244. indicesSize += 3 * (width - 1);
  245. }
  246. if (southCap) {
  247. indicesSize += 3 * (width - 1);
  248. }
  249. var indices = IndexDatatype.IndexDatatype.createTypedArray(size, indicesSize);
  250. var index = 0;
  251. var indicesIndex = 0;
  252. var i;
  253. for (i = 0; i < rowHeight - 1; ++i) {
  254. for (var j = 0; j < width - 1; ++j) {
  255. var upperLeft = index;
  256. var lowerLeft = upperLeft + width;
  257. var lowerRight = lowerLeft + 1;
  258. var upperRight = upperLeft + 1;
  259. indices[indicesIndex++] = upperLeft;
  260. indices[indicesIndex++] = lowerLeft;
  261. indices[indicesIndex++] = upperRight;
  262. indices[indicesIndex++] = upperRight;
  263. indices[indicesIndex++] = lowerLeft;
  264. indices[indicesIndex++] = lowerRight;
  265. ++index;
  266. }
  267. ++index;
  268. }
  269. if (northCap || southCap) {
  270. var northIndex = size - 1;
  271. var southIndex = size - 1;
  272. if (northCap && southCap) {
  273. northIndex = size - 2;
  274. }
  275. var p1;
  276. var p2;
  277. index = 0;
  278. if (northCap) {
  279. for (i = 0; i < width - 1; i++) {
  280. p1 = index;
  281. p2 = p1 + 1;
  282. indices[indicesIndex++] = northIndex;
  283. indices[indicesIndex++] = p1;
  284. indices[indicesIndex++] = p2;
  285. ++index;
  286. }
  287. }
  288. if (southCap) {
  289. index = (rowHeight - 1) * (width);
  290. for (i = 0; i < width - 1; i++) {
  291. p1 = index;
  292. p2 = p1 + 1;
  293. indices[indicesIndex++] = p1;
  294. indices[indicesIndex++] = southIndex;
  295. indices[indicesIndex++] = p2;
  296. ++index;
  297. }
  298. }
  299. }
  300. geo.indices = indices;
  301. if (vertexFormat.st) {
  302. geo.attributes.st = new GeometryAttribute.GeometryAttribute({
  303. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  304. componentsPerAttribute : 2,
  305. values : textureCoordinates
  306. });
  307. }
  308. return geo;
  309. }
  310. function addWallPositions(wallPositions, posIndex, i, topPositions, bottomPositions) {
  311. wallPositions[posIndex++] = topPositions[i];
  312. wallPositions[posIndex++] = topPositions[i + 1];
  313. wallPositions[posIndex++] = topPositions[i + 2];
  314. wallPositions[posIndex++] = bottomPositions[i];
  315. wallPositions[posIndex++] = bottomPositions[i + 1];
  316. wallPositions[posIndex] = bottomPositions[i + 2];
  317. return wallPositions;
  318. }
  319. function addWallTextureCoordinates(wallTextures, stIndex, i, st) {
  320. wallTextures[stIndex++] = st[i];
  321. wallTextures[stIndex++] = st[i + 1];
  322. wallTextures[stIndex++] = st[i];
  323. wallTextures[stIndex] = st[i + 1];
  324. return wallTextures;
  325. }
  326. var scratchVertexFormat = new VertexFormat.VertexFormat();
  327. function constructExtrudedRectangle(rectangleGeometry, computedOptions) {
  328. var shadowVolume = rectangleGeometry._shadowVolume;
  329. var offsetAttributeValue = rectangleGeometry._offsetAttribute;
  330. var vertexFormat = rectangleGeometry._vertexFormat;
  331. var minHeight = rectangleGeometry._extrudedHeight;
  332. var maxHeight = rectangleGeometry._surfaceHeight;
  333. var ellipsoid = rectangleGeometry._ellipsoid;
  334. var height = computedOptions.height;
  335. var width = computedOptions.width;
  336. var i;
  337. if (shadowVolume) {
  338. var newVertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, scratchVertexFormat);
  339. newVertexFormat.normal = true;
  340. rectangleGeometry._vertexFormat = newVertexFormat;
  341. }
  342. var topBottomGeo = constructRectangle(rectangleGeometry, computedOptions);
  343. if (shadowVolume) {
  344. rectangleGeometry._vertexFormat = vertexFormat;
  345. }
  346. var topPositions = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(topBottomGeo.attributes.position.values, maxHeight, ellipsoid, false);
  347. topPositions = new Float64Array(topPositions);
  348. var length = topPositions.length;
  349. var newLength = length * 2;
  350. var positions = new Float64Array(newLength);
  351. positions.set(topPositions);
  352. var bottomPositions = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(topBottomGeo.attributes.position.values, minHeight, ellipsoid);
  353. positions.set(bottomPositions, length);
  354. topBottomGeo.attributes.position.values = positions;
  355. var normals = (vertexFormat.normal) ? new Float32Array(newLength) : undefined;
  356. var tangents = (vertexFormat.tangent) ? new Float32Array(newLength) : undefined;
  357. var bitangents = (vertexFormat.bitangent) ? new Float32Array(newLength) : undefined;
  358. var textures = (vertexFormat.st) ? new Float32Array(newLength / 3 * 2) : undefined;
  359. var topSt;
  360. var topNormals;
  361. if (vertexFormat.normal) {
  362. topNormals = topBottomGeo.attributes.normal.values;
  363. normals.set(topNormals);
  364. for (i = 0; i < length; i++) {
  365. topNormals[i] = -topNormals[i];
  366. }
  367. normals.set(topNormals, length);
  368. topBottomGeo.attributes.normal.values = normals;
  369. }
  370. if (shadowVolume) {
  371. topNormals = topBottomGeo.attributes.normal.values;
  372. if (!vertexFormat.normal) {
  373. topBottomGeo.attributes.normal = undefined;
  374. }
  375. var extrudeNormals = new Float32Array(newLength);
  376. for (i = 0; i < length; i++) {
  377. topNormals[i] = -topNormals[i];
  378. }
  379. extrudeNormals.set(topNormals, length); //only get normals for bottom layer that's going to be pushed down
  380. topBottomGeo.attributes.extrudeDirection = new GeometryAttribute.GeometryAttribute({
  381. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  382. componentsPerAttribute : 3,
  383. values : extrudeNormals
  384. });
  385. }
  386. var offsetValue;
  387. var hasOffsets = defined.defined(offsetAttributeValue);
  388. if (hasOffsets) {
  389. var size = length / 3 * 2;
  390. var offsetAttribute = new Uint8Array(size);
  391. if (offsetAttributeValue === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  392. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, 1, 0, size / 2);
  393. } else {
  394. offsetValue = offsetAttributeValue === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  395. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, offsetValue);
  396. }
  397. topBottomGeo.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  398. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  399. componentsPerAttribute : 1,
  400. values : offsetAttribute
  401. });
  402. }
  403. if (vertexFormat.tangent) {
  404. var topTangents = topBottomGeo.attributes.tangent.values;
  405. tangents.set(topTangents);
  406. for (i = 0; i < length; i++) {
  407. topTangents[i] = -topTangents[i];
  408. }
  409. tangents.set(topTangents, length);
  410. topBottomGeo.attributes.tangent.values = tangents;
  411. }
  412. if (vertexFormat.bitangent) {
  413. var topBitangents = topBottomGeo.attributes.bitangent.values;
  414. bitangents.set(topBitangents);
  415. bitangents.set(topBitangents, length);
  416. topBottomGeo.attributes.bitangent.values = bitangents;
  417. }
  418. if (vertexFormat.st) {
  419. topSt = topBottomGeo.attributes.st.values;
  420. textures.set(topSt);
  421. textures.set(topSt, length / 3 * 2);
  422. topBottomGeo.attributes.st.values = textures;
  423. }
  424. var indices = topBottomGeo.indices;
  425. var indicesLength = indices.length;
  426. var posLength = length / 3;
  427. var newIndices = IndexDatatype.IndexDatatype.createTypedArray(newLength / 3, indicesLength * 2);
  428. newIndices.set(indices);
  429. for (i = 0; i < indicesLength; i += 3) {
  430. newIndices[i + indicesLength] = indices[i + 2] + posLength;
  431. newIndices[i + 1 + indicesLength] = indices[i + 1] + posLength;
  432. newIndices[i + 2 + indicesLength] = indices[i] + posLength;
  433. }
  434. topBottomGeo.indices = newIndices;
  435. var northCap = computedOptions.northCap;
  436. var southCap = computedOptions.southCap;
  437. var rowHeight = height;
  438. var widthMultiplier = 2;
  439. var perimeterPositions = 0;
  440. var corners = 4;
  441. var dupliateCorners = 4;
  442. if (northCap) {
  443. widthMultiplier -= 1;
  444. rowHeight -= 1;
  445. perimeterPositions += 1;
  446. corners -= 2;
  447. dupliateCorners -= 1;
  448. }
  449. if (southCap) {
  450. widthMultiplier -= 1;
  451. rowHeight -= 1;
  452. perimeterPositions += 1;
  453. corners -= 2;
  454. dupliateCorners -= 1;
  455. }
  456. perimeterPositions += (widthMultiplier * width + 2 * rowHeight - corners);
  457. var wallCount = (perimeterPositions + dupliateCorners) * 2;
  458. var wallPositions = new Float64Array(wallCount * 3);
  459. var wallExtrudeNormals = shadowVolume ? new Float32Array(wallCount * 3) : undefined;
  460. var wallOffsetAttribute = hasOffsets ? new Uint8Array(wallCount) : undefined;
  461. var wallTextures = (vertexFormat.st) ? new Float32Array(wallCount * 2) : undefined;
  462. var computeTopOffsets = offsetAttributeValue === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP;
  463. if (hasOffsets && !computeTopOffsets) {
  464. offsetValue = offsetAttributeValue === GeometryOffsetAttribute.GeometryOffsetAttribute.ALL ? 1 : 0;
  465. wallOffsetAttribute = GeometryOffsetAttribute.arrayFill(wallOffsetAttribute, offsetValue);
  466. }
  467. var posIndex = 0;
  468. var stIndex = 0;
  469. var extrudeNormalIndex = 0;
  470. var wallOffsetIndex = 0;
  471. var area = width * rowHeight;
  472. var threeI;
  473. for (i = 0; i < area; i += width) {
  474. threeI = i * 3;
  475. wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
  476. posIndex += 6;
  477. if (vertexFormat.st) {
  478. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i * 2, topSt);
  479. stIndex += 4;
  480. }
  481. if (shadowVolume) {
  482. extrudeNormalIndex += 3;
  483. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  484. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  485. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  486. }
  487. if (computeTopOffsets) {
  488. wallOffsetAttribute[wallOffsetIndex++] = 1;
  489. wallOffsetIndex += 1;
  490. }
  491. }
  492. if (!southCap) {
  493. for (i = area - width; i < area; i++) {
  494. threeI = i * 3;
  495. wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
  496. posIndex += 6;
  497. if (vertexFormat.st) {
  498. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i * 2, topSt);
  499. stIndex += 4;
  500. }
  501. if (shadowVolume) {
  502. extrudeNormalIndex += 3;
  503. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  504. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  505. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  506. }
  507. if (computeTopOffsets) {
  508. wallOffsetAttribute[wallOffsetIndex++] = 1;
  509. wallOffsetIndex += 1;
  510. }
  511. }
  512. } else {
  513. var southIndex = northCap ? area + 1 : area;
  514. threeI = southIndex * 3;
  515. for (i = 0; i < 2; i++) { // duplicate corner points
  516. wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
  517. posIndex += 6;
  518. if (vertexFormat.st) {
  519. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, southIndex * 2, topSt);
  520. stIndex += 4;
  521. }
  522. if (shadowVolume) {
  523. extrudeNormalIndex += 3;
  524. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  525. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  526. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  527. }
  528. if (computeTopOffsets) {
  529. wallOffsetAttribute[wallOffsetIndex++] = 1;
  530. wallOffsetIndex += 1;
  531. }
  532. }
  533. }
  534. for (i = area - 1; i > 0; i -= width) {
  535. threeI = i * 3;
  536. wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
  537. posIndex += 6;
  538. if (vertexFormat.st) {
  539. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i * 2, topSt);
  540. stIndex += 4;
  541. }
  542. if (shadowVolume) {
  543. extrudeNormalIndex += 3;
  544. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  545. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  546. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  547. }
  548. if (computeTopOffsets) {
  549. wallOffsetAttribute[wallOffsetIndex++] = 1;
  550. wallOffsetIndex += 1;
  551. }
  552. }
  553. if (!northCap) {
  554. for (i = width - 1; i >= 0; i--) {
  555. threeI = i * 3;
  556. wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
  557. posIndex += 6;
  558. if (vertexFormat.st) {
  559. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, i * 2, topSt);
  560. stIndex += 4;
  561. }
  562. if (shadowVolume) {
  563. extrudeNormalIndex += 3;
  564. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  565. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  566. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  567. }
  568. if (computeTopOffsets) {
  569. wallOffsetAttribute[wallOffsetIndex++] = 1;
  570. wallOffsetIndex += 1;
  571. }
  572. }
  573. } else {
  574. var northIndex = area;
  575. threeI = northIndex * 3;
  576. for (i = 0; i < 2; i++) { // duplicate corner points
  577. wallPositions = addWallPositions(wallPositions, posIndex, threeI, topPositions, bottomPositions);
  578. posIndex += 6;
  579. if (vertexFormat.st) {
  580. wallTextures = addWallTextureCoordinates(wallTextures, stIndex, northIndex * 2, topSt);
  581. stIndex += 4;
  582. }
  583. if (shadowVolume) {
  584. extrudeNormalIndex += 3;
  585. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI];
  586. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 1];
  587. wallExtrudeNormals[extrudeNormalIndex++] = topNormals[threeI + 2];
  588. }
  589. if (computeTopOffsets) {
  590. wallOffsetAttribute[wallOffsetIndex++] = 1;
  591. wallOffsetIndex += 1;
  592. }
  593. }
  594. }
  595. var geo = calculateAttributesWall(wallPositions, vertexFormat, ellipsoid);
  596. if (vertexFormat.st) {
  597. geo.attributes.st = new GeometryAttribute.GeometryAttribute({
  598. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  599. componentsPerAttribute : 2,
  600. values : wallTextures
  601. });
  602. }
  603. if (shadowVolume) {
  604. geo.attributes.extrudeDirection = new GeometryAttribute.GeometryAttribute({
  605. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  606. componentsPerAttribute : 3,
  607. values : wallExtrudeNormals
  608. });
  609. }
  610. if (hasOffsets) {
  611. geo.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  612. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  613. componentsPerAttribute : 1,
  614. values : wallOffsetAttribute
  615. });
  616. }
  617. var wallIndices = IndexDatatype.IndexDatatype.createTypedArray(wallCount, perimeterPositions * 6);
  618. var upperLeft;
  619. var lowerLeft;
  620. var lowerRight;
  621. var upperRight;
  622. length = wallPositions.length / 3;
  623. var index = 0;
  624. for (i = 0; i < length - 1; i += 2) {
  625. upperLeft = i;
  626. upperRight = (upperLeft + 2) % length;
  627. var p1 = Cartesian2.Cartesian3.fromArray(wallPositions, upperLeft * 3, v1Scratch);
  628. var p2 = Cartesian2.Cartesian3.fromArray(wallPositions, upperRight * 3, v2Scratch);
  629. if (Cartesian2.Cartesian3.equalsEpsilon(p1, p2, _Math.CesiumMath.EPSILON10)) {
  630. continue;
  631. }
  632. lowerLeft = (upperLeft + 1) % length;
  633. lowerRight = (lowerLeft + 2) % length;
  634. wallIndices[index++] = upperLeft;
  635. wallIndices[index++] = lowerLeft;
  636. wallIndices[index++] = upperRight;
  637. wallIndices[index++] = upperRight;
  638. wallIndices[index++] = lowerLeft;
  639. wallIndices[index++] = lowerRight;
  640. }
  641. geo.indices = wallIndices;
  642. geo = GeometryPipeline.GeometryPipeline.combineInstances([
  643. new GeometryInstance.GeometryInstance({
  644. geometry : topBottomGeo
  645. }),
  646. new GeometryInstance.GeometryInstance({
  647. geometry : geo
  648. })
  649. ]);
  650. return geo[0];
  651. }
  652. var scratchRectanglePoints = [new Cartesian2.Cartesian3(), new Cartesian2.Cartesian3(), new Cartesian2.Cartesian3(), new Cartesian2.Cartesian3()];
  653. var nwScratch = new Cartesian2.Cartographic();
  654. var stNwScratch = new Cartesian2.Cartographic();
  655. function computeRectangle(rectangle, granularity, rotation, ellipsoid, result) {
  656. if (rotation === 0.0) {
  657. return Cartesian2.Rectangle.clone(rectangle, result);
  658. }
  659. var computedOptions = RectangleGeometryLibrary.RectangleGeometryLibrary.computeOptions(rectangle, granularity, rotation, 0, rectangleScratch, nwScratch);
  660. var height = computedOptions.height;
  661. var width = computedOptions.width;
  662. var positions = scratchRectanglePoints;
  663. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(computedOptions, ellipsoid, false, 0, 0, positions[0]);
  664. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(computedOptions, ellipsoid, false, 0, width - 1, positions[1]);
  665. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(computedOptions, ellipsoid, false, height - 1, 0, positions[2]);
  666. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(computedOptions, ellipsoid, false, height - 1, width - 1, positions[3]);
  667. return Cartesian2.Rectangle.fromCartesianArray(positions, ellipsoid, result);
  668. }
  669. /**
  670. * 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}.
  671. *
  672. * @alias RectangleGeometry
  673. * @constructor
  674. *
  675. * @param {Object} options Object with the following properties:
  676. * @param {Rectangle} options.rectangle A cartographic rectangle with north, south, east and west properties in radians.
  677. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  678. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle lies.
  679. * @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.
  680. * @param {Number} [options.height=0.0] The distance in meters between the rectangle and the ellipsoid surface.
  681. * @param {Number} [options.rotation=0.0] The rotation of the rectangle, in radians. A positive rotation is counter-clockwise.
  682. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  683. * @param {Number} [options.extrudedHeight] The distance in meters between the rectangle's extruded face and the ellipsoid surface.
  684. *
  685. * @exception {DeveloperError} <code>options.rectangle.north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  686. * @exception {DeveloperError} <code>options.rectangle.south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  687. * @exception {DeveloperError} <code>options.rectangle.east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  688. * @exception {DeveloperError} <code>options.rectangle.west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  689. * @exception {DeveloperError} <code>options.rectangle.north</code> must be greater than <code>options.rectangle.south</code>.
  690. *
  691. * @see RectangleGeometry#createGeometry
  692. *
  693. * @demo {@link https://sandcastle.cesium.com/index.html?src=Rectangle.html|Cesium Sandcastle Rectangle Demo}
  694. *
  695. * @example
  696. * // 1. create a rectangle
  697. * var rectangle = new Cesium.RectangleGeometry({
  698. * ellipsoid : Cesium.Ellipsoid.WGS84,
  699. * rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
  700. * height : 10000.0
  701. * });
  702. * var geometry = Cesium.RectangleGeometry.createGeometry(rectangle);
  703. *
  704. * // 2. create an extruded rectangle without a top
  705. * var rectangle = new Cesium.RectangleGeometry({
  706. * ellipsoid : Cesium.Ellipsoid.WGS84,
  707. * rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
  708. * height : 10000.0,
  709. * extrudedHeight: 300000
  710. * });
  711. * var geometry = Cesium.RectangleGeometry.createGeometry(rectangle);
  712. */
  713. function RectangleGeometry(options) {
  714. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  715. var rectangle = options.rectangle;
  716. //>>includeStart('debug', pragmas.debug);
  717. Check.Check.typeOf.object('rectangle', rectangle);
  718. Cartesian2.Rectangle.validate(rectangle);
  719. if (rectangle.north < rectangle.south) {
  720. throw new Check.DeveloperError('options.rectangle.north must be greater than or equal to options.rectangle.south');
  721. }
  722. //>>includeEnd('debug');
  723. var height = defaultValue.defaultValue(options.height, 0.0);
  724. var extrudedHeight = defaultValue.defaultValue(options.extrudedHeight, height);
  725. this._rectangle = Cartesian2.Rectangle.clone(rectangle);
  726. this._granularity = defaultValue.defaultValue(options.granularity, _Math.CesiumMath.RADIANS_PER_DEGREE);
  727. this._ellipsoid = Cartesian2.Ellipsoid.clone(defaultValue.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84));
  728. this._surfaceHeight = Math.max(height, extrudedHeight);
  729. this._rotation = defaultValue.defaultValue(options.rotation, 0.0);
  730. this._stRotation = defaultValue.defaultValue(options.stRotation, 0.0);
  731. this._vertexFormat = VertexFormat.VertexFormat.clone(defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT));
  732. this._extrudedHeight = Math.min(height, extrudedHeight);
  733. this._shadowVolume = defaultValue.defaultValue(options.shadowVolume, false);
  734. this._workerName = 'createRectangleGeometry';
  735. this._offsetAttribute = options.offsetAttribute;
  736. this._rotatedRectangle = undefined;
  737. this._textureCoordinateRotationPoints = undefined;
  738. }
  739. /**
  740. * The number of elements used to pack the object into an array.
  741. * @type {Number}
  742. */
  743. RectangleGeometry.packedLength = Cartesian2.Rectangle.packedLength + Cartesian2.Ellipsoid.packedLength + VertexFormat.VertexFormat.packedLength + 7;
  744. /**
  745. * Stores the provided instance into the provided array.
  746. *
  747. * @param {RectangleGeometry} value The value to pack.
  748. * @param {Number[]} array The array to pack into.
  749. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  750. *
  751. * @returns {Number[]} The array that was packed into
  752. */
  753. RectangleGeometry.pack = function(value, array, startingIndex) {
  754. //>>includeStart('debug', pragmas.debug);
  755. Check.Check.typeOf.object('value', value);
  756. Check.Check.defined('array', array);
  757. //>>includeEnd('debug');
  758. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  759. Cartesian2.Rectangle.pack(value._rectangle, array, startingIndex);
  760. startingIndex += Cartesian2.Rectangle.packedLength;
  761. Cartesian2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  762. startingIndex += Cartesian2.Ellipsoid.packedLength;
  763. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  764. startingIndex += VertexFormat.VertexFormat.packedLength;
  765. array[startingIndex++] = value._granularity;
  766. array[startingIndex++] = value._surfaceHeight;
  767. array[startingIndex++] = value._rotation;
  768. array[startingIndex++] = value._stRotation;
  769. array[startingIndex++] = value._extrudedHeight;
  770. array[startingIndex++] = value._shadowVolume ? 1.0 : 0.0;
  771. array[startingIndex] = defaultValue.defaultValue(value._offsetAttribute, -1);
  772. return array;
  773. };
  774. var scratchRectangle = new Cartesian2.Rectangle();
  775. var scratchEllipsoid = Cartesian2.Ellipsoid.clone(Cartesian2.Ellipsoid.UNIT_SPHERE);
  776. var scratchOptions = {
  777. rectangle : scratchRectangle,
  778. ellipsoid : scratchEllipsoid,
  779. vertexFormat : scratchVertexFormat,
  780. granularity : undefined,
  781. height : undefined,
  782. rotation : undefined,
  783. stRotation : undefined,
  784. extrudedHeight : undefined,
  785. shadowVolume : undefined,
  786. offsetAttribute: undefined
  787. };
  788. /**
  789. * Retrieves an instance from a packed array.
  790. *
  791. * @param {Number[]} array The packed array.
  792. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  793. * @param {RectangleGeometry} [result] The object into which to store the result.
  794. * @returns {RectangleGeometry} The modified result parameter or a new RectangleGeometry instance if one was not provided.
  795. */
  796. RectangleGeometry.unpack = function(array, startingIndex, result) {
  797. //>>includeStart('debug', pragmas.debug);
  798. Check.Check.defined('array', array);
  799. //>>includeEnd('debug');
  800. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  801. var rectangle = Cartesian2.Rectangle.unpack(array, startingIndex, scratchRectangle);
  802. startingIndex += Cartesian2.Rectangle.packedLength;
  803. var ellipsoid = Cartesian2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  804. startingIndex += Cartesian2.Ellipsoid.packedLength;
  805. var vertexFormat = VertexFormat.VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  806. startingIndex += VertexFormat.VertexFormat.packedLength;
  807. var granularity = array[startingIndex++];
  808. var surfaceHeight = array[startingIndex++];
  809. var rotation = array[startingIndex++];
  810. var stRotation = array[startingIndex++];
  811. var extrudedHeight = array[startingIndex++];
  812. var shadowVolume = array[startingIndex++] === 1.0;
  813. var offsetAttribute = array[startingIndex];
  814. if (!defined.defined(result)) {
  815. scratchOptions.granularity = granularity;
  816. scratchOptions.height = surfaceHeight;
  817. scratchOptions.rotation = rotation;
  818. scratchOptions.stRotation = stRotation;
  819. scratchOptions.extrudedHeight = extrudedHeight;
  820. scratchOptions.shadowVolume = shadowVolume;
  821. scratchOptions.offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  822. return new RectangleGeometry(scratchOptions);
  823. }
  824. result._rectangle = Cartesian2.Rectangle.clone(rectangle, result._rectangle);
  825. result._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  826. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  827. result._granularity = granularity;
  828. result._surfaceHeight = surfaceHeight;
  829. result._rotation = rotation;
  830. result._stRotation = stRotation;
  831. result._extrudedHeight = extrudedHeight;
  832. result._shadowVolume = shadowVolume;
  833. result._offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  834. return result;
  835. };
  836. /**
  837. * Computes the bounding rectangle based on the provided options
  838. *
  839. * @param {Object} options Object with the following properties:
  840. * @param {Rectangle} options.rectangle A cartographic rectangle with north, south, east and west properties in radians.
  841. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle lies.
  842. * @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.
  843. * @param {Number} [options.rotation=0.0] The rotation of the rectangle, in radians. A positive rotation is counter-clockwise.
  844. * @param {Rectangle} [result] An object in which to store the result.
  845. *
  846. * @returns {Rectangle} The result rectangle
  847. */
  848. RectangleGeometry.computeRectangle = function(options, result) {
  849. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  850. var rectangle = options.rectangle;
  851. //>>includeStart('debug', pragmas.debug);
  852. Check.Check.typeOf.object('rectangle', rectangle);
  853. Cartesian2.Rectangle.validate(rectangle);
  854. if (rectangle.north < rectangle.south) {
  855. throw new Check.DeveloperError('options.rectangle.north must be greater than or equal to options.rectangle.south');
  856. }
  857. //>>includeEnd('debug');
  858. var granularity = defaultValue.defaultValue(options.granularity, _Math.CesiumMath.RADIANS_PER_DEGREE);
  859. var ellipsoid = defaultValue.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84);
  860. var rotation = defaultValue.defaultValue(options.rotation, 0.0);
  861. return computeRectangle(rectangle, granularity, rotation, ellipsoid, result);
  862. };
  863. var tangentRotationMatrixScratch = new Transforms.Matrix3();
  864. var quaternionScratch = new Transforms.Quaternion();
  865. var centerScratch = new Cartesian2.Cartographic();
  866. /**
  867. * Computes the geometric representation of a rectangle, including its vertices, indices, and a bounding sphere.
  868. *
  869. * @param {RectangleGeometry} rectangleGeometry A description of the rectangle.
  870. * @returns {Geometry|undefined} The computed vertices and indices.
  871. *
  872. * @exception {DeveloperError} Rotated rectangle is invalid.
  873. */
  874. RectangleGeometry.createGeometry = function(rectangleGeometry) {
  875. if ((_Math.CesiumMath.equalsEpsilon(rectangleGeometry._rectangle.north, rectangleGeometry._rectangle.south, _Math.CesiumMath.EPSILON10) ||
  876. (_Math.CesiumMath.equalsEpsilon(rectangleGeometry._rectangle.east, rectangleGeometry._rectangle.west, _Math.CesiumMath.EPSILON10)))) {
  877. return undefined;
  878. }
  879. var rectangle = rectangleGeometry._rectangle;
  880. var ellipsoid = rectangleGeometry._ellipsoid;
  881. var rotation = rectangleGeometry._rotation;
  882. var stRotation = rectangleGeometry._stRotation;
  883. var vertexFormat = rectangleGeometry._vertexFormat;
  884. var computedOptions = RectangleGeometryLibrary.RectangleGeometryLibrary.computeOptions(rectangle, rectangleGeometry._granularity, rotation, stRotation, rectangleScratch, nwScratch, stNwScratch);
  885. var tangentRotationMatrix = tangentRotationMatrixScratch;
  886. if (stRotation !== 0 || rotation !== 0) {
  887. var center = Cartesian2.Rectangle.center(rectangle, centerScratch);
  888. var axis = ellipsoid.geodeticSurfaceNormalCartographic(center, v1Scratch);
  889. Transforms.Quaternion.fromAxisAngle(axis, -stRotation, quaternionScratch);
  890. Transforms.Matrix3.fromQuaternion(quaternionScratch, tangentRotationMatrix);
  891. } else {
  892. Transforms.Matrix3.clone(Transforms.Matrix3.IDENTITY, tangentRotationMatrix);
  893. }
  894. var surfaceHeight = rectangleGeometry._surfaceHeight;
  895. var extrudedHeight = rectangleGeometry._extrudedHeight;
  896. var extrude = !_Math.CesiumMath.equalsEpsilon(surfaceHeight, extrudedHeight, 0, _Math.CesiumMath.EPSILON2);
  897. computedOptions.lonScalar = 1.0 / rectangleGeometry._rectangle.width;
  898. computedOptions.latScalar = 1.0 / rectangleGeometry._rectangle.height;
  899. computedOptions.tangentRotationMatrix = tangentRotationMatrix;
  900. var geometry;
  901. var boundingSphere;
  902. rectangle = rectangleGeometry._rectangle;
  903. if (extrude) {
  904. geometry = constructExtrudedRectangle(rectangleGeometry, computedOptions);
  905. var topBS = Transforms.BoundingSphere.fromRectangle3D(rectangle, ellipsoid, surfaceHeight, topBoundingSphere);
  906. var bottomBS = Transforms.BoundingSphere.fromRectangle3D(rectangle, ellipsoid, extrudedHeight, bottomBoundingSphere);
  907. boundingSphere = Transforms.BoundingSphere.union(topBS, bottomBS);
  908. } else {
  909. geometry = constructRectangle(rectangleGeometry, computedOptions);
  910. geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(geometry.attributes.position.values, surfaceHeight, ellipsoid, false);
  911. if (defined.defined(rectangleGeometry._offsetAttribute)) {
  912. var length = geometry.attributes.position.values.length;
  913. var applyOffset = new Uint8Array(length / 3);
  914. var offsetValue = rectangleGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  915. GeometryOffsetAttribute.arrayFill(applyOffset, offsetValue);
  916. geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  917. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  918. componentsPerAttribute : 1,
  919. values: applyOffset
  920. });
  921. }
  922. boundingSphere = Transforms.BoundingSphere.fromRectangle3D(rectangle, ellipsoid, surfaceHeight);
  923. }
  924. if (!vertexFormat.position) {
  925. delete geometry.attributes.position;
  926. }
  927. return new GeometryAttribute.Geometry({
  928. attributes : geometry.attributes,
  929. indices : geometry.indices,
  930. primitiveType : geometry.primitiveType,
  931. boundingSphere : boundingSphere,
  932. offsetAttribute : rectangleGeometry._offsetAttribute
  933. });
  934. };
  935. /**
  936. * @private
  937. */
  938. RectangleGeometry.createShadowVolume = function(rectangleGeometry, minHeightFunc, maxHeightFunc) {
  939. var granularity = rectangleGeometry._granularity;
  940. var ellipsoid = rectangleGeometry._ellipsoid;
  941. var minHeight = minHeightFunc(granularity, ellipsoid);
  942. var maxHeight = maxHeightFunc(granularity, ellipsoid);
  943. return new RectangleGeometry({
  944. rectangle : rectangleGeometry._rectangle,
  945. rotation : rectangleGeometry._rotation,
  946. ellipsoid : ellipsoid,
  947. stRotation : rectangleGeometry._stRotation,
  948. granularity : granularity,
  949. extrudedHeight : maxHeight,
  950. height : minHeight,
  951. vertexFormat : VertexFormat.VertexFormat.POSITION_ONLY,
  952. shadowVolume : true
  953. });
  954. };
  955. var unrotatedTextureRectangleScratch = new Cartesian2.Rectangle();
  956. var points2DScratch = [new Cartesian2.Cartesian2(), new Cartesian2.Cartesian2(), new Cartesian2.Cartesian2()];
  957. var rotation2DScratch = new GeometryAttribute.Matrix2();
  958. var rectangleCenterScratch = new Cartesian2.Cartographic();
  959. function textureCoordinateRotationPoints(rectangleGeometry) {
  960. if (rectangleGeometry._stRotation === 0.0) {
  961. return [0, 0, 0, 1, 1, 0];
  962. }
  963. var rectangle = Cartesian2.Rectangle.clone(rectangleGeometry._rectangle, unrotatedTextureRectangleScratch);
  964. var granularity = rectangleGeometry._granularity;
  965. var ellipsoid = rectangleGeometry._ellipsoid;
  966. // Rotate to align the texture coordinates with ENU
  967. var rotation = rectangleGeometry._rotation - rectangleGeometry._stRotation;
  968. var unrotatedTextureRectangle = computeRectangle(rectangle, granularity, rotation, ellipsoid, unrotatedTextureRectangleScratch);
  969. // Assume a computed "east-north" texture coordinate system based on spherical or planar tricks, bounded by `boundingRectangle`.
  970. // The "desired" texture coordinate system forms an oriented rectangle (un-oriented computed) around the geometry that completely and tightly bounds it.
  971. // We want to map from the "east-north" texture coordinate system into the "desired" system using a pair of lines (analagous planes in 2D)
  972. // Compute 3 corners of the "desired" texture coordinate system in "east-north" texture space by the following in cartographic space:
  973. // - rotate 3 of the corners in unrotatedTextureRectangle by stRotation around the center of the bounding rectangle
  974. // - apply the "east-north" system's normalization formula to the rotated cartographics, even though this is likely to produce values outside [0-1].
  975. // 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."
  976. var points2D = points2DScratch;
  977. points2D[0].x = unrotatedTextureRectangle.west;
  978. points2D[0].y = unrotatedTextureRectangle.south;
  979. points2D[1].x = unrotatedTextureRectangle.west;
  980. points2D[1].y = unrotatedTextureRectangle.north;
  981. points2D[2].x = unrotatedTextureRectangle.east;
  982. points2D[2].y = unrotatedTextureRectangle.south;
  983. var boundingRectangle = rectangleGeometry.rectangle;
  984. var toDesiredInComputed = GeometryAttribute.Matrix2.fromRotation(rectangleGeometry._stRotation, rotation2DScratch);
  985. var boundingRectangleCenter = Cartesian2.Rectangle.center(boundingRectangle, rectangleCenterScratch);
  986. for (var i = 0; i < 3; ++i) {
  987. var point2D = points2D[i];
  988. point2D.x -= boundingRectangleCenter.longitude;
  989. point2D.y -= boundingRectangleCenter.latitude;
  990. GeometryAttribute.Matrix2.multiplyByVector(toDesiredInComputed, point2D, point2D);
  991. point2D.x += boundingRectangleCenter.longitude;
  992. point2D.y += boundingRectangleCenter.latitude;
  993. // Convert point into east-north texture coordinate space
  994. point2D.x = (point2D.x - boundingRectangle.west) / boundingRectangle.width;
  995. point2D.y = (point2D.y - boundingRectangle.south) / boundingRectangle.height;
  996. }
  997. var minXYCorner = points2D[0];
  998. var maxYCorner = points2D[1];
  999. var maxXCorner = points2D[2];
  1000. var result = new Array(6);
  1001. Cartesian2.Cartesian2.pack(minXYCorner, result);
  1002. Cartesian2.Cartesian2.pack(maxYCorner, result, 2);
  1003. Cartesian2.Cartesian2.pack(maxXCorner, result, 4);
  1004. return result;
  1005. }
  1006. defineProperties.defineProperties(RectangleGeometry.prototype, {
  1007. /**
  1008. * @private
  1009. */
  1010. rectangle : {
  1011. get : function() {
  1012. if (!defined.defined(this._rotatedRectangle)) {
  1013. this._rotatedRectangle = computeRectangle(this._rectangle, this._granularity, this._rotation, this._ellipsoid);
  1014. }
  1015. return this._rotatedRectangle;
  1016. }
  1017. },
  1018. /**
  1019. * For remapping texture coordinates when rendering RectangleGeometries as GroundPrimitives.
  1020. * This version permits skew in textures by computing offsets directly in cartographic space and
  1021. * more accurately approximates rendering RectangleGeometries with height as standard Primitives.
  1022. * @see Geometry#_textureCoordinateRotationPoints
  1023. * @private
  1024. */
  1025. textureCoordinateRotationPoints : {
  1026. get : function() {
  1027. if (!defined.defined(this._textureCoordinateRotationPoints)) {
  1028. this._textureCoordinateRotationPoints = textureCoordinateRotationPoints(this);
  1029. }
  1030. return this._textureCoordinateRotationPoints;
  1031. }
  1032. }
  1033. });
  1034. function createRectangleGeometry(rectangleGeometry, offset) {
  1035. if (defined.defined(offset)) {
  1036. rectangleGeometry = RectangleGeometry.unpack(rectangleGeometry, offset);
  1037. }
  1038. rectangleGeometry._ellipsoid = Cartesian2.Ellipsoid.clone(rectangleGeometry._ellipsoid);
  1039. rectangleGeometry._rectangle = Cartesian2.Rectangle.clone(rectangleGeometry._rectangle);
  1040. return RectangleGeometry.createGeometry(rectangleGeometry);
  1041. }
  1042. return createRectangleGeometry;
  1043. });