Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom heightmap terrain provider #9604

Merged
merged 8 commits into from
Jun 18, 2021
Merged

Conversation

IanLilleyT
Copy link
Contributor

This PR adds CustomHeightmapTerrainProvider, a TerrainProvider that lets you set height values inside a callback function. This way you can create procedural terrain very easily without having to create a whole new TerrainProvider subclass. However, there are some limitations such as no water mask, no vertex normals, and no availability, so a full-fledged TerrainProvider is still the right thing to do a lot of the time.

One example: Procedural terrain in CustomHeightmapTerrainProvider dropdown of the Terrain Sandcastle (localhost).

Screenshot from 2021-06-08 22-15-43

Another example: Sine wave

Screenshot from 2021-06-08 23-15-06
Local Sandcastle

This is the code that does it:

var width = 32;
var height = 32;

var viewer = new Cesium.Viewer("cesiumContainer", {
  terrainProvider: new Cesium.CustomHeightmapTerrainProvider({
    callback: function (x, y, level) {
      var buffer = new Float32Array(width * height);
      for (var yy = 0; yy < height; yy++) {
        for (var xx = 0; xx < width; xx++) {
          var v = (y + yy / (height - 1)) / Math.pow(2, level);
          var heightValue = 8000 * (Math.sin(4000 * v) * 0.5 + 0.5);
          var index = yy * width + xx;
          buffer[index] = heightValue;
        }
      }
      return buffer;
    },
    width: width,
    height: height,
  }),
});

@cesium-concierge
Copy link

Thanks for the pull request @IanLilleyT!

  • ✔️ Signed CLA found.

Reviewers, don't forget to make sure that:

  • Cesium Viewer works.
  • Works in 2D/CV.
  • Works (or fails gracefully) in IE11.

CHANGES.md Outdated Show resolved Hide resolved
Source/Core/CustomHeightmapTerrainProvider.js Outdated Show resolved Hide resolved
Source/Core/CustomHeightmapTerrainProvider.js Outdated Show resolved Hide resolved
Apps/Sandcastle/gallery/Terrain.html Show resolved Hide resolved
Source/Core/CustomHeightmapTerrainProvider.js Show resolved Hide resolved
@IanLilleyT
Copy link
Contributor Author

@lilleyse This is ready for another review

@lilleyse
Copy link
Contributor

The updates look good. eslint and tests pass locally

We're still figuring out why all branches are suddenly failing CI after the travis-ci.org -> travis.ci.com switch. That doesn't need to hold up this PR.

@lilleyse lilleyse merged commit 5dfade4 into master Jun 18, 2021
@lilleyse lilleyse deleted the customHeightmapTerrainProvider branch June 18, 2021 18:45
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants