implement item renderer, add debug information to overlay

This commit is contained in:
LabyStudio
2022-02-05 16:18:31 +01:00
parent c8111badb9
commit 52427e610d
9 changed files with 160 additions and 33 deletions
@@ -8,7 +8,7 @@ window.BlockRenderer = class {
this.tessellator.bindTexture(worldRenderer.terrainTexture);
}
renderBlock(world, group, block, x, y, z) {
renderBlock(world, block, x, y, z) {
let boundingBox = block.getBoundingBox(world, x, y, z);
// Render all faces
@@ -47,7 +47,8 @@ window.BlockRenderer = class {
// Classic lightning
if (BlockRenderer.CLASSIC_LIGHTNING) {
let brightness = 0.9 / 15.0 * world.getTotalLightAt(minX + face.x, minY + face.y, minZ + face.z) + 0.1;
let level = world === null ? 15 : world.getTotalLightAt(minX + face.x, minY + face.y, minZ + face.z);
let brightness = 0.9 / 15.0 * level + 0.1;
let color = brightness * face.getShading();
this.tessellator.setColor(color, color, color);
}
@@ -112,6 +113,10 @@ window.BlockRenderer = class {
}
getAverageLightLevelAt(world, x, y, z) {
if (world === null) {
return 15;
}
let totalLightLevel = 0;
let totalBlocks = 0;
@@ -135,4 +140,28 @@ window.BlockRenderer = class {
// Calculate the average light level of all surrounding blocks
return totalBlocks === 0 ? 0 : totalLightLevel / totalBlocks;
}
renderGuiBlock(group, block, x, y, size) {
this.tessellator.startDrawing();
let boundingBox = block.getBoundingBox(null, 0, 0, 0);
this.renderFace(null, block, boundingBox, EnumBlockFace.TOP, 0, 0, 0);
this.renderFace(null, block, boundingBox, EnumBlockFace.NORTH, 0, 0, 0);
this.renderFace(null, block, boundingBox, EnumBlockFace.EAST, 0, 0, 0);
let mesh = this.tessellator.draw(group);
mesh.geometry.center();
mesh.rotation.x = -MathHelper.toRadians(45 / 1.5);
mesh.rotation.y = MathHelper.toRadians(45);
mesh.position.x = x;
mesh.position.y = -y;
mesh.position.z = -3;
//let scale = Math.cos(Date.now() / 1000) * 100;
mesh.scale.x = size;
mesh.scale.y = size;
mesh.scale.z = size;
}
}
@@ -3,7 +3,8 @@ window.Tessellator = class {
constructor() {
this.material = new THREE.MeshBasicMaterial({
vertexColors: THREE.VertexColors,
side: THREE.BackSide,
//side: THREE.BackSide,
side: THREE.DoubleSide,
transparent: true,
depthTest: true
});
@@ -66,6 +67,7 @@ window.Tessellator = class {
let mesh = new THREE.Mesh(geometry, this.material);
group.matrixAutoUpdate = false;
group.add(mesh);
return mesh;
}
}
@@ -15,6 +15,10 @@ window.WorldRenderer = class {
// Block Renderer
this.blockRenderer = new BlockRenderer(this);
this.initialize();
}
initialize() {
// Create world camera
this.camera = new THREE.PerspectiveCamera(0, 1, 0.001, 1000);
this.camera.rotation.order = 'ZYX';
@@ -0,0 +1,73 @@
window.ItemRenderer = class {
constructor(minecraft, window) {
this.minecraft = minecraft;
this.window = window;
this.items = [];
}
initialize() {
// Create item camera
this.camera = new THREE.OrthographicCamera(0, 0, 0, 0, 0, 300);
this.camera.near = 0;
this.camera.far = 15;
this.camera.rotation.order = 'ZYX';
this.camera.up = new THREE.Vector3(0, 1, 0);
// Create scene
this.scene = new THREE.Scene();
this.scene.matrixAutoUpdate = false;
// Create web renderer
this.webRenderer = new THREE.WebGLRenderer({
canvas: this.window.canvasItems,
antialias: true
});
// Settings
this.webRenderer.setSize(this.window.width, this.window.height);
this.webRenderer.shadowMap.enabled = true;
this.webRenderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
this.webRenderer.autoClear = false;
this.webRenderer.sortObjects = false;
this.webRenderer.setClearColor(0x000000, 0);
this.webRenderer.clear();
}
render(partialTicks) {
// Update camera
this.camera.left = -this.window.width / 2;
this.camera.right = this.window.width / 2;
this.camera.top = this.window.height / 2;
this.camera.bottom = -this.window.height / 2;
this.camera.setViewOffset(this.window.width, this.window.height, this.window.width / 2, this.window.height / 2, this.window.width, this.window.height);
this.camera.updateProjectionMatrix();
// Render scene
this.webRenderer.render(this.scene, this.camera);
}
renderItemInGui(renderId, block, x, y) {
let meta = this.items[renderId];
if (typeof meta === "undefined") {
let meta = {};
let group = new THREE.Group();
this.minecraft.worldRenderer.blockRenderer.renderGuiBlock(group, block, x, y, 10);
this.scene.add(group);
meta.group = group;
meta.typeId = block.getId();
meta.x = x;
meta.y = y;
this.items[renderId] = meta;
} else {
if (meta.typeId !== block.getId() || meta.x !== x || meta.y !== y) {
this.scene.remove(meta.group);
delete this.items[renderId];
this.renderItemInGui(renderId, block, x, y);
}
}
}
}