mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-25 14:44:28 +00:00
Initial QuadProvider API (#256)
* Initial model testing * Fix UV * More quad work * Update BlockRenderer.java * QuadProvider renders right! * Clean up * Update CubeModel.java * Initial colorization work * (Badly) color grass Unbreaks vanilla blocks * Leaves are easier * Add separate AO back * Relocate to Angelica * Cleanup + pool quads * Add TODOs for future reference * Update LightDataAccess.java * Replace code comment with TODO * Update WorldSlice.java Co-authored-by: makamys <[email protected]> * Default quads to UP * Only coerce facing in Quad#getFace Mirrors 1.16's face handling more accurately. Fixes face culling not working properly for blocks like tall grass * Iterate over unaligned quads too * Don't cull unaligned faces * Release QP quads --------- Co-authored-by: makamys <[email protected]>
This commit is contained in:
1 parent
cf454f287c
commit
509017551a
15 files changed
+548
-45
No files matched your search
@@ -0,0 +1,15 @@
|
||||
package com.gtnewhorizons.angelica.api;
|
||||
|
||||
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
|
||||
import com.gtnewhorizons.angelica.compat.nd.Quad;
|
||||
import com.gtnewhorizons.angelica.utils.ObjectPooler;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
public interface QuadProvider {
|
||||
|
||||
List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, ObjectPooler<Quad> quadPool);
|
||||
}
|
||||
@@ -12,7 +12,7 @@ public enum Axis {
|
||||
case DOWN, UP -> Y;
|
||||
case NORTH, SOUTH -> Z;
|
||||
case WEST, EAST -> X;
|
||||
default -> null;
|
||||
case UNASSIGNED -> null;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.gtnewhorizons.angelica.compat.nd;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.jellysquid.mods.sodium.client.model.quad.ModelQuadView;
|
||||
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.render.pipeline.BlockRenderer;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
@@ -35,6 +37,10 @@ public class Quad implements ModelQuadView {
|
||||
private boolean hasShade;
|
||||
private boolean hasNormals;
|
||||
|
||||
private int cachedFlags;
|
||||
private ForgeDirection face;
|
||||
private int colorIndex = -1;
|
||||
|
||||
public boolean hasColor() {
|
||||
return this.hasColor;
|
||||
}
|
||||
@@ -51,9 +57,9 @@ public class Quad implements ModelQuadView {
|
||||
return this.hasNormals;
|
||||
}
|
||||
|
||||
public ForgeDirection getFace() {
|
||||
// TODO: Sodium/Quad Facing
|
||||
return ForgeDirection.UP;
|
||||
/** Returns the face, forced to take one of 6 directions to mirror the behavior of baked quads in 1.16.5. */
|
||||
public ForgeDirection getCoercedFace() {
|
||||
return this.face != ForgeDirection.UNKNOWN ? this.face : ForgeDirection.UP;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,7 +94,7 @@ public class Quad implements ModelQuadView {
|
||||
|
||||
@Override
|
||||
public int getFlags() {
|
||||
return 0;
|
||||
return this.cachedFlags;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -103,7 +109,7 @@ public class Quad implements ModelQuadView {
|
||||
|
||||
@Override
|
||||
public int getColorIndex() {
|
||||
return 0;
|
||||
return colorIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -166,6 +172,8 @@ public class Quad implements ModelQuadView {
|
||||
vectorA.cross(vectorB, vectorC);
|
||||
|
||||
normal = ModelQuadFacing.fromVector(vectorC);
|
||||
this.face = ModelQuadFacing.toDirection(normal);
|
||||
this.cachedFlags = ModelQuadFlags.getQuadFlags(this);
|
||||
}
|
||||
|
||||
public static boolean isValid(Quad q) {
|
||||
|
||||
@@ -16,6 +16,11 @@ public class AngelicaConfig {
|
||||
@Config.RequiresMcRestart
|
||||
public static boolean enableSodiumFluidRendering;
|
||||
|
||||
@Config.Comment("Inject QuadProvider rendering into some vanilla blocks")
|
||||
@Config.DefaultBoolean(false)
|
||||
@Config.RequiresMcRestart
|
||||
public static boolean injectQPRendering;
|
||||
|
||||
@Config.Comment("Enable Iris Shaders [Requires Sodium]")
|
||||
@Config.DefaultBoolean(false)
|
||||
@Config.RequiresMcRestart
|
||||
|
||||
@@ -453,6 +453,17 @@ public enum Mixins {
|
||||
.addTargetedMod(TargetedMod.VANILLA)
|
||||
.addMixinClasses("mcpatcherforge.ctm_cc.MixinTextureMap")),
|
||||
|
||||
NOVIS_OCULIS(new Builder("Non-Tessellator Quad provider")
|
||||
.setSide(Side.CLIENT)
|
||||
.setPhase(Phase.EARLY)
|
||||
.setApplyIf(() -> AngelicaConfig.injectQPRendering)
|
||||
.addTargetedMod(TargetedMod.VANILLA)
|
||||
.addMixinClasses(
|
||||
"angelica.models.MixinBlockStone",
|
||||
"angelica.models.MixinBlockAir",
|
||||
"angelica.models.MixinBlockWorkbench",
|
||||
"angelica.models.MixinBlockLeaves")),
|
||||
|
||||
;
|
||||
|
||||
private final List<String> mixinClasses;
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.gtnewhorizons.angelica.mixins.early.angelica.models;
|
||||
|
||||
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
|
||||
import com.gtnewhorizons.angelica.compat.nd.Quad;
|
||||
import com.gtnewhorizons.angelica.utils.ObjectPooler;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectImmutableList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import com.gtnewhorizons.angelica.api.QuadProvider;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockAir;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
||||
@Mixin(BlockAir.class)
|
||||
public abstract class MixinBlockAir implements QuadProvider {
|
||||
|
||||
private static List<Quad> quads = ObjectImmutableList.of();
|
||||
|
||||
public List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, ObjectPooler<Quad> quadPool) {
|
||||
return quads;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.gtnewhorizons.angelica.mixins.early.angelica.models;
|
||||
|
||||
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
|
||||
import com.gtnewhorizons.angelica.compat.nd.Quad;
|
||||
import com.gtnewhorizons.angelica.utils.ObjectPooler;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import com.gtnewhorizons.angelica.models.CubeModel;
|
||||
import com.gtnewhorizons.angelica.api.QuadProvider;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockLeaves;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
||||
@Mixin(BlockLeaves.class)
|
||||
public abstract class MixinBlockLeaves extends Block implements QuadProvider {
|
||||
|
||||
// TODO: Use modern model API
|
||||
private static final ThreadLocal<QuadProvider> model = ThreadLocal.withInitial(() -> new CubeModel(true));
|
||||
|
||||
public List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, ObjectPooler<Quad> quadPool) {
|
||||
return model.get().getQuads(world, pos, block, meta, dir, random, quadPool);
|
||||
}
|
||||
|
||||
private MixinBlockLeaves(Material materialIn) {
|
||||
super(materialIn);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.gtnewhorizons.angelica.mixins.early.angelica.models;
|
||||
|
||||
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
|
||||
import com.gtnewhorizons.angelica.compat.nd.Quad;
|
||||
import com.gtnewhorizons.angelica.utils.ObjectPooler;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import com.gtnewhorizons.angelica.models.CubeModel;
|
||||
import com.gtnewhorizons.angelica.api.QuadProvider;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockStone;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
||||
@Mixin(BlockStone.class)
|
||||
public abstract class MixinBlockStone implements QuadProvider {
|
||||
|
||||
// TODO: Use modern model API
|
||||
public List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, ObjectPooler<Quad> quadPool) {
|
||||
return CubeModel.INSTANCE.get().getQuads(world, pos, block, meta, dir, random, quadPool);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.gtnewhorizons.angelica.mixins.early.angelica.models;
|
||||
|
||||
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
|
||||
import com.gtnewhorizons.angelica.compat.nd.Quad;
|
||||
import com.gtnewhorizons.angelica.utils.ObjectPooler;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import com.gtnewhorizons.angelica.models.CubeModel;
|
||||
import com.gtnewhorizons.angelica.api.QuadProvider;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockWorkbench;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
||||
@Mixin(BlockWorkbench.class)
|
||||
public class MixinBlockWorkbench implements QuadProvider {
|
||||
|
||||
// TODO: Use modern model API
|
||||
public List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, ObjectPooler<Quad> quadPool) {
|
||||
return CubeModel.INSTANCE.get().getQuads(world, pos, block, meta, dir, random, quadPool);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package com.gtnewhorizons.angelica.models;
|
||||
|
||||
import com.gtnewhorizons.angelica.api.QuadProvider;
|
||||
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
|
||||
import com.gtnewhorizons.angelica.compat.nd.Quad;
|
||||
import com.gtnewhorizons.angelica.utils.ObjectPooler;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectImmutableList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import me.jellysquid.mods.sodium.client.render.pipeline.BlockRenderer;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.GL_QUADS;
|
||||
|
||||
public class CubeModel implements QuadProvider {
|
||||
|
||||
/*
|
||||
* GL only cares if the coords are clockwise or counterclockwise - not the specific order [1].
|
||||
* Which mean I get to decide it! Behold, Omni's Standard Cube Vertex Order (TM)!
|
||||
*
|
||||
* SOUTH (+z)
|
||||
* (x + 1, y + 1, z + 1)
|
||||
* 5 - - - - - - - - - 6
|
||||
* | \ | \
|
||||
* | \ | \
|
||||
* | \ | \
|
||||
* | 4 - - - - - - - - - 7
|
||||
* | | | |
|
||||
* | | | |
|
||||
* | | | |
|
||||
* 1 - - - | - - - - - 2 |
|
||||
* \ | \ |
|
||||
* <- WEST (-x) \ | \ | EAST (+x) ->
|
||||
* \ | \ |
|
||||
* 0 - - - - - - - - - 3
|
||||
* (x, y, z)
|
||||
* NORTH (-z)
|
||||
*
|
||||
* The horizontal directions are labeled, up and down should be obvious (and would be annoying to add).
|
||||
* Vertices are written in counterclockwise order, as viewed from outside the cube, to orient the quad properly.
|
||||
* (x, y, z) is the world pos of the block. 0 and 6 have been labeled, you can figure out the rest.
|
||||
*
|
||||
* [1] So apparently I was a bit wrong. GL doesn't care, but Sodium does because that's how it applies AO.
|
||||
* Change it if you dare!
|
||||
*/
|
||||
|
||||
// I'm sure the following constants could be condensed to a single magic array, but for clarity's sake they remain.
|
||||
|
||||
// The (x, y, z) positions of the vertices
|
||||
private static final int[][] vertices = {
|
||||
{ 0, 0, 0 },
|
||||
{ 0, 0, 1 },
|
||||
{ 1, 0, 1 },
|
||||
{ 1, 0, 0 },
|
||||
{ 0, 1, 0 },
|
||||
{ 0, 1, 1 },
|
||||
{ 1, 1, 1 },
|
||||
{ 1, 1, 0 }
|
||||
};
|
||||
|
||||
private static final int[][] uv = {
|
||||
{ 0, 0 },
|
||||
{ 0, 1 },
|
||||
{ 1, 1 },
|
||||
{ 1, 0 }
|
||||
};
|
||||
|
||||
// Add the UV.
|
||||
private static final int[][][] shiftsByDirection = {
|
||||
|
||||
// DOWN 1 0 3 2
|
||||
{
|
||||
vertices[1],
|
||||
vertices[0],
|
||||
vertices[3],
|
||||
vertices[2]
|
||||
},
|
||||
|
||||
//UP 4 5 6 7
|
||||
{
|
||||
vertices[4],
|
||||
vertices[5],
|
||||
vertices[6],
|
||||
vertices[7]
|
||||
},
|
||||
|
||||
//NORTH 7 3 0 4
|
||||
{
|
||||
vertices[7],
|
||||
vertices[3],
|
||||
vertices[0],
|
||||
vertices[4]
|
||||
},
|
||||
|
||||
//SOUTH 5 1 2 6
|
||||
{
|
||||
vertices[5],
|
||||
vertices[1],
|
||||
vertices[2],
|
||||
vertices[6]
|
||||
},
|
||||
|
||||
//WEST 4 0 1 5
|
||||
{
|
||||
vertices[4],
|
||||
vertices[0],
|
||||
vertices[1],
|
||||
vertices[5]
|
||||
},
|
||||
|
||||
//EAST 6 2 3 7
|
||||
{
|
||||
vertices[6],
|
||||
vertices[2],
|
||||
vertices[3],
|
||||
vertices[7]
|
||||
}
|
||||
};
|
||||
|
||||
private static final int[][] initBufs = new int[6][32];
|
||||
|
||||
static {
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
|
||||
final int dirI = dir.ordinal();
|
||||
int[][] shifts = shiftsByDirection[dirI];
|
||||
|
||||
for (int vi = 0; vi < 4; ++vi) {
|
||||
final int i = vi * 8;
|
||||
|
||||
// XYZ
|
||||
initBufs[dirI][i + 0] = Float.floatToIntBits(shifts[vi][0]);
|
||||
initBufs[dirI][i + 1] = Float.floatToIntBits(shifts[vi][1]);
|
||||
initBufs[dirI][i + 2] = Float.floatToIntBits(shifts[vi][2]);
|
||||
|
||||
// UV
|
||||
initBufs[dirI][i + 3] = Float.floatToIntBits(0);
|
||||
initBufs[dirI][i + 4] = Float.floatToIntBits(0);
|
||||
|
||||
// Color, normal, brightness
|
||||
initBufs[dirI][i + 5] = 0xFFFFFFFF;
|
||||
initBufs[dirI][i + 6] = 0;
|
||||
initBufs[dirI][i + 7] = 15_728_880; // 15 sky 15 block
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final ThreadLocal<CubeModel> INSTANCE = ThreadLocal.withInitial(() -> new CubeModel(false));
|
||||
private final boolean[] colorized = new boolean[6];
|
||||
private final BlockRenderer.Flags flags = new BlockRenderer.Flags(true, true, false, false);
|
||||
private static final List<Quad> EMPTY = ObjectImmutableList.of();
|
||||
private final List<Quad> ONE = new ObjectArrayList<>(1);
|
||||
|
||||
public CubeModel(boolean colorized) {
|
||||
|
||||
Arrays.fill(this.colorized, colorized);
|
||||
this.ONE.add(null);
|
||||
}
|
||||
|
||||
public CubeModel(boolean[] colorized) {
|
||||
|
||||
System.arraycopy(colorized, 0, this.colorized, 0, 6);
|
||||
this.ONE.add(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, ObjectPooler<Quad> quadPool) {
|
||||
|
||||
if (dir == ForgeDirection.UNKNOWN)
|
||||
return EMPTY;
|
||||
|
||||
final Quad face = quadPool.getInstance();
|
||||
final int[] buf = Arrays.copyOf(initBufs[dir.ordinal()], 32); // 8 ints per vertex, four vertices per quad. A cube only has one quad per side
|
||||
// Format:
|
||||
// x
|
||||
// y
|
||||
// z
|
||||
// u
|
||||
// v
|
||||
// color
|
||||
// normals
|
||||
// brightness
|
||||
// repeat
|
||||
// all are float bits as ints
|
||||
|
||||
final IIcon tex = block.getIcon(dir.ordinal(), meta);
|
||||
final int color;
|
||||
if (this.colorized[dir.ordinal()]) {
|
||||
final int c = block.colorMultiplier(world, pos.x, pos.y, pos.z);
|
||||
final int r = (c >> 16) & 0xFF;
|
||||
final int g = (c >> 8) & 0xFF;
|
||||
final int b = c & 0xFF;
|
||||
color = 0xFF << 24 | b << 16 | g << 8 | r;
|
||||
} else { color = 0xFFFFFFFF; }
|
||||
this.flags.hasColor = this.colorized[dir.ordinal()];
|
||||
|
||||
for (int vi = 0; vi < 4; ++vi) {
|
||||
final int i = vi * 8;
|
||||
|
||||
// UV
|
||||
buf[i + 3] = Float.floatToIntBits(tex.getInterpolatedU(uv[vi][0] * 16));
|
||||
buf[i + 4] = Float.floatToIntBits(tex.getInterpolatedV(uv[vi][1] * 16));
|
||||
|
||||
// Color
|
||||
buf[i + 5] = color;
|
||||
}
|
||||
|
||||
face.setState(buf, 0, this.flags, GL_QUADS, 0, 0, 0);
|
||||
|
||||
// CubeModel is solid all over, and thus doesn't need to do this
|
||||
// However, you need to drop deleted quads in proper model code
|
||||
/*if (face.deleted) {
|
||||
|
||||
quadPool.releaseInstance(face);
|
||||
return EMPTY;
|
||||
}*/
|
||||
|
||||
ONE.set(0, face);
|
||||
return ONE;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user