CorridorGeometry.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. import arrayFill from './arrayFill.js';
  2. import arrayRemoveDuplicates from './arrayRemoveDuplicates.js';
  3. import BoundingSphere from './BoundingSphere.js';
  4. import Cartesian3 from './Cartesian3.js';
  5. import Cartographic from './Cartographic.js';
  6. import Check from './Check.js';
  7. import ComponentDatatype from './ComponentDatatype.js';
  8. import CornerType from './CornerType.js';
  9. import CorridorGeometryLibrary from './CorridorGeometryLibrary.js';
  10. import defaultValue from './defaultValue.js';
  11. import defined from './defined.js';
  12. import defineProperties from './defineProperties.js';
  13. import Ellipsoid from './Ellipsoid.js';
  14. import Geometry from './Geometry.js';
  15. import GeometryAttribute from './GeometryAttribute.js';
  16. import GeometryAttributes from './GeometryAttributes.js';
  17. import GeometryOffsetAttribute from './GeometryOffsetAttribute.js';
  18. import IndexDatatype from './IndexDatatype.js';
  19. import CesiumMath from './Math.js';
  20. import PolygonPipeline from './PolygonPipeline.js';
  21. import PrimitiveType from './PrimitiveType.js';
  22. import Rectangle from './Rectangle.js';
  23. import VertexFormat from './VertexFormat.js';
  24. var cartesian1 = new Cartesian3();
  25. var cartesian2 = new Cartesian3();
  26. var cartesian3 = new Cartesian3();
  27. var cartesian4 = new Cartesian3();
  28. var cartesian5 = new Cartesian3();
  29. var cartesian6 = new Cartesian3();
  30. var scratch1 = new Cartesian3();
  31. var scratch2 = new Cartesian3();
  32. function scaleToSurface(positions, ellipsoid) {
  33. for (var i = 0; i < positions.length; i++) {
  34. positions[i] = ellipsoid.scaleToGeodeticSurface(positions[i], positions[i]);
  35. }
  36. return positions;
  37. }
  38. function addNormals(attr, normal, left, front, back, vertexFormat) {
  39. var normals = attr.normals;
  40. var tangents = attr.tangents;
  41. var bitangents = attr.bitangents;
  42. var forward = Cartesian3.normalize(Cartesian3.cross(left, normal, scratch1), scratch1);
  43. if (vertexFormat.normal) {
  44. CorridorGeometryLibrary.addAttribute(normals, normal, front, back);
  45. }
  46. if (vertexFormat.tangent) {
  47. CorridorGeometryLibrary.addAttribute(tangents, forward, front, back);
  48. }
  49. if (vertexFormat.bitangent) {
  50. CorridorGeometryLibrary.addAttribute(bitangents, left, front, back);
  51. }
  52. }
  53. function combine(computedPositions, vertexFormat, ellipsoid) {
  54. var positions = computedPositions.positions;
  55. var corners = computedPositions.corners;
  56. var endPositions = computedPositions.endPositions;
  57. var computedLefts = computedPositions.lefts;
  58. var computedNormals = computedPositions.normals;
  59. var attributes = new GeometryAttributes();
  60. var corner;
  61. var leftCount = 0;
  62. var rightCount = 0;
  63. var i;
  64. var indicesLength = 0;
  65. var length;
  66. for (i = 0; i < positions.length; i += 2) {
  67. length = positions[i].length - 3;
  68. leftCount += length; //subtracting 3 to account for duplicate points at corners
  69. indicesLength += length*2;
  70. rightCount += positions[i + 1].length - 3;
  71. }
  72. leftCount += 3; //add back count for end positions
  73. rightCount += 3;
  74. for (i = 0; i < corners.length; i++) {
  75. corner = corners[i];
  76. var leftSide = corners[i].leftPositions;
  77. if (defined(leftSide)) {
  78. length = leftSide.length;
  79. leftCount += length;
  80. indicesLength += length;
  81. } else {
  82. length = corners[i].rightPositions.length;
  83. rightCount += length;
  84. indicesLength += length;
  85. }
  86. }
  87. var addEndPositions = defined(endPositions);
  88. var endPositionLength;
  89. if (addEndPositions) {
  90. endPositionLength = endPositions[0].length - 3;
  91. leftCount += endPositionLength;
  92. rightCount += endPositionLength;
  93. endPositionLength /= 3;
  94. indicesLength += endPositionLength * 6;
  95. }
  96. var size = leftCount + rightCount;
  97. var finalPositions = new Float64Array(size);
  98. var normals = (vertexFormat.normal) ? new Float32Array(size) : undefined;
  99. var tangents = (vertexFormat.tangent) ? new Float32Array(size) : undefined;
  100. var bitangents = (vertexFormat.bitangent) ? new Float32Array(size) : undefined;
  101. var attr = {
  102. normals : normals,
  103. tangents : tangents,
  104. bitangents : bitangents
  105. };
  106. var front = 0;
  107. var back = size - 1;
  108. var UL, LL, UR, LR;
  109. var normal = cartesian1;
  110. var left = cartesian2;
  111. var rightPos, leftPos;
  112. var halfLength = endPositionLength / 2;
  113. var indices = IndexDatatype.createTypedArray(size / 3, indicesLength);
  114. var index = 0;
  115. if (addEndPositions) { // add rounded end
  116. leftPos = cartesian3;
  117. rightPos = cartesian4;
  118. var firstEndPositions = endPositions[0];
  119. normal = Cartesian3.fromArray(computedNormals, 0, normal);
  120. left = Cartesian3.fromArray(computedLefts, 0, left);
  121. for (i = 0; i < halfLength; i++) {
  122. leftPos = Cartesian3.fromArray(firstEndPositions, (halfLength - 1 - i) * 3, leftPos);
  123. rightPos = Cartesian3.fromArray(firstEndPositions, (halfLength + i) * 3, rightPos);
  124. CorridorGeometryLibrary.addAttribute(finalPositions, rightPos, front);
  125. CorridorGeometryLibrary.addAttribute(finalPositions, leftPos, undefined, back);
  126. addNormals(attr, normal, left, front, back, vertexFormat);
  127. LL = front / 3;
  128. LR = LL + 1;
  129. UL = (back - 2) / 3;
  130. UR = UL - 1;
  131. indices[index++] = UL;
  132. indices[index++] = LL;
  133. indices[index++] = UR;
  134. indices[index++] = UR;
  135. indices[index++] = LL;
  136. indices[index++] = LR;
  137. front += 3;
  138. back -= 3;
  139. }
  140. }
  141. var posIndex = 0;
  142. var compIndex = 0;
  143. var rightEdge = positions[posIndex++]; //add first two edges
  144. var leftEdge = positions[posIndex++];
  145. finalPositions.set(rightEdge, front);
  146. finalPositions.set(leftEdge, back - leftEdge.length + 1);
  147. left = Cartesian3.fromArray(computedLefts, compIndex, left);
  148. var rightNormal;
  149. var leftNormal;
  150. length = leftEdge.length - 3;
  151. for (i = 0; i < length; i += 3) {
  152. rightNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(rightEdge, i, scratch1), scratch1);
  153. leftNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(leftEdge, length - i, scratch2), scratch2);
  154. normal = Cartesian3.normalize(Cartesian3.add(rightNormal, leftNormal, normal), normal);
  155. addNormals(attr, normal, left, front, back, vertexFormat);
  156. LL = front / 3;
  157. LR = LL + 1;
  158. UL = (back - 2) / 3;
  159. UR = UL - 1;
  160. indices[index++] = UL;
  161. indices[index++] = LL;
  162. indices[index++] = UR;
  163. indices[index++] = UR;
  164. indices[index++] = LL;
  165. indices[index++] = LR;
  166. front += 3;
  167. back -= 3;
  168. }
  169. rightNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(rightEdge, length, scratch1), scratch1);
  170. leftNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(leftEdge, length, scratch2), scratch2);
  171. normal = Cartesian3.normalize(Cartesian3.add(rightNormal, leftNormal, normal), normal);
  172. compIndex += 3;
  173. for (i = 0; i < corners.length; i++) {
  174. var j;
  175. corner = corners[i];
  176. var l = corner.leftPositions;
  177. var r = corner.rightPositions;
  178. var pivot;
  179. var start;
  180. var outsidePoint = cartesian6;
  181. var previousPoint = cartesian3;
  182. var nextPoint = cartesian4;
  183. normal = Cartesian3.fromArray(computedNormals, compIndex, normal);
  184. if (defined(l)) {
  185. addNormals(attr, normal, left, undefined, back, vertexFormat);
  186. back -= 3;
  187. pivot = LR;
  188. start = UR;
  189. for (j = 0; j < l.length / 3; j++) {
  190. outsidePoint = Cartesian3.fromArray(l, j * 3, outsidePoint);
  191. indices[index++] = pivot;
  192. indices[index++] = start - j - 1;
  193. indices[index++] = start - j;
  194. CorridorGeometryLibrary.addAttribute(finalPositions, outsidePoint, undefined, back);
  195. previousPoint = Cartesian3.fromArray(finalPositions, (start - j - 1) * 3, previousPoint);
  196. nextPoint = Cartesian3.fromArray(finalPositions, pivot * 3, nextPoint);
  197. left = Cartesian3.normalize(Cartesian3.subtract(previousPoint, nextPoint, left), left);
  198. addNormals(attr, normal, left, undefined, back, vertexFormat);
  199. back -= 3;
  200. }
  201. outsidePoint = Cartesian3.fromArray(finalPositions, pivot * 3, outsidePoint);
  202. previousPoint = Cartesian3.subtract(Cartesian3.fromArray(finalPositions, (start) * 3, previousPoint), outsidePoint, previousPoint);
  203. nextPoint = Cartesian3.subtract(Cartesian3.fromArray(finalPositions, (start - j) * 3, nextPoint), outsidePoint, nextPoint);
  204. left = Cartesian3.normalize(Cartesian3.add(previousPoint, nextPoint, left), left);
  205. addNormals(attr, normal, left, front, undefined, vertexFormat);
  206. front += 3;
  207. } else {
  208. addNormals(attr, normal, left, front, undefined, vertexFormat);
  209. front += 3;
  210. pivot = UR;
  211. start = LR;
  212. for (j = 0; j < r.length / 3; j++) {
  213. outsidePoint = Cartesian3.fromArray(r, j * 3, outsidePoint);
  214. indices[index++] = pivot;
  215. indices[index++] = start + j;
  216. indices[index++] = start + j + 1;
  217. CorridorGeometryLibrary.addAttribute(finalPositions, outsidePoint, front);
  218. previousPoint = Cartesian3.fromArray(finalPositions, pivot * 3, previousPoint);
  219. nextPoint = Cartesian3.fromArray(finalPositions, (start + j) * 3, nextPoint);
  220. left = Cartesian3.normalize(Cartesian3.subtract(previousPoint, nextPoint, left), left);
  221. addNormals(attr, normal, left, front, undefined, vertexFormat);
  222. front += 3;
  223. }
  224. outsidePoint = Cartesian3.fromArray(finalPositions, pivot * 3, outsidePoint);
  225. previousPoint = Cartesian3.subtract(Cartesian3.fromArray(finalPositions, (start + j) * 3, previousPoint), outsidePoint, previousPoint);
  226. nextPoint = Cartesian3.subtract(Cartesian3.fromArray(finalPositions, start * 3, nextPoint), outsidePoint, nextPoint);
  227. left = Cartesian3.normalize(Cartesian3.negate(Cartesian3.add(nextPoint, previousPoint, left), left), left);
  228. addNormals(attr, normal, left, undefined, back, vertexFormat);
  229. back -= 3;
  230. }
  231. rightEdge = positions[posIndex++];
  232. leftEdge = positions[posIndex++];
  233. rightEdge.splice(0, 3); //remove duplicate points added by corner
  234. leftEdge.splice(leftEdge.length - 3, 3);
  235. finalPositions.set(rightEdge, front);
  236. finalPositions.set(leftEdge, back - leftEdge.length + 1);
  237. length = leftEdge.length - 3;
  238. compIndex += 3;
  239. left = Cartesian3.fromArray(computedLefts, compIndex, left);
  240. for (j = 0; j < leftEdge.length; j += 3) {
  241. rightNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(rightEdge, j, scratch1), scratch1);
  242. leftNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(leftEdge, length - j, scratch2), scratch2);
  243. normal = Cartesian3.normalize(Cartesian3.add(rightNormal, leftNormal, normal), normal);
  244. addNormals(attr, normal, left, front, back, vertexFormat);
  245. LR = front / 3;
  246. LL = LR - 1;
  247. UR = (back - 2) / 3;
  248. UL = UR + 1;
  249. indices[index++] = UL;
  250. indices[index++] = LL;
  251. indices[index++] = UR;
  252. indices[index++] = UR;
  253. indices[index++] = LL;
  254. indices[index++] = LR;
  255. front += 3;
  256. back -= 3;
  257. }
  258. front -= 3;
  259. back += 3;
  260. }
  261. normal = Cartesian3.fromArray(computedNormals, computedNormals.length - 3, normal);
  262. addNormals(attr, normal, left, front, back, vertexFormat);
  263. if (addEndPositions) { // add rounded end
  264. front += 3;
  265. back -= 3;
  266. leftPos = cartesian3;
  267. rightPos = cartesian4;
  268. var lastEndPositions = endPositions[1];
  269. for (i = 0; i < halfLength; i++) {
  270. leftPos = Cartesian3.fromArray(lastEndPositions, (endPositionLength - i - 1) * 3, leftPos);
  271. rightPos = Cartesian3.fromArray(lastEndPositions, i * 3, rightPos);
  272. CorridorGeometryLibrary.addAttribute(finalPositions, leftPos, undefined, back);
  273. CorridorGeometryLibrary.addAttribute(finalPositions, rightPos, front);
  274. addNormals(attr, normal, left, front, back, vertexFormat);
  275. LR = front / 3;
  276. LL = LR - 1;
  277. UR = (back - 2) / 3;
  278. UL = UR + 1;
  279. indices[index++] = UL;
  280. indices[index++] = LL;
  281. indices[index++] = UR;
  282. indices[index++] = UR;
  283. indices[index++] = LL;
  284. indices[index++] = LR;
  285. front += 3;
  286. back -= 3;
  287. }
  288. }
  289. attributes.position = new GeometryAttribute({
  290. componentDatatype : ComponentDatatype.DOUBLE,
  291. componentsPerAttribute : 3,
  292. values : finalPositions
  293. });
  294. if (vertexFormat.st) {
  295. var st = new Float32Array(size / 3 * 2);
  296. var rightSt;
  297. var leftSt;
  298. var stIndex = 0;
  299. if (addEndPositions) {
  300. leftCount /= 3;
  301. rightCount /= 3;
  302. var theta = Math.PI / (endPositionLength + 1);
  303. leftSt = 1 / (leftCount - endPositionLength + 1);
  304. rightSt = 1 / (rightCount - endPositionLength + 1);
  305. var a;
  306. var halfEndPos = endPositionLength / 2;
  307. for (i = halfEndPos + 1; i < endPositionLength + 1; i++) { // lower left rounded end
  308. a = CesiumMath.PI_OVER_TWO + theta * i;
  309. st[stIndex++] = rightSt * (1 + Math.cos(a));
  310. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  311. }
  312. for (i = 1; i < rightCount - endPositionLength + 1; i++) { // bottom edge
  313. st[stIndex++] = i * rightSt;
  314. st[stIndex++] = 0;
  315. }
  316. for (i = endPositionLength; i > halfEndPos; i--) { // lower right rounded end
  317. a = CesiumMath.PI_OVER_TWO - i * theta;
  318. st[stIndex++] = 1 - rightSt * (1 + Math.cos(a));
  319. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  320. }
  321. for (i = halfEndPos; i > 0; i--) { // upper right rounded end
  322. a = CesiumMath.PI_OVER_TWO - theta * i;
  323. st[stIndex++] = 1 - leftSt * (1 + Math.cos(a));
  324. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  325. }
  326. for (i = leftCount - endPositionLength; i > 0; i--) { // top edge
  327. st[stIndex++] = i * leftSt;
  328. st[stIndex++] = 1;
  329. }
  330. for (i = 1; i < halfEndPos + 1; i++) { // upper left rounded end
  331. a = CesiumMath.PI_OVER_TWO + theta * i;
  332. st[stIndex++] = leftSt * (1 + Math.cos(a));
  333. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  334. }
  335. } else {
  336. leftCount /= 3;
  337. rightCount /= 3;
  338. leftSt = 1 / (leftCount - 1);
  339. rightSt = 1 / (rightCount - 1);
  340. for (i = 0; i < rightCount; i++) { // bottom edge
  341. st[stIndex++] = i * rightSt;
  342. st[stIndex++] = 0;
  343. }
  344. for (i = leftCount; i > 0; i--) { // top edge
  345. st[stIndex++] = (i - 1) * leftSt;
  346. st[stIndex++] = 1;
  347. }
  348. }
  349. attributes.st = new GeometryAttribute({
  350. componentDatatype : ComponentDatatype.FLOAT,
  351. componentsPerAttribute : 2,
  352. values : st
  353. });
  354. }
  355. if (vertexFormat.normal) {
  356. attributes.normal = new GeometryAttribute({
  357. componentDatatype : ComponentDatatype.FLOAT,
  358. componentsPerAttribute : 3,
  359. values : attr.normals
  360. });
  361. }
  362. if (vertexFormat.tangent) {
  363. attributes.tangent = new GeometryAttribute({
  364. componentDatatype : ComponentDatatype.FLOAT,
  365. componentsPerAttribute : 3,
  366. values : attr.tangents
  367. });
  368. }
  369. if (vertexFormat.bitangent) {
  370. attributes.bitangent = new GeometryAttribute({
  371. componentDatatype : ComponentDatatype.FLOAT,
  372. componentsPerAttribute : 3,
  373. values : attr.bitangents
  374. });
  375. }
  376. return {
  377. attributes : attributes,
  378. indices : indices
  379. };
  380. }
  381. function extrudedAttributes(attributes, vertexFormat) {
  382. if (!vertexFormat.normal && !vertexFormat.tangent && !vertexFormat.bitangent && !vertexFormat.st) {
  383. return attributes;
  384. }
  385. var positions = attributes.position.values;
  386. var topNormals;
  387. var topBitangents;
  388. if (vertexFormat.normal || vertexFormat.bitangent) {
  389. topNormals = attributes.normal.values;
  390. topBitangents = attributes.bitangent.values;
  391. }
  392. var size = attributes.position.values.length / 18;
  393. var threeSize = size * 3;
  394. var twoSize = size * 2;
  395. var sixSize = threeSize * 2;
  396. var i;
  397. if (vertexFormat.normal || vertexFormat.bitangent || vertexFormat.tangent) {
  398. var normals = (vertexFormat.normal) ? new Float32Array(threeSize * 6) : undefined;
  399. var tangents = (vertexFormat.tangent) ? new Float32Array(threeSize * 6) : undefined;
  400. var bitangents = (vertexFormat.bitangent) ? new Float32Array(threeSize * 6) : undefined;
  401. var topPosition = cartesian1;
  402. var bottomPosition = cartesian2;
  403. var previousPosition = cartesian3;
  404. var normal = cartesian4;
  405. var tangent = cartesian5;
  406. var bitangent = cartesian6;
  407. var attrIndex = sixSize;
  408. for (i = 0; i < threeSize; i += 3) {
  409. var attrIndexOffset = attrIndex + sixSize;
  410. topPosition = Cartesian3.fromArray(positions, i, topPosition);
  411. bottomPosition = Cartesian3.fromArray(positions, i + threeSize, bottomPosition);
  412. previousPosition = Cartesian3.fromArray(positions, (i + 3) % threeSize, previousPosition);
  413. bottomPosition = Cartesian3.subtract(bottomPosition, topPosition, bottomPosition);
  414. previousPosition = Cartesian3.subtract(previousPosition, topPosition, previousPosition);
  415. normal = Cartesian3.normalize(Cartesian3.cross(bottomPosition, previousPosition, normal), normal);
  416. if (vertexFormat.normal) {
  417. CorridorGeometryLibrary.addAttribute(normals, normal, attrIndexOffset);
  418. CorridorGeometryLibrary.addAttribute(normals, normal, attrIndexOffset + 3);
  419. CorridorGeometryLibrary.addAttribute(normals, normal, attrIndex);
  420. CorridorGeometryLibrary.addAttribute(normals, normal, attrIndex + 3);
  421. }
  422. if (vertexFormat.tangent || vertexFormat.bitangent) {
  423. bitangent = Cartesian3.fromArray(topNormals, i, bitangent);
  424. if (vertexFormat.bitangent) {
  425. CorridorGeometryLibrary.addAttribute(bitangents, bitangent, attrIndexOffset);
  426. CorridorGeometryLibrary.addAttribute(bitangents, bitangent, attrIndexOffset + 3);
  427. CorridorGeometryLibrary.addAttribute(bitangents, bitangent, attrIndex);
  428. CorridorGeometryLibrary.addAttribute(bitangents, bitangent, attrIndex + 3);
  429. }
  430. if (vertexFormat.tangent) {
  431. tangent = Cartesian3.normalize(Cartesian3.cross(bitangent, normal, tangent), tangent);
  432. CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndexOffset);
  433. CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndexOffset + 3);
  434. CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndex);
  435. CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndex + 3);
  436. }
  437. }
  438. attrIndex += 6;
  439. }
  440. if (vertexFormat.normal) {
  441. normals.set(topNormals); //top
  442. for (i = 0; i < threeSize; i += 3) { //bottom normals
  443. normals[i + threeSize] = -topNormals[i];
  444. normals[i + threeSize + 1] = -topNormals[i + 1];
  445. normals[i + threeSize + 2] = -topNormals[i + 2];
  446. }
  447. attributes.normal.values = normals;
  448. } else {
  449. attributes.normal = undefined;
  450. }
  451. if (vertexFormat.bitangent) {
  452. bitangents.set(topBitangents); //top
  453. bitangents.set(topBitangents, threeSize); //bottom
  454. attributes.bitangent.values = bitangents;
  455. } else {
  456. attributes.bitangent = undefined;
  457. }
  458. if (vertexFormat.tangent) {
  459. var topTangents = attributes.tangent.values;
  460. tangents.set(topTangents); //top
  461. tangents.set(topTangents, threeSize); //bottom
  462. attributes.tangent.values = tangents;
  463. }
  464. }
  465. if (vertexFormat.st) {
  466. var topSt = attributes.st.values;
  467. var st = new Float32Array(twoSize * 6);
  468. st.set(topSt); //top
  469. st.set(topSt, twoSize); //bottom
  470. var index = twoSize * 2;
  471. for ( var j = 0; j < 2; j++) {
  472. st[index++] = topSt[0];
  473. st[index++] = topSt[1];
  474. for (i = 2; i < twoSize; i += 2) {
  475. var s = topSt[i];
  476. var t = topSt[i + 1];
  477. st[index++] = s;
  478. st[index++] = t;
  479. st[index++] = s;
  480. st[index++] = t;
  481. }
  482. st[index++] = topSt[0];
  483. st[index++] = topSt[1];
  484. }
  485. attributes.st.values = st;
  486. }
  487. return attributes;
  488. }
  489. function addWallPositions(positions, index, wallPositions) {
  490. wallPositions[index++] = positions[0];
  491. wallPositions[index++] = positions[1];
  492. wallPositions[index++] = positions[2];
  493. for ( var i = 3; i < positions.length; i += 3) {
  494. var x = positions[i];
  495. var y = positions[i + 1];
  496. var z = positions[i + 2];
  497. wallPositions[index++] = x;
  498. wallPositions[index++] = y;
  499. wallPositions[index++] = z;
  500. wallPositions[index++] = x;
  501. wallPositions[index++] = y;
  502. wallPositions[index++] = z;
  503. }
  504. wallPositions[index++] = positions[0];
  505. wallPositions[index++] = positions[1];
  506. wallPositions[index++] = positions[2];
  507. return wallPositions;
  508. }
  509. function computePositionsExtruded(params, vertexFormat) {
  510. var topVertexFormat = new VertexFormat({
  511. position : vertexFormat.position,
  512. normal : (vertexFormat.normal || vertexFormat.bitangent || params.shadowVolume),
  513. tangent : vertexFormat.tangent,
  514. bitangent : (vertexFormat.normal || vertexFormat.bitangent),
  515. st : vertexFormat.st
  516. });
  517. var ellipsoid = params.ellipsoid;
  518. var computedPositions = CorridorGeometryLibrary.computePositions(params);
  519. var attr = combine(computedPositions, topVertexFormat, ellipsoid);
  520. var height = params.height;
  521. var extrudedHeight = params.extrudedHeight;
  522. var attributes = attr.attributes;
  523. var indices = attr.indices;
  524. var positions = attributes.position.values;
  525. var length = positions.length;
  526. var newPositions = new Float64Array(length * 6);
  527. var extrudedPositions = new Float64Array(length);
  528. extrudedPositions.set(positions);
  529. var wallPositions = new Float64Array(length * 4);
  530. positions = PolygonPipeline.scaleToGeodeticHeight(positions, height, ellipsoid);
  531. wallPositions = addWallPositions(positions, 0, wallPositions);
  532. extrudedPositions = PolygonPipeline.scaleToGeodeticHeight(extrudedPositions, extrudedHeight, ellipsoid);
  533. wallPositions = addWallPositions(extrudedPositions, length * 2, wallPositions);
  534. newPositions.set(positions);
  535. newPositions.set(extrudedPositions, length);
  536. newPositions.set(wallPositions, length * 2);
  537. attributes.position.values = newPositions;
  538. attributes = extrudedAttributes(attributes, vertexFormat);
  539. var i;
  540. var size = length / 3;
  541. if (params.shadowVolume) {
  542. var topNormals = attributes.normal.values;
  543. length = topNormals.length;
  544. var extrudeNormals = new Float32Array(length * 6);
  545. for (i = 0; i < length; i ++) {
  546. topNormals[i] = -topNormals[i];
  547. }
  548. //only get normals for bottom layer that's going to be pushed down
  549. extrudeNormals.set(topNormals, length); //bottom face
  550. extrudeNormals = addWallPositions(topNormals, length*4, extrudeNormals); //bottom wall
  551. attributes.extrudeDirection = new GeometryAttribute({
  552. componentDatatype : ComponentDatatype.FLOAT,
  553. componentsPerAttribute : 3,
  554. values : extrudeNormals
  555. });
  556. if (!vertexFormat.normal) {
  557. attributes.normal = undefined;
  558. }
  559. }
  560. if (defined(params.offsetAttribute)) {
  561. var applyOffset = new Uint8Array(size * 6);
  562. if (params.offsetAttribute === GeometryOffsetAttribute.TOP) {
  563. applyOffset = arrayFill(applyOffset, 1, 0, size); // top face
  564. applyOffset = arrayFill(applyOffset, 1, size*2, size * 4); // top wall
  565. } else {
  566. var applyOffsetValue = params.offsetAttribute === GeometryOffsetAttribute.NONE ? 0 : 1;
  567. applyOffset = arrayFill(applyOffset, applyOffsetValue);
  568. }
  569. attributes.applyOffset = new GeometryAttribute({
  570. componentDatatype : ComponentDatatype.UNSIGNED_BYTE,
  571. componentsPerAttribute : 1,
  572. values: applyOffset
  573. });
  574. }
  575. var iLength = indices.length;
  576. var twoSize = size + size;
  577. var newIndices = IndexDatatype.createTypedArray(newPositions.length / 3, iLength * 2 + twoSize * 3);
  578. newIndices.set(indices);
  579. var index = iLength;
  580. for (i = 0; i < iLength; i += 3) { // bottom indices
  581. var v0 = indices[i];
  582. var v1 = indices[i + 1];
  583. var v2 = indices[i + 2];
  584. newIndices[index++] = v2 + size;
  585. newIndices[index++] = v1 + size;
  586. newIndices[index++] = v0 + size;
  587. }
  588. var UL, LL, UR, LR;
  589. for (i = 0; i < twoSize; i += 2) { //wall indices
  590. UL = i + twoSize;
  591. LL = UL + twoSize;
  592. UR = UL + 1;
  593. LR = LL + 1;
  594. newIndices[index++] = UL;
  595. newIndices[index++] = LL;
  596. newIndices[index++] = UR;
  597. newIndices[index++] = UR;
  598. newIndices[index++] = LL;
  599. newIndices[index++] = LR;
  600. }
  601. return {
  602. attributes : attributes,
  603. indices : newIndices
  604. };
  605. }
  606. var scratchCartesian1 = new Cartesian3();
  607. var scratchCartesian2 = new Cartesian3();
  608. var scratchCartographic = new Cartographic();
  609. function computeOffsetPoints(position1, position2, ellipsoid, halfWidth, min, max) {
  610. // Compute direction of offset the point
  611. var direction = Cartesian3.subtract(position2, position1, scratchCartesian1);
  612. Cartesian3.normalize(direction, direction);
  613. var normal = ellipsoid.geodeticSurfaceNormal(position1, scratchCartesian2);
  614. var offsetDirection = Cartesian3.cross(direction, normal, scratchCartesian1);
  615. Cartesian3.multiplyByScalar(offsetDirection, halfWidth, offsetDirection);
  616. var minLat = min.latitude;
  617. var minLon = min.longitude;
  618. var maxLat = max.latitude;
  619. var maxLon = max.longitude;
  620. // Compute 2 offset points
  621. Cartesian3.add(position1, offsetDirection, scratchCartesian2);
  622. ellipsoid.cartesianToCartographic(scratchCartesian2, scratchCartographic);
  623. var lat = scratchCartographic.latitude;
  624. var lon = scratchCartographic.longitude;
  625. minLat = Math.min(minLat, lat);
  626. minLon = Math.min(minLon, lon);
  627. maxLat = Math.max(maxLat, lat);
  628. maxLon = Math.max(maxLon, lon);
  629. Cartesian3.subtract(position1, offsetDirection, scratchCartesian2);
  630. ellipsoid.cartesianToCartographic(scratchCartesian2, scratchCartographic);
  631. lat = scratchCartographic.latitude;
  632. lon = scratchCartographic.longitude;
  633. minLat = Math.min(minLat, lat);
  634. minLon = Math.min(minLon, lon);
  635. maxLat = Math.max(maxLat, lat);
  636. maxLon = Math.max(maxLon, lon);
  637. min.latitude = minLat;
  638. min.longitude = minLon;
  639. max.latitude = maxLat;
  640. max.longitude = maxLon;
  641. }
  642. var scratchCartesianOffset = new Cartesian3();
  643. var scratchCartesianEnds = new Cartesian3();
  644. var scratchCartographicMin = new Cartographic();
  645. var scratchCartographicMax = new Cartographic();
  646. function computeRectangle(positions, ellipsoid, width, cornerType, result) {
  647. positions = scaleToSurface(positions, ellipsoid);
  648. var cleanPositions = arrayRemoveDuplicates(positions, Cartesian3.equalsEpsilon);
  649. var length = cleanPositions.length;
  650. if (length < 2 || width <= 0) {
  651. return new Rectangle();
  652. }
  653. var halfWidth = width * 0.5;
  654. scratchCartographicMin.latitude = Number.POSITIVE_INFINITY;
  655. scratchCartographicMin.longitude = Number.POSITIVE_INFINITY;
  656. scratchCartographicMax.latitude = Number.NEGATIVE_INFINITY;
  657. scratchCartographicMax.longitude = Number.NEGATIVE_INFINITY;
  658. var lat, lon;
  659. if (cornerType === CornerType.ROUNDED) {
  660. // Compute start cap
  661. var first = cleanPositions[0];
  662. Cartesian3.subtract(first, cleanPositions[1], scratchCartesianOffset);
  663. Cartesian3.normalize(scratchCartesianOffset, scratchCartesianOffset);
  664. Cartesian3.multiplyByScalar(scratchCartesianOffset, halfWidth, scratchCartesianOffset);
  665. Cartesian3.add(first, scratchCartesianOffset, scratchCartesianEnds);
  666. ellipsoid.cartesianToCartographic(scratchCartesianEnds, scratchCartographic);
  667. lat = scratchCartographic.latitude;
  668. lon = scratchCartographic.longitude;
  669. scratchCartographicMin.latitude = Math.min(scratchCartographicMin.latitude, lat);
  670. scratchCartographicMin.longitude = Math.min(scratchCartographicMin.longitude, lon);
  671. scratchCartographicMax.latitude = Math.max(scratchCartographicMax.latitude, lat);
  672. scratchCartographicMax.longitude = Math.max(scratchCartographicMax.longitude, lon);
  673. }
  674. // Compute the rest
  675. for (var i = 0; i < length-1; ++i) {
  676. computeOffsetPoints(cleanPositions[i], cleanPositions[i+1], ellipsoid, halfWidth,
  677. scratchCartographicMin, scratchCartographicMax);
  678. }
  679. // Compute ending point
  680. var last = cleanPositions[length-1];
  681. Cartesian3.subtract(last, cleanPositions[length-2], scratchCartesianOffset);
  682. Cartesian3.normalize(scratchCartesianOffset, scratchCartesianOffset);
  683. Cartesian3.multiplyByScalar(scratchCartesianOffset, halfWidth, scratchCartesianOffset);
  684. Cartesian3.add(last, scratchCartesianOffset, scratchCartesianEnds);
  685. computeOffsetPoints(last, scratchCartesianEnds, ellipsoid, halfWidth,
  686. scratchCartographicMin, scratchCartographicMax);
  687. if (cornerType === CornerType.ROUNDED) {
  688. // Compute end cap
  689. ellipsoid.cartesianToCartographic(scratchCartesianEnds, scratchCartographic);
  690. lat = scratchCartographic.latitude;
  691. lon = scratchCartographic.longitude;
  692. scratchCartographicMin.latitude = Math.min(scratchCartographicMin.latitude, lat);
  693. scratchCartographicMin.longitude = Math.min(scratchCartographicMin.longitude, lon);
  694. scratchCartographicMax.latitude = Math.max(scratchCartographicMax.latitude, lat);
  695. scratchCartographicMax.longitude = Math.max(scratchCartographicMax.longitude, lon);
  696. }
  697. var rectangle = defined(result) ? result : new Rectangle();
  698. rectangle.north = scratchCartographicMax.latitude;
  699. rectangle.south = scratchCartographicMin.latitude;
  700. rectangle.east = scratchCartographicMax.longitude;
  701. rectangle.west = scratchCartographicMin.longitude;
  702. return rectangle;
  703. }
  704. /**
  705. * A description of a corridor. Corridor geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  706. *
  707. * @alias CorridorGeometry
  708. * @constructor
  709. *
  710. * @param {Object} options Object with the following properties:
  711. * @param {Cartesian3[]} options.positions An array of positions that define the center of the corridor.
  712. * @param {Number} options.width The distance between the edges of the corridor in meters.
  713. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  714. * @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.
  715. * @param {Number} [options.height=0] The distance in meters between the ellipsoid surface and the positions.
  716. * @param {Number} [options.extrudedHeight] The distance in meters between the ellipsoid surface and the extruded face.
  717. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  718. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  719. *
  720. * @see CorridorGeometry.createGeometry
  721. * @see Packable
  722. *
  723. * @demo {@link https://sandcastle.cesium.com/index.html?src=Corridor.html|Cesium Sandcastle Corridor Demo}
  724. *
  725. * @example
  726. * var corridor = new Cesium.CorridorGeometry({
  727. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY,
  728. * positions : Cesium.Cartesian3.fromDegreesArray([-72.0, 40.0, -70.0, 35.0]),
  729. * width : 100000
  730. * });
  731. */
  732. function CorridorGeometry(options) {
  733. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  734. var positions = options.positions;
  735. var width = options.width;
  736. //>>includeStart('debug', pragmas.debug);
  737. Check.defined('options.positions', positions);
  738. Check.defined('options.width', width);
  739. //>>includeEnd('debug');
  740. var height = defaultValue(options.height, 0.0);
  741. var extrudedHeight = defaultValue(options.extrudedHeight, height);
  742. this._positions = positions;
  743. this._ellipsoid = Ellipsoid.clone(defaultValue(options.ellipsoid, Ellipsoid.WGS84));
  744. this._vertexFormat = VertexFormat.clone(defaultValue(options.vertexFormat, VertexFormat.DEFAULT));
  745. this._width = width;
  746. this._height = Math.max(height, extrudedHeight);
  747. this._extrudedHeight = Math.min(height, extrudedHeight);
  748. this._cornerType = defaultValue(options.cornerType, CornerType.ROUNDED);
  749. this._granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
  750. this._shadowVolume = defaultValue(options.shadowVolume, false);
  751. this._workerName = 'createCorridorGeometry';
  752. this._offsetAttribute = options.offsetAttribute;
  753. this._rectangle = undefined;
  754. /**
  755. * The number of elements used to pack the object into an array.
  756. * @type {Number}
  757. */
  758. this.packedLength = 1 + positions.length * Cartesian3.packedLength + Ellipsoid.packedLength + VertexFormat.packedLength + 7;
  759. }
  760. /**
  761. * Stores the provided instance into the provided array.
  762. *
  763. * @param {CorridorGeometry} value The value to pack.
  764. * @param {Number[]} array The array to pack into.
  765. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  766. *
  767. * @returns {Number[]} The array that was packed into
  768. */
  769. CorridorGeometry.pack = function(value, array, startingIndex) {
  770. //>>includeStart('debug', pragmas.debug);
  771. Check.defined('value', value);
  772. Check.defined('array', array);
  773. //>>includeEnd('debug');
  774. startingIndex = defaultValue(startingIndex, 0);
  775. var positions = value._positions;
  776. var length = positions.length;
  777. array[startingIndex++] = length;
  778. for (var i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  779. Cartesian3.pack(positions[i], array, startingIndex);
  780. }
  781. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  782. startingIndex += Ellipsoid.packedLength;
  783. VertexFormat.pack(value._vertexFormat, array, startingIndex);
  784. startingIndex += VertexFormat.packedLength;
  785. array[startingIndex++] = value._width;
  786. array[startingIndex++] = value._height;
  787. array[startingIndex++] = value._extrudedHeight;
  788. array[startingIndex++] = value._cornerType;
  789. array[startingIndex++] = value._granularity;
  790. array[startingIndex++] = value._shadowVolume ? 1.0 : 0.0;
  791. array[startingIndex] = defaultValue(value._offsetAttribute, -1);
  792. return array;
  793. };
  794. var scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  795. var scratchVertexFormat = new VertexFormat();
  796. var scratchOptions = {
  797. positions : undefined,
  798. ellipsoid : scratchEllipsoid,
  799. vertexFormat : scratchVertexFormat,
  800. width : undefined,
  801. height : undefined,
  802. extrudedHeight : undefined,
  803. cornerType : undefined,
  804. granularity : undefined,
  805. shadowVolume: undefined,
  806. offsetAttribute: undefined
  807. };
  808. /**
  809. * Retrieves an instance from a packed array.
  810. *
  811. * @param {Number[]} array The packed array.
  812. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  813. * @param {CorridorGeometry} [result] The object into which to store the result.
  814. * @returns {CorridorGeometry} The modified result parameter or a new CorridorGeometry instance if one was not provided.
  815. */
  816. CorridorGeometry.unpack = function(array, startingIndex, result) {
  817. //>>includeStart('debug', pragmas.debug);
  818. Check.defined('array', array);
  819. //>>includeEnd('debug');
  820. startingIndex = defaultValue(startingIndex, 0);
  821. var length = array[startingIndex++];
  822. var positions = new Array(length);
  823. for (var i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  824. positions[i] = Cartesian3.unpack(array, startingIndex);
  825. }
  826. var ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  827. startingIndex += Ellipsoid.packedLength;
  828. var vertexFormat = VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  829. startingIndex += VertexFormat.packedLength;
  830. var width = array[startingIndex++];
  831. var height = array[startingIndex++];
  832. var extrudedHeight = array[startingIndex++];
  833. var cornerType = array[startingIndex++];
  834. var granularity = array[startingIndex++];
  835. var shadowVolume = array[startingIndex++] === 1.0;
  836. var offsetAttribute = array[startingIndex];
  837. if (!defined(result)) {
  838. scratchOptions.positions = positions;
  839. scratchOptions.width = width;
  840. scratchOptions.height = height;
  841. scratchOptions.extrudedHeight = extrudedHeight;
  842. scratchOptions.cornerType = cornerType;
  843. scratchOptions.granularity = granularity;
  844. scratchOptions.shadowVolume = shadowVolume;
  845. scratchOptions.offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  846. return new CorridorGeometry(scratchOptions);
  847. }
  848. result._positions = positions;
  849. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  850. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  851. result._width = width;
  852. result._height = height;
  853. result._extrudedHeight = extrudedHeight;
  854. result._cornerType = cornerType;
  855. result._granularity = granularity;
  856. result._shadowVolume = shadowVolume;
  857. result._offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  858. return result;
  859. };
  860. /**
  861. * Computes the bounding rectangle given the provided options
  862. *
  863. * @param {Object} options Object with the following properties:
  864. * @param {Cartesian3[]} options.positions An array of positions that define the center of the corridor.
  865. * @param {Number} options.width The distance between the edges of the corridor in meters.
  866. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  867. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  868. * @param {Rectangle} [result] An object in which to store the result.
  869. *
  870. * @returns {Rectangle} The result rectangle.
  871. */
  872. CorridorGeometry.computeRectangle = function(options, result) {
  873. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  874. var positions = options.positions;
  875. var width = options.width;
  876. //>>includeStart('debug', pragmas.debug);
  877. Check.defined('options.positions', positions);
  878. Check.defined('options.width', width);
  879. //>>includeEnd('debug');
  880. var ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  881. var cornerType = defaultValue(options.cornerType, CornerType.ROUNDED);
  882. return computeRectangle(positions, ellipsoid, width, cornerType, result);
  883. };
  884. /**
  885. * Computes the geometric representation of a corridor, including its vertices, indices, and a bounding sphere.
  886. *
  887. * @param {CorridorGeometry} corridorGeometry A description of the corridor.
  888. * @returns {Geometry|undefined} The computed vertices and indices.
  889. */
  890. CorridorGeometry.createGeometry = function(corridorGeometry) {
  891. var positions = corridorGeometry._positions;
  892. var width = corridorGeometry._width;
  893. var ellipsoid = corridorGeometry._ellipsoid;
  894. positions = scaleToSurface(positions, ellipsoid);
  895. var cleanPositions = arrayRemoveDuplicates(positions, Cartesian3.equalsEpsilon);
  896. if ((cleanPositions.length < 2) || (width <= 0)) {
  897. return;
  898. }
  899. var height = corridorGeometry._height;
  900. var extrudedHeight = corridorGeometry._extrudedHeight;
  901. var extrude = !CesiumMath.equalsEpsilon(height, extrudedHeight, 0, CesiumMath.EPSILON2);
  902. var vertexFormat = corridorGeometry._vertexFormat;
  903. var params = {
  904. ellipsoid : ellipsoid,
  905. positions : cleanPositions,
  906. width : width,
  907. cornerType : corridorGeometry._cornerType,
  908. granularity : corridorGeometry._granularity,
  909. saveAttributes: true
  910. };
  911. var attr;
  912. if (extrude) {
  913. params.height = height;
  914. params.extrudedHeight = extrudedHeight;
  915. params.shadowVolume = corridorGeometry._shadowVolume;
  916. params.offsetAttribute = corridorGeometry._offsetAttribute;
  917. attr = computePositionsExtruded(params, vertexFormat);
  918. } else {
  919. var computedPositions = CorridorGeometryLibrary.computePositions(params);
  920. attr = combine(computedPositions, vertexFormat, ellipsoid);
  921. attr.attributes.position.values = PolygonPipeline.scaleToGeodeticHeight(attr.attributes.position.values, height, ellipsoid);
  922. if (defined(corridorGeometry._offsetAttribute)) {
  923. var applyOffsetValue = corridorGeometry._offsetAttribute === GeometryOffsetAttribute.NONE ? 0 : 1;
  924. var length = attr.attributes.position.values.length;
  925. var applyOffset = new Uint8Array(length / 3);
  926. arrayFill(applyOffset, applyOffsetValue);
  927. attr.attributes.applyOffset = new GeometryAttribute({
  928. componentDatatype : ComponentDatatype.UNSIGNED_BYTE,
  929. componentsPerAttribute : 1,
  930. values: applyOffset
  931. });
  932. }
  933. }
  934. var attributes = attr.attributes;
  935. var boundingSphere = BoundingSphere.fromVertices(attributes.position.values, undefined, 3);
  936. if (!vertexFormat.position) {
  937. attr.attributes.position.values = undefined;
  938. }
  939. return new Geometry({
  940. attributes : attributes,
  941. indices : attr.indices,
  942. primitiveType : PrimitiveType.TRIANGLES,
  943. boundingSphere : boundingSphere,
  944. offsetAttribute : corridorGeometry._offsetAttribute
  945. });
  946. };
  947. /**
  948. * @private
  949. */
  950. CorridorGeometry.createShadowVolume = function(corridorGeometry, minHeightFunc, maxHeightFunc) {
  951. var granularity = corridorGeometry._granularity;
  952. var ellipsoid = corridorGeometry._ellipsoid;
  953. var minHeight = minHeightFunc(granularity, ellipsoid);
  954. var maxHeight = maxHeightFunc(granularity, ellipsoid);
  955. return new CorridorGeometry({
  956. positions : corridorGeometry._positions,
  957. width : corridorGeometry._width,
  958. cornerType : corridorGeometry._cornerType,
  959. ellipsoid : ellipsoid,
  960. granularity : granularity,
  961. extrudedHeight : minHeight,
  962. height : maxHeight,
  963. vertexFormat : VertexFormat.POSITION_ONLY,
  964. shadowVolume: true
  965. });
  966. };
  967. defineProperties(CorridorGeometry.prototype, {
  968. /**
  969. * @private
  970. */
  971. rectangle : {
  972. get : function() {
  973. if (!defined(this._rectangle)) {
  974. this._rectangle = computeRectangle(this._positions, this._ellipsoid, this._width, this._cornerType);
  975. }
  976. return this._rectangle;
  977. }
  978. },
  979. /**
  980. * For remapping texture coordinates when rendering CorridorGeometries as GroundPrimitives.
  981. *
  982. * Corridors don't support stRotation,
  983. * so just return the corners of the original system.
  984. * @private
  985. */
  986. textureCoordinateRotationPoints : {
  987. get : function() {
  988. return [0, 0, 0, 1, 1, 0];
  989. }
  990. }
  991. });
  992. export default CorridorGeometry;