Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.12.2] Retexturing IModel


TheTrollguy_
 Share

Recommended Posts

Hi,

I want to retexture my IModel, but the thing is I just end up with the 'missing texture' texture.

I've been stuck with this problem for hours now, soo any help would be appreciated :)

 

CODE:

 

Spoiler

package com.thetrollguy.traffic_stuff_mod.feature.cone.client;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

import javax.annotation.Nullable;
import javax.vecmath.Tuple4f;
import javax.vecmath.Vector4f;

import com.google.common.collect.ImmutableMap;
import com.thetrollguy.traffic_stuff_mod.feature.BlockAdaptable;
import com.thetrollguy.traffic_stuff_mod.feature.BlockTrafficStuffMod;
import com.thetrollguy.traffic_stuff_mod.feature.EnumBlockAdaptationType;
import com.thetrollguy.traffic_stuff_mod.feature.cone.BlockCone;
import com.thetrollguy.traffic_stuff_mod.feature.cone.ConeColor;
import com.thetrollguy.traffic_stuff_mod.feature.cone.ConePosition;
import com.thetrollguy.traffic_stuff_mod.feature.cone.ConeShape;
import com.thetrollguy.traffic_stuff_mod.utils.Info;
import com.thetrollguy.traffic_stuff_mod.utils.Utils;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemOverrideList;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.client.model.pipeline.UnpackedBakedQuad;
import net.minecraftforge.client.model.pipeline.VertexTransformer;
import net.minecraftforge.common.model.IModelState;
import net.minecraftforge.common.property.IExtendedBlockState;

public class BakedModelCone implements IBakedModel {
	
	public static HashMap<String, List<BakedQuad>> QUADS = new HashMap<String, List<BakedQuad>>();

	private IModel model;
	private boolean retextured = false;
	private IModelState modelState;                                           // *THE JUNK* Is there any way
	private VertexFormat vertexFormat;                                        // to make this tidy and not pass
	private Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter;// a bunch of objects to the construcor?
	
	public BakedModelCone(IModel model, IModelState modelState, VertexFormat vertexFormat, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter)
	{
		this.model = model;
		this.modelState = modelState;
		this.vertexFormat = vertexFormat;
		this.bakedTextureGetter = bakedTextureGetter;
	}

	@Override
	public List<BakedQuad> getQuads(@Nullable IBlockState state, @Nullable EnumFacing side, long random)
	{	
		ConeColor color = ((BlockCone) state.getBlock()).color;
		List<BakedQuad> quads = new LinkedList<BakedQuad>();
		
		if (!retextured)
		{
			this.model = model.retexture(
				ImmutableMap.of("particle",  "traffic_stuff_mod:textures/blocks/cone/cone_" + color + "_particle", 
				 				"primary",   "traffic_stuff_mod:textures/blocks/cone/cone_" + color, 
				 				"secondary", "traffic_stuff_mod:textures/blocks/cone/cone_" + color.getSecondaryColor()));
			this.retextured = true;		
		}
		if (state instanceof IExtendedBlockState)
		{
			ConeShape shape = ((BlockCone) state.getBlock()).shape;
			ConePosition position = state.getValue(BlockCone.POSITION);
			EnumBlockAdaptationType type = ((IExtendedBlockState) state).getValue(BlockAdaptable.ADAPTATION_TYPE);
			
			String key = Utils.String.combineToString(shape, color, position, type);

			if (QUADS.containsKey(key))
			{
				return QUADS.get(key);
			}
			else
			{
				IBakedModel bakedModel = model.bake(modelState, vertexFormat, bakedTextureGetter);
				float heightReduction = ((IExtendedBlockState) state).getValue(BlockAdaptable.ADAPTATION_TYPE).heightReduction;
				
				for (BakedQuad quad : bakedModel.getQuads(state, side, random))
				{
					quads.add(translate(quad, shape.getTranslationVector(position, heightReduction)));				
				}
				QUADS.put(key, quads);
			}
		}
		return quads;
	}

	protected static BakedQuad translate(BakedQuad quad, Vector4f translationVector)
	{
		UnpackedBakedQuad.Builder builder = new UnpackedBakedQuad.Builder(DefaultVertexFormats.ITEM);

		quad.pipe(new VertexTransformer(builder)
		{
			@Override
			public void put(int element, float... data)
			{
				VertexFormatElement formatElement = DefaultVertexFormats.ITEM.getElement(element);
				
				if (formatElement.getUsage() == VertexFormatElement.EnumUsage.POSITION)
				{
					float[] newData = new float[4];
					Vector4f vec = new Vector4f(data);
					vec.add(translationVector);
					vec.get(newData);
					this.parent.put(element, newData);
				}
				else
				{
					this.parent.put(element, data);	
				}
			}
		});
		return builder.build();
	}

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

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

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


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

	@Override
	public TextureAtlasSprite getParticleTexture() {
		// TODO Auto-generated method stub
		return null;
	}

}

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Thanks diesieben07!

Now another thing that makes zero sense to me related to IBakedModel.

 

The method getQuads accepts EnumFacing as one of its arguments. While I was experimenting with something, I found out that sometimes, when the method gets called, null gets passed as the EnumFacing argument. Why is that? I know that if IBlockState argument is null, the quads are for an item model.

 

The reason is written on the Forge Docs: mcforge.readthedocs.io/en/latest/models/advanced/ibakedmodel/#getquads

Edited by TheTrollguy_
Found the answer
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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.