Advertisement
Masterchoc

Untitled

Dec 23rd, 2017
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var config = {
  2.     heightmap: null,
  3.     heightmapWidth: 325,
  4.     heightmapHeight: 325,
  5.     SIZE: 64,
  6.     subdivisions: 64,
  7.     filter: [1, 1, 1],
  8.     MAX_HEIGHT: 5,
  9.     MIN_HEIGHT: 0
  10. };
  11.  
  12. const getPixel = function (x, z) {
  13.     // Clamp from world space to heightmap space
  14.     x = (((x + config.SIZE / 2) / config.SIZE) * (config.heightmapWidth - 1)) | 0;
  15.     z = ((1.0 - (z + config.SIZE / 2) / config.SIZE) * (config.heightmapHeight - 1)) | 0;
  16.  
  17.     // Get the pixel value from the buffer
  18.     var p = (x + z * config.heightmapWidth) * 4;
  19.     var r = config.heightmap[p] / 255;
  20.     var g = config.heightmap[p + 1] / 255;
  21.     var b = config.heightmap[p + 2] / 255;
  22.  
  23.     var p = (x + z * config.heightmapWidth) * 4;
  24.     var color = {
  25.         r: config.blendmap[p],
  26.         g: config.blendmap[p + 1],
  27.         b: config.blendmap[p + 2]
  28.     };
  29.  
  30.     return {
  31.         r: r,
  32.         g: g,
  33.         b: b,
  34.         color: color
  35.     }
  36. };
  37.  
  38. const getHeight = function (x, z) {
  39.     var pixel = getPixel(x, z);
  40.     return {
  41.         color: pixel.color,
  42.         height: config.MIN_HEIGHT + (config.MAX_HEIGHT - config.MIN_HEIGHT) *
  43.         (pixel.r * config.filter[0] + pixel.g * config.filter[1] + pixel.b * config.filter[2])
  44.     }
  45. };
  46.  
  47. const calculateNormal = function (x, z) {
  48.     // Find vertex neighbors
  49.     var a = getHeight(x - 1, z).height;
  50.     var b = getHeight(x + 1, z).height;
  51.     var c = getHeight(x, z - 1).height;
  52.     var d = getHeight(x, z + 1).height;
  53.  
  54.     // Compute the cross product
  55.     var u = a - b;
  56.     var v = 2;
  57.     var w = c - d;
  58.  
  59.     // Normalize
  60.     var m = Math.sqrt(u * u + v * v + w * w);
  61.     u /= m;
  62.     v /= m;
  63.     w /= m;
  64.  
  65.     return [u, v, w];
  66. };
  67.  
  68. const generate = function () {
  69.     var vertices = [];
  70.     var normals = [];
  71.     var texCoords = [];
  72.     var indices = [];
  73.  
  74.     // Generate plane vertices, normals and texture coordinates.
  75.     for (var row = 0; row <= config.subdivisions; row++) {
  76.         for (var col = 0; col <= config.subdivisions; col++) {
  77.             var x = (col * config.SIZE) / config.subdivisions - (config.SIZE / 2.0);
  78.             var z = ((config.subdivisions - row) * config.SIZE) / config.subdivisions - (config.SIZE / 2.0);
  79.  
  80.             var position = [x, 0, z];
  81.             position[1] = getHeight(x, z).height;
  82.             var normal = calculateNormal(x, z);
  83.             vertices.push(position[0], position[1], position[2]);
  84.             normals.push(normal[0], normal[1], normal[2]);
  85.             texCoords.push(1 + col / config.subdivisions, (1 - row / config.subdivisions));
  86.         }
  87.     }
  88.  
  89.     for (var row = 0; row < config.subdivisions; row++) {
  90.         for (var col = 0; col < config.subdivisions; col++) {
  91.             indices.push(col + 1 + (row + 1) * (config.subdivisions + 1));
  92.             indices.push(col + 1 + row * (config.subdivisions + 1));
  93.             indices.push(col + row * (config.subdivisions + 1));
  94.  
  95.             indices.push(col + (row + 1) * (config.subdivisions + 1));
  96.             indices.push(col + 1 + (row + 1) * (config.subdivisions + 1));
  97.             indices.push(col + row * (config.subdivisions + 1));
  98.         }
  99.     }
  100.  
  101.     return {
  102.         generated: true,
  103.         vertices: vertices,
  104.         normals: normals,
  105.         texCoords: texCoords,
  106.         indices: indices
  107.     }
  108. };
  109.  
  110. self.addEventListener('message', function (event) {
  111.     if (typeof event.data.config != 'undefined') {
  112.         config = event.data.config;
  113.         self.postMessage(generate());
  114.     }
  115.  
  116.     if (typeof event.data.getHeights != 'undefined') {
  117.         var heights = [];
  118.         for (var i = 0; i < event.data.getHeights.length; i++) {
  119.             var pos = event.data.getHeights[i];
  120.             var data = getHeight(pos[0], pos[1]);
  121.             heights.push({
  122.                 position: [pos[0], data.height, pos[1]],
  123.                 color: data.color
  124.             });
  125.         }
  126.  
  127.         self.postMessage({heights: heights})
  128.     }
  129. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement