Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const CHUNK_SIZE = 3;
- const INDICES_PER_TRIANGLE = 3;
- const TRIANGLES_PER_QUAD = 2;
- const COORDS_PER_VERTEX = 3;
- const NEXT_ROW_OFFSET = CHUNK_SIZE + 1;
- const CHUNK_CENTRALIZE_OFFSET = -(CHUNK_SIZE / 2);
- const VERTEX_Z_POINT_OFFSET = 2;
- const obj = {
- vertices() {
- const verticesBufferSize = (NEXT_ROW_OFFSET ** 2) * COORDS_PER_VERTEX;
- const verticesBuffer = new Float32Array(verticesBufferSize);
- let vertexBufferIndex = 0;
- for (let y = 0; y < NEXT_ROW_OFFSET; y++) {
- for (let x = 0; x < NEXT_ROW_OFFSET; x++) {
- verticesBuffer[vertexBufferIndex++] = x + CHUNK_CENTRALIZE_OFFSET;
- verticesBuffer[vertexBufferIndex++] = y + CHUNK_CENTRALIZE_OFFSET;
- verticesBuffer[vertexBufferIndex++] = Math.random() * 40;
- }
- }
- return verticesBuffer;
- },
- indices(verticesBuffer) {
- const indicesBufferSize = (CHUNK_SIZE ** 2) * INDICES_PER_TRIANGLE * TRIANGLES_PER_QUAD;
- const indicesBuffer = new Uint16Array(indicesBufferSize);
- let indicesBufferIndex = 0;
- for (let y = 0; y < CHUNK_SIZE; y++) {
- for (let x = 0; x < CHUNK_SIZE; x++) {
- const bottomLeftIndex = y * NEXT_ROW_OFFSET + x;
- const bottomRightIndex = bottomLeftIndex + 1;
- const topLeftIndex = bottomLeftIndex + NEXT_ROW_OFFSET;
- const topRightIndex = topLeftIndex + 1;
- const bottomLeftVertexZ = verticesBuffer[bottomLeftIndex * 3 + VERTEX_Z_POINT_OFFSET];
- const bottomRightVertexZ = verticesBuffer[bottomRightIndex * 3 + VERTEX_Z_POINT_OFFSET];
- const topLeftVertexZ = verticesBuffer[topLeftIndex * 3 + VERTEX_Z_POINT_OFFSET];
- const topRightVertexZ = verticesBuffer[topRightIndex * 3 + VERTEX_Z_POINT_OFFSET];
- const heightDifferentFirst = Math.abs(bottomLeftVertexZ - topRightVertexZ);
- const heightDifferentSecond = Math.abs(bottomRightVertexZ - topLeftVertexZ);
- const rotate = heightDifferentFirst > heightDifferentSecond;
- indicesBuffer[indicesBufferIndex++] = bottomLeftIndex;
- indicesBuffer[indicesBufferIndex++] = rotate ? topLeftIndex : topRightIndex;
- indicesBuffer[indicesBufferIndex++] = bottomRightIndex;
- indicesBuffer[indicesBufferIndex++] = rotate ? bottomRightIndex : bottomLeftIndex;
- indicesBuffer[indicesBufferIndex++] = topLeftIndex;
- indicesBuffer[indicesBufferIndex++] = topRightIndex;
- }
- }
- return indicesBuffer;
- },
- };
- const debug = function (indices) {
- const chunk = 3;
- const length = indices.length;
- const pad = indices[length - 1].toString().length + 1;
- for (let i = 0; i < length; i += chunk) {
- const triangle = indices.slice(i, i + chunk);
- for (let index of triangle) {
- const string = index.toString().padStart(pad, " ");
- process.stdout.write(string);
- }
- process.stdout.write(i % 2 === 1 ? "\n" : " | ");
- }
- process.stdout.write("\n");
- };
- const vertices = obj.vertices();
- debug(vertices);
- debug(obj.indices(vertices));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement