Jump to content

Getting U and V for texture map based on blockstate


Lenardjee

Recommended Posts

Hey everyone,

In my previous post I had a question about rendering a JSON model using a TESR, but I was told that using a JSON model would be a very inefficient way of doing it. The situation is this: I have a block with a TileEntity, and the blocks is like a block from chisel and bits, so it can change shape. The shape is made from a ArrayList<Obj> where Obj is a class extending AxisAlignedBB with the only change being that it has a field for the blockstate and that a blockstate must be passed in into the constructor. I've done that to be able to retrieve the texture for each of the pieces that the block is constructed with. Now what I want to do is render the block using a TESR, but I can't figure out how to get the texture specific for that blockstate from the TextureMap. This is my code so far:

	@Override
	public void render(TileEntityMiniBlock te, double x, double y, double z, float partialTicks, int destroyStage, float alpha) {
//		System.out.println("render");
		renderBlock(te, x, y, z, partialTicks, destroyStage, alpha);
	}

	public void renderBlock(TileEntityMiniBlock te1, double x, double y, double z, float partialTicks, int destroyStage, float alpha) {
//		System.out.println("renderBlock");
		if(Minecraft.getMinecraft().getIntegratedServer().getWorld(Minecraft.getMinecraft().world.provider.getDimension()).getTileEntity(te1.getPos()) != null) {
			if(Minecraft.getMinecraft().getIntegratedServer().getWorld(Minecraft.getMinecraft().world.provider.getDimension()).getTileEntity(te1.getPos()) instanceof TileEntityMiniBlock) {
				te = (TileEntityMiniBlock) Minecraft.getMinecraft().getIntegratedServer().getWorld(Minecraft.getMinecraft().world.provider.getDimension()).getTileEntity(te1.getPos());
				BufferBuilder ver = Tessellator.getInstance().getBuffer();
				ArrayList<Obj> objs = te.objs;
				for(int i = 0; i < objs.size(); i++) {
					Obj o = objs.get(i);
					ResourceLocation n = o.blockType.getBlock().getRegistryName();
					GlStateManager.pushMatrix();
					ver.reset();
//					GlStateManager.enableLighting();
					RenderHelper.enableStandardItemLighting();
					GlStateManager.enableRescaleNormal();
					GlStateManager.enableBlend();
					ver.setTranslation(x, y, z);
					Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
					String path = o.blockType.getBlock().getRegistryName().getResourceDomain() + ":textures/block/" + o.blockType.getBlock().getRegistryName().getResourcePath();
//					System.out.println(path);
					path = "minecraft:textures/blocks/stone.png";
//					float minU = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getInterpolatedU(o.minZ * 16.0);
//					float minV = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getInterpolatedV(16.0 - (o.maxZ * 16.0));
//					float maxU = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getInterpolatedU(16.0 - (o.minZ * 16.0));
//					float maxV = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getInterpolatedV(o.maxZ * 16.0);
					
					float minU = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getMinU();
					float minV = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getMinV();
					float maxU = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getMaxU();
					float maxV = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getMaxV();
					
//					System.out.println(minU);
//					System.out.println(minV);
//					System.out.println(maxU);
//					System.out.println(maxV);
//					Minecraft.getMinecraft().getTextureManager().bindTexture(o.blockType.getBlock().getRegistryName());
					
					int posU = 0;
					int posV = 0;
					
					ver.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
//					ver.pos(o.minX, o.minY, o.maxZ).tex(1.0 / 32.0 * (posU + 	o.maxZ),	1.0 / 32.0 * (posV + (16 -	o.minY))).endVertex();
//					ver.pos(o.minX, o.maxY, o.maxZ).tex(1.0 / 32.0 * (posU + 	o.maxZ),	1.0 / 32.0 * (posV + (16 -	o.maxY))).endVertex();
//					ver.pos(o.minX, o.maxY, o.minZ).tex(1.0 / 32.0 * (posU + 	o.minZ),	1.0 / 32.0 * (posV + (16 -	o.maxY))).endVertex();
//					ver.pos(o.minX, o.minY, o.minZ).tex(1.0 / 32.0 * (posU + 	o.minZ),	1.0 / 32.0 * (posV + (16 -	o.minY))).endVertex();
					
					ver.pos(o.minX, o.minY, o.maxZ).tex(maxU, maxV).endVertex();
					ver.pos(o.minX, o.maxY, o.maxZ).tex(maxU, minV).endVertex();
					ver.pos(o.minX, o.maxY, o.minZ).tex(minU, minV).endVertex();
					ver.pos(o.minX, o.minY, o.minZ).tex(minU, maxV).endVertex();
					
//					ver.pos(o.minX, o.minY, o.maxZ).tex(0.34375, 0.84375).endVertex();
//					ver.pos(o.minX, o.maxY, o.maxZ).tex(0.34375, 0.8125).endVertex();
//					ver.pos(o.minX, o.maxY, o.minZ).tex(0.3125, 0.8125).endVertex();
//					ver.pos(o.minX, o.minY, o.minZ).tex(0.3125, 0.84375).endVertex();
					
//					ver.pos(o.minX, o.minY, o.maxZ).tex(0.1, 0.11).endVertex();
//					ver.pos(o.minX, o.maxY, o.maxZ).tex(0.1, 0.1).endVertex();
//					ver.pos(o.minX, o.maxY, o.minZ).tex(0.11, 0.1).endVertex();
//					ver.pos(o.minX, o.minY, o.minZ).tex(0.11, 0.11).endVertex();
					ver.endVertex();
					
					Tessellator.getInstance().draw();
					
					RenderHelper.enableStandardItemLighting();
					GlStateManager.disableRescaleNormal();
					GlStateManager.disableBlend();
			        GlStateManager.popMatrix();
				}
			}
		}

As you can see, I've tried quite some different ways of rendering the textures, but I am still not getting what I want. What I can do with this code is render a part of the block texture map onto my block (although it glitches quite a bit) but I don't know how to retrieve the Icon for the blockstate. If I could get the Icon name, which is a string, then I could use these lines of code to retrieve the U and V of the icon on the texture map:

float minU = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getMinU();
float minV = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getMinV();
float maxU = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getMaxU();
float maxV = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(path).getMaxV();

So, if you have any idea on how to retrieve the U and V coordinates based on a blockstate, please tell me, because I just can't figure it out.

 

Thanks in advance,

Lenardjee

Edited by Lenardjee
Link to comment
Share on other sites

Ok, I found out how to get the correct TextureAtlasSprite. the path variable should have been like this:

String path = block.getRegistryName().getResourceDomain() + ":blocks/" + block.getRegistryName().getResourcePath();

but the problem is that a lot of blocks have block properties such as sides etc..

So, when I try to do this on sandstone, I have to add "_side" to the end to be able to get the TextureAtlasSprite and the problem is that when there are other mods active, the blocks from that mod will probably have all sorts of properties, and I don't know how to get around that problem, because I want to be able to display the chiseled block just the same as the normal block, but then with parts of the block changed or removed.

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.



×
×
  • Create New...

Important Information

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