Skip to content

Commit

Permalink
Fix z-fighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna0x01 committed Dec 28, 2024
1 parent c3f3189 commit f3c372f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

package dev.vexor.radium.frapi.impl.renderer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import dev.vexor.radium.compat.mojang.minecraft.random.RandomSource;
import net.legacyfabric.fabric.api.util.TriState;
import net.minecraft.block.*;
import org.jetbrains.annotations.Nullable;

import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.util.math.Direction;
Expand All @@ -42,29 +46,76 @@ public class VanillaModelEncoder {
private static final RenderMaterial STANDARD_MATERIAL = Renderer.get().materialFinder().shadeMode(ShadeMode.VANILLA).find();
private static final RenderMaterial NO_AO_MATERIAL = Renderer.get().materialFinder().shadeMode(ShadeMode.VANILLA).ambientOcclusion(TriState.FALSE).find();

public static void emitBlockQuads(QuadEmitter emitter, BakedModel model, @Nullable BlockState state, Predicate<@Nullable Direction> cullTest) {
final RenderMaterial defaultMaterial = model.useAmbientOcclusion() ? STANDARD_MATERIAL : NO_AO_MATERIAL;
public static void emitBlockQuads(QuadEmitter emitter, BakedModel model, @Nullable BlockState state, Predicate<@Nullable Direction> cullTest) {
final RenderMaterial defaultMaterial = model.useAmbientOcclusion() ? STANDARD_MATERIAL : NO_AO_MATERIAL;

for (int i = 0; i <= ModelHelper.NULL_FACE_ID; i++) {
final Direction cullFace = ModelHelper.faceFromIndex(i);
for (int i = 0; i <= ModelHelper.NULL_FACE_ID; i++) {
final Direction cullFace = ModelHelper.faceFromIndex(i);

if (cullTest.test(cullFace)) {
// Skip entire quad list if possible.
continue;
}
if (cullTest.test(cullFace)) {
continue;
}

final List<BakedQuad> quads;
final List<BakedQuad> quads;

if (cullFace != null) {
quads = model.getByDirection(cullFace);
} else {
quads = model.getQuads();
}

for (final BakedQuad quad : quads) {
emitter.fromVanilla(quad, defaultMaterial, cullFace);
emitter.emit();

Set<Direction> allowedFaces = Arrays.stream(Direction.values()).collect(Collectors.toSet());

for (Class<?> clazz : FILTER) {
if (clazz.isInstance(state.getBlock())) {
allowedFaces = findAllowedFaces(quads);
break;
}
}

if (cullFace != null) {
for (final BakedQuad quad : quads) {
emitter.fromVanilla(quad, defaultMaterial, cullFace);
emitter.emit();
}
} else {
for (final BakedQuad quad : quads) {
if (allowedFaces.contains(quad.getFace())) {
emitter.fromVanilla(quad, defaultMaterial, cullFace);
emitter.emit();
}
}
}
}
}

/**
* Finds allowed faces from a list of baked quads
*
* @param quads quads to find the allowed faces from
* @return the allowed faces
*/
private static Set<Direction> findAllowedFaces(List<BakedQuad> quads) {
Set<Direction> allowedFaces = new HashSet<>();

for (BakedQuad quad : quads) {
Direction faceDirection = quad.getFace();

if (faceDirection == Direction.NORTH || faceDirection == Direction.SOUTH) {
if (!allowedFaces.contains(Direction.NORTH)) {
allowedFaces.add(Direction.NORTH);
}
} else if (faceDirection == Direction.WEST || faceDirection == Direction.EAST) {
if (!allowedFaces.contains(Direction.EAST)) {
allowedFaces.add(Direction.EAST);
}
} else {
allowedFaces.add(faceDirection);
}
}
}
}

return allowedFaces;
}

private static final Class<?>[] FILTER = { PlantBlock.class, Growable.class, GrassBlock.class };
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public void makeActive() {

this.stateTracker.clear();
this.isActive = true;

GlStateManager.disableCull();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected int compute(int x, int y, int z) {
boolean em = block.getLightLevel() != 0;
boolean op = block.isFullBlock() || block.getOpacity() != 0;
boolean fo = block.isNormalBlock();
boolean fc = block.renderAsNormalBlock();
boolean fc = block.isFullCube();

int lu = state.getBlock().getLightLevel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public void setLevel(ClientWorld level) {
}

// If we have a level is already loaded, unload the renderer
// if (this.level != null) {
// this.unloadLevel();
// }
if (this.level != null) {
this.unloadLevel();
}

// If we're loading a new level, load the renderer
if (level != null) {
Expand Down Expand Up @@ -249,6 +249,8 @@ private void processChunkEvents() {
* Performs a render pass for the given {@link RenderLayer} and draws all visible chunks for it.
*/
public void drawChunkLayer(RenderLayer renderLayer, ChunkRenderMatrices matrices, double x, double y, double z) {
GlStateManager.disableCull();

this.renderSectionManager.renderLayer(matrices, DefaultTerrainRenderPasses.fromLayer(renderLayer), x, y, z);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.legacyfabric.fabric.api.util.TriState;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FlowerBlock;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -70,7 +71,6 @@ public void release() {
public void renderModel(BakedModel model, BlockState state, BlockPos pos, BlockPos origin) {
this.state = state;
this.pos = pos;

this.randomSeed = 42L;

this.posOffset.set(origin.getX(), origin.getY(), origin.getZ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.vexor.radium.compat.mojang.minecraft.random.RandomSource;
import dev.vexor.radium.compat.mojang.minecraft.render.LightTexture;
import dev.vexor.radium.frapi.api.renderer.v1.material.BlendMode;
import net.caffeinemc.mods.sodium.client.model.light.LightMode;
import net.caffeinemc.mods.sodium.client.model.light.LightPipeline;
import net.caffeinemc.mods.sodium.client.model.light.LightPipelineProvider;
Expand Down

0 comments on commit f3c372f

Please sign in to comment.