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

3D terrain mesh #12

Open
kylebarron opened this issue Dec 11, 2019 · 5 comments
Open

3D terrain mesh #12

kylebarron opened this issue Dec 11, 2019 · 5 comments

Comments

@kylebarron
Copy link
Member Author

Three terrain plugin?:
https://github.com/IceCreamYou/THREE.Terrain

@kylebarron
Copy link
Member Author

Wrapper for THREE.js for web and react-native!! <3
https://github.com/react-spring/react-three-fiber

@kylebarron
Copy link
Member Author

@kylebarron
Copy link
Member Author

This snippet gets a grid of terrain values from a mapbox terrain-rgb tile, and passes those values to martini.

I couldn't get the martini import to work in ijavascript because it uses the new es6 syntax, but everything else is tested.

var getPixels = require("get-pixels")
var Martini = require('@mapbox/martini')
var path = '/Users/kyle/github/mapping/nst-guide/hillshade/data/tmp-rgbify/terrain_cubicspline/12/720/1638.png'


let data
getPixels(path, function(err, pixels) {
  if(err) {
    console.log("Bad image path")
    return
  }
  data = pixels
  console.log("got pixels", pixels.shape.slice())
})

tileSize = data.shape[0]
gridSize = tileSize + 1

var terrain = new Float32Array(gridSize * gridSize);

let r, g, b;
let height_values = []
for (let x = 0; x < tileSize; x++) {
  for (let y = 0; y < tileSize; y++) {
    R = data.get(x, y, 0)
    G = data.get(x, y, 1)
    B = data.get(x, y, 2)
    height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)
    terrain[y * gridSize + x] = height;
    height_values.push(height)
  }
}

// backfill right and bottom borders
for (let x = 0; x < gridSize - 1; x++) {
  terrain[gridSize * (gridSize - 1) + x] = terrain[gridSize * (gridSize - 2) + x];
}
for (let y = 0; y < gridSize; y++) {
  terrain[gridSize * y + gridSize - 1] = terrain[gridSize * y + gridSize - 2];
}

// set up mesh generator for a certain 2^k+1 grid size
const martini = new Martini(gridSize);

// generate RTIN hierarchy from terrain data (an array of size^2 length)
const tile = martini.createTile(terrain);

// get a mesh (vertices and triangles indices) for a 10m error
const mesh = tile.getMesh(10);

@kylebarron kylebarron transferred this issue from nst-guide/mobile Dec 13, 2019
@kylebarron
Copy link
Member Author

Merged from mobile; mobile is nver going to really happen, though web hopefully will through deck.gl

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

No branches or pull requests

1 participant