Advertisement
lignite0

ROBO24 - generate terrain

Oct 17th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const CHUNK_SIZE = 3;
  2. const INDICES_PER_TRIANGLE = 3;
  3. const TRIANGLES_PER_QUAD = 2;
  4. const COORDS_PER_VERTEX = 3;
  5. const NEXT_ROW_OFFSET = CHUNK_SIZE + 1;
  6. const CHUNK_CENTRALIZE_OFFSET = -(CHUNK_SIZE / 2);
  7. const VERTEX_Z_POINT_OFFSET = 2;
  8.  
  9. const obj = {
  10.     vertices() {
  11.         const verticesBufferSize = (NEXT_ROW_OFFSET ** 2) * COORDS_PER_VERTEX;
  12.         const verticesBuffer = new Float32Array(verticesBufferSize);
  13.  
  14.         let vertexBufferIndex = 0;
  15.         for (let y = 0; y < NEXT_ROW_OFFSET; y++) {
  16.             for (let x = 0; x < NEXT_ROW_OFFSET; x++) {
  17.                 verticesBuffer[vertexBufferIndex++] = x + CHUNK_CENTRALIZE_OFFSET;
  18.                 verticesBuffer[vertexBufferIndex++] = y + CHUNK_CENTRALIZE_OFFSET;
  19.                 verticesBuffer[vertexBufferIndex++] = Math.random() * 40;
  20.             }
  21.         }
  22.  
  23.         return verticesBuffer;
  24.     },
  25.     indices(verticesBuffer) {
  26.         const indicesBufferSize = (CHUNK_SIZE ** 2) * INDICES_PER_TRIANGLE * TRIANGLES_PER_QUAD;
  27.         const indicesBuffer = new Uint16Array(indicesBufferSize);
  28.  
  29.         let indicesBufferIndex = 0;
  30.         for (let y = 0; y < CHUNK_SIZE; y++) {
  31.             for (let x = 0; x < CHUNK_SIZE; x++) {
  32.  
  33.                 const bottomLeftIndex = y * NEXT_ROW_OFFSET + x;
  34.                 const bottomRightIndex = bottomLeftIndex + 1;
  35.                 const topLeftIndex = bottomLeftIndex + NEXT_ROW_OFFSET;
  36.                 const topRightIndex = topLeftIndex + 1;
  37.  
  38.                 const bottomLeftVertexZ = verticesBuffer[bottomLeftIndex * 3 + VERTEX_Z_POINT_OFFSET];
  39.                 const bottomRightVertexZ = verticesBuffer[bottomRightIndex * 3 + VERTEX_Z_POINT_OFFSET];
  40.                 const topLeftVertexZ = verticesBuffer[topLeftIndex * 3 + VERTEX_Z_POINT_OFFSET];
  41.                 const topRightVertexZ = verticesBuffer[topRightIndex * 3 + VERTEX_Z_POINT_OFFSET];
  42.  
  43.                 const heightDifferentFirst = Math.abs(bottomLeftVertexZ - topRightVertexZ);
  44.                 const heightDifferentSecond = Math.abs(bottomRightVertexZ - topLeftVertexZ);
  45.                 const rotate = heightDifferentFirst > heightDifferentSecond;
  46.  
  47.                 indicesBuffer[indicesBufferIndex++] = bottomLeftIndex;
  48.                 indicesBuffer[indicesBufferIndex++] = rotate ? topLeftIndex : topRightIndex;
  49.                 indicesBuffer[indicesBufferIndex++] = bottomRightIndex;
  50.  
  51.                 indicesBuffer[indicesBufferIndex++] = rotate ? bottomRightIndex : bottomLeftIndex;
  52.                 indicesBuffer[indicesBufferIndex++] = topLeftIndex;
  53.                 indicesBuffer[indicesBufferIndex++] = topRightIndex;
  54.             }
  55.         }
  56.  
  57.         return indicesBuffer;
  58.     },
  59. };
  60.  
  61. const debug = function (indices) {
  62.     const chunk = 3;
  63.     const length = indices.length;
  64.     const pad = indices[length - 1].toString().length + 1;
  65.     for (let i = 0; i < length; i += chunk) {
  66.         const triangle = indices.slice(i, i + chunk);
  67.         for (let index of triangle) {
  68.             const string = index.toString().padStart(pad, " ");
  69.             process.stdout.write(string);
  70.         }
  71.         process.stdout.write(i % 2 === 1 ? "\n" : "  | ");
  72.     }
  73.     process.stdout.write("\n");
  74. };
  75.  
  76. const vertices = obj.vertices();
  77.  
  78. debug(vertices);
  79. debug(obj.indices(vertices));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement