Fix JEI Scene Lighting

- Fix sudden light transitions in JEI scenes
- Allow block GuiGameElements to set custom lighting
- Remove deprecated GuiGameElement methods
This commit is contained in:
PepperBell 2021-05-06 14:14:31 -07:00
parent 4ed97b3655
commit 970a5c2f53
6 changed files with 103 additions and 41 deletions

View file

@ -1,7 +1,6 @@
package com.simibubi.create.compat.jei.category.animations;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.foundation.gui.GuiGameElement;
@ -14,11 +13,11 @@ public class AnimatedCrushingWheels extends AnimatedKinetics {
@Override
public void draw(MatrixStack matrixStack, int xOffset, int yOffset) {
RenderSystem.enableDepthTest();
matrixStack.push();
matrixStack.translate(xOffset, yOffset, 100);
matrixStack.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(-22.5f));
int scale = 22;
BlockState wheel = AllBlocks.CRUSHING_WHEEL.get()
.getDefaultState()
.with(BlockStateProperties.AXIS, Axis.X);
@ -33,6 +32,8 @@ public class AnimatedCrushingWheels extends AnimatedKinetics {
.atLocal(2, 0, 0)
.scale(scale)
.render(matrixStack);
matrixStack.pop();
}
}

View file

@ -35,12 +35,15 @@ import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector2f;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.fluids.FluidStack;
public class GuiGameElement {
public static Vector2f defaultBlockLighting = new Vector2f(30.0f, 7.5f);
public static GuiRenderBuilder of(ItemStack stack) {
return new GuiItemRenderBuilder(stack);
}
@ -64,11 +67,13 @@ public class GuiGameElement {
}
public static abstract class GuiRenderBuilder extends RenderElement {
double xLocal, yLocal, zLocal;
double xRot, yRot, zRot;
double scale = 1;
int color = 0xFFFFFF;
Vector3d rotationOffset = Vector3d.ZERO;
protected double xLocal, yLocal, zLocal;
protected double xRot, yRot, zRot;
protected double scale = 1;
protected int color = 0xFFFFFF;
protected Vector3d rotationOffset = Vector3d.ZERO;
protected boolean hasCustomLighting = false;
protected float lightingXRot, lightingYRot;
public GuiRenderBuilder atLocal(double x, double y, double z) {
this.xLocal = x;
@ -104,33 +109,25 @@ public class GuiGameElement {
return this;
}
public abstract void render(MatrixStack matrixStack);
public GuiRenderBuilder lighting(float xRot, float yRot) {
hasCustomLighting = true;
lightingXRot = xRot;
lightingYRot = yRot;
return this;
}
@Deprecated
protected void prepare() {}
public abstract void render(MatrixStack matrixStack);
protected void prepareMatrix(MatrixStack matrixStack) {
matrixStack.push();
RenderSystem.enableBlend();
RenderSystem.enableRescaleNormal();
RenderSystem.enableAlphaTest();
RenderHelper.enableGuiDepthLighting();
RenderSystem.alphaFunc(516, 0.1F);
RenderSystem.blendFunc(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA);
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
}
@Deprecated
protected void transform() {
RenderSystem.translated(x, y, 0);
RenderSystem.scaled(scale, scale, scale);
RenderSystem.translated(xLocal, yLocal, zLocal);
RenderSystem.scaled(1, -1, 1);
RenderSystem.translated(rotationOffset.x, rotationOffset.y, rotationOffset.z);
RenderSystem.rotatef((float) zRot, 0, 0, 1);
RenderSystem.rotatef((float) xRot, 1, 0, 0);
RenderSystem.rotatef((float) yRot, 0, 1, 0);
RenderSystem.translated(-rotationOffset.x, -rotationOffset.y, -rotationOffset.z);
RenderSystem.alphaFunc(516, 0.1F);
RenderSystem.enableAlphaTest();
RenderSystem.blendFunc(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA);
RenderSystem.enableBlend();
RenderSystem.enableDepthTest();
RenderSystem.enableRescaleNormal();
prepareLighting(matrixStack);
}
protected void transformMatrix(MatrixStack matrixStack) {
@ -145,13 +142,18 @@ public class GuiGameElement {
matrixStack.translate(-rotationOffset.x, -rotationOffset.y, -rotationOffset.z);
}
@Deprecated
protected void cleanUp() {}
protected void cleanUpMatrix(MatrixStack matrixStack) {
matrixStack.pop();
RenderSystem.disableAlphaTest();
RenderSystem.disableRescaleNormal();
RenderSystem.disableAlphaTest();
cleanUpLighting(matrixStack);
}
protected void prepareLighting(MatrixStack matrixStack) {
RenderHelper.enableGuiDepthLighting();
}
protected void cleanUpLighting(MatrixStack matrixStack) {
}
}
@ -197,6 +199,20 @@ public class GuiGameElement {
0xF000F0, OverlayTexture.DEFAULT_UV, VirtualEmptyModelData.INSTANCE);
buffer.draw();
}
@Override
protected void prepareLighting(MatrixStack matrixStack) {
if (hasCustomLighting) {
UIRenderHelper.setupSimpleCustomLighting(lightingXRot, lightingYRot);
} else {
UIRenderHelper.setupSimpleCustomLighting(defaultBlockLighting.x, defaultBlockLighting.y);
}
}
@Override
protected void cleanUpLighting(MatrixStack matrixStack) {
RenderHelper.enableGuiDepthLighting();
}
}
public static class GuiBlockStateRenderBuilder extends GuiBlockModelRenderBuilder {
@ -214,8 +230,8 @@ public class GuiGameElement {
RenderHelper.disableGuiDepthLighting();
blockRenderer.renderBlock(blockState, ms, buffer, 0xF000F0, OverlayTexture.DEFAULT_UV,
VirtualEmptyModelData.INSTANCE);
RenderHelper.enable();
buffer.draw();
RenderHelper.enableGuiDepthLighting();
return;
}
@ -281,19 +297,19 @@ public class GuiGameElement {
matrixStack.translate((float) 0, (float) 0, 100.0F + renderer.zLevel);
matrixStack.translate(8.0F, -8.0F, 0.0F);
matrixStack.scale(16.0F, 16.0F, 16.0F);
IRenderTypeBuffer.Impl irendertypebuffer$impl = Minecraft.getInstance()
IRenderTypeBuffer.Impl buffer = Minecraft.getInstance()
.getBufferBuilders()
.getEntityVertexConsumers();
boolean flag = !bakedModel.isSideLit();
if (flag) {
boolean flatLighting = !bakedModel.isSideLit();
if (flatLighting) {
RenderHelper.disableGuiDepthLighting();
}
renderer.renderItem(stack, ItemCameraTransforms.TransformType.GUI, false, matrixStack,
irendertypebuffer$impl, 15728880, OverlayTexture.DEFAULT_UV, bakedModel);
irendertypebuffer$impl.draw();
buffer, 0xF000F0, OverlayTexture.DEFAULT_UV, bakedModel);
buffer.draw();
RenderSystem.enableDepthTest();
if (flag) {
if (flatLighting) {
RenderHelper.enableGuiDepthLighting();
}

View file

@ -10,6 +10,7 @@ import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.simibubi.create.foundation.utility.ColorHelper;
import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.VecHelper;
import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft;
@ -257,4 +258,14 @@ public class UIRenderHelper {
RenderSystem.enableAlphaTest();
WorldVertexBufferUploader.draw(bufferbuilder);
}
public static void setupSimpleCustomLighting(float xRot, float yRot) {
Matrix4f lightingMatrix = new Matrix4f();
lightingMatrix.loadIdentity();
lightingMatrix.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(yRot));
lightingMatrix.multiply(Vector3f.POSITIVE_X.getDegreesQuaternion(xRot));
lightingMatrix.multiply(Matrix4f.translate(0, 0, 1));
RenderSystem.setupLevelDiffuseLighting(VecHelper.ZERO_3F, VecHelper.ZERO_3F, lightingMatrix);
}
}

View file

@ -0,0 +1,32 @@
package com.simibubi.create.foundation.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.util.math.MathHelper;
/**
* Vanilla's fast inverse cube root function returns nonsensical results for negative
* numbers, which results in incorrect vertex normal scaling. By negating the input
* and output accordingly, this issue can be prevented.
*/
@Mixin(MathHelper.class)
public class FixInverseCbrtMixin {
@ModifyVariable(at = @At("HEAD"), method = "fastInverseCbrt(F)F")
private static float negateAtHead(float input) {
if (input < 0) {
input *= -1;
}
return input;
}
@Inject(at = @At("TAIL"), method = "fastInverseCbrt(F)F", cancellable = true)
private static void negateAtTail(float input, CallbackInfoReturnable<Float> cir) {
if (input < 0) {
cir.setReturnValue(cir.getReturnValueF() * -1);
}
}
}

View file

@ -21,6 +21,7 @@ import net.minecraft.util.math.vector.Vector3i;
public class VecHelper {
public static final Vector3f ZERO_3F = new Vector3f(0, 0, 0);
public static final Vector3d CENTER_OF_ORIGIN = new Vector3d(.5, .5, .5);
public static Vector3d rotate(Vector3d vec, Vector3d rotationVec) {

View file

@ -9,6 +9,7 @@
"client": [
"CancelTileEntityRenderMixin",
"EntityContraptionInteractionMixin",
"FixInverseCbrtMixin",
"FogColorTrackerMixin",
"HeavyBootsOnPlayerMixin",
"LightUpdateMixin",