From 66d6a4d75e5feae334a9a2281eaf537c8a4adf2b Mon Sep 17 00:00:00 2001 From: Nedelosk Date: Thu, 5 May 2016 12:48:10 +0200 Subject: [PATCH] Work on ModelBaker --- .../java/forestry/api/core/IModelBaker.java | 13 ++-- .../forestry/api/core/IModelBakerModel.java | 8 ++- .../models/ModelDecorativeLeaves.java | 4 +- .../arboriculture/models/ModelLeaves.java | 9 +-- .../core/models/ModelBlockOverlay.java | 4 +- .../core/models/baker/ModelBaker.java | 63 ++++++++++++------- .../core/models/baker/ModelBakerFace.java | 6 +- .../core/models/baker/ModelBakerModel.java | 39 +++++++++--- .../farming/models/ModelFarmBlock.java | 9 +-- .../greenhouse/models/ModelGreenhouse.java | 6 +- 10 files changed, 106 insertions(+), 55 deletions(-) diff --git a/src/main/java/forestry/api/core/IModelBaker.java b/src/main/java/forestry/api/core/IModelBaker.java index cd10fc206..776e15b73 100644 --- a/src/main/java/forestry/api/core/IModelBaker.java +++ b/src/main/java/forestry/api/core/IModelBaker.java @@ -12,6 +12,7 @@ import net.minecraft.block.Block; import net.minecraft.client.renderer.block.model.IBakedModel; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -22,15 +23,19 @@ import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public interface IModelBaker { - void setRenderBoundsFromBlock(@Nonnull Block block); + void setRenderBounds(@Nonnull AxisAlignedBB renderBounds); - void setRenderBounds(double d, double e, double f, double g, double h, double i ); + void setRenderBounds(double d, double e, double f, double g, double h, double i); void setColorIndex(int color); - void addBlockModel(@Nonnull Block block, @Nullable BlockPos pos, @Nonnull TextureAtlasSprite[] sprites, int colorIndex); + void addBlockModel(@Nonnull Block block, @Nonnull AxisAlignedBB renderBounds, @Nullable BlockPos pos, @Nonnull TextureAtlasSprite[] sprites, int colorIndex); - void addBlockModel(@Nonnull Block block, @Nullable BlockPos pos, @Nonnull TextureAtlasSprite sprites, int colorIndex); + void addBlockModel(@Nonnull Block block, @Nonnull AxisAlignedBB renderBounds, @Nullable BlockPos pos, @Nonnull TextureAtlasSprite sprites, int colorIndex); + + void addModel(@Nonnull AxisAlignedBB renderBounds, @Nonnull TextureAtlasSprite[] textures, int colorIndex); + + void addModel(@Nonnull AxisAlignedBB renderBounds, @Nonnull TextureAtlasSprite texture, int colorIndex); void addBakedModel(@Nonnull IBakedModel model); diff --git a/src/main/java/forestry/api/core/IModelBakerModel.java b/src/main/java/forestry/api/core/IModelBakerModel.java index ff030e3a1..3eb9a9387 100644 --- a/src/main/java/forestry/api/core/IModelBakerModel.java +++ b/src/main/java/forestry/api/core/IModelBakerModel.java @@ -5,10 +5,14 @@ ******************************************************************************/ package forestry.api.core; +import java.util.List; + +import net.minecraft.client.renderer.block.model.BakedQuad; import net.minecraft.client.renderer.block.model.IBakedModel; +import net.minecraft.client.renderer.block.model.SimpleBakedModel; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.vertex.VertexFormat; - +import net.minecraft.util.EnumFacing; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -23,6 +27,8 @@ public interface IModelBakerModel extends IBakedModel { // void setFormat(VertexFormat formate); + void addQuad(EnumFacing facing, BakedQuad quad); + void setRotation(float[] rotation); void setTranslation(float[] translation); diff --git a/src/main/java/forestry/arboriculture/models/ModelDecorativeLeaves.java b/src/main/java/forestry/arboriculture/models/ModelDecorativeLeaves.java index 4a68300f0..253e7f31d 100644 --- a/src/main/java/forestry/arboriculture/models/ModelDecorativeLeaves.java +++ b/src/main/java/forestry/arboriculture/models/ModelDecorativeLeaves.java @@ -92,13 +92,13 @@ public class ModelDecorativeLeaves extends ModelBlockOverlay { return; } - baker.addBlockModel(block, null, leavesIcon, 0); + baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, leavesIcon, 0); // add fruit if (!leaves.hasFruit()) { @@ -63,7 +64,7 @@ public class ModelLeaves extends ModelBlockOverlay { if (fruitTexture == null) { return; } - baker.addBlockModel(block, null, fruitTexture, 1); + baker.addBlockModel(block, Block.FULL_BLOCK_AABB, null, fruitTexture, 1); } @Override @@ -76,13 +77,13 @@ public class ModelLeaves extends ModelBlockOverlay { TextureAtlasSprite leaveSprite = tile.getLeaveSprite(Proxies.render.fancyGraphicsEnabled()); // Render the plain leaf block. - baker.addBlockModel(block, pos, leaveSprite, 0); + baker.addBlockModel(block, Block.FULL_BLOCK_AABB, pos, leaveSprite, 0); // Render overlay for fruit leaves. TextureAtlasSprite fruitSprite = tile.getFruitSprite(); if (fruitSprite != null) { - baker.addBlockModel(block, pos, fruitSprite, 1); + baker.addBlockModel(block, Block.FULL_BLOCK_AABB, pos, fruitSprite, 1); } // Set the particle sprite diff --git a/src/main/java/forestry/core/models/ModelBlockOverlay.java b/src/main/java/forestry/core/models/ModelBlockOverlay.java index 74938834b..dc1ee30ee 100644 --- a/src/main/java/forestry/core/models/ModelBlockOverlay.java +++ b/src/main/java/forestry/core/models/ModelBlockOverlay.java @@ -63,7 +63,7 @@ public abstract class ModelBlockOverlay implements IBakedModel } B bBlock = blockClass.cast(block); - baker.setRenderBoundsFromBlock(block); + baker.setRenderBounds(block.getBoundingBox(state, world, pos)); bakeWorldBlock(bBlock, world, pos, stateExtended, baker); latestBlockModel = baker.bakeModel(false); @@ -145,7 +145,7 @@ public abstract class ModelBlockOverlay implements IBakedModel } B bBlock = blockClass.cast(block); - baker.setRenderBoundsFromBlock(block); + baker.setRenderBounds(block.getBoundingBox(block.getStateFromMeta(stack.getItemDamage()), world, null)); bakeInventoryBlock(bBlock, stack, baker); return latestItemModel = baker.bakeModel(true); diff --git a/src/main/java/forestry/core/models/baker/ModelBaker.java b/src/main/java/forestry/core/models/baker/ModelBaker.java index cd8aafd63..c754407e8 100644 --- a/src/main/java/forestry/core/models/baker/ModelBaker.java +++ b/src/main/java/forestry/core/models/baker/ModelBaker.java @@ -58,13 +58,12 @@ public class ModelBaker implements IModelBaker { protected final float[] defUVs = new float[] { 0, 0, 1, 1 }; @Override - public void setRenderBoundsFromBlock(@Nonnull Block block) { - if (block == null) { + public void setRenderBounds(@Nonnull AxisAlignedBB renderBounds) { + if (renderBounds == null) { return; } - //TODO: Fixme - renderBounds = Block.FULL_BLOCK_AABB; + this.renderBounds = renderBounds; } @Override @@ -80,25 +79,49 @@ public class ModelBaker implements IModelBaker { } @Override - public void addBlockModel(@Nonnull Block block, @Nullable BlockPos pos, @Nonnull TextureAtlasSprite[] textures, int colorIndex) { - setRenderBoundsFromBlock(block); + public void addModel(AxisAlignedBB renderBounds, @Nonnull TextureAtlasSprite[] textures, int colorIndex) { + setRenderBounds(renderBounds); setColorIndex(colorIndex); - World world = Proxies.common.getRenderWorld(); - IBlockState blockState = world.getBlockState(pos); for (EnumFacing facing : EnumFacing.VALUES) { - if (pos == null || block.shouldSideBeRendered(blockState, world, pos, facing)) { + addFace(facing, textures[facing.ordinal()]); + } + + setRenderBounds(Block.FULL_BLOCK_AABB); + } + + @Override + public void addModel(AxisAlignedBB renderBounds, @Nonnull TextureAtlasSprite texture, int colorIndex) { + addModel(renderBounds, new TextureAtlasSprite[]{ texture, texture, texture, texture, texture, texture }, colorIndex); + } + + @Override + public void addBlockModel(@Nonnull Block block, AxisAlignedBB renderBounds, @Nullable BlockPos pos, @Nonnull TextureAtlasSprite[] textures, int colorIndex) { + setRenderBounds(renderBounds); + + setColorIndex(colorIndex); + + if(pos != null){ + World world = Proxies.common.getRenderWorld(); + IBlockState blockState = world.getBlockState(pos); + for (EnumFacing facing : EnumFacing.VALUES) { + if (block.shouldSideBeRendered(blockState, world, pos, facing)) { + addFace(facing, textures[facing.ordinal()]); + } + } + }else { + for (EnumFacing facing : EnumFacing.VALUES) { addFace(facing, textures[facing.ordinal()]); } } - setRenderBounds(0, 0, 0, 1, 1, 1); + setRenderBounds(Block.FULL_BLOCK_AABB); } @Override - public void addBlockModel(@Nonnull Block block, @Nullable BlockPos pos, @Nonnull TextureAtlasSprite texture, int colorIndex) { - addBlockModel(block, pos, new TextureAtlasSprite[]{ texture , texture, texture, texture, texture, texture }, colorIndex); + public void addBlockModel(@Nonnull Block block, @Nonnull AxisAlignedBB renderBounds, @Nullable BlockPos pos, @Nonnull TextureAtlasSprite texture, int colorIndex) { + addBlockModel(block, renderBounds, pos, new TextureAtlasSprite[]{ texture, texture, texture, texture, texture, texture }, colorIndex); } @Override @@ -178,7 +201,7 @@ public class ModelBaker implements IModelBaker { return; } - boolean isEdge; + /*boolean isEdge; switch (facing) { case WEST: isEdge = renderBounds.minX < 0.0001; @@ -200,12 +223,12 @@ public class ModelBaker implements IModelBaker { break; default: return; - } + }*/ Vector3f to = new Vector3f((float) renderBounds.minX * 16.0f, (float) renderBounds.minY * 16.0f, (float) renderBounds.minZ * 16.0f); Vector3f from = new Vector3f((float) renderBounds.maxX * 16.0f, (float) renderBounds.maxY * 16.0f, (float) renderBounds.maxZ * 16.0f); - faces.add(new ModelBakerFace(facing, isEdge, colorIndex, to, from, defUVs, sprite)); + faces.add(new ModelBakerFace(facing/*, isEdge*/, colorIndex, to, from, defUVs, sprite)); } @Override @@ -218,7 +241,7 @@ public class ModelBaker implements IModelBaker { //Add baked models to the current model. for(IBakedModel bakedModel : bakedModels){ - this.currentModel.addModelQuads(bakedModel); + currentModel.addModelQuads(bakedModel); } for (ModelBakerFace face : faces) { @@ -229,13 +252,9 @@ public class ModelBaker implements IModelBaker { final BlockPartFace bpf = new BlockPartFace(myFace, face.colorIndex, "", uv); BakedQuad bf = faceBakery.makeBakedQuad(face.to, face.from, bpf, face.spite, myFace, mr, null, true, true); -// bf = new IColoredBakedQuad.ColoredBakedQuad(bf.getVertexData(), face.colorIndex, bf.getFace()); -// if (face.isEdge) { -// this.currentModel.getFaceQuads(myFace).add(bf); -// } else { -// this.currentModel.getGeneralQuads().add(bf); -// } + //if (face.isEdge) { + currentModel.addQuad(myFace, bf); } return currentModel; diff --git a/src/main/java/forestry/core/models/baker/ModelBakerFace.java b/src/main/java/forestry/core/models/baker/ModelBakerFace.java index f6bf969d3..72e5750e4 100644 --- a/src/main/java/forestry/core/models/baker/ModelBakerFace.java +++ b/src/main/java/forestry/core/models/baker/ModelBakerFace.java @@ -27,7 +27,7 @@ import org.lwjgl.util.vector.Vector3f; public class ModelBakerFace { @Nonnull public final EnumFacing face; - public final boolean isEdge; + //public final boolean isEdge; public final Vector3f to; public final Vector3f from; @@ -38,10 +38,10 @@ public class ModelBakerFace { public final int colorIndex; - public ModelBakerFace(@Nonnull EnumFacing face, boolean isEdge, int colorIndex, Vector3f to, Vector3f from, float[] defUVs2, TextureAtlasSprite sprite) { + public ModelBakerFace(@Nonnull EnumFacing face/*, boolean isEdge*/, int colorIndex, Vector3f to, Vector3f from, float[] defUVs2, TextureAtlasSprite sprite) { this.colorIndex = colorIndex; this.face = face; - this.isEdge = isEdge; + //this.isEdge = isEdge; this.to = to; this.from = from; this.uv = defUVs2; diff --git a/src/main/java/forestry/core/models/baker/ModelBakerModel.java b/src/main/java/forestry/core/models/baker/ModelBakerModel.java index cd8b6e909..d48841e9d 100644 --- a/src/main/java/forestry/core/models/baker/ModelBakerModel.java +++ b/src/main/java/forestry/core/models/baker/ModelBakerModel.java @@ -12,7 +12,9 @@ package forestry.core.models.baker; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import net.minecraft.block.state.IBlockState; import net.minecraft.client.renderer.block.model.BakedQuad; @@ -36,19 +38,29 @@ public class ModelBakerModel implements IModelBakerModel { private boolean isAmbientOcclusion; private VertexFormat format; private TextureAtlasSprite particleSprite; + private final Map> faceQuads; + private final List generalQuads; private float[] rotation = getDefaultRotation(); private float[] translation = getDefaultTranslation(); private float[] scale = getDefaultScale(); public ModelBakerModel() { models = new ArrayList<>(); + faceQuads = new HashMap<>(); + generalQuads = new ArrayList<>(); format = DefaultVertexFormats.BLOCK; isGui3d = true; isAmbientOcclusion = false; + + for(EnumFacing face : EnumFacing.VALUES){ + faceQuads.put(face, new ArrayList()); + } } - private ModelBakerModel(List models, boolean isGui3d, boolean isAmbientOcclusion, VertexFormat format, float[] rotation, float[] translation, float[] scale, TextureAtlasSprite particleSprite) { + private ModelBakerModel(List models, Map> faceQuads, List generalQuads, boolean isGui3d, boolean isAmbientOcclusion, VertexFormat format, float[] rotation, float[] translation, float[] scale, TextureAtlasSprite particleSprite) { this.models = models; + this.faceQuads = faceQuads; + this.generalQuads = generalQuads; this.isGui3d = isGui3d; this.isAmbientOcclusion = isAmbientOcclusion; this.format = format; @@ -148,23 +160,30 @@ public class ModelBakerModel implements IModelBakerModel { public void addModelQuads(IBakedModel model) { this.models.add(model); } + + @Override + public void addQuad(EnumFacing facing, BakedQuad quad) { + if(facing != null){ + faceQuads.get(facing).add(quad); + }else{ + generalQuads.add(quad); + } + } @Override public List getQuads(IBlockState state, EnumFacing side, long rand) { List quads = new ArrayList<>(); - if (side == null) { - for (IBakedModel model : this.models) { - quads.addAll(model.getQuads(state, null, rand)); - } - } else { - for (IBakedModel model : this.models) { - quads.addAll(model.getQuads(state, side, rand)); - } + for (IBakedModel model : this.models) { + quads.addAll(model.getQuads(state, side, rand)); } + if(side != null){ + quads.addAll(faceQuads.get(side)); + } + quads.addAll(generalQuads); return quads; } public ModelBakerModel copy() { - return new ModelBakerModel(models, isGui3d, isAmbientOcclusion, format, rotation, translation, scale, particleSprite); + return new ModelBakerModel(models, faceQuads, generalQuads, isGui3d, isAmbientOcclusion, format, rotation, translation, scale, particleSprite); } } diff --git a/src/main/java/forestry/farming/models/ModelFarmBlock.java b/src/main/java/forestry/farming/models/ModelFarmBlock.java index 0fd6578b3..f132a6b64 100644 --- a/src/main/java/forestry/farming/models/ModelFarmBlock.java +++ b/src/main/java/forestry/farming/models/ModelFarmBlock.java @@ -2,6 +2,7 @@ package forestry.farming.models; import javax.annotation.Nonnull; +import net.minecraft.block.Block; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; @@ -30,9 +31,9 @@ public class ModelFarmBlock extends ModelBlockOverlay { EnumFarmBlockTexture type = EnumFarmBlockTexture.getFromCompound(item.getTagCompound()); // Add the plain block. - baker.addBlockModel(blockFarm, null, getSprites(type), 0); + baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, null, getSprites(type), 0); // Add the overlay block. - baker.addBlockModel(blockFarm, null, getOverlaySprites(EnumFarmBlockType.VALUES[item.getItemDamage()]), 0); + baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, null, getOverlaySprites(EnumFarmBlockType.VALUES[item.getItemDamage()]), 0); } @Override @@ -43,9 +44,9 @@ public class ModelFarmBlock extends ModelBlockOverlay { TextureAtlasSprite[] textures = getSprites(farm.getFarmBlockTexture()); // Add the plain block. - baker.addBlockModel(blockFarm, pos, textures, 0); + baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, pos, textures, 0); // Add the overlay block. - baker.addBlockModel(blockFarm, pos, getOverlaySprites(farm.getFarmBlockType()), 0); + baker.addBlockModel(blockFarm, Block.FULL_BLOCK_AABB, pos, getOverlaySprites(farm.getFarmBlockType()), 0); // Set the particle sprite baker.setParticleSprite(textures[0]); diff --git a/src/main/java/forestry/greenhouse/models/ModelGreenhouse.java b/src/main/java/forestry/greenhouse/models/ModelGreenhouse.java index 5e7978900..d30dad0de 100644 --- a/src/main/java/forestry/greenhouse/models/ModelGreenhouse.java +++ b/src/main/java/forestry/greenhouse/models/ModelGreenhouse.java @@ -60,12 +60,12 @@ public class ModelGreenhouse extends ModelBlockOverlay { else if (block.getGreenhouseType() == BlockGreenhouseType.GLASS) { TextureAtlasSprite glassSprite = BlockGreenhouseType.getSprite(BlockGreenhouseType.GLASS, null, null, world, pos); - baker.addBlockModel(block, pos, BlockGreenhouseType.getSprite(BlockGreenhouseType.GLASS, null, null, world, pos), 100); + baker.addBlockModel(block, Block.FULL_BLOCK_AABB, pos, BlockGreenhouseType.getSprite(BlockGreenhouseType.GLASS, null, null, world, pos), 100); baker.setParticleSprite(glassSprite); } else { TextureAtlasSprite plainSprite = BlockGreenhouseType.getSprite(BlockGreenhouseType.PLAIN, null, null, world, pos); - baker.addBlockModel(block, pos, BlockGreenhouseType.getSprite(BlockGreenhouseType.PLAIN, null, null, world, pos), 100); + baker.addBlockModel(block, Block.FULL_BLOCK_AABB, pos, BlockGreenhouseType.getSprite(BlockGreenhouseType.PLAIN, null, null, world, pos), 100); baker.setParticleSprite(plainSprite); } @@ -74,7 +74,7 @@ public class ModelGreenhouse extends ModelBlockOverlay { for (EnumFacing facing : EnumFacing.VALUES) { sprite[facing.ordinal()] = BlockGreenhouseType.getSprite(block.getGreenhouseType(), facing, stateExtended, world, pos); } - baker.addBlockModel(block, pos, sprite, 101); + baker.addBlockModel(block, Block.FULL_BLOCK_AABB, pos, sprite, 101); } }