mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-19 11:49:11 +00:00
Add basic itemblock JSON models renderer (#211)
Co-authored-by: MalTeeez <[email protected]>
This commit is contained in:
1 parent
43fd2090a1
commit
66f07dafbf
9 files changed
+420
-11
No files matched your search
@@ -14,10 +14,60 @@ trivial mixin, but the requirement is still there to avoid
|
||||
spurious lookups on the vast majority of blocks that (currently)
|
||||
don't have models. This may change in the future.
|
||||
|
||||
## Examples
|
||||
### BlockColor
|
||||
<p>
|
||||
Example using BlockColor register:
|
||||
|
||||
<pre>
|
||||
BlockColor.registerBlockColors(new IBlockColor() {
|
||||
|
||||
@Override
|
||||
public int colorMultiplier(IBlockAccess world, int x, int y, int z, int tintIndex) {
|
||||
// Return red for main layer, green for secondary
|
||||
return tintIndex == 0 ? 0xFF0000 : 0x00FF00;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int colorMultiplier(ItemStack stack, int tintIndex) {
|
||||
// Return blue for main layer, yellow for secondary
|
||||
return tintIndex == 0 ? 0x0000FF : 0xFFFF00;
|
||||
}
|
||||
}, ModBlocks.MY_CUSTOM_BLOCK);
|
||||
</pre>
|
||||
|
||||
Example: implement IBlockColor directly in a block
|
||||
<pre>
|
||||
public class BlockTestTint extends Block implements IBlockColor {
|
||||
|
||||
public BlockTestTint() {
|
||||
super(Material.wood);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int colorMultiplier(IBlockAccess world, int x, int y, int z, int tintIndex) {
|
||||
return switch(tintIndex) {
|
||||
case 0 -> 0xFF0000; // red
|
||||
case 1 -> 0x00FF00; // green
|
||||
case 2 -> 0x0000FF; // blue
|
||||
case 3 -> 0xFFFF00; // yellow
|
||||
case 4 -> 0xFF00FF; // magenta
|
||||
case 5 -> 0x00FFFF; // cyan
|
||||
default -> 0xFFFFFF; // white
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int colorMultiplier(ItemStack stack, int tintIndex) {
|
||||
return colorMultiplier(null, 0, 0, 0, tintIndex);
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
|
||||
### TODO
|
||||
- Autoload item textures too.
|
||||
- Add smooth shading to ModelISBRH.
|
||||
- Add face culling to models.
|
||||
- Implement UV locking.
|
||||
- Integrate with a proper BlockState API.
|
||||
- BlockItem rendering.
|
||||
- BlockItem display (Use BlockBench display transforms provided in '{}.display').
|
||||
@@ -2,12 +2,15 @@ package com.gtnewhorizon.gtnhlib;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.GTNHLib.MODID;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.resources.SimpleReloadableResourceManager;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
import net.minecraftforge.client.ClientCommandHandler;
|
||||
import net.minecraftforge.client.MinecraftForgeClient;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.model.ModelISBRH;
|
||||
@@ -75,6 +78,17 @@ public class ClientProxy extends CommonProxy {
|
||||
final var resourceManager = ((SimpleReloadableResourceManager) Minecraft.getMinecraft().getResourceManager());
|
||||
resourceManager.registerReloadListener(new ModelRegistry.ReloadListener());
|
||||
MinecraftForge.EVENT_BUS.register(new ModelRegistry.EventHandler());
|
||||
|
||||
// Applied Item Render for Block Use ModelISBRH
|
||||
for (Object obj : Block.blockRegistry) {
|
||||
Block block = (Block) obj;
|
||||
if (block.getRenderType() == ModelISBRH.JSON_ISBRH_ID) {
|
||||
Item item = Item.getItemFromBlock(block);
|
||||
if (item != null) {
|
||||
MinecraftForgeClient.registerItemRenderer(item, new ModelISBRH());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,6 +7,7 @@ import net.minecraft.util.IChatComponent;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.block.BlockTest;
|
||||
import com.gtnewhorizon.gtnhlib.block.BlockTestTint;
|
||||
import com.gtnewhorizon.gtnhlib.brigadier.BrigadierApi;
|
||||
import com.gtnewhorizon.gtnhlib.config.ConfigurationManager;
|
||||
import com.gtnewhorizon.gtnhlib.eventbus.AutoEventBus;
|
||||
@@ -43,6 +44,7 @@ public class CommonProxy {
|
||||
|
||||
if (GTNHLibConfig.enableTestBlocks) {
|
||||
GameRegistry.registerBlock(new BlockTest(), "model_test");
|
||||
GameRegistry.registerBlock(new BlockTestTint(), "model_test_tint");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.gtnewhorizon.gtnhlib.block;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.client.model.ModelISBRH.JSON_ISBRH_ID;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.model.color.IBlockColor;
|
||||
|
||||
public class BlockTestTint extends Block implements IBlockColor {
|
||||
|
||||
public BlockTestTint() {
|
||||
super(Material.wood);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpaqueCube() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onBlockPlaced(@NotNull World worldIn, int x, int y, int z, int side, float subX, float subY, float subZ,
|
||||
int meta) {
|
||||
|
||||
// Face NORTH if placed up or down
|
||||
final var s = ForgeDirection.getOrientation(side);
|
||||
if (s == ForgeDirection.UP || s == ForgeDirection.DOWN) return 2;
|
||||
|
||||
// Face the placed side
|
||||
return side - 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRenderType() {
|
||||
return JSON_ISBRH_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int colorMultiplier(IBlockAccess world, int x, int y, int z, int tintIndex) {
|
||||
switch (tintIndex) {
|
||||
case 0:
|
||||
return 0xFF0000; // red
|
||||
case 1:
|
||||
return 0x00FF00; // green
|
||||
case 2:
|
||||
return 0x0000FF; // blue
|
||||
case 3:
|
||||
return 0xFFFF00; // yellow
|
||||
case 4:
|
||||
return 0xFF00FF; // purple
|
||||
case 5:
|
||||
return 0x00FFFF; // cyan
|
||||
default:
|
||||
return 0xFFFFFF;// white
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int colorMultiplier(ItemStack stack, int tintIndex) {
|
||||
// Like block
|
||||
return colorMultiplier(null, 0, 0, 0, tintIndex);
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,17 @@ import java.util.Random;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.renderer.RenderBlocks;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.client.IItemRenderer;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.model.baked.BakedModel;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.color.BlockColor;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.loading.ModelRegistry;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.state.BlockState;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager;
|
||||
@@ -29,7 +33,7 @@ import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||
|
||||
@ThreadSafeISBRH(perThread = true)
|
||||
public class ModelISBRH implements ISimpleBlockRenderingHandler {
|
||||
public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer {
|
||||
|
||||
/**
|
||||
* Any blocks using a JSON model should return this for {@link Block#getRenderType()}.
|
||||
@@ -42,14 +46,12 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler {
|
||||
|
||||
/// Override this if you want programmatic model selection
|
||||
@SuppressWarnings("unused")
|
||||
protected BakedModel getModel(IBlockAccess world, Block block, int meta, int x, int y, int z) {
|
||||
public BakedModel getModel(IBlockAccess world, Block block, int meta, int x, int y, int z) {
|
||||
return ModelRegistry.getBakedModel(new BlockState(block, meta));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderInventoryBlock(Block block, int metadata, int modelId, RenderBlocks renderer) {
|
||||
|
||||
}
|
||||
public void renderInventoryBlock(Block block, int meta, int modelId, RenderBlocks renderer) {}
|
||||
|
||||
@Override
|
||||
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId,
|
||||
@@ -73,14 +75,16 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler {
|
||||
// iterates over the quads and dumps em into the tesselator, nothing special
|
||||
rendered = true;
|
||||
for (final var quad : quads) {
|
||||
int quadColor = color;
|
||||
|
||||
if (quad.getColorIndex() != -1 && color == -1) {
|
||||
color = block.colorMultiplier(world, x, y, z);
|
||||
// If true use tintIndex color
|
||||
if (quad.getColorIndex() != -1) {
|
||||
quadColor = BlockColor.getColor(block, world, x, y, z, meta, quad.getColorIndex());
|
||||
}
|
||||
|
||||
final float r = (color & 255) / 255f;
|
||||
final float g = (color >> 8 & 255) / 255f;
|
||||
final float b = (color >> 16 & 255) / 255f;
|
||||
final float r = (quadColor & 255) / 255f;
|
||||
final float g = (quadColor >> 8 & 255) / 255f;
|
||||
final float b = (quadColor >> 16 & 255) / 255f;
|
||||
|
||||
final int lm = getLightMap(block, quad, dir, world, x, y, z, renderer);
|
||||
tesselator.setBrightness(lm);
|
||||
@@ -175,4 +179,80 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler {
|
||||
final var nz = unpackZ(normal);
|
||||
return Math.min(nx * nx * 0.6F + ny * ny * ((3.0F + ny) / 4.0F) + nz * nz * 0.8F, 1.0F);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleRenderType(ItemStack item, ItemRenderType type) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRendererHelper helper) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderItem(ItemRenderType type, ItemStack stack, Object... data) {
|
||||
Block block = Block.getBlockFromItem(stack.getItem());
|
||||
if (block == null) return;
|
||||
int meta = stack.getItemDamage();
|
||||
|
||||
final Tessellator tesselator = TessellatorManager.get();
|
||||
final BakedModel model = getModel(null, block, meta, 0, 0, 0);
|
||||
|
||||
GL11.glPushMatrix();
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
GL11.glDisable(GL11.GL_LIGHTING);
|
||||
tesselator.startDrawingQuads();
|
||||
|
||||
int color = model.getColor(null, 0, 0, 0, block, meta, RAND);
|
||||
|
||||
for (ModelQuadFacing dir : DIRECTIONS) {
|
||||
|
||||
final var quads = model.getQuads(null, 0, 0, 0, block, meta, dir, RAND, color, null);
|
||||
if (quads.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (ModelQuadView quad : quads) {
|
||||
int quadColor = color;
|
||||
|
||||
// If true use tintIndex color
|
||||
if (quad.getColorIndex() != -1) {
|
||||
quadColor = BlockColor.getColor(block, stack, quad.getColorIndex());
|
||||
}
|
||||
|
||||
float r = (quadColor & 0xFF) / 255f;
|
||||
float g = (quadColor >> 8 & 0xFF) / 255f;
|
||||
float b = (quadColor >> 16 & 0xFF) / 255f;
|
||||
|
||||
final float shade = diffuseLight(quad.getComputedFaceNormal());
|
||||
tesselator.setColorOpaque_F(r * shade, g * shade, b * shade);
|
||||
renderQuad(quad, -0.5f, -0.5f, -0.5f, tesselator, null);
|
||||
}
|
||||
}
|
||||
|
||||
// Rotated to exact side
|
||||
GL11.glRotated(-90f, 0f, 1f, 0f);
|
||||
|
||||
// TODO: Use BlockBench display transforms provided in '{}.display'
|
||||
// Translated to vanilla position
|
||||
if (type == ItemRenderType.EQUIPPED) {
|
||||
GL11.glTranslated(0.5f, 0.5f, -0.5f);
|
||||
}
|
||||
if (type == ItemRenderType.EQUIPPED_FIRST_PERSON) {
|
||||
GL11.glTranslated(0.5f, 0.5f, -0.5f);
|
||||
}
|
||||
if (type == ItemRenderType.ENTITY) {
|
||||
// No op
|
||||
}
|
||||
if (type == ItemRenderType.INVENTORY) {
|
||||
// No op
|
||||
}
|
||||
|
||||
tesselator.draw();
|
||||
GL11.glEnable(GL11.GL_LIGHTING);
|
||||
GL11.glDisable(GL11.GL_BLEND);
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.gtnewhorizon.gtnhlib.client.model.color;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
|
||||
/**
|
||||
* Utility class for managing block and item color handlers.
|
||||
* <p>
|
||||
* This allows modders to register {@link IBlockColor} handlers for blocks and retrieve color values for rendering, both
|
||||
* in-world and in inventory.
|
||||
*/
|
||||
public class BlockColor {
|
||||
|
||||
private static final Map<Block, IBlockColor> HANDLERS = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Registers a color handler for one or more blocks.
|
||||
*
|
||||
* @param handler The IBlockColor handler.
|
||||
* @param blocks The blocks to register the handler for.
|
||||
*/
|
||||
public static void registerBlockColors(IBlockColor handler, Block... blocks) {
|
||||
for (Block b : blocks) {
|
||||
HANDLERS.put(b, handler);
|
||||
}
|
||||
}
|
||||
|
||||
public IBlockColor getBlockColor(Block block) {
|
||||
return HANDLERS.get(block);
|
||||
}
|
||||
|
||||
public static int getColor(Block block, IBlockAccess world, int x, int y, int z, int meta, int tintIndex) {
|
||||
return getColor(block, world, null, x, y, z, meta, tintIndex);
|
||||
}
|
||||
|
||||
public static int getColor(Block block, ItemStack stack, int tintIndex) {
|
||||
return getColor(block, null, stack, 0, 0, 0, stack.getItemDamage(), tintIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method that fetches the color of a block or item stack, using a registered {@link IBlockColor} if
|
||||
* present.
|
||||
* <p>
|
||||
* Fallback order:
|
||||
* <ul>
|
||||
* <li>If handler exists and returns != -1, use that value.</li>
|
||||
* <li>If item stack exists, call stack.getItem().getColorFromItemStack()</li>
|
||||
* <li>If world exists, call block.colorMultiplier(world, x, y, z)</li>
|
||||
* <li>Else return white (0xFFFFFF)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param block The block.
|
||||
* @param world The world, may be null if rendering item.
|
||||
* @param stack The item stack, may be null if rendering block.
|
||||
* @param x X position (for block color lookup).
|
||||
* @param y Y position.
|
||||
* @param z Z position.
|
||||
* @param tintIndex Tint index.
|
||||
* @return The final color multiplier as 0xRRGGBB.
|
||||
*/
|
||||
|
||||
public static int getColor(Block block, @Nullable IBlockAccess world, @Nullable ItemStack stack, int x, int y,
|
||||
int z, int meta, int tintIndex) {
|
||||
|
||||
int color = -1;
|
||||
|
||||
// 1. Check for registered handler first, fallback to block's own IBlockColor implementation
|
||||
IBlockColor handler = HANDLERS.get(block);
|
||||
if (handler == null && block instanceof IBlockColor) {
|
||||
handler = (IBlockColor) block;
|
||||
}
|
||||
|
||||
if (handler != null) {
|
||||
color = applyHandler(handler, world, stack, x, y, z, tintIndex);
|
||||
if (color != -1) {
|
||||
return color;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. ItemStack color
|
||||
if (stack != null && stack.getItem() != null) {
|
||||
return stack.getItem().getColorFromItemStack(stack, tintIndex);
|
||||
}
|
||||
|
||||
// 4. World color
|
||||
if (world != null) {
|
||||
return block.colorMultiplier(world, x, y, z);
|
||||
}
|
||||
|
||||
// 5. Meta color fallback
|
||||
return block.getRenderColor(meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to invoke the handler with correct parameters.
|
||||
*/
|
||||
private static int applyHandler(@Nullable IBlockColor handler, @Nullable IBlockAccess world,
|
||||
@Nullable ItemStack stack, int x, int y, int z, int tintIndex) {
|
||||
|
||||
if (handler == null) return -1;
|
||||
|
||||
if (stack != null) {
|
||||
return handler.colorMultiplier(stack, tintIndex);
|
||||
}
|
||||
|
||||
if (world != null) {
|
||||
return handler.colorMultiplier(world, x, y, z, tintIndex);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.gtnewhorizon.gtnhlib.client.model.color;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
|
||||
/**
|
||||
* Interface for providing color multipliers for blocks and item blocks. Implement this interface to provide custom
|
||||
* coloring logic for a block or its item form.
|
||||
*/
|
||||
public interface IBlockColor {
|
||||
|
||||
/**
|
||||
* Returns the color multiplier for a block in the world at the given position.
|
||||
* <p>
|
||||
* Returning -1 means "do not override"; vanilla block color will be used.
|
||||
*
|
||||
* @param world The IBlockAccess world instance.
|
||||
* @param x Block X coordinate.
|
||||
* @param y Block Y coordinate.
|
||||
* @param z Block Z coordinate.
|
||||
* @param tintIndex The tint index, usually 0 for the main texture.
|
||||
* @return Color multiplier as 0xRRGGBB or -1 to use default.
|
||||
*/
|
||||
default int colorMultiplier(@Nullable IBlockAccess world, int x, int y, int z, int tintIndex) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color multiplier for an item stack.
|
||||
* <p>
|
||||
* Returning -1 means "do not override"; vanilla item color will be used.
|
||||
*
|
||||
* @param stack The ItemStack instance.
|
||||
* @param tintIndex The tint index, usually 0 for the main texture.
|
||||
* @return Color multiplier as 0xRRGGBB or -1 to use default.
|
||||
*/
|
||||
default int colorMultiplier(@Nullable ItemStack stack, int tintIndex) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"variants": {
|
||||
"meta=0": { "model": "gtnhlib:blocks/model_test_tint", "y": 270 },
|
||||
"meta=1": { "model": "gtnhlib:blocks/model_test_tint", "y": 90 },
|
||||
"meta=2": { "model": "gtnhlib:blocks/model_test_tint", "y": 0 },
|
||||
"meta=3": { "model": "gtnhlib:blocks/model_test_tint", "y": 180 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"parent": "block/cube",
|
||||
"textures": {
|
||||
"particle": "minecraft:stone",
|
||||
"north": "minecraft:stone",
|
||||
"south": "minecraft:stone",
|
||||
"east": "minecraft:stone",
|
||||
"west": "minecraft:stone",
|
||||
"up": "minecraft:stone",
|
||||
"down": "minecraft:stone"
|
||||
},
|
||||
"elements": [
|
||||
{
|
||||
"from": [0, 0, 0],
|
||||
"to": [16, 16, 16],
|
||||
"faces": {
|
||||
"north": { "texture": "#north", "tintindex": 0 },
|
||||
"south": { "texture": "#south", "tintindex": 1 },
|
||||
"east": { "texture": "#east", "tintindex": 2 },
|
||||
"west": { "texture": "#west", "tintindex": 3 },
|
||||
"up": { "texture": "#up", "tintindex": 4 },
|
||||
"down": { "texture": "#down", "tintindex": 5 }
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user