implement swing arm animation and entity brightness

(cherry picked from commit b9e76a3d9bd1c0c89f1ffde1a3516ff115cf3235)
This commit is contained in:
LabyStudio
2022-04-14 18:36:06 +02:00
parent 62d7ba24b9
commit 96b287bedc
14 changed files with 147 additions and 48 deletions
@@ -92,6 +92,8 @@ window.WorldRenderer = class {
// Render entities
for (let entity of this.minecraft.world.entities) {
if (entity === player && this.minecraft.settings.thirdPersonView === 0) {
entity.group.clear();
entity.lastRenderedBrightness = -1; // TODO: Find a better way to trigger this
continue;
}
this.renderEntity(entity, partialTicks);
@@ -2,13 +2,26 @@ window.EntityRenderer = class {
constructor(model) {
this.model = model;
this.tessellator = new Tessellator();
}
rebuild(tessellator, entity) {
this.model.rebuild(tessellator, entity.group);
rebuild(entity) {
let brightness = entity.getEntityBrightness();
entity.lastRenderedBrightness = brightness;
// Apply brightness
this.tessellator.setColor(brightness, brightness, brightness);
// Rebuild
this.model.rebuild(this.tessellator, entity.group);
}
render(entity, partialTicks) {
let brightness = entity.getEntityBrightness();
if (entity.lastRenderedBrightness !== brightness) {
this.rebuild(entity);
}
let group = entity.group;
let rotationBody = this.interpolateRotation(entity.prevRenderYawOffset, entity.renderYawOffset, partialTicks);
@@ -32,19 +45,20 @@ window.EntityRenderer = class {
// Actual size of the entity
let scale = 7.0 / 120.0;
group.scale.set(scale, -scale, scale);
group.scale.set(-scale,- scale, scale);
// Rotate entity model
group.rotation.y = MathHelper.toRadians(-rotationBody + 180);
// Render entity model
let timeAlive = entity.ticksExisted + partialTicks;
this.model.render(entity, limbSwingAmount, limbSwing, timeAlive, yaw, pitch);
this.model.render(entity, limbSwingAmount, limbSwing, timeAlive, yaw, pitch, partialTicks);
}
interpolateRotation(prevValue, value, partialTicks) {
let factor;
for (factor = value - prevValue; factor < -180.0; factor += 360.0) {}
for (factor = value - prevValue; factor < -180.0; factor += 360.0) {
}
while (factor >= 180.0) {
factor -= 360.0;
}
@@ -10,13 +10,12 @@ window.PlayerRenderer = class extends EntityRenderer {
}
rebuild(tessellator, entity) {
tessellator.bindTexture(this.textureCharacter);
this.tessellator.bindTexture(this.textureCharacter);
super.rebuild(tessellator, entity);
}
render(entity, partialTicks) {
super.render(entity, partialTicks);
}
}
@@ -8,6 +8,7 @@ window.FontRenderer = class {
constructor() {
this.charWidths = [];
this.isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
this.texture = Gui.loadTexture("gui/font.png")
let bitMap = this.createBitMap(this.texture);
@@ -44,7 +45,9 @@ window.FontRenderer = class {
}
drawString(stack, string, x, y, color = -1) {
this.drawStringRaw(stack, string, x + 1, y + 1, (color & 0xFCFCFC) >> 2, true);
if (!this.isSafari) { // TODO Fix brightness filter on Safari
this.drawStringRaw(stack, string, x + 1, y + 1, (color & 0xFCFCFC) >> 2, true);
}
this.drawStringRaw(stack, string, x, y, color, false);
}
@@ -19,18 +19,18 @@ window.ModelPlayer = class extends ModelBase {
.setTextureOffset(16, 16)
.setBox(-4.0, 0.0, -2.0, 8, 12, 4);
// Right arm ModelRenderer
this.rightArm = new ModelRenderer(width, height)
.setTextureOffset(40, 16)
.setRotationPoint(-5.0, 2.0, 0.0)
.setBox(-3.0, -2.0, -2.0, 4, 12, 4);
// Left arm ModelRenderer
this.leftArm = new ModelRenderer(width, height)
.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);
// Right arm ModelRenderer
this.rightArm = new ModelRenderer(width, height)
.setTextureOffset(40, 16)
.setRotationPoint(-3.0, 2.0, -2.0)
.setBox(-3.0, -2.0, -2.0, 4, 12, 4);
// Right Legs ModelRenderer
this.rightLeg = new ModelRenderer(width, height)
.setTextureOffset(0, 16)
@@ -55,7 +55,7 @@ window.ModelPlayer = class extends ModelBase {
this.rightLeg.rebuild(tessellator, group);
}
render(entity, limbSwingAmount, limbSwing, timeAlive, yaw, pitch) {
render(entity, limbSwingAmount, limbSwing, timeAlive, yaw, pitch, partialTicks) {
let group = entity.group;
this.head.rotateAngleY = MathHelper.toRadians(yaw);
@@ -71,6 +71,40 @@ window.ModelPlayer = class extends ModelBase {
this.rightArm.rotateAngleY = 0.0;
this.rightArm.rotateAngleZ = 0.0;
this.leftArm.rotateAngleY = 0.0;
// Swing progress
let swingProgress = entity.swingProgress - entity.prevSwingProgress;
if (swingProgress < 0.0) {
swingProgress++;
}
let interpolatedSwingProgress = entity.prevSwingProgress + swingProgress * partialTicks;
if (interpolatedSwingProgress > -9990.0) {
let swingProgress = interpolatedSwingProgress;
this.body.rotateAngleY = Math.sin(Math.sqrt(swingProgress) * Math.PI * 2.0) * 0.2;
this.rightArm.rotationPointZ = Math.sin(this.body.rotateAngleY) * 5.0;
this.rightArm.rotationPointX = -Math.cos(this.body.rotateAngleY) * 5.0;
this.leftArm.rotationPointZ = -Math.sin(this.body.rotateAngleY) * 5.0;
this.leftArm.rotationPointX = Math.cos(this.body.rotateAngleY) * 5.0;
this.rightArm.rotateAngleY += this.body.rotateAngleY;
this.leftArm.rotateAngleY += this.body.rotateAngleY;
this.leftArm.rotateAngleX += this.body.rotateAngleY;
swingProgress = 1.0 - interpolatedSwingProgress;
swingProgress = swingProgress * swingProgress;
swingProgress = swingProgress * swingProgress;
swingProgress = 1.0 - swingProgress;
let value1 = Math.sin(swingProgress * Math.PI);
let value2 = Math.sin(interpolatedSwingProgress * Math.PI) * -(this.head.rotateAngleX - 0.7) * 0.75;
this.rightArm.rotateAngleX = (this.rightArm.rotateAngleX - (value1 * 1.2 + value2));
this.rightArm.rotateAngleY += this.body.rotateAngleY * 2.0;
this.rightArm.rotateAngleZ += Math.sin(interpolatedSwingProgress * Math.PI) * -0.4;
}
if (entity.sneaking) {
this.body.rotateAngleX = 0.5;
@@ -157,7 +157,7 @@ window.ModelRenderer = class {
this.bone.rotation.order = 'ZYX';
this.bone.rotation.x = this.rotateAngleX;
this.bone.rotation.y = -this.rotateAngleY;
this.bone.rotation.y = this.rotateAngleY;
this.bone.rotation.z = this.rotateAngleZ;
this.bone.updateMatrix();
@@ -21,9 +21,6 @@ window.Polygon = class {
}
render(tessellator) {
// Set color of polygon
tessellator.setColor(1, 1, 1);
// Render all vertices
for (let i = 3; i >= 0; i--) {
let vertex = this.vertices[i];