Skip to content

Instantly share code, notes, and snippets.

@slawo-ch
Created December 23, 2019 10:08
Show Gist options
  • Save slawo-ch/f926d617936d4fb04c4d68c6b10b7486 to your computer and use it in GitHub Desktop.
Save slawo-ch/f926d617936d4fb04c4d68c6b10b7486 to your computer and use it in GitHub Desktop.
// offsets for height maps
// for now, we leave them at upper left corner
let dx1 = 0;
let dy1 = 0;
let dx2 = 0;
let dy2 = 0;
// update our image data array with greyscale values
// as per our height maps
const updateImageData = () => {
for (let u = 0; u < imgSize; u++) {
for (let v = 0; v < imgSize; v++) {
// indexes into height maps for pixel
const i = (u + dy1) * mapSize + (v + dx1);
const k = (u + dy2) * mapSize + (v + dx2);
// index for pixel in image data
// remember it's 4 bytes per pixel
const j = u * imgSize * 4 + v * 4;
// height value of 0..255
let h = heightMap1[i] + heightMap2[k];
// greyscale color according to height
let c = { r: h, g: h, b: h };
// set pixel data
image.data[j] = c.r;
image.data[j + 1] = c.g;
image.data[j + 2] = c.b;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment