Jump to content

[1.9.4] [Solved] Dynamic block textures


grim3212

Recommended Posts

Okay, so I am trying to update some of my mods and have everything going except for the dynamic textures on a few blocks. These blocks pretty much allow to take on the texture of any block.

 

I have been messing around for a bit and I was able to get the models to show up but not the textures applied to the blocks so now I am trying another route with a custommodelloader. But, getQuads gets stuck in an infinite loop and I am not sure how else it would work.

 

I am not even sure if this is necessary because all I want is to be able to retexture the model not change the model itself.

In 1.8.9 I just would return a modified Builder from SimpleBakedModel but I don't have access to a blockstate now.

So I am open to ideas. Thanks!

 

IModel and CustomModelLoader

package com.grim3212.mc.pack.decor.client.model;

import java.io.IOException;
import java.util.Collection;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.grim3212.mc.pack.GrimPack;

import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.ICustomModelLoader;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.IPerspectiveAwareModel;
import net.minecraftforge.client.model.IRetexturableModel;
import net.minecraftforge.common.model.IModelState;
import net.minecraftforge.common.model.TRSRTransformation;

public class FurnitureModel implements IRetexturableModel {

public static final FurnitureModel MODEL = new FurnitureModel();
private final ResourceLocation textureLocation;

public FurnitureModel() {
	this(null);
}

public FurnitureModel(ResourceLocation textureLocation) {
	this.textureLocation = textureLocation;
}

@Override
public Collection<ResourceLocation> getDependencies() {
	return ImmutableList.of();
}

@Override
public Collection<ResourceLocation> getTextures() {
	ImmutableSet.Builder<ResourceLocation> builder = ImmutableSet.builder();
	if (textureLocation != null)
		builder.add(textureLocation);

	return builder.build();
}

@Override
public IBakedModel bake(IModelState state, VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter) {
	ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> transformMap = IPerspectiveAwareModel.MapWrapper.getTransforms(state);
	TextureAtlasSprite base = null;

	if (textureLocation != null)
		base = bakedTextureGetter.apply(textureLocation);

	return new BakedFurnitureModel(this, base, format, transformMap);
}

@Override
public IModelState getDefaultState() {
	return TRSRTransformation.identity();
}

@Override
public IModel retexture(ImmutableMap<String, String> textures) {
	ResourceLocation base = textureLocation;

	if (textures.containsKey("texture"))
		base = new ResourceLocation(textures.get("texture"));

	return new FurnitureModel(base);
}

public static enum FurnitureModelLoader implements ICustomModelLoader {
	instance;

	@Override
	public boolean accepts(ResourceLocation modelLocation) {
		return modelLocation.getResourceDomain().equals(GrimPack.modID) && modelLocation.getResourcePath().contains("custom_counter");
	}

	@Override
	public IModel loadModel(ResourceLocation modelLocation) throws IOException {
		System.out.println("Accepted: " + modelLocation);

		return MODEL;
	}

	@Override
	public void onResourceManagerReload(IResourceManager resourceManager) {
	}
}
}

 

BakedModel

package com.grim3212.mc.pack.decor.client.model;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.vecmath.Matrix4f;

import org.apache.commons.lang3.tuple.Pair;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.grim3212.mc.pack.core.util.NBTHelper;
import com.grim3212.mc.pack.decor.block.BlockTextured;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockModelShapes;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms.TransformType;
import net.minecraft.client.renderer.block.model.ItemOverride;
import net.minecraft.client.renderer.block.model.ItemOverrideList;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.IPerspectiveAwareModel;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelProcessingHelper;
import net.minecraftforge.client.model.SimpleModelState;
import net.minecraftforge.common.model.TRSRTransformation;
import net.minecraftforge.common.property.IExtendedBlockState;

public class BakedFurnitureModel implements IPerspectiveAwareModel, IResourceManagerReloadListener {

private final IModel model;
private final ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> transforms;
private final TextureAtlasSprite particle;
private final VertexFormat format;

public BakedFurnitureModel(IModel model, TextureAtlasSprite particle, VertexFormat fmt, ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> transforms) {
	this.model = model;
	this.particle = particle;
	this.format = fmt;
	this.transforms = transforms;
}

@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
	this.cache.clear();
}

@Override
public List<BakedQuad> getQuads(IBlockState state, EnumFacing side, long rand) {

	if (state instanceof IExtendedBlockState) {
		IExtendedBlockState exState = (IExtendedBlockState) state;
		if (exState.getValue(BlockTextured.BLOCKID) != null && exState.getValue(BlockTextured.BLOCKMETA) != null) {
			int blockID = exState.getValue(BlockTextured.BLOCKID);
			int blockMeta = exState.getValue(BlockTextured.BLOCKMETA);
			return this.getCachedModel(blockID, blockMeta).getQuads(state, side, rand);
		}
	}
	return ImmutableList.of();
}

private final Map<List<Integer>, IBakedModel> cache = new HashMap<List<Integer>, IBakedModel>();

public IBakedModel getCachedModel(int blockID, int blockMeta) {
	List<Integer> key = Arrays.asList(blockID, blockMeta);

	if (!this.cache.containsKey(key)) {
		ImmutableMap.Builder<String, String> newTexture = ImmutableMap.builder();

		if (Block.getBlockById(blockID) == Blocks.GRASS) {
			newTexture.put("texture", "minecraft:blocks/grass_top");
		} else if (Block.getBlockById(blockID) == Blocks.DIRT && blockMeta == 2) {
			newTexture.put("texture", "minecraft:blocks/dirt_podzol_top");
		} else if (Block.getBlockById(blockID) == Blocks.MYCELIUM) {
			newTexture.put("texture", "minecraft:blocks/mycelium_top");
		} else {
			BlockModelShapes blockModel = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes();
			IBlockState blockState = Block.getBlockById(blockID).getStateFromMeta(blockMeta);
			TextureAtlasSprite blockTexture = blockModel.getTexture(blockState);

			newTexture.put("texture", blockTexture.getIconName());
		}

		System.out.println("Adding to cache " + key);
		this.cache.put(key, ModelProcessingHelper.retexture(model, newTexture.build()).bake(new SimpleModelState(transforms), format, ModelLoader.defaultTextureGetter()));
	}

	System.out.println(key);
	return this.cache.get(key);
}

@Override
public boolean isAmbientOcclusion() {
	return true;
}

@Override
public boolean isGui3d() {
	return true;
}

@Override
public boolean isBuiltInRenderer() {
	return false;
}

@Override
public TextureAtlasSprite getParticleTexture() {
	return particle;
}

@Override
public ItemCameraTransforms getItemCameraTransforms() {
	return ItemCameraTransforms.DEFAULT;
}

@Override
public ItemOverrideList getOverrides() {
	return itemHandler;
}

private final ItemOverrideList itemHandler = new ItemOverrideList(Lists.<ItemOverride> newArrayList()) {
	@Override
	public IBakedModel handleItemState(IBakedModel model, ItemStack stack, World world, EntityLivingBase entity) {
		if (stack.hasTagCompound() && stack.getTagCompound().hasKey("blockID") && stack.getTagCompound().hasKey("blockMeta")) {
			int blockID = NBTHelper.getInt(stack, "blockID");
			int blockMeta = NBTHelper.getInt(stack, "blockMeta");
			return BakedFurnitureModel.this.getCachedModel(blockID, blockMeta);
		}
		return BakedFurnitureModel.this;
	}
};

@Override
public Pair<? extends IBakedModel, Matrix4f> handlePerspective(TransformType cameraTransformType) {
	return MapWrapper.handlePerspective(this, transforms, cameraTransformType);
}
}

 

EDIT:

 

Well I was able to get it working. Might not be the most efficient way though.

 

CKqPko2.png

 

So what I did was modify the ICustomModelLoader to include the related block model needed for each loaded modelLocation.

In bake I would return instead of the IModel itself that location and the texturelocation set. In the getCachedModel I get the model using [embed=425,349]ModelLoaderRegistry.getModelOrMissing[/embed] then use [embed=425,349]ModelProcessingHelper.retexture[/embed] to re-texture the elements that need to be re-textured and then finally bake the model and add it to the cache.

 

Like I said I am not sure if this is the best way to achieve this result but it is working but I am always open to ideas.

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

    • i notice a change if i add the min and max ram in the line like this for example:    # Xmx and Xms set the maximum and minimum RAM usage, respectively. # They can take any number, followed by an M or a G. # M means Megabyte, G means Gigabyte. # For example, to set the maximum to 3GB: -Xmx3G # To set the minimum to 2.5GB: -Xms2500M # A good default for a modded server is 4GB. # Uncomment the next line to set it. -Xmx10240M -Xms8192M    i need to make more experiments but for now this apparently works.
    • Selamat datang di OLXTOTO, situs slot gacor terpanas yang sedang booming di industri perjudian online. Jika Anda mencari pengalaman bermain yang luar biasa, maka OLXTOTO adalah tempat yang tepat untuk Anda. Dapatkan sensasi tidak biasa dengan variasi slot online terlengkap dan peluang memenangkan jackpot slot maxwin yang sering. Di sini, Anda akan merasakan keseruan yang luar biasa dalam bermain judi slot. DAFTAR OLXTOTO DISINI LOGIN OLXTOTO DISINI AKUN PRO OLXTOTO DISINI   Slot Gacor untuk Sensasi Bermain Maksimal Olahraga cepat dan seru dengan slot gacor di OLXTOTO. Rasakan sensasi bermain maksimal dengan mesin slot yang memberikan kemenangan beruntun. Temukan keberuntungan Anda di antara berbagai pilihan slot gacor yang tersedia dan rasakan kegembiraan bermain judi slot yang tak terlupakan. Situs Slot Terpercaya dengan Pilihan Terlengkap OLXTOTO adalah situs slot terpercaya yang menawarkan pilihan terlengkap dalam perjudian online. Nikmati berbagai genre dan tema slot online yang menarik, dari slot klasik hingga slot video yang inovatif. Dipercaya oleh jutaan pemain, OLXTOTO memberikan pengalaman bermain yang aman dan terjamin.   Jackpot Slot Maxwin Sering Untuk Peluang Besar Di OLXTOTO, kami tidak hanya memberikan hadiah slot biasa, tapi juga memberikan kesempatan kepada pemain untuk memenangkan jackpot slot maxwin yang sering. Dengan demikian, Anda dapat meraih keberuntungan besar dan memenangkan ribuan rupiah sebagai hadiah jackpot slot maxwin kami. Jackpot slot maxwin merupakan peluang besar bagi para pemain judi slot untuk meraih keuntungan yang lebih besar. Dalam permainan kami, Anda tidak harus terpaku pada kemenangan biasa saja. Kami hadir dengan jackpot slot maxwin yang sering, sehingga Anda memiliki peluang yang lebih besar untuk meraih kemenangan besar dengan hadiah yang menggiurkan. Dalam permainan judi slot, pengalaman bermain bukan hanya tentang keseruan dan hiburan semata. Kami memahami bahwa para pemain juga menginginkan kesempatan untuk meraih keberuntungan besar. Oleh karena itu, OLXTOTO hadir dengan jackpot slot maxwin yang sering untuk memberikan peluang besar kepada para pemain kami. Peluang Besar Menang Jackpot Slot Maxwin Peluang menang jackpot slot maxwin di OLXTOTO sangatlah besar. Anda tidak perlu khawatir tentang batasan atau pembatasan dalam meraih jackpot tersebut. Kami ingin memberikan kesempatan kepada semua pemain kami untuk merasakan sensasi menang dalam jumlah yang luar biasa. Jackpot slot maxwin kami dibuka untuk semua pemain judi slot di OLXTOTO. Anda memiliki peluang yang sama dengan pemain lainnya untuk memenangkan hadiah jackpot yang besar. Kami percaya bahwa semua orang memiliki kesempatan untuk meraih keberuntungan besar, dan itulah mengapa kami menyediakan jackpot slot maxwin yang sering untuk memenuhi harapan dan keinginan Anda.  
    • LOGIN DAN DAFTAR DISINI SEKARANG !!!! Blacktogel adalah situs judi slot online yang menjadi pilihan banyak penggemar judi slot gacor di Indonesia. Dengan platform yang sangat user-friendly dan berbagai macam permainan slot yang tersedia, Blacktogel menjadi tempat yang tepat untuk penggemar judi slot online. Dalam artikel ini, kami akan membahas tentang Blacktogel dan keunggulan situs slot gacor online yang disediakan.  
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
  • Topics

×
×
  • Create New...

Important Information

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