Created
September 8, 2016 00:07
-
-
Save TaylorMutch/51eaece807bc1fe2f2620612f31a2a8e to your computer and use it in GitHub Desktop.
TypeScript function for converting a heightmap texture into Float32Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function computeHeights(hmTexture: THREE.Texture, stats: any) { | |
const image = hmTexture.image | |
let w = image.naturalWidth | |
let h = image.naturalHeight | |
let canvas = document.createElement('canvas') | |
canvas.width = w | |
canvas.height = h | |
let ctx = canvas.getContext('2d') | |
ctx.drawImage(image, 0, 0, w, h) | |
let data = ctx.getImageData(0, 0, w, h).data | |
const heights = new Float32Array(w * h) | |
let idx: number | |
for (let y = 0; y < h; ++y) { | |
for (let x = 0; x < w; ++x) { | |
// idx pixel we want to get. Image has rgba, but we only need the r channel | |
idx = (x + y * w) * 4 | |
// scale & store this altitude | |
heights[x + y * w] = data[idx] / 255.0 * stats.dem_max | |
} | |
} | |
// Free the resources and return | |
data = ctx = canvas = null | |
return heights | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment