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



×
×
  • Create New...

Important Information

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