Created
December 22, 2019 19:27
-
-
Save slawo-ch/e0a8a2a58d39904f5ac90d139075ece4 to your computer and use it in GitHub Desktop.
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
// size of our height maps | |
const mapSize = 1024; | |
// returns the distance of point x,y from the origin 0,0 | |
const distance = (x, y) => Math.sqrt(x * x + y * y); | |
// init height map 1 | |
const heightMap1 = []; | |
for (let u = 0; u < mapSize; u++) { | |
for (let v = 0; v < mapSize; v++) { | |
// index of coordinate in height map array | |
const i = u * mapSize + v; | |
// u,v are coordinates with origin at upper left corner | |
// cx and cy are coordinates with origin at the | |
// center of the map | |
const cx = u - mapSize / 2; | |
const cy = v - mapSize / 2; | |
// distance from middle of map | |
const d = distance(cx, cy); | |
// stretching so we get the desired ripple density on our map | |
const stretch = (3 * Math.PI) / (mapSize / 2); | |
// wavy height value between -1 and 1 | |
const ripple = Math.sin(d * stretch); | |
// wavy height value normalized to 0..1 | |
const normalized = (ripple + 1) / 2; | |
// height map value 0..128, integer | |
heightMap1[i] = Math.floor(normalized * 128); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment