Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var config = {
- heightmap: null,
- heightmapWidth: 325,
- heightmapHeight: 325,
- SIZE: 64,
- subdivisions: 64,
- filter: [1, 1, 1],
- MAX_HEIGHT: 5,
- MIN_HEIGHT: 0
- };
- const getPixel = function (x, z) {
- // Clamp from world space to heightmap space
- x = (((x + config.SIZE / 2) / config.SIZE) * (config.heightmapWidth - 1)) | 0;
- z = ((1.0 - (z + config.SIZE / 2) / config.SIZE) * (config.heightmapHeight - 1)) | 0;
- // Get the pixel value from the buffer
- var p = (x + z * config.heightmapWidth) * 4;
- var r = config.heightmap[p] / 255;
- var g = config.heightmap[p + 1] / 255;
- var b = config.heightmap[p + 2] / 255;
- var p = (x + z * config.heightmapWidth) * 4;
- var color = {
- r: config.blendmap[p],
- g: config.blendmap[p + 1],
- b: config.blendmap[p + 2]
- };
- return {
- r: r,
- g: g,
- b: b,
- color: color
- }
- };
- const getHeight = function (x, z) {
- var pixel = getPixel(x, z);
- return {
- color: pixel.color,
- height: config.MIN_HEIGHT + (config.MAX_HEIGHT - config.MIN_HEIGHT) *
- (pixel.r * config.filter[0] + pixel.g * config.filter[1] + pixel.b * config.filter[2])
- }
- };
- const calculateNormal = function (x, z) {
- // Find vertex neighbors
- var a = getHeight(x - 1, z).height;
- var b = getHeight(x + 1, z).height;
- var c = getHeight(x, z - 1).height;
- var d = getHeight(x, z + 1).height;
- // Compute the cross product
- var u = a - b;
- var v = 2;
- var w = c - d;
- // Normalize
- var m = Math.sqrt(u * u + v * v + w * w);
- u /= m;
- v /= m;
- w /= m;
- return [u, v, w];
- };
- const generate = function () {
- var vertices = [];
- var normals = [];
- var texCoords = [];
- var indices = [];
- // Generate plane vertices, normals and texture coordinates.
- for (var row = 0; row <= config.subdivisions; row++) {
- for (var col = 0; col <= config.subdivisions; col++) {
- var x = (col * config.SIZE) / config.subdivisions - (config.SIZE / 2.0);
- var z = ((config.subdivisions - row) * config.SIZE) / config.subdivisions - (config.SIZE / 2.0);
- var position = [x, 0, z];
- position[1] = getHeight(x, z).height;
- var normal = calculateNormal(x, z);
- vertices.push(position[0], position[1], position[2]);
- normals.push(normal[0], normal[1], normal[2]);
- texCoords.push(1 + col / config.subdivisions, (1 - row / config.subdivisions));
- }
- }
- for (var row = 0; row < config.subdivisions; row++) {
- for (var col = 0; col < config.subdivisions; col++) {
- indices.push(col + 1 + (row + 1) * (config.subdivisions + 1));
- indices.push(col + 1 + row * (config.subdivisions + 1));
- indices.push(col + row * (config.subdivisions + 1));
- indices.push(col + (row + 1) * (config.subdivisions + 1));
- indices.push(col + 1 + (row + 1) * (config.subdivisions + 1));
- indices.push(col + row * (config.subdivisions + 1));
- }
- }
- return {
- generated: true,
- vertices: vertices,
- normals: normals,
- texCoords: texCoords,
- indices: indices
- }
- };
- self.addEventListener('message', function (event) {
- if (typeof event.data.config != 'undefined') {
- config = event.data.config;
- self.postMessage(generate());
- }
- if (typeof event.data.getHeights != 'undefined') {
- var heights = [];
- for (var i = 0; i < event.data.getHeights.length; i++) {
- var pos = event.data.getHeights[i];
- var data = getHeight(pos[0], pos[1]);
- heights.push({
- position: [pos[0], data.height, pos[1]],
- color: data.color
- });
- }
- self.postMessage({heights: heights})
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement