CylinderGeometryLibrary.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import CesiumMath from "./Math.js";
  2. /**
  3. * @private
  4. */
  5. const CylinderGeometryLibrary = {};
  6. /**
  7. * @private
  8. */
  9. CylinderGeometryLibrary.computePositions = function (
  10. length,
  11. topRadius,
  12. bottomRadius,
  13. slices,
  14. fill
  15. ) {
  16. const topZ = length * 0.5;
  17. const bottomZ = -topZ;
  18. const twoSlice = slices + slices;
  19. const size = fill ? 2 * twoSlice : twoSlice;
  20. const positions = new Float64Array(size * 3);
  21. let i;
  22. let index = 0;
  23. let tbIndex = 0;
  24. const bottomOffset = fill ? twoSlice * 3 : 0;
  25. const topOffset = fill ? (twoSlice + slices) * 3 : slices * 3;
  26. for (i = 0; i < slices; i++) {
  27. const angle = (i / slices) * CesiumMath.TWO_PI;
  28. const x = Math.cos(angle);
  29. const y = Math.sin(angle);
  30. const bottomX = x * bottomRadius;
  31. const bottomY = y * bottomRadius;
  32. const topX = x * topRadius;
  33. const topY = y * topRadius;
  34. positions[tbIndex + bottomOffset] = bottomX;
  35. positions[tbIndex + bottomOffset + 1] = bottomY;
  36. positions[tbIndex + bottomOffset + 2] = bottomZ;
  37. positions[tbIndex + topOffset] = topX;
  38. positions[tbIndex + topOffset + 1] = topY;
  39. positions[tbIndex + topOffset + 2] = topZ;
  40. tbIndex += 3;
  41. if (fill) {
  42. positions[index++] = bottomX;
  43. positions[index++] = bottomY;
  44. positions[index++] = bottomZ;
  45. positions[index++] = topX;
  46. positions[index++] = topY;
  47. positions[index++] = topZ;
  48. }
  49. }
  50. return positions;
  51. };
  52. export default CylinderGeometryLibrary;