createVerticesFromHeightmap.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import Ellipsoid from "../Core/Ellipsoid.js";
  2. import HeightmapEncoding from "../Core/HeightmapEncoding.js";
  3. import HeightmapTessellator from "../Core/HeightmapTessellator.js";
  4. import Rectangle from "../Core/Rectangle.js";
  5. import RuntimeError from "../Core/RuntimeError.js";
  6. import Lerc from "lerc";
  7. import createTaskProcessorWorker from "./createTaskProcessorWorker.js";
  8. function createVerticesFromHeightmap(parameters, transferableObjects) {
  9. // LERC encoded buffers must be decoded, then we can process them like normal
  10. if (parameters.encoding === HeightmapEncoding.LERC) {
  11. let result;
  12. try {
  13. result = Lerc.decode(parameters.heightmap);
  14. } catch (error) {
  15. throw new RuntimeError(error);
  16. }
  17. const lercStatistics = result.statistics[0];
  18. if (lercStatistics.minValue === Number.MAX_VALUE) {
  19. throw new RuntimeError("Invalid tile data");
  20. }
  21. parameters.heightmap = result.pixels[0];
  22. parameters.width = result.width;
  23. parameters.height = result.height;
  24. }
  25. parameters.ellipsoid = Ellipsoid.clone(parameters.ellipsoid);
  26. parameters.rectangle = Rectangle.clone(parameters.rectangle);
  27. const statistics = HeightmapTessellator.computeVertices(parameters);
  28. const vertices = statistics.vertices;
  29. transferableObjects.push(vertices.buffer);
  30. return {
  31. vertices: vertices.buffer,
  32. numberOfAttributes: statistics.encoding.stride,
  33. minimumHeight: statistics.minimumHeight,
  34. maximumHeight: statistics.maximumHeight,
  35. gridWidth: parameters.width,
  36. gridHeight: parameters.height,
  37. boundingSphere3D: statistics.boundingSphere3D,
  38. orientedBoundingBox: statistics.orientedBoundingBox,
  39. occludeePointInScaledSpace: statistics.occludeePointInScaledSpace,
  40. encoding: statistics.encoding,
  41. westIndicesSouthToNorth: statistics.westIndicesSouthToNorth,
  42. southIndicesEastToWest: statistics.southIndicesEastToWest,
  43. eastIndicesNorthToSouth: statistics.eastIndicesNorthToSouth,
  44. northIndicesWestToEast: statistics.northIndicesWestToEast,
  45. };
  46. }
  47. export default createTaskProcessorWorker(createVerticesFromHeightmap);