Skip to content

Commit

Permalink
new: fabric-rendering-fluids-v1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jellysquid3 committed Jun 27, 2021
1 parent db6121d commit 5444aeb
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 65 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API
modImplementation(fabricApi.module("fabric-rendering-fluids-v1", project.fabric_version))
}

if (project.use_third_party_mods) {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ org.gradle.jvmargs=-Xmx1G
minecraft_version=1.17
yarn_mappings=1.17+build.1
loader_version=0.11.3
fabric_version=0.34.9+1.17

# Mod Properties
mod_version=0.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.jellysquid.mods.sodium.client.model.quad;

import net.minecraft.block.BlockState;
import net.minecraft.state.State;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockRenderView;
import org.jetbrains.annotations.Nullable;

public interface ModelQuadColorProvider<T> {
int getColor(T state, @Nullable BlockRenderView world, @Nullable BlockPos pos, int tintIndex);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.jellysquid.mods.sodium.client.model.quad.blender;

import me.jellysquid.mods.sodium.client.model.quad.ModelQuadView;
import net.minecraft.block.BlockState;
import net.minecraft.client.color.block.BlockColorProvider;
import me.jellysquid.mods.sodium.client.model.quad.ModelQuadColorProvider;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockRenderView;

Expand All @@ -12,13 +11,12 @@ public interface BiomeColorBlender {
* The array returned by this method may be re-used in subsequent calls in order to reduce memory allocations, and
* as such, the contents of an array returned by this method is undefined after a subsequent call.
*
* @param colorizer The color sampling source
* @param world The world to sample biomes (and as a result, colors) from
* @param state The block state being rendered
* @param origin The position of the block being rendered
* @param quad The quad which will be colorized
* @param colorizer The color sampling source
* @param state The block state being rendered
* @return An array of integer colors in ABGR format
*/
int[] getColors(BlockColorProvider colorizer, BlockRenderView world, BlockState state, BlockPos origin,
ModelQuadView quad);
<T> int[] getColors(BlockRenderView world, BlockPos origin, ModelQuadView quad, ModelQuadColorProvider<T> colorizer, T state);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package me.jellysquid.mods.sodium.client.model.quad.blender;

import me.jellysquid.mods.sodium.client.model.quad.ModelQuadView;
import me.jellysquid.mods.sodium.client.model.quad.ModelQuadColorProvider;
import me.jellysquid.mods.sodium.client.util.color.ColorARGB;
import net.minecraft.block.BlockState;
import net.minecraft.client.color.block.BlockColorProvider;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockRenderView;

Expand All @@ -16,8 +15,7 @@ public class FlatBiomeColorBlender implements BiomeColorBlender {
private final int[] cachedRet = new int[4];

@Override
public int[] getColors(BlockColorProvider colorizer, BlockRenderView world, BlockState state, BlockPos origin,
ModelQuadView quad) {
public <T> int[] getColors(BlockRenderView world, BlockPos origin, ModelQuadView quad, ModelQuadColorProvider<T> colorizer, T state) {
Arrays.fill(this.cachedRet, ColorARGB.toABGR(colorizer.getColor(state, world, origin, quad.getColorIndex())));

return this.cachedRet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import me.jellysquid.mods.sodium.client.model.quad.ModelQuadView;
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadFlags;
import me.jellysquid.mods.sodium.client.model.quad.ModelQuadColorProvider;
import me.jellysquid.mods.sodium.client.util.color.ColorABGR;
import me.jellysquid.mods.sodium.client.util.color.ColorARGB;
import me.jellysquid.mods.sodium.client.util.color.ColorU8;
import net.minecraft.block.BlockState;
import net.minecraft.client.color.block.BlockColorProvider;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockRenderView;

Expand All @@ -16,8 +15,7 @@ public class SmoothBiomeColorBlender implements BiomeColorBlender {
private final BlockPos.Mutable mpos = new BlockPos.Mutable();

@Override
public int[] getColors(BlockColorProvider colorizer, BlockRenderView world, BlockState state, BlockPos origin,
ModelQuadView quad) {
public <T> int[] getColors(BlockRenderView world, BlockPos origin, ModelQuadView quad, ModelQuadColorProvider<T> colorizer, T state) {
final int[] colors = this.cachedRet;

boolean aligned = ModelQuadFlags.contains(quad.getFlags(), ModelQuadFlags.IS_ALIGNED);
Expand All @@ -34,8 +32,8 @@ public int[] getColors(BlockColorProvider colorizer, BlockRenderView world, Bloc
return colors;
}

private int getVertexColor(BlockColorProvider colorizer, BlockRenderView world, BlockState state, BlockPos origin,
ModelQuadView quad, int vertexIdx) {
private <T> int getVertexColor(ModelQuadColorProvider<T> colorizer, BlockRenderView world, T state, BlockPos origin,
ModelQuadView quad, int vertexIdx) {
final int x = origin.getX() + (int) quad.getX(vertexIdx);
final int z = origin.getZ() + (int) quad.getZ(vertexIdx);

Expand All @@ -44,13 +42,13 @@ private int getVertexColor(BlockColorProvider colorizer, BlockRenderView world,
return ColorARGB.toABGR(color);
}

private int getBlockColor(BlockColorProvider colorizer, BlockRenderView world, BlockState state, BlockPos origin,
int x, int z, int colorIdx) {
private <T> int getBlockColor(ModelQuadColorProvider<T> colorizer, BlockRenderView world, T state, BlockPos origin,
int x, int z, int colorIdx) {
return colorizer.getColor(state, world, this.mpos.set(x, origin.getY(), z), colorIdx);
}

private int getInterpolatedVertexColor(BlockColorProvider colorizer, BlockRenderView world, BlockState state,
BlockPos origin, ModelQuadView quad, int vertexIdx) {
private <T> int getInterpolatedVertexColor(ModelQuadColorProvider<T> colorizer, BlockRenderView world, T state,
BlockPos origin, ModelQuadView quad, int vertexIdx) {
final float x = quad.getX(vertexIdx);
final float z = quad.getZ(vertexIdx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadOrientation;
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadWinding;
import me.jellysquid.mods.sodium.client.model.quad.ModelQuadColorProvider;
import me.jellysquid.mods.sodium.client.render.chunk.compile.buffers.ChunkModelBuilder;
import me.jellysquid.mods.sodium.client.render.chunk.format.ModelVertexSink;
import me.jellysquid.mods.sodium.client.render.occlusion.BlockOcclusionCache;
Expand All @@ -19,7 +20,6 @@
import me.jellysquid.mods.sodium.common.util.DirectionUtil;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.color.block.BlockColorProvider;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.texture.Sprite;
Expand Down Expand Up @@ -91,7 +91,7 @@ public boolean renderModel(BlockRenderView world, BlockState state, BlockPos pos

private void renderQuadList(BlockRenderView world, BlockState state, BlockPos pos, BlockPos origin, LightPipeline lighter, Vec3d offset,
ChunkModelBuilder buffers, List<BakedQuad> quads, ModelQuadFacing facing) {
BlockColorProvider colorizer = null;
ModelQuadColorProvider<BlockState> colorizer = null;

ModelVertexSink vertices = buffers.getVertexSink();
vertices.ensureCapacity(quads.size() * 4);
Expand All @@ -117,14 +117,14 @@ private void renderQuadList(BlockRenderView world, BlockState state, BlockPos po
}

private void renderQuad(BlockRenderView world, BlockState state, BlockPos pos, BlockPos origin, ModelVertexSink vertices, IndexBufferBuilder indices, Vec3d blockOffset,
BlockColorProvider colorProvider, BakedQuad bakedQuad, QuadLightData light, ChunkModelBuilder model) {
ModelQuadColorProvider<BlockState> colorProvider, BakedQuad bakedQuad, QuadLightData light, ChunkModelBuilder model) {
ModelQuadView src = (ModelQuadView) bakedQuad;
ModelQuadOrientation orientation = ModelQuadOrientation.orientByBrightness(light.br);

int[] colors = null;

if (bakedQuad.hasColor()) {
colors = this.biomeColorBlender.getColors(colorProvider, world, state, pos, src);
colors = this.biomeColorBlender.getColors(world, pos, src, colorProvider, state);
}

int vertexStart = vertices.getVertexCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@
import me.jellysquid.mods.sodium.client.model.quad.blender.BiomeColorBlender;
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadFlags;
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadOrientation;
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadWinding;
import me.jellysquid.mods.sodium.client.model.quad.ModelQuadColorProvider;
import me.jellysquid.mods.sodium.client.render.chunk.compile.buffers.ChunkModelBuilder;
import me.jellysquid.mods.sodium.client.render.chunk.format.ModelVertexSink;
import me.jellysquid.mods.sodium.client.util.Norm3b;
import me.jellysquid.mods.sodium.client.util.color.ColorABGR;
import me.jellysquid.mods.sodium.common.util.DirectionUtil;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandler;
import net.fabricmc.fabric.impl.client.rendering.fluid.FluidRenderHandlerRegistryImpl;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.StainedGlassBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.color.block.BlockColorProvider;
import net.minecraft.client.color.world.BiomeColors;
import net.minecraft.client.render.block.BlockModels;
import net.minecraft.client.render.model.ModelLoader;
import net.minecraft.client.texture.Sprite;
import net.minecraft.fluid.Fluid;
Expand All @@ -37,40 +36,29 @@
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockRenderView;
import org.jetbrains.annotations.Nullable;

public class FluidRenderer {
// TODO: allow this to be changed by vertex format
// TODO: move fluid rendering to a separate render pass and control glPolygonOffset and glDepthFunc to fix this properly
private static final float EPSILON = 0.001f;

private static final BlockColorProvider FLUID_COLOR_PROVIDER = (state, world, pos, tintIndex) -> {
if (world == null) return 0xFFFFFFFF;
return BiomeColors.getWaterColor(world, pos);
};

private final BlockPos.Mutable scratchPos = new BlockPos.Mutable();

private final Sprite[] lavaSprites = new Sprite[2];
private final Sprite[] waterSprites = new Sprite[2];
private final Sprite waterOverlaySprite;

private final ModelQuadViewMutable quad = new ModelQuad();

private final LightPipelineProvider lighters;
private final BiomeColorBlender biomeColorBlender;

// Cached wrapper type that adapts FluidRenderHandler to support QuadColorProvider<FluidState>
private final FabricFluidColorizerAdapter fabricColorProviderAdapter = new FabricFluidColorizerAdapter();

private final QuadLightData quadLightData = new QuadLightData();
private final int[] quadColors = new int[4];

public FluidRenderer(MinecraftClient client, LightPipelineProvider lighters, BiomeColorBlender biomeColorBlender) {
BlockModels models = client.getBakedModelManager().getBlockModels();

this.lavaSprites[0] = models.getModel(Blocks.LAVA.getDefaultState()).getSprite();
this.lavaSprites[1] = ModelLoader.LAVA_FLOW.getSprite();

this.waterSprites[0] = models.getModel(Blocks.WATER.getDefaultState()).getSprite();
this.waterSprites[1] = ModelLoader.WATER_FLOW.getSprite();

public FluidRenderer(LightPipelineProvider lighters, BiomeColorBlender biomeColorBlender) {
this.waterOverlaySprite = ModelLoader.WATER_OVERLAY.getSprite();

int normal = Norm3b.pack(0.0f, 1.0f, 0.0f);
Expand Down Expand Up @@ -130,8 +118,12 @@ public boolean render(BlockRenderView world, FluidState fluidState, BlockPos pos
return false;
}

boolean lava = fluidState.isIn(FluidTags.LAVA);
Sprite[] sprites = lava ? this.lavaSprites : this.waterSprites;
boolean isWater = fluidState.isIn(FluidTags.WATER);

FluidRenderHandler handler = FluidRenderHandlerRegistryImpl.INSTANCE.getOverride(fluidState.getFluid());
ModelQuadColorProvider<FluidState> colorizer = this.createColorProviderAdapter(handler);

Sprite[] sprites = handler.getFluidSprites(world, pos, fluidState);

boolean rendered = false;

Expand All @@ -144,7 +136,7 @@ public boolean render(BlockRenderView world, FluidState fluidState, BlockPos pos

final ModelQuadViewMutable quad = this.quad;

LightMode lightMode = !lava && MinecraftClient.isAmbientOcclusionEnabled() ? LightMode.SMOOTH : LightMode.FLAT;
LightMode lightMode = isWater && MinecraftClient.isAmbientOcclusionEnabled() ? LightMode.SMOOTH : LightMode.FLAT;
LightPipeline lighter = this.lighters.getLighter(lightMode);

quad.setFlags(0);
Expand Down Expand Up @@ -211,7 +203,7 @@ public boolean render(BlockRenderView world, FluidState fluidState, BlockPos pos
this.setVertex(quad, 2, 1.0F, h3, 1.0F, u3, v3);
this.setVertex(quad, 3, 1.0F, h4, 0.0f, u4, v4);

this.calculateQuadColors(quad, world, pos, lighter, Direction.UP, 1.0F, !lava);
this.calculateQuadColors(quad, world, pos, lighter, Direction.UP, 1.0F, colorizer, fluidState);

int vertexStart = this.writeVertices(buffers, offset, quad);

Expand Down Expand Up @@ -240,7 +232,7 @@ public boolean render(BlockRenderView world, FluidState fluidState, BlockPos pos
this.setVertex(quad, 2, 1.0F, yOffset, 0.0f, maxU, minV);
this.setVertex(quad, 3, 1.0F, yOffset, 1.0F, maxU, maxV);

this.calculateQuadColors(quad, world, pos, lighter, Direction.DOWN, 1.0F, !lava);
this.calculateQuadColors(quad, world, pos, lighter, Direction.DOWN, 1.0F, colorizer, fluidState);

int vertexStart = this.writeVertices(buffers, offset, quad);

Expand Down Expand Up @@ -320,7 +312,7 @@ public boolean render(BlockRenderView world, FluidState fluidState, BlockPos pos

Sprite sprite = sprites[1];

if (!lava) {
if (isWater) {
BlockPos posAdj = this.scratchPos.set(adjX, adjY, adjZ);
Block block = world.getBlockState(posAdj).getBlock();

Expand All @@ -346,7 +338,7 @@ public boolean render(BlockRenderView world, FluidState fluidState, BlockPos pos

ModelQuadFacing facing = ModelQuadFacing.fromDirection(dir);

this.calculateQuadColors(quad, world, pos, lighter, dir, br, !lava);
this.calculateQuadColors(quad, world, pos, lighter, dir, br, colorizer, fluidState);

int vertexStart = this.writeVertices(buffers, offset, quad);

Expand All @@ -365,15 +357,19 @@ public boolean render(BlockRenderView world, FluidState fluidState, BlockPos pos
return rendered;
}

private void calculateQuadColors(ModelQuadView quad, BlockRenderView world, BlockPos pos, LightPipeline lighter, Direction dir, float brightness, boolean colorized) {
private ModelQuadColorProvider<FluidState> createColorProviderAdapter(FluidRenderHandler handler) {
FabricFluidColorizerAdapter adapter = this.fabricColorProviderAdapter;
adapter.setHandler(handler);

return adapter;
}

private void calculateQuadColors(ModelQuadView quad, BlockRenderView world, BlockPos pos, LightPipeline lighter, Direction dir, float brightness,
ModelQuadColorProvider<FluidState> handler, FluidState fluidState) {
QuadLightData light = this.quadLightData;
lighter.calculate(quad, pos, light, dir, false);

int[] biomeColors = null;

if (colorized) {
biomeColors = this.biomeColorBlender.getColors(FLUID_COLOR_PROVIDER, world, Blocks.WATER.getDefaultState(), pos, quad);
}
int[] biomeColors = this.biomeColorBlender.getColors(world, pos, quad, handler, fluidState);

for (int i = 0; i < 4; i++) {
this.quadColors[i] = ColorABGR.mul(biomeColors != null ? biomeColors[i] : 0xFFFFFFFF, light.br[i] * brightness);
Expand Down Expand Up @@ -454,4 +450,21 @@ private float getCornerHeight(BlockRenderView world, int x, int y, int z, Fluid

return totalHeight / (float) samples;
}

private static class FabricFluidColorizerAdapter implements ModelQuadColorProvider<FluidState> {
private FluidRenderHandler handler;

public void setHandler(FluidRenderHandler handler) {
this.handler = handler;
}

@Override
public int getColor(FluidState state, @Nullable BlockRenderView world, @Nullable BlockPos pos, int tintIndex) {
if (this.handler == null) {
return -1;
}

return this.handler.getFluidColor(world, pos, state);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ChunkRenderCacheLocal(MinecraftClient client, World world) {
BiomeColorBlender biomeColorBlender = this.createBiomeColorBlender();

this.blockRenderer = new BlockRenderer(client, lightPipelineProvider, biomeColorBlender);
this.fluidRenderer = new FluidRenderer(client, lightPipelineProvider, biomeColorBlender);
this.fluidRenderer = new FluidRenderer(lightPipelineProvider, biomeColorBlender);

this.blockModels = client.getBakedModelManager().getBlockModels();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package me.jellysquid.mods.sodium.client.world.biome;

import me.jellysquid.mods.sodium.client.model.quad.ModelQuadColorProvider;
import net.minecraft.block.BlockState;
import net.minecraft.client.color.block.BlockColorProvider;

public interface BlockColorsExtended {
BlockColorProvider getColorProvider(BlockState state);
ModelQuadColorProvider<BlockState> getColorProvider(BlockState state);
}
Loading

0 comments on commit 5444aeb

Please sign in to comment.