Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FRAPI improvements: context getters, full removal of fallback consumers, small enhancements #3287

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@

import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.json.ModelTransformationMode;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockRenderView;

import net.fabricmc.fabric.api.renderer.v1.mesh.Mesh;
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter;
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadView;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;

/**
Expand All @@ -54,6 +57,15 @@ public interface RenderContext {
*/
QuadEmitter getEmitter();

/**
* Returns whether this context currently has at least one transform.
*
* @apiNote The default implementation will be removed in the next breaking release.
*/
default boolean hasTransform() {
return true;
}

/**
* Causes all models/quads/meshes sent to this consumer to be transformed by the provided
* {@link QuadTransform} that edits each quad before buffering. Quads in the mesh will
Expand All @@ -76,6 +88,34 @@ public interface RenderContext {
*/
void popTransform();

/**
* Returns {@code true} if the given face will be culled away.
*
* <p>This function can be used to avoid complex transformations of quads that will be culled anyway.
Technici4n marked this conversation as resolved.
Show resolved Hide resolved
* The cull face of a quad is determined by {@link QuadView#cullFace()}.
*
* <p>This function can only be used on a block render context (i.e. in {@link FabricBakedModel#emitBlockQuads}).
* Calling it on another context (e.g. in {@link FabricBakedModel#emitItemQuads}) will throw an exception.
*
* @apiNote The default implementation will be removed in the next breaking release.
*/
// TODO: allow null params?
default boolean isFaceCulled(Direction face) {
return false;
}

/**
* Returns the current transformation mode.
*
* <p>This function can only be used on an item render context (i.e. in {@link FabricBakedModel#emitItemQuads}).
* Calling it on another context (e.g. in {@link FabricBakedModel#emitBlockQuads}) will throw an exception.
*
* @apiNote The default implementation will be removed in the next breaking release.
*/
default ModelTransformationMode itemTransformationMode() {
return ModelTransformationMode.NONE;
}

@FunctionalInterface
interface QuadTransform {
/**
Expand All @@ -89,7 +129,9 @@ interface QuadTransform {
* @deprecated Use {@link Mesh#outputTo(QuadEmitter)} instead.
*/
@Deprecated
Consumer<Mesh> meshConsumer();
default Consumer<Mesh> meshConsumer() {
return mesh -> mesh.outputTo(getEmitter());
}

/**
* @deprecated Use {@link FabricBakedModel#emitBlockQuads(BlockRenderView, BlockState, BlockPos, Supplier, RenderContext) emitBlockQuads}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.render.model.json.ModelTransformationMode;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;

Expand Down Expand Up @@ -80,6 +81,16 @@ public QuadEmitter getEmitter() {
return editorQuad;
}

@Override
public boolean isFaceCulled(Direction face) {
return !blockInfo.shouldDrawFace(face);
}

@Override
public ModelTransformationMode itemTransformationMode() {
throw new IllegalStateException("itemTransformationMode() can only be called on an item render context.");
}

@Override
public BakedModelConsumer bakedModelConsumer() {
return vanillaModelConsumer;
Expand All @@ -90,7 +101,7 @@ private void renderQuad(MutableQuadViewImpl quad, boolean isVanilla) {
return;
}

if (!blockInfo.shouldDrawFace(quad.cullFace())) {
if (isFaceCulled(quad.cullFace())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ protected final boolean transform(MutableQuadView q) {
return activeTransform.transform(q);
}

protected boolean hasTransform() {
@Override
public boolean hasTransform() {
return activeTransform != NO_TRANSFORM;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ public QuadEmitter getEmitter() {
return editorQuad;
}

@Override
public boolean isFaceCulled(Direction face) {
throw new IllegalStateException("isFaceCulled can only be called on a block render context.");
}

@Override
public ModelTransformationMode itemTransformationMode() {
return transformMode;
}

@Override
public BakedModelConsumer bakedModelConsumer() {
return vanillaModelConsumer;
Expand Down