working blockstates

This commit is contained in:
2026-07-03 22:32:48 -04:00
parent 2fa76da8bc
commit a702916de3
7 changed files with 380 additions and 178 deletions

View File

@@ -1,4 +1,3 @@
// renderer.js
import {mat4Identity, mat4Multiply, mat4Ortho, mat4LookAt} from './math.js';
const VS_SRC = `#version 300 es
@@ -6,10 +5,11 @@ layout(location=0) in vec3 a_position;
layout(location=1) in vec3 a_normal;
layout(location=2) in vec2 a_uv;
layout(location=3) in float a_tint;
layout(location=4) in float a_shade; // 0.0 = emissive, 1.0 = shaded
// Instanced attributes
layout(location=4) in mat4 i_matrix;
layout(location=8) in vec3 i_color;
// Instanced attributes shifted to account for a_shade
layout(location=5) in mat4 i_matrix;
layout(location=9) in vec3 i_color;
uniform mat4 u_viewProj;
uniform vec3 u_lightDir;
@@ -22,30 +22,28 @@ void main() {
gl_Position = u_viewProj * i_matrix * vec4(a_position, 1.0);
v_uv = a_uv;
// Apply geometry transform to normals for accurate lighting
vec3 normal = normalize(mat3(i_matrix) * a_normal);
// Simple Lambertian lighting with ambient base
v_light = max(dot(normal, normalize(u_lightDir)), 0.0) * 0.6 + 0.4;
v_color = mix(vec3(1.0), i_color, a_tint); // Blend tint if face is tintable
// Lambertian lighting
float baseLight = max(dot(normal, normalize(u_lightDir)), 0.0) * 0.6 + 0.4;
// Mix between full brightness (1.0) and shaded based on the element's shade flag
v_light = mix(1.0, baseLight, a_shade);
v_color = mix(vec3(1.0), i_color, a_tint);
}
`;
const FS_SRC = `#version 300 es
precision highp float;
in vec2 v_uv;
in float v_light;
in vec3 v_color;
uniform sampler2D u_texture;
out vec4 fragColor;
void main() {
vec4 texColor = texture(u_texture, v_uv);
if (texColor.a < 0.1) discard; // Alpha test for foliage/glass
if (texColor.a < 0.1) discard;
fragColor = vec4(texColor.rgb * v_color * v_light, texColor.a);
}
`;
@@ -97,12 +95,13 @@ export class Renderer {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
}
createModelBuffer(geometry) {
createInstancedMesh(geometry, instanceCount, matrices, colors) {
const gl = this.gl;
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const bindAttr = (loc, data, size) => {
// 1. Bind standard geometry (divisor = 0)
const bindGeomAttr = (loc, data, size) => {
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
@@ -110,20 +109,43 @@ export class Renderer {
gl.vertexAttribPointer(loc, size, gl.FLOAT, false, 0, 0);
};
bindAttr(0, geometry.positions, 3);
bindAttr(1, geometry.normals, 3);
bindAttr(2, geometry.uvs, 2);
bindAttr(3, geometry.tints, 1);
bindGeomAttr(0, geometry.positions, 3);
bindGeomAttr(1, geometry.normals, 3);
bindGeomAttr(2, geometry.uvs, 2);
bindGeomAttr(3, geometry.tints, 1);
bindGeomAttr(4, geometry.shades, 1);
// 2. Bind Index Buffer
const ebo = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, geometry.indices, gl.STATIC_DRAW);
// 3. Bind Instanced Attributes (divisor = 1)
const matrixBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, matrixBuffer);
gl.bufferData(gl.ARRAY_BUFFER, matrices, gl.STATIC_DRAW);
// Mat4 requires 4 separate vec4 attributes in WebGL
for (let i = 0; i < 4; i++) {
const loc = 5 + i;
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc, 4, gl.FLOAT, false, 64, i * 16);
gl.vertexAttribDivisor(loc, 1);
}
const colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
gl.enableVertexAttribArray(9);
gl.vertexAttribPointer(9, 3, gl.FLOAT, false, 0, 0);
gl.vertexAttribDivisor(9, 1);
gl.bindVertexArray(null);
return {vao, indexCount: geometry.indices.length};
return {vao, indexCount: geometry.indices.length, instanceCount};
}
drawSingleInstance(modelBuffer, viewProjMatrix, modelMatrix) {
drawInstanced(mesh, viewProjMatrix) {
const gl = this.gl;
gl.useProgram(this.program);
@@ -134,22 +156,7 @@ export class Renderer {
gl.bindTexture(gl.TEXTURE_2D, this.atlasTexture);
gl.uniform1i(this.uniforms.texture, 0);
gl.bindVertexArray(modelBuffer.vao);
gl.disableVertexAttribArray(4);
gl.disableVertexAttribArray(5);
gl.disableVertexAttribArray(6);
gl.disableVertexAttribArray(7);
gl.disableVertexAttribArray(8);
// Push the calculated model matrix into the instanced attributes manually
gl.vertexAttrib4f(4, modelMatrix[0], modelMatrix[1], modelMatrix[2], modelMatrix[3]);
gl.vertexAttrib4f(5, modelMatrix[4], modelMatrix[5], modelMatrix[6], modelMatrix[7]);
gl.vertexAttrib4f(6, modelMatrix[8], modelMatrix[9], modelMatrix[10], modelMatrix[11]);
gl.vertexAttrib4f(7, modelMatrix[12], modelMatrix[13], modelMatrix[14], modelMatrix[15]);
gl.vertexAttrib3f(8, 1, 1, 1); // White tint
gl.drawElements(gl.TRIANGLES, modelBuffer.indexCount, gl.UNSIGNED_SHORT, 0);
gl.bindVertexArray(mesh.vao);
gl.drawElementsInstanced(gl.TRIANGLES, mesh.indexCount, gl.UNSIGNED_SHORT, 0, mesh.instanceCount);
}
}