support multiple cubes in ModuleRenderer, prepare first person item rendering

(cherry picked from commit a3f91b396f3199fc6c24db445b562d3ce1645758)
This commit is contained in:
LabyStudio
2022-04-15 04:17:26 +02:00
parent 5c47dd6599
commit dacd2496ea
4 changed files with 175 additions and 47 deletions
@@ -10,10 +10,17 @@ window.IngameOverlay = class extends Gui {
} }
render(stack, mouseX, mouseY, partialTicks) { render(stack, mouseX, mouseY, partialTicks) {
// Render crosshair
if (this.minecraft.hasInGameFocus()) { if (this.minecraft.hasInGameFocus()) {
this.renderCrosshair(stack, this.window.width / 2, this.window.height / 2) this.renderCrosshair(stack, this.window.width / 2, this.window.height / 2)
} }
// Render holding item
if (this.minecraft.settings.thirdPersonView === 0) {
this.minecraft.itemRenderer.renderItemInHand();
}
// Render hotbar
this.renderHotbar(stack, this.window.width / 2 - 91, this.window.height - 22); this.renderHotbar(stack, this.window.width / 2 - 91, this.window.height - 22);
// Debug // Debug
@@ -5,6 +5,8 @@ window.ItemRenderer = class {
this.window = window; this.window = window;
this.items = []; this.items = [];
this.itemInHand = null;
this.itemInHandGroup = new THREE.Object3D();
} }
initialize() { initialize() {
@@ -33,6 +35,8 @@ window.ItemRenderer = class {
this.webRenderer.sortObjects = false; this.webRenderer.sortObjects = false;
this.webRenderer.setClearColor(0x000000, 0); this.webRenderer.setClearColor(0x000000, 0);
this.webRenderer.clear(); this.webRenderer.clear();
this.scene.add(this.itemInHandGroup);
} }
render(partialTicks) { render(partialTicks) {
@@ -48,6 +52,28 @@ window.ItemRenderer = class {
this.webRenderer.render(this.scene, this.camera); this.webRenderer.render(this.scene, this.camera);
} }
renderItemInHand() {
let typeId = this.minecraft.player.inventory.getItemInSelectedSlot();
if (typeId === this.itemInHand) {
return; // Skip rebuilding if the item hasn't changed
}
// Clear previous mesh
this.itemInHandGroup.clear();
// Should render hand or item
if (typeId === 0) {
} else {
let block = Block.getById(typeId);
// this.minecraft.worldRenderer.blockRenderer.renderBlockInHand(block, this.itemInHandGroup, 1);
}
// Store item in hand meta
this.itemInHand = typeId;
}
renderItemInGui(renderId, block, x, y) { renderItemInGui(renderId, block, x, y) {
let meta = this.items[renderId]; let meta = this.items[renderId];
if (typeof meta === "undefined") { if (typeof meta === "undefined") {
@@ -83,5 +109,6 @@ window.ItemRenderer = class {
for (let i in this.items) { for (let i in this.items) {
this.items[i].dirty = true; this.items[i].dirty = true;
} }
this.itemInHand = null;
} }
} }
@@ -10,38 +10,38 @@ window.ModelPlayer = class extends ModelBase {
let height = 32; let height = 32;
// Create head ModelRenderer // Create head ModelRenderer
this.head = new ModelRenderer(width, height) this.head = new ModelRenderer("head", width, height)
.setTextureOffset(0, 0) .setTextureOffset(0, 0)
.setBox(-4.0, -8.0, -4.0, 8, 8, 8); .addBox(-4.0, -8.0, -4.0, 8, 8, 8);
// Create body ModelRenderer // Create body ModelRenderer
this.body = new ModelRenderer(width, height) this.body = new ModelRenderer("body", width, height)
.setTextureOffset(16, 16) .setTextureOffset(16, 16)
.setBox(-4.0, 0.0, -2.0, 8, 12, 4); .addBox(-4.0, 0.0, -2.0, 8, 12, 4);
// Left arm ModelRenderer // Left arm ModelRenderer
this.leftArm = new ModelRenderer(width, height) this.leftArm = new ModelRenderer("left_arm", width, height)
.setTextureOffset(40, 16) .setTextureOffset(40, 16)
.setRotationPoint(-5.0, 2.0, 0.0) .setRotationPoint(-5.0, 2.0, 0.0)
.setBox(-1.0, -2.0, -2.0, 4, 12, 4); .addBox(-1.0, -2.0, -2.0, 4, 12, 4);
// Right arm ModelRenderer // Right arm ModelRenderer
this.rightArm = new ModelRenderer(width, height) this.rightArm = new ModelRenderer("right_arm", width, height)
.setTextureOffset(40, 16) .setTextureOffset(40, 16)
.setRotationPoint(-3.0, 2.0, -2.0) .setRotationPoint(-3.0, 2.0, -2.0)
.setBox(-3.0, -2.0, -2.0, 4, 12, 4); .addBox(-3.0, -2.0, -2.0, 4, 12, 4);
// Right Legs ModelRenderer // Right Legs ModelRenderer
this.rightLeg = new ModelRenderer(width, height) this.rightLeg = new ModelRenderer("right_leg", width, height)
.setTextureOffset(0, 16) .setTextureOffset(0, 16)
.setRotationPoint(-2.0, 12.0, 0.0) .setRotationPoint(-2.0, 12.0, 0.0)
.setBox(-2.0, 0.0, -2.0, 4, 12, 4); .addBox(-2.0, 0.0, -2.0, 4, 12, 4);
// Left leg ModelRenderer // Left leg ModelRenderer
this.leftLeg = new ModelRenderer(width, height) this.leftLeg = new ModelRenderer("left_leg", width, height)
.setTextureOffset(0, 16) .setTextureOffset(0, 16)
.setRotationPoint(2.0, 12.0, 0.0) .setRotationPoint(2.0, 12.0, 0.0)
.setBox(-2.0, 0.0, -2.0, 4, 12, 4); .addBox(-2.0, 0.0, -2.0, 4, 12, 4);
} }
rebuild(tessellator, group) { rebuild(tessellator, group) {
@@ -143,12 +143,12 @@ window.ModelPlayer = class extends ModelBase {
this.leftArm.rotateAngleX -= Math.sin(timeAlive * 0.067) * 0.05; this.leftArm.rotateAngleX -= Math.sin(timeAlive * 0.067) * 0.05;
// Render cubes // Render cubes
this.head.render(group); this.head.render();
this.body.render(group); this.body.render();
this.rightArm.render(group); this.rightArm.render();
this.leftArm.render(group); this.leftArm.render();
this.rightLeg.render(group); this.rightLeg.render();
this.leftLeg.render(group); this.leftLeg.render();
super.render(entity, limbSwingAmount, limbSwing, timeAlive, yaw, pitch); super.render(entity, limbSwingAmount, limbSwing, timeAlive, yaw, pitch);
} }
@@ -3,7 +3,9 @@ window.ModelRenderer = class {
/** /**
* Create cube object * Create cube object
*/ */
constructor(textureWidth, textureHeight) { constructor(name, textureWidth, textureHeight) {
this.name = name;
this.textureWidth = textureWidth; this.textureWidth = textureWidth;
this.textureHeight = textureHeight; this.textureHeight = textureHeight;
@@ -18,6 +20,13 @@ window.ModelRenderer = class {
this.rotationPointY = 0; this.rotationPointY = 0;
this.rotationPointZ = 0; this.rotationPointZ = 0;
this.scaleX = 1;
this.scaleY = 1;
this.scaleZ = 1;
this.cubes = [];
this.children = [];
this.bone = new THREE.Object3D(); this.bone = new THREE.Object3D();
} }
@@ -30,7 +39,57 @@ window.ModelRenderer = class {
setTextureOffset(textureOffsetX, textureOffsetY) { setTextureOffset(textureOffsetX, textureOffsetY) {
this.textureOffsetX = textureOffsetX; this.textureOffsetX = textureOffsetX;
this.textureOffsetY = textureOffsetY; this.textureOffsetY = textureOffsetY;
return this;
}
/**
* Set texture size
* @param width Texture width
* @param height Texture height
*/
setTextureSize(width, height) {
this.textureWidth = width;
this.textureHeight = height;
return this;
}
/**
* Set the absolute position of the cube
*
* @param x Absolute x position of cube
* @param y Absolute y position of cube
* @param z Absolute z position of cube
*/
setRotationPoint(x, y, z) {
this.rotationPointX = x;
this.rotationPointY = y;
this.rotationPointZ = z;
return this;
}
/**
* Set the rotation of the cube
* @param x Rotation x
* @param y Rotation y
* @param z Rotation z
*/
setRotationAngle(x, y, z) {
this.rotateAngleX = x;
this.rotateAngleY = y;
this.rotateAngleZ = z;
return this;
}
/**
* Set the rotation of the cube
* @param x Rotation x
* @param y Rotation y
* @param z Rotation z
*/
setScale(x, y, z) {
this.scaleX = x;
this.scaleY = y;
this.scaleZ = z;
return this; return this;
} }
@@ -43,9 +102,11 @@ window.ModelRenderer = class {
* @param width Cube width * @param width Cube width
* @param height Cube height * @param height Cube height
* @param depth Cube depth * @param depth Cube depth
* @param inflate Inflate the cube
* @param mirror Mirror the cube
*/ */
setBox(offsetX, offsetY, offsetZ, width, height, depth) { addBox(offsetX, offsetY, offsetZ, width, height, depth, inflate = 0, mirror = false) {
this.polygons = []; let polygons = [];
let x = offsetX + width; let x = offsetX + width;
let y = offsetY + height; let y = offsetY + height;
@@ -64,7 +125,7 @@ window.ModelRenderer = class {
let vertexTop4 = new Vertex(offsetX, y, offsetZ); let vertexTop4 = new Vertex(offsetX, y, offsetZ);
// Create polygons for each cube side // Create polygons for each cube side
this.polygons[0] = new Polygon( polygons[0] = new Polygon(
[vertexBottom4, vertexBottom2, vertexTop3, vertexTop1], [vertexBottom4, vertexBottom2, vertexTop3, vertexTop1],
this.textureOffsetX + depth + width, this.textureOffsetX + depth + width,
this.textureOffsetY + depth, this.textureOffsetY + depth,
@@ -73,7 +134,7 @@ window.ModelRenderer = class {
this.textureWidth, this.textureHeight this.textureWidth, this.textureHeight
); );
this.polygons[1] = new Polygon( polygons[1] = new Polygon(
[vertexBottom1, vertexBottom3, vertexTop2, vertexTop4], [vertexBottom1, vertexBottom3, vertexTop2, vertexTop4],
this.textureOffsetX, this.textureOffsetX,
this.textureOffsetY + depth, this.textureOffsetY + depth,
@@ -82,7 +143,7 @@ window.ModelRenderer = class {
this.textureWidth, this.textureHeight this.textureWidth, this.textureHeight
); );
this.polygons[2] = new Polygon( polygons[2] = new Polygon(
[vertexBottom4, vertexBottom3, vertexBottom1, vertexBottom2], [vertexBottom4, vertexBottom3, vertexBottom1, vertexBottom2],
this.textureOffsetX + depth, this.textureOffsetX + depth,
this.textureOffsetY, this.textureOffsetY,
@@ -91,7 +152,7 @@ window.ModelRenderer = class {
this.textureWidth, this.textureHeight this.textureWidth, this.textureHeight
); );
this.polygons[3] = new Polygon( polygons[3] = new Polygon(
[vertexTop3, vertexTop4, vertexTop2, vertexTop1], [vertexTop3, vertexTop4, vertexTop2, vertexTop1],
this.textureOffsetX + depth + width, this.textureOffsetX + depth + width,
this.textureOffsetY, this.textureOffsetY,
@@ -100,7 +161,7 @@ window.ModelRenderer = class {
this.textureWidth, this.textureHeight this.textureWidth, this.textureHeight
); );
this.polygons[4] = new Polygon( polygons[4] = new Polygon(
[vertexBottom2, vertexBottom1, vertexTop4, vertexTop3], [vertexBottom2, vertexBottom1, vertexTop4, vertexTop3],
this.textureOffsetX + depth, this.textureOffsetX + depth,
this.textureOffsetY + depth, this.textureOffsetY + depth,
@@ -109,7 +170,7 @@ window.ModelRenderer = class {
this.textureWidth, this.textureHeight this.textureWidth, this.textureHeight
); );
this.polygons[5] = new Polygon( polygons[5] = new Polygon(
[vertexBottom3, vertexBottom4, vertexTop1, vertexTop2], [vertexBottom3, vertexBottom4, vertexTop1, vertexTop2],
this.textureOffsetX + depth + width + depth, this.textureOffsetX + depth + width + depth,
this.textureOffsetY + depth, this.textureOffsetY + depth,
@@ -118,41 +179,65 @@ window.ModelRenderer = class {
this.textureWidth, this.textureHeight this.textureWidth, this.textureHeight
); );
this.cubes.push(polygons);
return this; return this;
} }
/** addChild(model) {
* Set the absolute position of the cube this.children.push(model);
* }
* @param x Absolute x position of cube
* @param y Absolute y position of cube removeChild(model) {
* @param z Absolute z position of cube let index = this.children.indexOf(model);
*/ if (index !== -1) {
setRotationPoint(x, y, z) { this.children.splice(index, 1);
this.rotationPointX = x; }
this.rotationPointY = y; }
this.rotationPointZ = z;
return this; getModelByName(name) {
if (this.name === name) {
return this;
}
for (const child of this.children) {
let innerResult = child.getModelByName(name);
if (innerResult != null) {
return innerResult;
}
}
return null;
} }
rebuild(tessellator, group) { rebuild(tessellator, group) {
// Clear meshes
this.bone.clear(); this.bone.clear();
// Start drawing // Draw cubes
tessellator.startDrawing(); tessellator.startDrawing();
for (let i = 0; i < this.cubes.length; i++) {
let polygons = this.cubes[i];
// Render polygons // Render polygons
for (let i = 0; i < 6; i++) { for (let face = 0; face < 6; face++) {
let polygon = this.polygons[i]; let polygon = polygons[face];
polygon.render(tessellator); polygon.render(tessellator);
}
}
tessellator.draw(this.bone);
// Draw children
for (let i = 0; i < this.children.length; i++) {
let child = this.children[i];
child.rebuild(tessellator, this.bone);
} }
// Finish drawing // Add to group
tessellator.draw(this.bone);
group.add(this.bone); group.add(this.bone);
} }
render(group) { render() {
this.bone.position.setX(this.rotationPointX); this.bone.position.setX(this.rotationPointX);
this.bone.position.setY(this.rotationPointY); this.bone.position.setY(this.rotationPointY);
this.bone.position.setZ(this.rotationPointZ); this.bone.position.setZ(this.rotationPointZ);
@@ -162,6 +247,15 @@ window.ModelRenderer = class {
this.bone.rotation.y = this.rotateAngleY; this.bone.rotation.y = this.rotateAngleY;
this.bone.rotation.z = this.rotateAngleZ; this.bone.rotation.z = this.rotateAngleZ;
this.bone.scale.setX(this.scaleX);
this.bone.scale.setY(this.scaleY);
this.bone.scale.setZ(this.scaleZ);
for (let i = 0; i < this.children.length; i++) {
let child = this.children[i];
child.render();
}
this.bone.updateMatrix(); this.bone.updateMatrix();
} }