Jump to content

[1.11.2] How to make variable water level in a block


TheUnlocked

Recommended Posts

The title basically says it. Similar to what you might see in a Tinkers Construct lava tank.

I have basically no knowledge of custom block rendering, and looking online I've only found nothing super useful. Yes, I'm aware TC is open source, but it's hard to learn something from example when you have no knowledge to kick off from.

 

Here's what I already have:

  • I already have some variable that represents the water level stored in a tile entity. I can easily convert this number to a percentage.
  • I already have block model and block state json files, which I have responding to IProperties.

 

Here's what I want to know how to do:

  • I want to render water on the sides and top (the user can only look in from the north, south, and top, but since the block can be rotated it might be easier to render on all sides) with a height according to water level value
  • I want to register my custom renderer if necessary (this is something I haven't managed to find anywhere)
  • I would prefer if I could do this algorithmically rather than needing to make a ton of block model files because I'm sure this can be done algorithmically
  • I do NOT need to have support for any liquid other than water

 

Again, emphasis on I've never touched custom block renderers before, even in older versions of forge.

If there's already a really good explanation out there of how to do this, feel free to link me to that.

Thanks in advance to anyone who helps.

Edited by TheUnlocked
Link to comment
Share on other sites

BlockFluidRenderer does appear to have code for rendering fluids based on height, but it's generalized for ALL fluids, takes things like slope into account, and is completely unreadable to someone who has no experience with the specific topic. It's the same reason that reading the TC code is hard, but to an extreme.

Edited by TheUnlocked
Link to comment
Share on other sites

26 minutes ago, jeffryfisher said:

See the blockstates and model JSONs for cauldron

I want a smooth animation, so blockstates/models are basically out of the option. From my OP:

18 hours ago, TheUnlocked said:
  • I would prefer if I could do this algorithmically rather than needing to make a ton of block model files because I'm sure this can be done algorithmically

 

Link to comment
Share on other sites

Depends on what exactly do you want to acheve here and how do you want it to look. 

For a fluid tank I would use either a cusom IBakedModel (if I do not need the client to see smooth water level transitions in real time as the fluid level raises/loweres through say a pipe) or a FastTESR, and I think that you want the later.

Register your TESR using ClientRegistry::bindTileEntitySpecialRenderer. In the renderer itself use water texture UVs and render your desired image at desired positions with Tessellator(or rather put your vertex data in Tessellator's buffer). You can get the water texture using BlockModelShapes::getTexture(state). BlockModelShapes can be obtained through BlockRendererDispatcher::getBlockModelShapes and BlockRendererDispatcher lies at Minecraft::getBlockRendererDispatcher. You can see how exactly to use tessellator in vanilla TESRs. Or you might ask here for more clarification ;)

Just remember that with FastTESR you do not need to bind your textures or call neither begin nor draw methods.

  • Like 1
Link to comment
Share on other sites

1 hour ago, V0idWa1k3r said:

You can see how exactly to use tessellator in vanilla TESRs. Or you might ask here for more clarification ;)

So I've made... a thing. It doesn't work, but to be fair I also didn't really expect it to. Where do I go from here?

 

Inside of renderTileEntityFast

final float PX = 1f / 16f; // Aligning with the block models
final float YOFF = 1 * PX;
final float BORDER = 2 * PX;
final float MAXHEIGHT = 10 * PX;

float actualHeight = (MAXHEIGHT * te.waterLevel) + YOFF;
BlockModelShapes bm = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes();
TextureAtlasSprite still =  bm.getTexture(Blocks.WATER.getDefaultState()); // What do I do with this?

//vertexBuffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX); // It complained at me (aka crashed) when I tried to use this
vertexBuffer.pos(BORDER, actualHeight, BORDER).tex(still.getMinU(), still.getMinV()).endVertex(); // I feel like I'm doing this all wrong
vertexBuffer.pos(1 - BORDER, actualHeight, BORDER).tex(still.getMaxU(), still.getMinV()).endVertex();
vertexBuffer.pos(1 - BORDER, actualHeight, 1 - BORDER).tex(still.getMaxU(), still.getMaxV()).endVertex();
vertexBuffer.pos(BORDER, actualHeight, 1 - BORDER).tex(still.getMinU(), still.getMaxV()).endVertex();

 

Inside of preInit

ClientRegistry.bindTileEntitySpecialRenderer(BlockExample.TileEntityExample.class, new RenderExample());

 

Edited by TheUnlocked
Link to comment
Share on other sites

10 minutes ago, TheUnlocked said:

TextureAtlasSprite still = bm.getTexture(Blocks.WATER.getDefaultState()); // What do I do with this?

This is fine, you do not need to do anything with this ;)

It is not rendering because it is... at 0,0,0. Why do you think there are x/y/z parameters given to you in renderTileEntityFast? :D

Vanilla translates TEs in it's rendering using GlStateManager, but you can't do that because of FastTESR. Simply translate your vertices.

Also the default vertexformat is BLOCK. Meaning that you need to specify more than xyz+uv in your vertices. You need to specify xyz + color(as 4 bytes(rgba)) + uv + lightmap

Edited by V0idWa1k3r
Link to comment
Share on other sites

13 minutes ago, V0idWa1k3r said:

This is fine, you do not need to do anything with this ;)

After doing what you said, it does render the water, but why? It's very un-intuitive for a get function to set a value.

 

13 minutes ago, V0idWa1k3r said:

You need to specify xyz + color(as a hex integer) + uv + lightmap

I'm wonder what I'm supposed to put for the lightmap. My version of forge has those parameters obfuscated, and I don't completely get what the lightmap is. I think it has something to do with sun/artificial light, but ¯\_(ツ)_/¯

 

The same goes for color. I just stuck 

.color(1f,1f,1f,1f)

onto all of the vertices, but idk if that's what I'm supposed to do.

 

I have a feeling this is related to the lightmap and color, but the water texture is super dark (as seen in the attached image).

2017-05-14_13.58.49.png

Edited by TheUnlocked
Link to comment
Share on other sites

1 minute ago, TheUnlocked said:

It's very un-intuitive for a get function to set a value.

It does not. It just takes a blockstate and returns a texture of that blockstate. Different blockstates can have different textures :) This is not the case for a water block, thus you do not have to do anything with it.

A lightmap is... kinda obscure but it just controls the 'final' brightness of your texture, it's exposure to light and light's color(you know how in vanilla torches, sun and moon slightly vary in color of light they cast? That is your lightmap coordinates.) You can see how lightmap is normally calculated at Particle::renderParticle. To be precise, the

int i = this.getBrightnessForRender(partialTicks);
int j = i >> 16 & 65535;
int k = i & 65535;

j and k are the lightmap coordinates here and getBrightnessForRender simply returns the combined light value for a specified block position(in particle's case the position the particle is at)

  • Like 1
Link to comment
Share on other sites

Thanks so much for the help. One more question: How would I go about adding more faces? It feels a bit sketchy to just add more vertices because I don't really get where or when I would add them.

 

I'm fine with making a whole cube rather than just three faces if that's easier. If not, that's fine too.

Link to comment
Share on other sites

A whole cube is 6 more faces. You would add them just after the 4 vertices definitions for your up face - add 4 more for 1 more face and so on. You might need to experiment a bit with UVs/positions to make your face render the correct way but is it quite easy once you get used to vertexbuffer :)

  • Like 1
Link to comment
Share on other sites

Thanks for all the help! It works now! :D

 

For others who are looking for examples, here's my complete RenderExample class

public class RenderExample extends FastTESR<BlockExample.TileEntityExample>{
    @Override
    public void renderTileEntityFast(@Nonnull BlockExample.TileEntityExample te, double x, double y, double z, float partialTicks, int destroyStage, @Nonnull VertexBuffer vertexBuffer) {
        final float PX = 1f / 16f;
        final float YOFF = 1 * PX;
        final float BORDER = 1.9f * PX;
        final float MAXHEIGHT = 10 * PX;
        final float LOW = 5.9f * PX;

        float actualHeight = (MAXHEIGHT * te.waterLevel) + YOFF;
        BlockModelShapes bm = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes();
        TextureAtlasSprite still =  bm.getTexture(Blocks.WATER.getDefaultState());
        TextureAtlasSprite flow =  bm.getTexture(Blocks.FLOWING_WATER.getDefaultState());

        // Lightmap calculations
        int upCombined = getWorld().getCombinedLight(te.getPos().up(), 0);
        int upLMa = upCombined >> 16 & 65535;
        int upLMb = upCombined & 65535;

        int northCombined = getWorld().getCombinedLight(te.getPos().add(new BlockPos(0,0,1)), 0);
        int northLMa = northCombined >> 16 & 65535;
        int northLMb = northCombined & 65535;

        int southCombined = getWorld().getCombinedLight(te.getPos().add(new BlockPos(0,0,-1)), 0);
        int southLMa = southCombined >> 16 & 65535;
        int southLMb = southCombined & 65535;

        int westCombined = getWorld().getCombinedLight(te.getPos().add(new BlockPos(-1,0,0)), 0);
        int westLMa = westCombined >> 16 & 65535;
        int westLMb = westCombined & 65535;

        int eastCombined = getWorld().getCombinedLight(te.getPos().add(new BlockPos(1,0,0)), 0);
        int eastLMa = eastCombined >> 16 & 65535;
        int eastLMb = eastCombined & 65535;

        vertexBuffer.setTranslation(x,y,z);

        //UP face
        vertexBuffer.pos(BORDER, actualHeight, BORDER).color(1f,1f,1f,0.8f).tex(still.getMinU(), still.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, actualHeight, BORDER).color(1f,1f,1f,0.8f).tex(still.getMaxU(), still.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, actualHeight, 1 - BORDER).color(1f,1f,1f,0.8f).tex(still.getMaxU(), still.getMaxV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(BORDER, actualHeight, 1 - BORDER).color(1f,1f,1f,0.8f).tex(still.getMinU(), still.getMaxV()).lightmap(upLMa,upLMb).endVertex();

        //NORTH face

        vertexBuffer.pos(BORDER, actualHeight, 1 - BORDER).color(1f,1f,1f,0.8f).tex(flow.getMinU(), flow.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, actualHeight, 1 - BORDER).color(1f,1f,1f,0.8f).tex(flow.getMaxU(), flow.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, LOW, 1 - BORDER).color(1f,1f,1f,0.8f).tex(flow.getMaxU(), flow.getMaxV()).lightmap(northLMa,northLMb).endVertex();
        vertexBuffer.pos(BORDER, LOW, 1 - BORDER).color(1f,1f,1f,0.8f).tex(flow.getMinU(), flow.getMaxV()).lightmap(northLMa,northLMb).endVertex();

        //SOUTH face
        vertexBuffer.pos(BORDER, actualHeight, BORDER).color(1f,1f,1f,0.8f).tex(flow.getMinU(), flow.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, actualHeight, BORDER).color(1f,1f,1f,0.8f).tex(flow.getMaxU(), flow.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, LOW, BORDER).color(1f,1f,1f,0.8f).tex(flow.getMaxU(), flow.getMaxV()).lightmap(southLMa,southLMb).endVertex();
        vertexBuffer.pos(BORDER, LOW, BORDER).color(1f,1f,1f,0.8f).tex(flow.getMinU(), flow.getMaxV()).lightmap(southLMa,southLMb).endVertex();

        //WEST face
        vertexBuffer.pos(BORDER, actualHeight, BORDER).color(1f,1f,1f,0.8f).tex(flow.getMinU(), flow.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(BORDER, actualHeight, 1 - BORDER).color(1f,1f,1f,0.8f).tex(flow.getMaxU(), flow.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(BORDER, LOW, 1 - BORDER).color(1f,1f,1f,0.8f).tex(flow.getMaxU(), flow.getMaxV()).lightmap(westLMa,westLMb).endVertex();
        vertexBuffer.pos(BORDER, LOW, BORDER).color(1f,1f,1f,0.8f).tex(flow.getMinU(), flow.getMaxV()).lightmap(westLMa,westLMb).endVertex();

        //EAST face
        vertexBuffer.pos(1 - BORDER, actualHeight, BORDER).color(1f,1f,1f,0.8f).tex(flow.getMinU(), flow.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, actualHeight, 1 - BORDER).color(1f,1f,1f,0.8f).tex(flow.getMaxU(), flow.getMinV()).lightmap(upLMa,upLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, LOW, 1 - BORDER).color(1f,1f,1f,0.8f).tex(flow.getMaxU(), flow.getMaxV()).lightmap(eastLMa,eastLMb).endVertex();
        vertexBuffer.pos(1 - BORDER, LOW, BORDER).color(1f,1f,1f,0.8f).tex(flow.getMinU(), flow.getMaxV()).lightmap(eastLMa,eastLMb).endVertex();
    }
}

 

And registering the FastTESR put this in preInit:

ClientRegistry.bindTileEntitySpecialRenderer(BlockCrystalGrowthChamber.TileEntityCrystalGrowthChamber.class, new RenderCrystalGrowthChamber());

 

And finally what it looks like (the rest of the model is registered like a regular block model):

2017-05-14_15.30.57.png

Edited by TheUnlocked
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.