Skip to content

Commit

Permalink
model shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Oct 9, 2023
1 parent 5d50dc5 commit 1232161
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.fusionflux.portalcubed.accessor;

import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import org.jetbrains.annotations.Nullable;

public interface BakedQuadExt {
@Nullable
String portalcubed$getRenderType();
RenderMaterial portalcubed$getRenderMaterial();

void portalcubed$setRenderType(String type);
void portalcubed$setRenderMaterial(RenderMaterial material);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.fusionflux.portalcubed.accessor;

import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import org.jetbrains.annotations.Nullable;

public interface BlockElementExt {
@Nullable
String portalcubed$getRenderType();
String portalcubed$getName();

void portalcubed$setRenderType(String type);
void portalcubed$setName(String name);

@Nullable
RenderMaterial portalcubed$getRenderMaterial();

void portalcubed$setRenderMaterial(RenderMaterial material);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fusionflux.portalcubed.client.gui.FaithPlateScreen;
import com.fusionflux.portalcubed.client.gui.VelocityHelperScreen;
import com.fusionflux.portalcubed.client.packet.PortalCubedClientPackets;
import com.fusionflux.portalcubed.client.render.models.PortalCubedModelLoadingPlugin;
import com.fusionflux.portalcubed.client.render.PortalHud;
import com.fusionflux.portalcubed.client.render.block.EmissiveSpriteRegistry;
import com.fusionflux.portalcubed.client.render.block.entity.*;
Expand All @@ -37,6 +38,7 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.brigadier.arguments.BoolArgumentType;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.client.render.fluid.v1.SimpleFluidRenderHandler;
import net.fabricmc.fabric.api.client.rendering.v1.*;
Expand Down Expand Up @@ -161,6 +163,8 @@ public void onInitializeClient(ModContainer mod) {
PortalCubedKeyBindings.register();
AnimatedEntityTextures.init();

ModelLoadingPlugin.register(PortalCubedModelLoadingPlugin.INSTANCE);

HudRenderCallback.EVENT.register(PortalHud::renderPortals);

// Thanks to https://github.com/JulianWww/Amazia-fabric/blob/main/src/main/java/net/denanu/amazia/GUI/debug/VillagePathingOverlay.java for some code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fusionflux.portalcubed.client.render.block;

import com.fusionflux.portalcubed.client.render.models.emissive.EmissiveBakedModel;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.resources.ResourceLocation;

Expand All @@ -14,7 +15,7 @@ public static boolean isEmissive(ResourceLocation spriteId) {
}

public static void register(ResourceLocation modelId, ResourceLocation spriteId) {
EmissiveBakedModel.register(modelId);
// EmissiveBakedModel.register(modelId);
register(spriteId);
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.fusionflux.portalcubed.client.render.models;

import com.fusionflux.portalcubed.accessor.BlockElementExt;
import com.fusionflux.portalcubed.client.render.models.rendertype.MultiRenderTypeUnbakedModel;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelModifier;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelModifier.BeforeBake;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;

import net.minecraft.client.renderer.block.model.BlockElement;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.UnbakedModel;

public enum PortalCubedModelLoadingPlugin implements ModelLoadingPlugin {
INSTANCE;

@Override
public void onInitializeModelLoader(Context ctx) {
Event<BeforeBake> beforeBake = ctx.modifyModelBeforeBake();
beforeBake.register(ModelModifier.WRAP_PHASE, MultiRenderTypeWrapper.INSTANCE);
beforeBake.register(ModelModifier.WRAP_PHASE, EmissiveWrapper.INSTANCE);
}

private enum MultiRenderTypeWrapper implements BeforeBake {
INSTANCE;

@Override
public UnbakedModel modifyModelBeforeBake(UnbakedModel model, Context context) {
if (model instanceof BlockModel blockModel) {
for (BlockElement element : blockModel.getElements()) {
RenderMaterial material = ((BlockElementExt) element).portalcubed$getRenderMaterial();
if (material != null) {
return new MultiRenderTypeUnbakedModel(blockModel);
}
}
}

return model;
}
}

private enum EmissiveWrapper implements BeforeBake {
INSTANCE;

@Override
public UnbakedModel modifyModelBeforeBake(UnbakedModel model, Context context) {
return model;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.fusionflux.portalcubed.client.render.models;

import java.util.HashMap;
import java.util.Map;

import com.fusionflux.portalcubed.PortalCubed;
import com.google.gson.JsonParseException;
import net.fabricmc.fabric.api.renderer.v1.Renderer;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.material.BlendMode;
import net.fabricmc.fabric.api.renderer.v1.material.MaterialFinder;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.util.TriState;

public class RenderMaterials {
private static final Renderer renderer = RendererAccess.INSTANCE.getRenderer();
private static final MaterialFinder finder = renderer == null ? null : renderer.materialFinder();

public static final boolean ARE_SUPPORTED = checkSupport();

public static final Map<String, RenderMaterial> BY_NAME = new HashMap<>();

public static final RenderMaterial DEFAULT = makeMaterial("default", BlendMode.DEFAULT, false);
public static final RenderMaterial SOLID = makeMaterial("solid", BlendMode.SOLID, false);
public static final RenderMaterial CUTOUT = makeMaterial("cutout", BlendMode.CUTOUT, false);
public static final RenderMaterial CUTOUT_MIPPED = makeMaterial("cutout_mipped", BlendMode.CUTOUT_MIPPED, false);
public static final RenderMaterial TRANSLUCENT = makeMaterial("translucent", BlendMode.TRANSLUCENT, false);

public static final RenderMaterial DEFAULT_EMISSIVE = makeMaterial("default_emissive", BlendMode.DEFAULT, true);
public static final RenderMaterial SOLID_EMISSIVE = makeMaterial("solid_emissive", BlendMode.SOLID, true);
public static final RenderMaterial CUTOUT_EMISSIVE = makeMaterial("cutout_emissive", BlendMode.CUTOUT, true);
public static final RenderMaterial CUTOUT_MIPPED_EMISSIVE = makeMaterial("cutout_mipped_emissive", BlendMode.CUTOUT_MIPPED, true);
public static final RenderMaterial TRANSLUCENT_EMISSIVE = makeMaterial("translucent_emissive", BlendMode.TRANSLUCENT, true);

public static final String SUPPORTED_TYPES = String.join(", ", BY_NAME.keySet());

public static RenderMaterial get(BlendMode mode, boolean emissive) {
return switch (mode) {
case DEFAULT -> emissive ? DEFAULT_EMISSIVE : DEFAULT;
case SOLID -> emissive ? SOLID_EMISSIVE : SOLID;
case CUTOUT -> emissive ? CUTOUT_EMISSIVE : CUTOUT;
case CUTOUT_MIPPED -> emissive ? CUTOUT_MIPPED_EMISSIVE : CUTOUT_MIPPED;
case TRANSLUCENT -> emissive ? TRANSLUCENT_EMISSIVE : TRANSLUCENT;
};
}

public static RenderMaterial parse(String name) {
RenderMaterial material = BY_NAME.get(name);
if (material == null) {
throw new JsonParseException("Invalid render material \"" + name + "\"; must be one of: " + SUPPORTED_TYPES);
}
return material;
}

private static RenderMaterial makeMaterial(String name, BlendMode mode, boolean emissive) {
if (finder == null)
return null;

finder.clear().blendMode(mode);
if (emissive) {
finder.emissive(true)
.disableDiffuse(true)
.ambientOcclusion(TriState.FALSE);
}
RenderMaterial material = finder.find();
BY_NAME.put(name, material);
return material;
}

private static boolean checkSupport() {
if (renderer == null) {
PortalCubed.LOGGER.error("No renderer present, rendering will be wrong. If you have Sodium, install Indium!");
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.fusionflux.portalcubed.client.render.block;
package com.fusionflux.portalcubed.client.render.models.emissive;

import com.fusionflux.portalcubed.client.render.block.EmissiveSpriteRegistry;
import com.fusionflux.portalcubed.client.render.models.RenderMaterials;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel;
Expand Down Expand Up @@ -89,11 +91,12 @@ public static RenderMaterial getEmissiveMaterial(RenderMaterial base) {
}

private static RenderMaterial makeEmissiveMaterial(RenderMaterial base) {
return RenderMaterials.FINDER.copyFrom(base)
.emissive(true)
.disableDiffuse(true)
.ambientOcclusion(TriState.FALSE)
.find();
throw new RuntimeException("a");
// return RenderMaterials.FINDER.copyFrom(base)
// .emissive(true)
// .disableDiffuse(true)
// .ambientOcclusion(TriState.FALSE)
// .find();
}

private static SpriteFinder getSpriteFinder() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.fusionflux.portalcubed.client.render.block;
package com.fusionflux.portalcubed.client.render.models.rendertype;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import com.fusionflux.portalcubed.accessor.BakedQuadExt;
import com.fusionflux.portalcubed.client.render.models.RenderMaterials;
import com.fusionflux.portalcubed.mixin.client.SimpleBakedModelAccessor;
import com.google.gson.JsonParseException;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter;
import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel;
Expand All @@ -25,18 +25,10 @@
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;

public class MultiRenderTypeSimpleBakedModel extends ForwardingBakedModel {
public static final Map<String, Supplier<RenderMaterial>> SUPPORTED_TYPES = Map.of(
"default", () -> RenderMaterials.DEFAULT_MATERIAL,
"solid", () -> RenderMaterials.SOLID_MATERIAL,
"cutout", () -> RenderMaterials.CUTOUT_MATERIAL,
"translucent", () -> RenderMaterials.TRANSLUCENT_MATERIAL
);
public static final String SUPPORTED_TYPE_LIST = String.join(", ", SUPPORTED_TYPES.keySet());

public class MultiRenderTypeBakedModel extends ForwardingBakedModel {
protected List<Triple<BakedQuad, RenderMaterial, Direction>> quads = new ArrayList<>();

public MultiRenderTypeSimpleBakedModel(SimpleBakedModel model) {
public MultiRenderTypeBakedModel(SimpleBakedModel model) {
this.wrapped = model;

List<BakedQuad> unculled = ((SimpleBakedModelAccessor) model).getUnculledFaces();
Expand All @@ -48,10 +40,10 @@ public MultiRenderTypeSimpleBakedModel(SimpleBakedModel model) {
}

private void addQuad(BakedQuad quad, @Nullable Direction cullFace) {
String renderType = ((BakedQuadExt) quad).portalcubed$getRenderType();
if (renderType == null)
renderType = "default";
RenderMaterial material = parseType(renderType);
RenderMaterial material = ((BakedQuadExt) quad).portalcubed$getRenderMaterial();
if (material == null) {
material = RenderMaterials.DEFAULT;
}
this.quads.add(Triple.of(quad, material, cullFace));
}

Expand Down Expand Up @@ -85,11 +77,4 @@ public List<BakedQuad> getQuads(BlockState blockState, Direction face, RandomSou
public boolean isVanillaAdapter() {
return false;
}

public static RenderMaterial parseType(String name) throws JsonParseException {
Supplier<RenderMaterial> type = SUPPORTED_TYPES.get(name);
if (type != null)
return type.get();
throw new JsonParseException(name + " is not a supported RenderType. must be one of: " + SUPPORTED_TYPE_LIST);
}
}
Loading

0 comments on commit 1232161

Please sign in to comment.