Placing blocks. Also better terrain. And still water.

Just a few things.
This commit is contained in:
Paul Mathieu
2021-12-20 01:25:40 -08:00
parent 4d0f406669
commit 8939a90307
4 changed files with 304 additions and 119 deletions

192
world.js
View File

@@ -1,6 +1,7 @@
import { makeBufferFromFaces, makeFace} from "./geometry";
import { loadTexture, makeProgram } from "./gl";
import * as se3 from './se3';
import { makeTerrain } from "./terrain";
const VSHADER = `
attribute vec3 aPosition;
@@ -54,59 +55,9 @@ export const BlockType = {
DIRT: 2,
GRASS: 3,
STONE: 4,
WATER: 5,
};
function ghettoPerlinNoise(seed, x, y, gridSize = 16) {
const dot = (vx, vy) => vx[0] * vy[0] + vx[1] * vy[1];
// super ghetto random
const xorshift = (x) => {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return x;
};
const randGrad = (x0, y0) => {
const rand = xorshift(1337 * x0 + seed + 80085 * y0);
return [Math.sin(rand), Math.cos(rand)];
};
const interpol = (a, b, x) => a + (x*x*(3 - 2*x)) * (b - a);
const x0 = Math.floor(x / gridSize);
const y0 = Math.floor(y / gridSize);
const sx = x / gridSize - x0;
const sy = y / gridSize - y0;
const n0 = dot(randGrad(x0, y0), [sx, sy]);
const n1 = dot(randGrad(x0 + 1, y0), [sx - 1, sy]);
const n2 = dot(randGrad(x0, y0 + 1), [sx, sy - 1]);
const n3 = dot(randGrad(x0 + 1, y0 + 1), [sx - 1, sy - 1]);
return interpol(interpol(n0, n1, sx), interpol(n2, n3, sx), sy);
}
function makeTerrain(x, y) {
const seed = 1337;
const fractalNoise = (x, y) => (
ghettoPerlinNoise(seed, x, y, 8) * 0.06
+ ghettoPerlinNoise(seed, x, y, 16) * 0.125
+ ghettoPerlinNoise(seed, x, y, 32) * 0.25
+ ghettoPerlinNoise(seed, x, y, 64) * 0.5
);
const terrain = Array(16 * 16);
for (let i = 0; i < 16; i++) {
for (let j = 0; j < 16; j++) {
terrain[i * 16 + j] = fractalNoise(x + i, y + j);
}
}
return terrain;
}
function makeChunk(z, x) {
const terrain = makeTerrain(z, x);
@@ -114,18 +65,23 @@ function makeChunk(z, x) {
for (let i = 0; i < 16; i++) {
for (let j = 0; j < 16; j++) {
const height = Math.floor(64 + 64 * terrain[i * 16 + j]);
const height = terrain[i * 16 + j];
// everything above is air
// that block is grass
// everything below is dirt
const offset = i * (16 * 256) + j * 256;
const stoneHeight = Math.min(52, height);
const stoneHeight = Math.max(48, height);
const grassHeight = stoneHeight + 8;
const waterHeight = 67;
data.set(Array(stoneHeight).fill(BlockType.STONE), offset);
if (stoneHeight < height) {
data.set(Array(height - 1 - stoneHeight).fill(BlockType.DIRT), offset + stoneHeight);
data[offset + height - 1] = BlockType.GRASS;
data.set(Array(grassHeight - 1 - stoneHeight).fill(BlockType.DIRT), offset + stoneHeight);
if (grassHeight < waterHeight) {
data.set(Array(waterHeight - grassHeight + 1).fill(BlockType.WATER), offset + grassHeight - 1);
} else {
data[offset + grassHeight - 1] = BlockType.GRASS;
}
data.set(Array(256 - height).fill(BlockType.AIR), offset + height);
const surfaceHeight = Math.max(waterHeight, grassHeight);
data.set(Array(256 - surfaceHeight).fill(BlockType.AIR), offset + surfaceHeight);
}
}
@@ -183,6 +139,7 @@ function faceTexture(type, dir) {
}
case BlockType.DIRT: return [2, 15];
case BlockType.STONE: return [3, 15];
case BlockType.WATER: return [4, 15];
default: return [0, 0];
}
}
@@ -218,7 +175,8 @@ function makeFaceList(data, chunkz, chunkx, blockLookup) {
{ block: lookup(i, j, k + 1), dir: '+y', faceCenter: [chunkx + j, k + 0.5, chunkz + i] },
];
const faces = [];
const solidFaces = [];
const waterFaces = [];
for (let i = 0; i < 16; i++) {
for (let j = 0; j < 16; j++) {
@@ -227,17 +185,31 @@ function makeFaceList(data, chunkz, chunkx, blockLookup) {
if (data[bi] === BlockType.AIR) {
continue;
}
for (const {block, dir, faceCenter} of neighbors(i, j, k).filter(({block}) => block === BlockType.AIR)) {
faces.push({
blockIndex: bi,
face: makeFace(dir, faceTexture(data[bi], dir), faceCenter),
});
for (const {dir, faceCenter} of neighbors(i, j, k)
.filter(({ block }) => block === BlockType.AIR || (block === BlockType.WATER && data[bi] !== BlockType.WATER))) {
if (data[bi] !== BlockType.WATER) {
solidFaces.push({
blockIndex: bi,
dir,
face: makeFace(dir, faceTexture(data[bi], dir), faceCenter),
});
} else {
faceCenter[1] -= 0.15;
waterFaces.push({
blockIndex: bi,
dir,
face: makeFace(dir, faceTexture(data[bi], dir), faceCenter),
})
}
}
}
}
}
return faces;
return {
solidFaces,
waterFaces,
};
}
// - data <-- need to generate the first time, update when m&p
@@ -315,6 +287,7 @@ function invalidateChunkGeometry(world, i, j) {
export function createChunkFace(block, dir) {
return {
dir,
blockIndex: block.blockIndex,
face: makeFace(dir, faceTexture(block.type, dir), faceCenter(block.centerPosition, dir)),
};
@@ -342,10 +315,13 @@ export function updateWorldGeometry(gl, world, z, x, timeLimit = 10000) {
const chunkx = 16 * j;
const lookup = (i, j, k) => blockLookup(world, j + chunkx, k, i + chunkz).type;
chunk.faces = makeFaceList(chunk.data, chunk.position.z, chunk.position.x, lookup);
const faces = makeFaceList(chunk.data, chunk.position.z, chunk.position.x, lookup);
chunk.faces = faces.solidFaces;
chunk.transparentFaces = faces.waterFaces;
}
chunk.buffer = makeBufferFromFaces(gl, chunk.faces.map(f => f.face));
chunk.transparentBuffer = makeBufferFromFaces(gl, chunk.transparentFaces.map(f => f.face));
// throttle this for fluidity
if (performance.now() - start > timeLimit) {
@@ -435,7 +411,7 @@ function rayThroughGrid(origin, direction, maxDistance) {
return {position, normal, distance: rayLength};
}
function castRay(world, origin, direction, maxDistance) {
export function castRay(world, origin, direction, maxDistance) {
let currentPoint = origin;
while (maxDistance > 0) {
@@ -488,49 +464,33 @@ export function markBlock(world, cameraPosition, direction, maxDistance) {
}
function viewDirection(params) {
const dir = [0, 0, -1, 1.0];
const camori = params.camera.orientation;
const ori = se3.inverse(se3.rotxyz(-camori[0], -camori[1], -camori[2]));
return se3.apply(ori, dir).slice(0, 3);
}
export function destroySelectedBlock(params) {
const hit = castRay(params.world, params.camera.position, viewDirection(params), params.blockSelectDistance);
if (hit === undefined || hit.block.type === BlockType.UNDEFINED) {
return;
}
export function destroyBlock(world, block) {
const trimFaces = chunk => {
chunk.faces = chunk.faces.filter(({blockIndex}) => chunk.data[blockIndex] !== BlockType.AIR);
}
hit.block.chunk.data[hit.block.blockIndex] = BlockType.AIR;
if (hit.block.chunk.buffer !== undefined) {
hit.block.chunk.buffer.delete();
delete hit.block.chunk.buffer;
block.chunk.data[block.blockIndex] = BlockType.AIR;
if (block.chunk.buffer !== undefined) {
block.chunk.buffer.delete();
delete block.chunk.buffer;
}
trimFaces(hit.block.chunk);
trimFaces(block.chunk);
const [bx, by, bz] = hit.block.centerPosition;
const [bx, by, bz] = block.centerPosition;
const neighbors = [
{ block: blockLookup(params.world, bx - 1, by, bz), dir: '+x' },
{ block: blockLookup(params.world, bx + 1, by, bz), dir: '-x' },
{ block: blockLookup(params.world, bx, by - 1, bz), dir: '+y' },
{ block: blockLookup(params.world, bx, by + 1, bz), dir: '-y' },
{ block: blockLookup(params.world, bx, by, bz - 1), dir: '+z' },
{ block: blockLookup(params.world, bx, by, bz + 1), dir: '-z' },
{ block: blockLookup(world, bx - 1, by, bz), dir: '+x' },
{ block: blockLookup(world, bx + 1, by, bz), dir: '-x' },
{ block: blockLookup(world, bx, by - 1, bz), dir: '+y' },
{ block: blockLookup(world, bx, by + 1, bz), dir: '-y' },
{ block: blockLookup(world, bx, by, bz - 1), dir: '+z' },
{ block: blockLookup(world, bx, by, bz + 1), dir: '-z' },
];
neighbors
.filter(({ block }) => block.type !== BlockType.AIR &&
block.type !== BlockType.UNDEFINED)
.forEach(({ block, dir }) => {
const blocki = Math.floor(block.blockIndex / (16 * 256));
const blockj = Math.floor(block.blockIndex / 256) - 16 * blocki;
const blockk = block.blockIndex % 256;
block.chunk.faces.push(createChunkFace(block, dir));
trimFaces(block.chunk);
@@ -541,6 +501,48 @@ export function destroySelectedBlock(params) {
});
}
export function makeBlock(world, position, type) {
const block = blockLookup(world, ...position);
console.assert(block.type === BlockType.AIR);
block.chunk.data[block.blockIndex] = type;
block.type = type;
const [bx, by, bz] = block.centerPosition;
const neighbors = [
{ block: blockLookup(world, bx - 1, by, bz), dir: '-x', ndir: '+x' },
{ block: blockLookup(world, bx + 1, by, bz), dir: '+x', ndir: '-x' },
{ block: blockLookup(world, bx, by - 1, bz), dir: '-y', ndir: '+y' },
{ block: blockLookup(world, bx, by + 1, bz), dir: '+y', ndir: '-y' },
{ block: blockLookup(world, bx, by, bz - 1), dir: '-z', ndir: '+z' },
{ block: blockLookup(world, bx, by, bz + 1), dir: '+z', ndir: '-z' },
];
const refresh = chunk => {
if (chunk.buffer !== undefined) {
chunk.buffer.delete();
delete chunk.buffer;
}
}
neighbors
.filter(({ block }) => block.type !== BlockType.UNDEFINED)
.forEach(({ block: nblock, dir, ndir }) => {
if (nblock.type === BlockType.AIR) {
block.chunk.faces.push(createChunkFace(block, dir));
} else {
nblock.chunk.faces = nblock.chunk.faces.filter(f => (
f.blockIndex !== nblock.blockIndex ||
f.dir !== ndir
));
refresh(nblock.chunk);
}
});
refresh(block.chunk);
}
export async function initWorldGl(gl) {
const program = makeProgram(gl, VSHADER, FSHADER);
const texture = await loadTexture(gl, 'texture.png');