createRectangleGeometry.js 59 KB

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