Skip to content

Commit

Permalink
Lots of work on improving various aspects of rendering (mekanism#6776)…
Browse files Browse the repository at this point in the history
… particularly pertaining the RenderResizableCuboid:

- Rewrote RenderResizableCuboid to be more based off of how Mantle/Tinkers render things. No longer renders backfaces so not much will be seen if you decide to jump inside an evap tower or something but for now it will do
- Fixed scale calculations sometimes not allowing for it to get to 100% causing things like full dynamic tanks to have a small gap at the top
- Fixed lighting of fluids in multiblocks being a bit incorrect at times
- Fix how fission reactor scales down the heated coolant rendering to greatly reduce the amount of z-fighting that takes place (some still happens especially at greater distances but it looks a lot better up close)
  • Loading branch information
pupnewfster committed Feb 7, 2021
1 parent 823580b commit 7307d6d
Show file tree
Hide file tree
Showing 19 changed files with 438 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static void onStitch(TextureStitchEvent.Pre event) {
return;
}
RenderBioGenerator.resetCachedModels();
RenderFissionReactor.resetCachedModels();
GeneratorsSpecialColors.GUI_OBJECTS.parse(MekanismGenerators.rl("textures/colormap/gui_objects.png"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,36 @@ private Model3D getModel(Direction side, int stage) {
model.setTexture(MekanismRenderer.getFluidTexture(GeneratorsFluids.BIOETHANOL.getFluidStack(1), FluidType.STILL));
switch (side) {
case NORTH:
model.minZ = 0.499;
model.maxZ = 0.875;
model.minZ = 0.499F;
model.maxZ = 0.875F;

model.minX = 0.188;
model.maxX = 0.821;
model.minX = 0.188F;
model.maxX = 0.821F;
break;
case SOUTH:
model.minZ = 0.125;
model.maxZ = 0.499;
model.minZ = 0.125F;
model.maxZ = 0.499F;

model.minX = 0.188;
model.maxX = 0.821;
model.minX = 0.188F;
model.maxX = 0.821F;
break;
case WEST:
model.minX = 0.499;
model.maxX = 0.875;
model.minX = 0.499F;
model.maxX = 0.875F;

model.minZ = 0.187;
model.maxZ = 0.821;
model.minZ = 0.187F;
model.maxZ = 0.821F;
break;
case EAST:
model.minX = 0.125;
model.maxX = 0.499;
model.minX = 0.125F;
model.maxX = 0.499F;

model.minZ = 0.186;
model.maxZ = 0.821;
model.minZ = 0.186F;
model.maxZ = 0.821F;
break;
}
model.minY = 0.4375 + 0.001; //prevent z fighting at low fuel levels
model.maxY = 0.4375 + ((float) stage / stages) * 0.4375 + 0.001;
model.minY = 0.4385F;//0.4375 + 0.001; - prevent z fighting at low fuel levels
model.maxY = 0.4385F + 0.4375F * (stage / (float) stages);//0.4375 + 0.001 + 0.4375 * (stage / (float) stages);
energyDisplays.computeIfAbsent(side, s -> new Int2ObjectOpenHashMap<>()).putIfAbsent(stage, model);
return model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.util.Map;
import javax.annotation.ParametersAreNonnullByDefault;
import mekanism.client.render.MekanismRenderer;
import mekanism.client.render.MekanismRenderer.Model3D;
import mekanism.client.render.ModelRenderer;
import mekanism.client.render.data.ChemicalRenderData.GasRenderData;
import mekanism.client.render.data.FluidRenderData;
import mekanism.client.render.data.RenderData;
import mekanism.client.render.tileentity.MekanismTileEntityRenderer;
import mekanism.generators.common.GeneratorsProfilerConstants;
import mekanism.generators.common.content.fission.FissionReactorMultiblockData;
import mekanism.generators.common.content.fission.FissionReactorValidator.FormedAssembly;
import mekanism.generators.common.tile.fission.TileEntityFissionReactorCasing;
import net.minecraft.client.renderer.Atlases;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.profiler.IProfiler;
import net.minecraft.util.math.BlockPos;

@ParametersAreNonnullByDefault
public class RenderFissionReactor extends MekanismTileEntityRenderer<TileEntityFissionReactorCasing> {

private static final Map<RenderData, Model3D> cachedHeatedCoolantModels = new Object2ObjectOpenHashMap<>();
private static Model3D glowModel;

public static void resetCachedModels() {
cachedHeatedCoolantModels.clear();
glowModel = null;
}

public RenderFissionReactor(TileEntityRendererDispatcher renderer) {
super(renderer);
}
Expand All @@ -38,14 +46,16 @@ protected void render(TileEntityFissionReactorCasing tile, float partialTick, Ma
BlockPos pos = tile.getPos();
IVertexBuilder buffer = renderer.getBuffer(Atlases.getTranslucentCullBlockType());
if (multiblock.isBurning()) {
//TODO - 10.1: Convert the glow model and stuff to being part of the baked model and using model data
// as I am fairly sure that should give a decent boost to performance
if (glowModel == null) {
glowModel = new Model3D();
glowModel.minX = 0.05;
glowModel.minY = 0.01;
glowModel.minZ = 0.05;
glowModel.maxX = 0.95;
glowModel.maxY = 0.99;
glowModel.maxZ = 0.95;
glowModel.minX = 0.05F;
glowModel.minY = 0.01F;
glowModel.minZ = 0.05F;
glowModel.maxX = 0.95F;
glowModel.maxY = 0.99F;
glowModel.maxZ = 0.95F;
glowModel.setTexture(MekanismRenderer.whiteIcon);
}
for (FormedAssembly assembly : multiblock.assemblies) {
Expand All @@ -65,7 +75,7 @@ protected void render(TileEntityFissionReactorCasing tile, float partialTick, Ma
data.height = height;
data.length = multiblock.length();
data.width = multiblock.width();
int glow = data.calculateGlowLight(LightTexture.packLight(0, 15));
int glow = data.calculateGlowLight(MekanismRenderer.FULL_SKY_LIGHT);
matrix.push();
matrix.translate(data.location.getX() - pos.getX(), data.location.getY() - pos.getY(), data.location.getZ() - pos.getZ());
MekanismRenderer.renderObject(ModelRenderer.getModel(data, multiblock.prevCoolantScale), matrix, buffer,
Expand All @@ -82,11 +92,23 @@ protected void render(TileEntityFissionReactorCasing tile, float partialTick, Ma
data.height = height;
data.length = multiblock.length();
data.width = multiblock.width();
int glow = data.calculateGlowLight(LightTexture.packLight(0, 15));
int glow = data.calculateGlowLight(MekanismRenderer.FULL_SKY_LIGHT);
Model3D gasModel;
if (cachedHeatedCoolantModels.containsKey(data)) {
gasModel = cachedHeatedCoolantModels.get(data);
} else {
//Create a slightly shrunken version of the model if it is missing to prevent z-fighting
gasModel = ModelRenderer.getModel(data, 1).copy();
gasModel.minX += 0.01F;
gasModel.minY += 0.01F;
gasModel.minZ += 0.01F;
gasModel.maxX -= 0.01F;
gasModel.maxY -= 0.01F;
gasModel.maxZ -= 0.01F;
cachedHeatedCoolantModels.put(data, gasModel);
}
matrix.push();
matrix.scale(0.998F, 0.998F, 0.998F);
matrix.translate(data.location.getX() - pos.getX() + 0.001, data.location.getY() - pos.getY() + 0.001, data.location.getZ() - pos.getZ() + 0.001);
Model3D gasModel = ModelRenderer.getModel(data, 1);
matrix.translate(data.location.getX() - pos.getX(), data.location.getY() - pos.getY(), data.location.getZ() - pos.getZ());
MekanismRenderer.renderObject(gasModel, matrix, buffer, data.getColorARGB(multiblock.prevHeatedCoolantScale), glow, overlayLight);
matrix.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import mekanism.generators.common.tile.turbine.TileEntityTurbineRotor;
import net.minecraft.client.renderer.Atlases;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.profiler.IProfiler;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -44,7 +43,7 @@ protected void render(TileEntityTurbineCasing tile, float partialTick, MatrixSta
}
matrix.push();
matrix.translate(complexPos.getX() - pos.getX(), complexPos.getY() - pos.getY(), complexPos.getZ() - pos.getZ());
RenderTurbineRotor.INSTANCE.render(rotor, matrix, buffer, LightTexture.packLight(0, 15), overlayLight);
RenderTurbineRotor.INSTANCE.render(rotor, matrix, buffer, MekanismRenderer.FULL_SKY_LIGHT, overlayLight);
matrix.pop();
}
profiler.endSection();
Expand All @@ -56,7 +55,7 @@ protected void render(TileEntityTurbineCasing tile, float partialTick, MatrixSta
data.height = height;
data.length = multiblock.length();
data.width = multiblock.width();
int glow = data.calculateGlowLight(LightTexture.packLight(0, 15));
int glow = data.calculateGlowLight(MekanismRenderer.FULL_SKY_LIGHT);
matrix.push();
matrix.translate(data.location.getX() - pos.getX(), data.location.getY() - pos.getY(), data.location.getZ() - pos.getZ());
Model3D gasModel = ModelRenderer.getModel(data, 1);
Expand Down
82 changes: 50 additions & 32 deletions src/main/java/mekanism/client/render/MekanismRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import mekanism.api.tier.BaseTier;
import mekanism.client.SpecialColors;
import mekanism.client.model.baked.DigitalMinerBakedModel;
import mekanism.client.render.MekanismRenderer.Model3D.SpriteInfo;
import mekanism.client.render.data.FluidRenderData;
import mekanism.client.render.data.ValveRenderData;
import mekanism.client.render.item.block.RenderFluidTankItem;
Expand Down Expand Up @@ -64,6 +65,7 @@ public class MekanismRenderer {
//TODO: Replace various usages of this with the getter for calculating glow light, at least if we end up making it only
// effect block light for the glow rather than having it actually become full light
public static final int FULL_LIGHT = 0xF000F0;
public static final int FULL_SKY_LIGHT = LightTexture.packLight(0, 15);

public static OBJModel contentsModel;
public static TextureAtlasSprite energyIcon;
Expand Down Expand Up @@ -112,14 +114,14 @@ public static TextureAtlasSprite getSprite(ResourceLocation spriteLocation) {
}

public static void prepFlowing(Model3D model, @Nonnull FluidStack fluid) {
TextureAtlasSprite still = getFluidTexture(fluid, FluidType.STILL);
TextureAtlasSprite flowing = getFluidTexture(fluid, FluidType.FLOWING);
SpriteInfo still = new SpriteInfo(getFluidTexture(fluid, FluidType.STILL), 16);
SpriteInfo flowing = new SpriteInfo(getFluidTexture(fluid, FluidType.FLOWING), 8);
model.setTextures(still, still, flowing, flowing, flowing, flowing);
}

public static void renderObject(@Nullable Model3D object, @Nonnull MatrixStack matrix, IVertexBuilder buffer, int argb, int light, int overlay) {
if (object != null) {
RenderResizableCuboid.INSTANCE.renderCube(object, matrix, buffer, argb, light, overlay);
RenderResizableCuboid.renderCube(object, matrix, buffer, argb, light, overlay);
}
}

Expand Down Expand Up @@ -251,17 +253,13 @@ public static int getColorARGB(int red, int green, int blue, float alpha) {
return argb;
}

public static int calculateGlowLight(int light, @Nonnull FluidStack fluid) {
return fluid.isEmpty() ? light : calculateGlowLight(light, fluid.getFluid().getAttributes().getLuminosity(fluid));
public static int calculateGlowLight(int combinedLight, @Nonnull FluidStack fluid) {
return fluid.isEmpty() ? combinedLight : calculateGlowLight(combinedLight, fluid.getFluid().getAttributes().getLuminosity(fluid));
}

public static int calculateGlowLight(int light, int glow) {
if (glow >= 15) {
return MekanismRenderer.FULL_LIGHT;
}
int blockLight = LightTexture.getLightBlock(light);
int skyLight = LightTexture.getLightSky(light);
return LightTexture.packLight(Math.max(blockLight, glow), Math.max(skyLight, glow));
public static int calculateGlowLight(int combinedLight, int glow) {
//Only factor the glow into the block light portion
return (combinedLight & 0xFFFF0000) | Math.max(Math.min(glow, 15) << 4, combinedLight & 0xFFFF);
}

public static void renderColorOverlay(MatrixStack matrix, int x, int y, int width, int height, int color) {
Expand Down Expand Up @@ -412,44 +410,64 @@ public enum FluidType {

public static class Model3D {

public double minX, minY, minZ;
public double maxX, maxY, maxZ;

public final TextureAtlasSprite[] textures = new TextureAtlasSprite[6];
public float minX, minY, minZ;
public float maxX, maxY, maxZ;

public final boolean[] renderSides = new boolean[]{true, true, true, true, true, true, false};

public double sizeX() {
return maxX - minX;
}
private final SpriteInfo[] textures = new SpriteInfo[6];
private final boolean[] renderSides = new boolean[]{true, true, true, true, true, true};

public double sizeY() {
return maxY - minY;
public void setSideRender(Direction side, boolean value) {
renderSides[side.ordinal()] = value;
}

public double sizeZ() {
return maxZ - minZ;
public Model3D copy() {
Model3D copy = new Model3D();
System.arraycopy(textures, 0, copy.textures, 0, textures.length);
System.arraycopy(renderSides, 0, copy.renderSides, 0, renderSides.length);
copy.minX = minX;
copy.minY = minY;
copy.minZ = minZ;
copy.maxX = maxX;
copy.maxY = maxY;
copy.maxZ = maxZ;
return copy;
}

public void setSideRender(Direction side, boolean value) {
renderSides[side.ordinal()] = value;
@Nullable
public SpriteInfo getSpriteToRender(Direction side) {
int ordinal = side.ordinal();
if (renderSides[ordinal]) {
return textures[ordinal];
}
return null;
}

public boolean shouldSideRender(Direction side) {
return renderSides[side.ordinal()];
public void setTexture(TextureAtlasSprite tex) {
setTexture(tex, 16);
}

public void setTexture(TextureAtlasSprite tex) {
Arrays.fill(textures, tex);
public void setTexture(TextureAtlasSprite tex, int size) {
Arrays.fill(textures, new SpriteInfo(tex, size));
}

public void setTextures(TextureAtlasSprite down, TextureAtlasSprite up, TextureAtlasSprite north, TextureAtlasSprite south, TextureAtlasSprite west, TextureAtlasSprite east) {
public void setTextures(SpriteInfo down, SpriteInfo up, SpriteInfo north, SpriteInfo south, SpriteInfo west, SpriteInfo east) {
textures[0] = down;
textures[1] = up;
textures[2] = north;
textures[3] = south;
textures[4] = west;
textures[5] = east;
}

public static class SpriteInfo {

public final TextureAtlasSprite sprite;
public final int size;

private SpriteInfo(TextureAtlasSprite sprite, int size) {
this.sprite = sprite;
this.size = size;
}
}
}
}
Loading

0 comments on commit 7307d6d

Please sign in to comment.