diff --git a/src/main/java/slimeknights/mantle/client/model/fluid/FluidCuboid.java b/src/main/java/slimeknights/mantle/client/model/fluid/FluidCuboid.java new file mode 100644 index 0000000..d95f85b --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/model/fluid/FluidCuboid.java @@ -0,0 +1,158 @@ +package slimeknights.mantle.client.model.fluid; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import net.minecraft.util.Direction; +import net.minecraft.util.JSONUtils; +import net.minecraft.util.math.vector.Vector3f; +import slimeknights.mantle.client.model.ModelJsonUtils; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +@SuppressWarnings("WeakerAccess") +public class FluidCuboid { + protected static final Map DEFAULT_FACES; + static { + DEFAULT_FACES = new EnumMap<>(Direction.class); + for (Direction direction : Direction.values()) { + DEFAULT_FACES.put(direction, FluidFace.NORMAL); + } + } + + /** Fluid start, scaled for block models */ + @Getter + private final Vector3f from; + /** Fluid end, scaled for block models */ + @Getter + private final Vector3f to; + /** Block faces for the fluid */ + @Getter(value = AccessLevel.PROTECTED) + private Map faces; + + /** Cache for scaled from */ + @Nullable + private Vector3f fromScaled; + /** Cache for scaled to */ + @Nullable + private Vector3f toScaled; + + public FluidCuboid(Vector3f from, Vector3f to, Map faces) { + this.from = from; + this.to = to; + this.faces = faces; + } + + /** + * Checks if the fluid has the given face + * @param face Face to check + * @return True if the face is present + */ + @Nullable + public FluidFace getFace(Direction face) { + return faces.get(face); + } + + /** + * Gets fluid from, scaled for renderer + * @return Scaled from + */ + public Vector3f getFromScaled() { + if (fromScaled == null) { + fromScaled = from.copy(); + fromScaled.mul(1 / 16f); + } + return fromScaled; + } + + /** + * Gets fluid to, scaled for renderer + * @return Scaled from + */ + public Vector3f getToScaled() { + if (toScaled == null) { + toScaled = to.copy(); + toScaled.mul(1 / 16f); + } + return toScaled; + } + + /** + * Creates a new fluid cuboid from JSON + * @param json JSON object + * @return Fluid cuboid + */ + public static FluidCuboid fromJson(JsonObject json) { + Vector3f from = ModelJsonUtils.arrayToVector(json, "from"); + Vector3f to = ModelJsonUtils.arrayToVector(json, "to"); + // if faces is defined, fill with specified faces + Map faces = getFaces(json); + return new FluidCuboid(from, to, faces); + } + + /** + * Gets a list of fluid cuboids from the given parent + * @param parent Parent JSON + * @param key List key + * @return List of cuboids + */ + public static List listFromJson(JsonObject parent, String key) { + JsonElement json = parent.get(key); + + // object: one cube + if (json.isJsonObject()) { + return Collections.singletonList(fromJson(json.getAsJsonObject())); + } + + // array: multiple cubes + if (json.isJsonArray()) { + return ModelJsonUtils.parseList(json.getAsJsonArray(), FluidCuboid::fromJson, key); + } + + throw new JsonSyntaxException("Invalid fluid '" + key + "', must be an array or an object"); + } + + /** + * Gets a face set from the given json element + * @param json JSON parent + * @return Set of faces + */ + protected static Map getFaces(JsonObject json) { + if (!json.has("faces")) { + return DEFAULT_FACES; + } + + Map faces = new EnumMap<>(Direction.class); + JsonObject object = JSONUtils.getJsonObject(json, "faces"); + for (Entry entry : object.entrySet()) { + // if the direction is a face, add it + String name = entry.getKey(); + Direction dir = Direction.byName(name); + if (dir != null) { + JsonObject face = JSONUtils.getJsonObject(entry.getValue(), name); + boolean flowing = JSONUtils.getBoolean(face, "flowing", false); + int rotation = ModelJsonUtils.getRotation(face, "rotation"); + faces.put(dir, new FluidFace(flowing, rotation)); + } else { + throw new JsonSyntaxException("Unknown face '" + name + "'"); + } + } + return faces; + } + + @AllArgsConstructor + @Getter + public static class FluidFace { + public static final FluidFace NORMAL = new FluidFace(false, 0); + private final boolean isFlowing; + private final int rotation; + } +} diff --git a/src/main/java/slimeknights/mantle/client/model/fluid/FluidsModel.java b/src/main/java/slimeknights/mantle/client/model/fluid/FluidsModel.java new file mode 100644 index 0000000..de17483 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/model/fluid/FluidsModel.java @@ -0,0 +1,76 @@ +package slimeknights.mantle.client.model.fluid; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonObject; +import com.mojang.datafixers.util.Pair; +import lombok.AllArgsConstructor; +import lombok.Getter; +import net.minecraft.client.renderer.model.BlockModel; +import net.minecraft.client.renderer.model.IBakedModel; +import net.minecraft.client.renderer.model.IModelTransform; +import net.minecraft.client.renderer.model.IUnbakedModel; +import net.minecraft.client.renderer.model.ItemOverrideList; +import net.minecraft.client.renderer.model.ModelBakery; +import net.minecraft.client.renderer.model.RenderMaterial; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.resources.IResourceManager; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.client.model.BakedModelWrapper; +import net.minecraftforge.client.model.IModelConfiguration; +import net.minecraftforge.client.model.IModelLoader; +import net.minecraftforge.client.model.geometry.IModelGeometry; +import slimeknights.mantle.client.model.ModelJsonUtils; + +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.function.Function; + +/** + * This model contains a list of fluid cuboids for the sake of rendering multiple fluid regions in world. It is used by the faucet at this time + */ +@AllArgsConstructor +public class FluidsModel implements IModelGeometry { + private final BlockModel model; + private final List fluids; + + @Override + public Collection getTextures(IModelConfiguration owner, Function modelGetter, Set> missingTextureErrors) { + return model.getTextures(modelGetter, missingTextureErrors); + } + + @Override + public IBakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function spriteGetter, IModelTransform transform, ItemOverrideList overrides, ResourceLocation location) { + IBakedModel baked = model.bakeModel(bakery, model, spriteGetter, transform, location, true); + return new BakedModel(baked, fluids); + } + + /** Baked model, mostly a data wrapper around a normal model */ + @SuppressWarnings("WeakerAccess") + public static class BakedModel extends BakedModelWrapper { + @Getter + private final List fluids; + private BakedModel(IBakedModel originalModel, List fluids) { + super(originalModel); + this.fluids = fluids; + } + } + + /** Loader for this model */ + public static class Loader implements IModelLoader { + /** + * Shared loader instance + */ + public static final Loader INSTANCE = new Loader(); + + @Override + public void onResourceManagerReload(IResourceManager resourceManager) {} + + @Override + public FluidsModel read(JsonDeserializationContext deserializationContext, JsonObject modelContents) { + BlockModel model = ModelJsonUtils.deserialize(deserializationContext, modelContents); + List fluid = FluidCuboid.listFromJson(modelContents, "fluids"); + return new FluidsModel(model, fluid); + } + } +} diff --git a/src/main/java/slimeknights/mantle/client/model/fluid/package-info.java b/src/main/java/slimeknights/mantle/client/model/fluid/package-info.java new file mode 100644 index 0000000..5aebce2 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/model/fluid/package-info.java @@ -0,0 +1,7 @@ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package slimeknights.mantle.client.model.fluid; + +import mcp.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/src/main/java/slimeknights/mantle/client/render/FluidRenderer.java b/src/main/java/slimeknights/mantle/client/render/FluidRenderer.java new file mode 100644 index 0000000..7712c4a --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/render/FluidRenderer.java @@ -0,0 +1,289 @@ +package slimeknights.mantle.client.render; + +import com.mojang.blaze3d.matrix.MatrixStack; +import com.mojang.blaze3d.vertex.IVertexBuilder; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.IRenderTypeBuffer; +import net.minecraft.client.renderer.RenderState; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.client.renderer.vertex.DefaultVertexFormats; +import net.minecraft.inventory.container.PlayerContainer; +import net.minecraft.util.Direction; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.vector.Matrix4f; +import net.minecraft.util.math.vector.Vector3f; +import net.minecraftforge.fluids.FluidAttributes; +import net.minecraftforge.fluids.FluidStack; +import slimeknights.mantle.Mantle; +import slimeknights.mantle.client.model.fluid.FluidCuboid; +import slimeknights.mantle.client.model.fluid.FluidCuboid.FluidFace; + +import java.util.List; +import java.util.function.Function; + +@SuppressWarnings({"WeakerAccess", "unused"}) +public class FluidRenderer { + /** Render type used for rendering fluids */ + public static final RenderType RENDER_TYPE = RenderType.makeType( + Mantle.modId + ":block_render_type", + DefaultVertexFormats.POSITION_COLOR_TEX_LIGHTMAP, 7, 256, true, false, + RenderType.State.getBuilder().texture(new RenderState.TextureState(PlayerContainer.LOCATION_BLOCKS_TEXTURE, false, false)) + .shadeModel(RenderType.SHADE_ENABLED) + .lightmap(RenderType.LIGHTMAP_ENABLED) + .texture(RenderType.BLOCK_SHEET_MIPPED) + .transparency(RenderType.TRANSLUCENT_TRANSPARENCY) + .build(false)); + + /* Fluid cuboids */ + + /** + * Adds a quad to the renderer + * @param renderer Renderer instnace + * @param matrix Render matrix + * @param sprite Sprite to render + * @param from Quad start + * @param to Quad end + * @param face Face to render + * @param color Color to use in rendering + * @param brightness Face brightness + * @param flowing If true, half texture coordinates + */ + public static void putTexturedQuad(IVertexBuilder renderer, Matrix4f matrix, TextureAtlasSprite sprite, Vector3f from, Vector3f to, Direction face, int color, int brightness, int rotation, boolean flowing) { + // start with texture coordinates + float x1 = from.getX(), y1 = from.getY(), z1 = from.getZ(); + float x2 = to.getX(), y2 = to.getY(), z2 = to.getZ(); + // choose UV based on opposite two axis + float u1, u2, v1, v2; + switch (face.getAxis()) { + case Y: + default: + u1 = x1; u2 = x2; + v1 = z2; v2 = z1; + break; + case Z: + u1 = x2; u2 = x1; + v1 = y1; v2 = y2; + break; + case X: + u1 = z2; u2 = z1; + v1 = y1; v2 = y2; + break; + } + // flip V when relevant + if (rotation == 0 || rotation == 270) { + float temp = v1; + v1 = 1f - v2; + v2 = 1f - temp; + } + // flip U when relevant + if (rotation >= 180) { + float temp = u1; + u1 = 1f - u2; + u2 = 1f - temp; + } + // if rotating by 90 or 270, swap U and V + float minU, maxU, minV, maxV; + double size = flowing ? 8 : 16; + if ((rotation % 180) == 90) { + minU = sprite.getInterpolatedU(v1 * size); + maxU = sprite.getInterpolatedU(v2 * size); + minV = sprite.getInterpolatedV(u1 * size); + maxV = sprite.getInterpolatedV(u2 * size); + } else { + minU = sprite.getInterpolatedU(u1 * size); + maxU = sprite.getInterpolatedU(u2 * size); + minV = sprite.getInterpolatedV(v1 * size); + maxV = sprite.getInterpolatedV(v2 * size); + } + // based on rotation, put coords into place + float u3, u4, v3, v4; + switch(rotation) { + case 0: + default: + u1 = minU; v1 = maxV; + u2 = minU; v2 = minV; + u3 = maxU; v3 = minV; + u4 = maxU; v4 = maxV; + break; + case 90: + u1 = minU; v1 = minV; + u2 = maxU; v2 = minV; + u3 = maxU; v3 = maxV; + u4 = minU; v4 = maxV; + break; + case 180: + u1 = maxU; v1 = minV; + u2 = maxU; v2 = maxV; + u3 = minU; v3 = maxV; + u4 = minU; v4 = minV; + break; + case 270: + u1 = maxU; v1 = maxV; + u2 = minU; v2 = maxV; + u3 = minU; v3 = minV; + u4 = maxU; v4 = minV; + break; + } + // add quads + int light1 = brightness >> 0x10 & 0xFFFF; + int light2 = brightness & 0xFFFF; + int a = color >> 24 & 0xFF; + int r = color >> 16 & 0xFF; + int g = color >> 8 & 0xFF; + int b = color & 0xFF; + switch (face) { + case DOWN: + renderer.pos(matrix, x1, y1, z2).color(r, g, b, a).tex(u1, v1).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x1, y1, z1).color(r, g, b, a).tex(u2, v2).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y1, z1).color(r, g, b, a).tex(u3, v3).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y1, z2).color(r, g, b, a).tex(u4, v4).lightmap(light1, light2).endVertex(); + break; + case UP: + renderer.pos(matrix, x1, y2, z1).color(r, g, b, a).tex(u1, v1).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x1, y2, z2).color(r, g, b, a).tex(u2, v2).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y2, z2).color(r, g, b, a).tex(u3, v3).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y2, z1).color(r, g, b, a).tex(u4, v4).lightmap(light1, light2).endVertex(); + break; + case NORTH: + renderer.pos(matrix, x1, y1, z1).color(r, g, b, a).tex(u1, v1).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x1, y2, z1).color(r, g, b, a).tex(u2, v2).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y2, z1).color(r, g, b, a).tex(u3, v3).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y1, z1).color(r, g, b, a).tex(u4, v4).lightmap(light1, light2).endVertex(); + break; + case SOUTH: + renderer.pos(matrix, x2, y1, z2).color(r, g, b, a).tex(u1, v1).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y2, z2).color(r, g, b, a).tex(u2, v2).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x1, y2, z2).color(r, g, b, a).tex(u3, v3).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x1, y1, z2).color(r, g, b, a).tex(u4, v4).lightmap(light1, light2).endVertex(); + break; + case WEST: + renderer.pos(matrix, x1, y1, z2).color(r, g, b, a).tex(u1, v1).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x1, y2, z2).color(r, g, b, a).tex(u2, v2).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x1, y2, z1).color(r, g, b, a).tex(u3, v3).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x1, y1, z1).color(r, g, b, a).tex(u4, v4).lightmap(light1, light2).endVertex(); + break; + case EAST: + renderer.pos(matrix, x2, y1, z1).color(r, g, b, a).tex(u1, v1).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y2, z1).color(r, g, b, a).tex(u2, v2).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y2, z2).color(r, g, b, a).tex(u3, v3).lightmap(light1, light2).endVertex(); + renderer.pos(matrix, x2, y1, z2).color(r, g, b, a).tex(u4, v4).lightmap(light1, light2).endVertex(); + break; + } + } + + /** + * Renders a full fluid cuboid for the given data + * @param matrices Matrix stack instance + * @param buffer Buffer type + * @param still Still sprite + * @param flowing Flowing sprite + * @param cube Fluid cuboid + * @param from Fluid start + * @param to Fluid end + * @param color Fluid color + * @param light Quad lighting + * @param isGas If true, fluid is a gas + */ + public static void renderCuboid(MatrixStack matrices, IVertexBuilder buffer, FluidCuboid cube, TextureAtlasSprite still, TextureAtlasSprite flowing, Vector3f from, Vector3f to, int color, int light, boolean isGas) { + Matrix4f matrix = matrices.getLast().getMatrix(); + int rotation = isGas ? 180 : 0; + for (Direction dir : Direction.values()) { + FluidFace face = cube.getFace(dir); + if (face != null) { + boolean isFlowing = face.isFlowing(); + int faceRot = (rotation + face.getRotation()) % 360; + putTexturedQuad(buffer, matrix, isFlowing ? flowing : still, from, to, dir, color, light, faceRot, isFlowing); + } + } + } + + /** + * Renders a list of fluid cuboids + * @param matrices Matrix stack instance + * @param buffer Buffer instance + * @param cubes List of cubes to render + * @param fluid Fluid to use in rendering + * @param light Light level from TER + */ + public static void renderCuboids(MatrixStack matrices, IVertexBuilder buffer, List cubes, FluidStack fluid, int light) { + // fluid attributes, fetch once for all fluids to save effort + FluidAttributes attributes = fluid.getFluid().getAttributes(); + Function spriteGetter = Minecraft.getInstance().getAtlasSpriteGetter(PlayerContainer.LOCATION_BLOCKS_TEXTURE); + TextureAtlasSprite still = spriteGetter.apply(attributes.getStillTexture(fluid)); + TextureAtlasSprite flowing = spriteGetter.apply(attributes.getFlowingTexture(fluid)); + int color = attributes.getColor(fluid); + boolean isGas = attributes.isGaseous(fluid); + + // render all given cuboids + for (FluidCuboid cube : cubes) { + renderCuboid(matrices, buffer, cube, still, flowing, cube.getFromScaled(), cube.getToScaled(), color, light, isGas); + } + } + + /** + * Renders a fluid cuboid with the given offset, used to manually place cuboids from a list for rendering {@link #renderCuboids(MatrixStack, IVertexBuilder, List, FluidStack, int)} + * @param matrices Matrix stack instance + * @param buffer Buffer type + * @param cube Fluid cuboid + * @param yOffset Amount to offset the cube in the Y direction, used in faucets for rendering fluid in lower block + * @param still Still sprite + * @param flowing Flowing sprite + * @param color Fluid color + * @param light Quad lighting from TER + * @param isGas If true, fluid is a gas + */ + public static void renderCuboid(MatrixStack matrices, IVertexBuilder buffer, FluidCuboid cube, float yOffset, TextureAtlasSprite still, TextureAtlasSprite flowing, int color, int light, boolean isGas) { + if (yOffset != 0) { + matrices.push(); + matrices.translate(0, yOffset, 0); + } + renderCuboid(matrices, buffer, cube, still, flowing, cube.getFromScaled(), cube.getToScaled(), color, light, isGas); + if (yOffset != 0) { + matrices.pop(); + } + } + + /** + * Renders a fluid cuboid with partial height based on the capacity + * @param matrices Matrix stack instance + * @param buffer Render type buffer instance + * @param fluid Fluid to render + * @param offset Fluid amount offset, used to animate transitions + * @param capacity Fluid tank capacity, must be above 0 + * @param light Quad lighting from TER + * @param cube Fluid cuboid instance + * @param flipGas If true, flips gas cubes + */ + public static void renderScaledCuboid(MatrixStack matrices, IRenderTypeBuffer buffer, FluidCuboid cube, FluidStack fluid, float offset, int capacity, int light, boolean flipGas) { + // nothing to render + if (fluid.isEmpty() || capacity <= 0) { + return; + } + + // fluid attributes + FluidAttributes attributes = fluid.getFluid().getAttributes(); + Function spriteGetter = Minecraft.getInstance().getAtlasSpriteGetter(PlayerContainer.LOCATION_BLOCKS_TEXTURE); + TextureAtlasSprite still = spriteGetter.apply(attributes.getStillTexture(fluid)); + TextureAtlasSprite flowing = spriteGetter.apply(attributes.getFlowingTexture(fluid)); + boolean isGas = attributes.isGaseous(fluid); + + // determine height based on fluid amount + Vector3f from = cube.getFromScaled(); + Vector3f to = cube.getToScaled(); + // gas renders upside down + float minY = from.getY(); + float maxY = to.getY(); + float height = (fluid.getAmount() - offset) / capacity; + if (isGas && flipGas) { + from = from.copy(); + from.setY(maxY + (height * (minY - maxY))); + } else { + to = to.copy(); + to.setY(minY + (height * (maxY - minY))); + } + + // draw cuboid + renderCuboid(matrices, buffer.getBuffer(RENDER_TYPE), cube, still, flowing, from, to, attributes.getColor(fluid), light, isGas); + } +} diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index 8366ea0..436fed7 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -24,3 +24,9 @@ public net.minecraft.client.renderer.model.MultipartBakedModel field_188626_f # # Recipe helper public net.minecraft.item.crafting.RecipeManager func_215366_a(Lnet/minecraft/item/crafting/IRecipeType;)Ljava/util/Map; # getRecipes + +# Fluid rendering +public net.minecraft.client.renderer.RenderState field_228520_l_ # SHADE_ENABLED +public net.minecraft.client.renderer.RenderState field_228528_t_ # LIGHTMAP_ENABLED +public net.minecraft.client.renderer.RenderState field_228521_m_ # BLOCK_SHEET_MIPPED +public net.minecraft.client.renderer.RenderState field_228515_g_ # TRANSLUCENT_TRANSPARENCY