[1.19.x] Fluid API Overhaul (#8695)

This commit is contained in:
ChampionAsh5357 authored and GitHub committed 2022-06-14 23:07:51 +01:00
1 parent e386229b66
commit 2a05c7c01d
67 files changed
+3856 -740

No files matched your search

@@ -17,7 +17,7 @@ public class FluidContainerColorer implements ItemColor
{
if (tintIndex != 1) return 0xFFFFFFFF;
return FluidUtil.getFluidContained(stack)
.map(fstack -> fstack.getFluid().getAttributes().getColor(fstack))
.map(fstack -> RenderProperties.get(fstack.getFluid()).getColorTint(fstack))
.orElse(0xFFFFFFFF);
}
}
@@ -383,8 +383,28 @@ public class ForgeHooksClient
MinecraftForge.EVENT_BUS.post(new ScreenEvent.DrawScreenEvent.Post(screen, poseStack, mouseX, mouseY, partialTick));
}
public static void onFogRender(FogType type, Camera camera, float partialTick, float nearDistance, float farDistance, FogShape shape)
public static Vector3f getFogColor(Camera camera, float partialTick, ClientLevel level, int renderDistance, float darkenWorldAmount, float fogRed, float fogGreen, float fogBlue)
{
// Modify fog color depending on the fluid
FluidState state = level.getFluidState(camera.getBlockPosition());
Vector3f fluidFogColor = new Vector3f(fogRed, fogGreen, fogBlue);
if (camera.getPosition().y < (double)((float)camera.getBlockPosition().getY() + state.getHeight(level, camera.getBlockPosition())))
fluidFogColor = RenderProperties.get(state).modifyFogColor(camera, partialTick, level, renderDistance, darkenWorldAmount, fluidFogColor);
EntityViewRenderEvent.FogColors event = new net.minecraftforge.client.event.EntityViewRenderEvent.FogColors(camera, partialTick, fluidFogColor.x(), fluidFogColor.y(), fluidFogColor.z());
MinecraftForge.EVENT_BUS.post(event);
fluidFogColor.set(event.getRed(), event.getGreen(), event.getBlue());
return fluidFogColor;
}
public static void onFogRender(FogRenderer.FogMode mode, FogType type, Camera camera, float partialTick, float renderDistance, float nearDistance, float farDistance, FogShape shape)
{
// Modify fog rendering depending on the fluid
FluidState state = camera.getEntity().level.getFluidState(camera.getBlockPosition());
if (camera.getPosition().y < (double)((float)camera.getBlockPosition().getY() + state.getHeight(camera.getEntity().level, camera.getBlockPosition())))
RenderProperties.get(state).modifyFogRender(camera, mode, renderDistance, partialTick, nearDistance, farDistance, shape);
EntityViewRenderEvent.RenderFogEvent event = new EntityViewRenderEvent.RenderFogEvent(type, camera, partialTick, nearDistance, farDistance, shape);
if (MinecraftForge.EVENT_BUS.post(event))
{
@@ -441,10 +461,11 @@ public class ForgeHooksClient
@SuppressWarnings("deprecation")
public static TextureAtlasSprite[] getFluidSprites(BlockAndTintGetter level, BlockPos pos, FluidState fluidStateIn)
{
ResourceLocation overlayTexture = fluidStateIn.getType().getAttributes().getOverlayTexture();
IFluidTypeRenderProperties props = RenderProperties.get(fluidStateIn);
ResourceLocation overlayTexture = props.getOverlayTexture(fluidStateIn, level, pos);
return new TextureAtlasSprite[] {
Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(fluidStateIn.getType().getAttributes().getStillTexture(level, pos)),
Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(fluidStateIn.getType().getAttributes().getFlowingTexture(level, pos)),
Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(props.getStillTexture(fluidStateIn, level, pos)),
Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(props.getFlowingTexture(fluidStateIn, level, pos)),
overlayTexture == null ? null : Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(overlayTexture),
};
}
@@ -458,7 +479,7 @@ public class ForgeHooksClient
public static Stream<Material> getFluidMaterials(Fluid fluid)
{
return fluid.getAttributes().getTextures()
return RenderProperties.get(fluid).getTextures()
.filter(Objects::nonNull)
.map(ForgeHooksClient::getBlockMaterial);
}
@@ -0,0 +1,373 @@
package net.minecraftforge.client;
import com.mojang.blaze3d.shaders.FogShape;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import com.mojang.math.Vector3f;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.FogRenderer;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraftforge.client.model.FluidModel;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.stream.Stream;
/**
* An interface which defines properties relating to how a {@link FluidType}
* should render.
*
* <p>Note: This does not define any data regarding {@link RenderType}s as those
* are specified by the Fluid via {@link ItemBlockRenderTypes#setRenderLayer(Fluid, RenderType)}
* in the {@link FMLClientSetupEvent}.
*/
public interface IFluidTypeRenderProperties
{
/**
* A dummy instance returned when no render properties are specified.
*/
IFluidTypeRenderProperties DUMMY = new IFluidTypeRenderProperties()
{
};
/* Default Accessors */
/**
* Returns the tint applied to the fluid's textures.
*
* <p>The result represents a 32-bit integer where each 8-bits represent
* the alpha, red, green, and blue channel respectively.
*
* @return the tint applied to the fluid's textures in ARGB format
*/
default int getColorTint()
{
return 0xFFFFFFFF;
}
/**
* Returns the reference of the texture to apply to a source fluid.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_still} will point to
* {@code assets/minecraft/textures/block/water_still.png}).
*
* <p>Important: This method should only return {@code null} for {@link Fluids#EMPTY}.
* All other implementations must define this property.
*
* @return the reference of the texture to apply to a source fluid
*/
default ResourceLocation getStillTexture()
{
return null;
}
/**
* Returns the reference of the texture to apply to a flowing fluid.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_flow} will point to
* {@code assets/minecraft/textures/block/water_flow.png}).
*
* <p>Important: This method should only return {@code null} for {@link Fluids#EMPTY}.
* All other implementations must define this property.
*
* @return the reference of the texture to apply to a flowing fluid
*/
default ResourceLocation getFlowingTexture()
{
return null;
}
/**
* Returns the reference of the texture to apply to a fluid directly touching
* a non-opaque block other than air. If no reference is specified, either
* {@code #getStillTexture} or {@code #getFlowingTexture} will be applied
* instead.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_overlay} will point to
* {@code assets/minecraft/textures/block/water_overlay.png}).
*
* @return the reference of the texture to apply to a fluid directly touching
* a non-opaque block
*/
@Nullable
default ResourceLocation getOverlayTexture()
{
return null;
}
/**
* Returns a stream of textures applied to a fluid.
*
* <p>This is used by the {@link FluidModel} to load in all textures that
* can be applied on reload.
*
* @return a stream of textures applied to a fluid
*/
default Stream<ResourceLocation> getTextures()
{
return Stream.of(this.getStillTexture(), this.getFlowingTexture(), this.getOverlayTexture())
.filter(Objects::nonNull);
}
/**
* Returns the location of the texture to apply to the camera when it is
* within the fluid. If no location is specified, no overlay will be applied.
*
* <p>This should return a location to the texture and not a reference
* (e.g. {@code minecraft:textures/misc/underwater.png} will use the texture
* at {@code assets/minecraft/textures/misc/underwater.png}).
*
* @param mc the client instance
* @return the location of the texture to apply to the camera when it is
* within the fluid
*/
@Nullable
default ResourceLocation getRenderOverlayTexture(Minecraft mc)
{
return null;
}
/**
* Renders {@code #getRenderOverlayTexture} onto the camera when within
* the fluid.
*
* @param mc the client instance
* @param stack the transformations representing the current rendering position
*/
default void renderOverlay(Minecraft mc, PoseStack stack)
{
ResourceLocation texture = this.getRenderOverlayTexture(mc);
if (texture == null) return;
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.enableTexture();
RenderSystem.setShaderTexture(0, texture);
BufferBuilder buffer = Tesselator.getInstance().getBuilder();
BlockPos playerEyePos = new BlockPos(mc.player.getX(), mc.player.getEyeY(), mc.player.getZ());
float brightness = LightTexture.getBrightness(mc.player.level.dimensionType(), mc.player.level.getMaxLocalRawBrightness(playerEyePos));
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.setShaderColor(brightness, brightness, brightness, 0.1F);
float uOffset = -mc.player.getYRot() / 64.0F;
float vOffset = mc.player.getXRot() / 64.0F;
Matrix4f pose = stack.last().pose();
buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
buffer.vertex(pose, -1.0F, -1.0F, -0.5F).uv(4.0F + uOffset, 4.0F + vOffset).endVertex();
buffer.vertex(pose, 1.0F, -1.0F, -0.5F).uv(uOffset, 4.0F + vOffset).endVertex();
buffer.vertex(pose, 1.0F, 1.0F, -0.5F).uv(uOffset, vOffset).endVertex();
buffer.vertex(pose, -1.0F, 1.0F, -0.5F).uv(4.0F + uOffset, vOffset).endVertex();
BufferUploader.drawWithShader(buffer.end());
RenderSystem.disableBlend();
}
/**
* Modifies the color of the fog when the camera is within the fluid.
*
* <p>The result expects a three float vector representing the red, green,
* and blue channels respectively. Each channel should be between [0,1].
*
* @param camera the camera instance
* @param partialTick the delta time of where the current frame is within a tick
* @param level the level the camera is located in
* @param renderDistance the render distance of the client
* @param darkenWorldAmount the amount to darken the world by
* @param fluidFogColor the current color of the fog
* @return the color of the fog
*/
@NotNull
default Vector3f modifyFogColor(Camera camera, float partialTick, ClientLevel level, int renderDistance, float darkenWorldAmount, Vector3f fluidFogColor)
{
return fluidFogColor;
}
/**
* Modifies how the fog is currently being rendered when the camera is
* within a fluid.
*
* @param camera the camera instance
* @param mode the type of fog being rendered
* @param renderDistance the render distance of the client
* @param partialTick the delta time of where the current frame is within a tick
* @param nearDistance the near plane of where the fog starts to render
* @param farDistance the far plane of where the fog ends rendering
* @param shape the shape of the fog being rendered
*/
default void modifyFogRender(Camera camera, FogRenderer.FogMode mode, float renderDistance, float partialTick, float nearDistance, float farDistance, FogShape shape)
{
}
/* Level-Based Accessors */
/**
* Returns the reference of the texture to apply to a source fluid.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_still} will point to
* {@code assets/minecraft/textures/block/water_still.png}).
*
* <p>Important: This method should only return {@code null} for {@link Fluids#EMPTY}.
* All other implementations must define this property.
*
* @param state the state of the fluid
* @param getter the getter the fluid can be obtained from
* @param pos the position of the fluid
* @return the reference of the texture to apply to a source fluid
*/
default ResourceLocation getStillTexture(FluidState state, BlockAndTintGetter getter, BlockPos pos)
{
return this.getStillTexture();
}
/**
* Returns the reference of the texture to apply to a flowing fluid.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_flow} will point to
* {@code assets/minecraft/textures/block/water_flow.png}).
*
* <p>Important: This method should only return {@code null} for {@link Fluids#EMPTY}.
* All other implementations must define this property.
*
* @param state the state of the fluid
* @param getter the getter the fluid can be obtained from
* @param pos the position of the fluid
* @return the reference of the texture to apply to a flowing fluid
*/
default ResourceLocation getFlowingTexture(FluidState state, BlockAndTintGetter getter, BlockPos pos)
{
return this.getFlowingTexture();
}
/**
* Returns the reference of the texture to apply to a fluid directly touching
* a non-opaque block other than air. If no reference is specified, either
* {@code #getStillTexture} or {@code #getFlowingTexture} will be applied
* instead.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_overlay} will point to
* {@code assets/minecraft/textures/block/water_overlay.png}).
*
* @param state the state of the fluid
* @param getter the getter the fluid can be obtained from
* @param pos the position of the fluid
* @return the reference of the texture to apply to a fluid directly touching
* a non-opaque block
*/
default ResourceLocation getOverlayTexture(FluidState state, BlockAndTintGetter getter, BlockPos pos)
{
return this.getOverlayTexture();
}
/**
* Returns the tint applied to the fluid's textures.
*
* <p>The result represents a 32-bit integer where each 8-bits represent
* the alpha, red, green, and blue channel respectively.
*
* @param state the state of the fluid
* @param getter the getter the fluid can be obtained from
* @param pos the position of the fluid
* @return the tint applied to the fluid's textures in ARGB format
*/
default int getColorTint(FluidState state, BlockAndTintGetter getter, BlockPos pos)
{
return this.getColorTint();
}
/* Stack-Based Accessors */
/**
* Returns the tint applied to the fluid's textures.
*
* <p>The result represents a 32-bit integer where each 8-bits represent
* the alpha, red, green, and blue channel respectively.
*
* @param stack the stack the fluid is in
* @return the tint applied to the fluid's textures in ARGB format
*/
default int getColorTint(FluidStack stack)
{
return this.getColorTint();
}
/**
* Returns the reference of the texture to apply to a source fluid.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_still} will point to
* {@code assets/minecraft/textures/block/water_still.png}).
*
* <p>Important: This method should only return {@code null} for {@link Fluids#EMPTY}.
* All other implementations must define this property.
*
* @param stack the stack the fluid is in
* @return the reference of the texture to apply to a source fluid
*/
default ResourceLocation getStillTexture(FluidStack stack)
{
return this.getStillTexture();
}
/**
* Returns the reference of the texture to apply to a flowing fluid.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_flow} will point to
* {@code assets/minecraft/textures/block/water_flow.png}).
*
* <p>Important: This method should only return {@code null} for {@link Fluids#EMPTY}.
* All other implementations must define this property.
*
* @param stack the stack the fluid is in
* @return the reference of the texture to apply to a flowing fluid
*/
default ResourceLocation getFlowingTexture(FluidStack stack)
{
return this.getFlowingTexture();
}
/**
* Returns the reference of the texture to apply to a fluid directly touching
* a non-opaque block other than air. If no reference is specified, either
* {@code #getStillTexture} or {@code #getFlowingTexture} will be applied
* instead.
*
* <p>This should return a reference to the texture and not the actual
* texture itself (e.g. {@code minecraft:block/water_overlay} will point to
* {@code assets/minecraft/textures/block/water_overlay.png}).
*
* @param stack the stack the fluid is in
* @return the reference of the texture to apply to a fluid directly touching
* a non-opaque block
*/
default ResourceLocation getOverlayTexture(FluidStack stack)
{
return this.getOverlayTexture();
}
}
@@ -11,6 +11,9 @@ import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraftforge.fluids.FluidType;
public class RenderProperties
{
@@ -44,6 +47,21 @@ public class RenderProperties
return block.getRenderPropertiesInternal() instanceof IBlockRenderProperties props ? props : IBlockRenderProperties.DUMMY;
}
public static IFluidTypeRenderProperties get(FluidState state)
{
return get(state.getFluidType());
}
public static IFluidTypeRenderProperties get(Fluid fluid)
{
return get(fluid.getFluidType());
}
public static IFluidTypeRenderProperties get(FluidType type)
{
return type.getRenderPropertiesInternal() instanceof IFluidTypeRenderProperties props ? props : IFluidTypeRenderProperties.DUMMY;
}
private RenderProperties()
{
// Not instantiable.
@@ -41,6 +41,7 @@ import net.minecraft.world.level.GameType;
import net.minecraftforge.client.RenderProperties;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.common.ForgeMod;
import net.minecraftforge.common.MinecraftForge;
import org.apache.logging.log4j.LogManager;
@@ -469,7 +470,7 @@ public class ForgeIngameGui extends Gui
int top = height - right_height;
int air = player.getAirSupply();
if (player.isEyeInFluid(FluidTags.WATER) || air < 300)
if (player.isEyeInFluidType(ForgeMod.WATER_TYPE.get()) || air < 300)
{
int full = Mth.ceil((double)(air - 2) * 10.0D / 300.0D);
int partial = Mth.ceil((double)air * 10.0D / 300.0D) - full;
@@ -22,6 +22,7 @@ import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.RenderProperties;
import net.minecraftforge.client.model.geometry.IModelGeometry;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.registries.ForgeRegistries;
@@ -93,7 +94,7 @@ public final class DynamicBucketModel implements IModelGeometry<DynamicBucketMod
ModelState transformsFromModel = owner.getCombinedTransform();
TextureAtlasSprite fluidSprite = fluid != Fluids.EMPTY ? spriteGetter.apply(ForgeHooksClient.getBlockMaterial(fluid.getAttributes().getStillTexture())) : null;
TextureAtlasSprite fluidSprite = fluid != Fluids.EMPTY ? spriteGetter.apply(ForgeHooksClient.getBlockMaterial(RenderProperties.get(fluid).getStillTexture())) : null;
TextureAtlasSprite coverSprite = (coverLocation != null && (!coverIsMask || baseLocation != null)) ? spriteGetter.apply(coverLocation) : null;
ImmutableMap<TransformType, Transformation> transformMap =
@@ -105,7 +106,7 @@ public final class DynamicBucketModel implements IModelGeometry<DynamicBucketMod
if (particleSprite == null && !coverIsMask) particleSprite = coverSprite;
// if the fluid is lighter than air, will manipulate the initial state to be rotated 180deg to turn it upside down
if (flipGas && fluid != Fluids.EMPTY && fluid.getAttributes().isLighterThanAir())
if (flipGas && fluid != Fluids.EMPTY && fluid.getFluidType().isLighterThanAir())
{
modelTransform = new SimpleModelState(
modelTransform.getRotation().blockCornerToCenter().compose(
@@ -128,8 +129,8 @@ public final class DynamicBucketModel implements IModelGeometry<DynamicBucketMod
if (templateSprite != null)
{
// build liquid layer (inside)
int luminosity = applyFluidLuminosity ? fluid.getAttributes().getLuminosity() : 0;
int color = tint ? fluid.getAttributes().getColor() : 0xFFFFFFFF;
int luminosity = applyFluidLuminosity ? fluid.getFluidType().getLightLevel() : 0;
int color = tint ? RenderProperties.get(fluid).getColorTint() : 0xFFFFFFFF;
builder.addQuads(ItemLayerModel.getLayerRenderType(luminosity > 0), ItemTextureQuadConverter.convertTexture(transform, templateSprite, fluidSprite, NORTH_Z_FLUID, Direction.NORTH, color, 1, luminosity));
builder.addQuads(ItemLayerModel.getLayerRenderType(luminosity > 0), ItemTextureQuadConverter.convertTexture(transform, templateSprite, fluidSprite, SOUTH_Z_FLUID, Direction.SOUTH, color, 1, luminosity));
}
@@ -28,12 +28,13 @@ import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.IFluidTypeRenderProperties;
import net.minecraftforge.client.RenderProperties;
import net.minecraftforge.client.model.data.IModelData;
import net.minecraftforge.client.model.geometry.IModelGeometry;
import net.minecraftforge.client.model.pipeline.BakedQuadBuilder;
import net.minecraftforge.client.model.pipeline.IVertexConsumer;
import net.minecraftforge.client.model.pipeline.TRSRTransformer;
import net.minecraftforge.fluids.FluidAttributes;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@@ -72,16 +73,16 @@ public final class FluidModel implements IModelGeometry<FluidModel>
@Override
public BakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function<Material, TextureAtlasSprite> spriteGetter, ModelState modelTransform, ItemOverrides overrides, ResourceLocation modelLocation)
{
FluidAttributes attrs = fluid.getAttributes();
IFluidTypeRenderProperties props = RenderProperties.get(fluid);
return new CachingBakedFluid(
modelTransform.getRotation(),
PerspectiveMapWrapper.getTransforms(modelTransform),
modelLocation,
attrs.getColor(),
spriteGetter.apply(ForgeHooksClient.getBlockMaterial(attrs.getStillTexture())),
spriteGetter.apply(ForgeHooksClient.getBlockMaterial(attrs.getFlowingTexture())),
Optional.ofNullable(attrs.getOverlayTexture()).map(ForgeHooksClient::getBlockMaterial).map(spriteGetter),
attrs.isLighterThanAir(),
props.getColorTint(),
spriteGetter.apply(ForgeHooksClient.getBlockMaterial(props.getStillTexture())),
spriteGetter.apply(ForgeHooksClient.getBlockMaterial(props.getFlowingTexture())),
Optional.ofNullable(props.getOverlayTexture()).map(ForgeHooksClient::getBlockMaterial).map(spriteGetter),
fluid.getFluidType().isLighterThanAir(),
null
);
}