|
| 1 | +package org.embeddedt.modernfix.testmod.client; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import com.mojang.datafixers.util.Pair; |
| 5 | +import net.fabricmc.fabric.api.renderer.v1.Renderer; |
| 6 | +import net.fabricmc.fabric.api.renderer.v1.RendererAccess; |
| 7 | +import net.fabricmc.fabric.api.renderer.v1.mesh.Mesh; |
| 8 | +import net.fabricmc.fabric.api.renderer.v1.mesh.MeshBuilder; |
| 9 | +import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView; |
| 10 | +import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter; |
| 11 | +import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel; |
| 12 | +import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper; |
| 13 | +import net.fabricmc.fabric.api.renderer.v1.render.RenderContext; |
| 14 | +import net.minecraft.client.renderer.block.model.BakedQuad; |
| 15 | +import net.minecraft.client.renderer.block.model.ItemOverrides; |
| 16 | +import net.minecraft.client.renderer.block.model.ItemTransforms; |
| 17 | +import net.minecraft.client.renderer.texture.TextureAtlas; |
| 18 | +import net.minecraft.client.renderer.texture.TextureAtlasSprite; |
| 19 | +import net.minecraft.client.resources.model.*; |
| 20 | +import net.minecraft.core.BlockPos; |
| 21 | +import net.minecraft.core.Direction; |
| 22 | +import net.minecraft.resources.ResourceLocation; |
| 23 | +import net.minecraft.world.item.ItemStack; |
| 24 | +import net.minecraft.world.level.BlockAndTintGetter; |
| 25 | +import net.minecraft.world.level.block.state.BlockState; |
| 26 | +import org.embeddedt.modernfix.testmod.TestMod; |
| 27 | +import org.jetbrains.annotations.Nullable; |
| 28 | + |
| 29 | +import java.util.*; |
| 30 | +import java.util.function.Function; |
| 31 | +import java.util.function.Supplier; |
| 32 | + |
| 33 | +public class TestModBlockModel implements UnbakedModel, BakedModel, FabricBakedModel { |
| 34 | + private static final Material BASE_WOOL = new Material(TextureAtlas.LOCATION_BLOCKS, new ResourceLocation(TestMod.ID, "block/base_wool")); |
| 35 | + |
| 36 | + private Mesh mesh; |
| 37 | + private TextureAtlasSprite texture; |
| 38 | + |
| 39 | + private final int r, g, b; |
| 40 | + |
| 41 | + public TestModBlockModel(int r, int g, int b) { |
| 42 | + this.r = r; |
| 43 | + this.g = g; |
| 44 | + this.b = b; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean isVanillaAdapter() { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) { |
| 54 | + context.meshConsumer().accept(mesh); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context) { |
| 59 | + context.meshConsumer().accept(mesh); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) { |
| 64 | + return Collections.emptyList(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean useAmbientOcclusion() { |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean isGui3d() { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean usesBlockLight() { |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean isCustomRenderer() { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public TextureAtlasSprite getParticleIcon() { |
| 89 | + return texture; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public ItemTransforms getTransforms() { |
| 94 | + return ModelHelper.MODEL_TRANSFORM_BLOCK; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public ItemOverrides getOverrides() { |
| 99 | + return ItemOverrides.EMPTY; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Collection<ResourceLocation> getDependencies() { |
| 104 | + return Collections.emptyList(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Collection<Material> getMaterials(Function<ResourceLocation, UnbakedModel> modelGetter, Set<Pair<String, String>> missingTextureErrors) { |
| 109 | + return ImmutableList.of(BASE_WOOL); |
| 110 | + } |
| 111 | + |
| 112 | + private static int scaleColor(int c) { |
| 113 | + return c * 255 / TestMod.MAX_COLOR; |
| 114 | + } |
| 115 | + |
| 116 | + @Nullable |
| 117 | + @Override |
| 118 | + public BakedModel bake(ModelBakery modelBakery, Function<Material, TextureAtlasSprite> spriteGetter, ModelState transform, ResourceLocation location) { |
| 119 | + // Build the mesh using the Renderer API |
| 120 | + Renderer renderer = RendererAccess.INSTANCE.getRenderer(); |
| 121 | + MeshBuilder builder = renderer.meshBuilder(); |
| 122 | + QuadEmitter emitter = builder.getEmitter(); |
| 123 | + |
| 124 | + texture = spriteGetter.apply(BASE_WOOL); |
| 125 | + |
| 126 | + for(Direction direction : Direction.values()) { |
| 127 | + // Add a new face to the mesh |
| 128 | + emitter.square(direction, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f); |
| 129 | + // Set the sprite of the face, must be called after .square() |
| 130 | + // We haven't specified any UV coordinates, so we want to use the whole texture. BAKE_LOCK_UV does exactly that. |
| 131 | + emitter.spriteBake(0, texture, MutableQuadView.BAKE_LOCK_UV); |
| 132 | + int color = (255 << 24) | (scaleColor(r) << 16) | (scaleColor(g) << 8) | scaleColor(b); |
| 133 | + // Enable texture usage |
| 134 | + emitter.spriteColor(0, color, color, color, color); |
| 135 | + // Add the quad to the mesh |
| 136 | + emitter.emit(); |
| 137 | + } |
| 138 | + mesh = builder.build(); |
| 139 | + |
| 140 | + return this; |
| 141 | + } |
| 142 | +} |
0 commit comments