mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +00:00
Colored glow panels, in block form at least! Now to get them to render in item form...
This commit is contained in:
1 parent
a10c57fb92
commit
64f60150df
3 files changed
+227
-17
No files matched your search
@@ -1,36 +1,58 @@
|
||||
package mekanism.client.render.obj;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import mcmultipart.client.multipart.ISmartMultipartModel;
|
||||
import mekanism.api.EnumColor;
|
||||
import mekanism.common.multipart.GlowPanelBlockState;
|
||||
import mekanism.common.multipart.PartGlowPanel;
|
||||
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.ItemCameraTransforms.TransformType;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.renderer.vertex.VertexFormat;
|
||||
import net.minecraft.client.resources.model.IBakedModel;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.client.model.IModelPart;
|
||||
import net.minecraftforge.client.model.IModelState;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.client.model.TRSRTransformation;
|
||||
import net.minecraftforge.client.model.obj.OBJModel;
|
||||
import net.minecraftforge.client.model.obj.OBJModel.Face;
|
||||
import net.minecraftforge.client.model.obj.OBJModel.Group;
|
||||
import net.minecraftforge.client.model.obj.OBJModel.Normal;
|
||||
import net.minecraftforge.client.model.obj.OBJModel.OBJBakedModel;
|
||||
import net.minecraftforge.client.model.obj.OBJModel.OBJState;
|
||||
import net.minecraftforge.client.model.obj.OBJModel.TextureCoordinate;
|
||||
import net.minecraftforge.client.model.obj.OBJModel.Vertex;
|
||||
import net.minecraftforge.client.model.pipeline.LightUtil;
|
||||
import net.minecraftforge.client.model.pipeline.UnpackedBakedQuad;
|
||||
import net.minecraftforge.common.property.IExtendedBlockState;
|
||||
import codechicken.lib.vec.Matrix4;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class MekanismSmartOBJModel extends OBJBakedModel
|
||||
public class MekanismSmartOBJModel extends OBJBakedModel implements ISmartMultipartModel
|
||||
{
|
||||
private IBakedModel baseModel;
|
||||
|
||||
private HashMap<TransformType, Matrix4> transformationMap = new HashMap<TransformType, Matrix4>();
|
||||
|
||||
private static Map<EnumColor, MekanismSmartOBJModel> glowPanelCache = new HashMap<EnumColor, MekanismSmartOBJModel>();
|
||||
private static Map<Integer, MekanismSmartOBJModel> glowPanelCache = new HashMap<Integer, MekanismSmartOBJModel>();
|
||||
|
||||
private Set<BakedQuad> bakedQuads;
|
||||
private IBlockState tempState;
|
||||
private TextureAtlasSprite tempSprite;
|
||||
|
||||
public MekanismSmartOBJModel(IBakedModel base, OBJModel model, IModelState state, VertexFormat format, ImmutableMap<String, TextureAtlasSprite> textures, HashMap<TransformType, Matrix4> transform)
|
||||
{
|
||||
@@ -40,36 +62,170 @@ public class MekanismSmartOBJModel extends OBJBakedModel
|
||||
}
|
||||
|
||||
@Override
|
||||
public OBJBakedModel handleBlockState(IBlockState state)
|
||||
public OBJBakedModel handlePartState(IBlockState state)
|
||||
{
|
||||
if(state instanceof GlowPanelBlockState)
|
||||
if(isGlowPanel(state))
|
||||
{
|
||||
EnumColor color = state.getValue(PartGlowPanel.colorProperty);
|
||||
int hash = PartGlowPanel.hash((IExtendedBlockState)state);
|
||||
EnumColor color = ((IExtendedBlockState)state).getValue(GlowPanelBlockState.colorState).color;
|
||||
|
||||
if(!glowPanelCache.containsKey(color))
|
||||
if(!glowPanelCache.containsKey(hash))
|
||||
{
|
||||
MekanismSmartOBJModel model = new MekanismSmartOBJModel(baseModel, getModel(), getState(), getFormat(), getTextures(), transformationMap);
|
||||
model.tempState = state;
|
||||
glowPanelCache.put(color, model);
|
||||
glowPanelCache.put(hash, model);
|
||||
}
|
||||
|
||||
return glowPanelCache.get(color);
|
||||
return glowPanelCache.get(hash);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isGlowPanel(IBlockState state)
|
||||
{
|
||||
return state instanceof IExtendedBlockState && ((IExtendedBlockState)state).getUnlistedProperties().containsKey(GlowPanelBlockState.colorState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BakedQuad> getGeneralQuads()
|
||||
{
|
||||
for(Map.Entry<String, Group> g : getModel().getMatLib().getGroups().entrySet())
|
||||
if(bakedQuads == null)
|
||||
{
|
||||
System.out.println("BAKED " + g.getKey() + " " + g.getValue().getName() + " " + g.getValue().getFaces());
|
||||
bakedQuads = Collections.synchronizedSet(new LinkedHashSet<BakedQuad>());
|
||||
Set<Face> faces = Collections.synchronizedSet(new LinkedHashSet<Face>());
|
||||
Optional<TRSRTransformation> transform = Optional.absent();
|
||||
Set<Face> coloredFaces = new HashSet<Face>();
|
||||
|
||||
for(Group g : this.getModel().getMatLib().getGroups().values())
|
||||
{
|
||||
if(getState() instanceof OBJState)
|
||||
{
|
||||
OBJState state = (OBJState) this.getState();
|
||||
|
||||
if(state.parent != null)
|
||||
{
|
||||
transform = state.parent.apply(Optional.<IModelPart> absent());
|
||||
}
|
||||
|
||||
if(state.getGroupsWithVisibility(true).contains(g.getName()))
|
||||
{
|
||||
faces.addAll(g.applyTransform(transform));
|
||||
}
|
||||
}
|
||||
else {
|
||||
transform = getState().apply(Optional.<IModelPart> absent());
|
||||
LinkedHashSet<Face> tempFaces = g.applyTransform(transform);
|
||||
faces.addAll(tempFaces);
|
||||
|
||||
if(isGlowPanel(tempState) && g.getName().equals("light"))
|
||||
{
|
||||
coloredFaces.addAll(tempFaces);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(Face f : faces)
|
||||
{
|
||||
if(getModel().getMatLib().getMaterial(f.getMaterialName()).isWhite())
|
||||
{
|
||||
for(Vertex v : f.getVertices())
|
||||
{
|
||||
if(!v.getMaterial().equals(this.getModel().getMatLib().getMaterial(v.getMaterial().getName())))
|
||||
{
|
||||
v.setMaterial(this.getModel().getMatLib().getMaterial(v.getMaterial().getName()));
|
||||
}
|
||||
}
|
||||
|
||||
tempSprite = ModelLoader.White.instance;
|
||||
}
|
||||
else {
|
||||
tempSprite = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(this.getModel().getMatLib().getMaterial(f.getMaterialName()).getTexture().getTextureLocation().toString());
|
||||
}
|
||||
|
||||
float[] color = new float[] { 1, 1, 1, 1 };
|
||||
|
||||
if(coloredFaces.contains(f))
|
||||
{
|
||||
EnumColor c = ((IExtendedBlockState) tempState).getValue(GlowPanelBlockState.colorState).color;
|
||||
color = new float[] {c.getColor(0), c.getColor(1), c.getColor(2), 1};
|
||||
}
|
||||
|
||||
UnpackedBakedQuad.Builder builder = new UnpackedBakedQuad.Builder(getFormat());
|
||||
builder.setQuadOrientation(EnumFacing.getFacingFromVector(f.getNormal().x, f.getNormal().y, f.getNormal().z));
|
||||
builder.setQuadColored();
|
||||
builder.setQuadTint(0);
|
||||
|
||||
Normal faceNormal = f.getNormal();
|
||||
putVertexData(builder, f.getVertices()[0], faceNormal, TextureCoordinate.getDefaultUVs()[0], tempSprite, color);
|
||||
putVertexData(builder, f.getVertices()[1], faceNormal, TextureCoordinate.getDefaultUVs()[1], tempSprite, color);
|
||||
putVertexData(builder, f.getVertices()[2], faceNormal, TextureCoordinate.getDefaultUVs()[2], tempSprite, color);
|
||||
putVertexData(builder, f.getVertices()[3], faceNormal, TextureCoordinate.getDefaultUVs()[3], tempSprite, color);
|
||||
|
||||
bakedQuads.add(builder.build());
|
||||
}
|
||||
}
|
||||
|
||||
List<BakedQuad> quadList = Collections.synchronizedList(Lists.newArrayList(bakedQuads));
|
||||
|
||||
return super.getGeneralQuads();
|
||||
return quadList;
|
||||
}
|
||||
|
||||
|
||||
private final void putVertexData(UnpackedBakedQuad.Builder builder, Vertex v, Normal faceNormal, TextureCoordinate defUV, TextureAtlasSprite sprite, float[] color)
|
||||
{
|
||||
for(int e = 0; e < getFormat().getElementCount(); e++)
|
||||
{
|
||||
switch(getFormat().getElement(e).getUsage())
|
||||
{
|
||||
case POSITION:
|
||||
builder.put(e, v.getPos().x, v.getPos().y, v.getPos().z, v.getPos().w);
|
||||
break;
|
||||
case COLOR:
|
||||
float d;
|
||||
|
||||
if(v.hasNormal())
|
||||
{
|
||||
d = LightUtil.diffuseLight(v.getNormal().x, v.getNormal().y, v.getNormal().z);
|
||||
}
|
||||
else {
|
||||
d = LightUtil.diffuseLight(faceNormal.x, faceNormal.y, faceNormal.z);
|
||||
}
|
||||
|
||||
if(v.getMaterial() != null)
|
||||
{
|
||||
builder.put(e, d * v.getMaterial().getColor().x * color[0], d * v.getMaterial().getColor().y * color[1], d * v.getMaterial().getColor().z * color[2], v.getMaterial().getColor().w * color[3]);
|
||||
}
|
||||
else {
|
||||
builder.put(e, d, d, d, 1);
|
||||
}
|
||||
|
||||
break;
|
||||
case UV:
|
||||
if(!v.hasTextureCoordinate())
|
||||
{
|
||||
builder.put(e, sprite.getInterpolatedU(defUV.u * 16), sprite.getInterpolatedV((1 - defUV.v) * 16), 0, 1);
|
||||
}
|
||||
else {
|
||||
builder.put(e, sprite.getInterpolatedU(v.getTextureCoordinate().u * 16), sprite.getInterpolatedV((1 - v.getTextureCoordinate().v) * 16), 0, 1);
|
||||
}
|
||||
|
||||
break;
|
||||
case NORMAL:
|
||||
if(!v.hasNormal())
|
||||
{
|
||||
builder.put(e, faceNormal.x, faceNormal.y, faceNormal.z, 0);
|
||||
}
|
||||
else {
|
||||
builder.put(e, v.getNormal().x, v.getNormal().y, v.getNormal().z, 0);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
builder.put(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Field f_textures;
|
||||
|
||||
public static ImmutableMap<String, TextureAtlasSprite> getTexturesForOBJModel(IBakedModel model)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mekanism.common.multipart;
|
||||
|
||||
import mcmultipart.MCMultiPartMod;
|
||||
import mekanism.api.EnumColor;
|
||||
import mekanism.common.block.states.BlockStateFacing;
|
||||
import net.minecraft.block.properties.IProperty;
|
||||
import net.minecraftforge.common.property.ExtendedBlockState;
|
||||
@@ -8,8 +9,46 @@ import net.minecraftforge.common.property.IUnlistedProperty;
|
||||
|
||||
public class GlowPanelBlockState extends ExtendedBlockState
|
||||
{
|
||||
public static GlowPanelColorProperty colorState = new GlowPanelColorProperty();
|
||||
|
||||
public GlowPanelBlockState()
|
||||
{
|
||||
super(MCMultiPartMod.multipart, new IProperty[] {BlockStateFacing.facingProperty, PartGlowPanel.colorProperty}, new IUnlistedProperty[] {});
|
||||
super(MCMultiPartMod.multipart, new IProperty[] {BlockStateFacing.facingProperty}, new IUnlistedProperty[] {colorState});
|
||||
}
|
||||
|
||||
public static class GlowPanelColorProperty implements IUnlistedProperty<GlowPanelColorProperty>
|
||||
{
|
||||
public EnumColor color;
|
||||
|
||||
public GlowPanelColorProperty() {}
|
||||
|
||||
public GlowPanelColorProperty(EnumColor c)
|
||||
{
|
||||
color = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return "color";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(GlowPanelColorProperty value)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class getType()
|
||||
{
|
||||
return getClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String valueToString(GlowPanelColorProperty value)
|
||||
{
|
||||
return color.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import mekanism.api.Coord4D;
|
||||
import mekanism.api.EnumColor;
|
||||
import mekanism.common.MekanismItems;
|
||||
import mekanism.common.block.states.BlockStateFacing;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import mekanism.common.multipart.GlowPanelBlockState.GlowPanelColorProperty;
|
||||
import net.minecraft.block.state.BlockState;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
@@ -25,6 +25,7 @@ import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.AxisAlignedBB;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.Vec3;
|
||||
import net.minecraftforge.common.property.IExtendedBlockState;
|
||||
/*
|
||||
import codechicken.lib.data.MCDataInput;
|
||||
import codechicken.lib.data.MCDataOutput;
|
||||
@@ -44,8 +45,6 @@ import codechicken.multipart.TileMultipart;
|
||||
|
||||
public class PartGlowPanel extends Multipart implements IOccludingPart
|
||||
{
|
||||
public static PropertyEnum<EnumColor> colorProperty = PropertyEnum.create("color", EnumColor.class);
|
||||
|
||||
public EnumColor colour = EnumColor.WHITE;
|
||||
public EnumFacing side = EnumFacing.DOWN;
|
||||
|
||||
@@ -189,6 +188,22 @@ public class PartGlowPanel extends Multipart implements IOccludingPart
|
||||
@Override
|
||||
public IBlockState getExtendedState(IBlockState state)
|
||||
{
|
||||
return state.withProperty(BlockStateFacing.facingProperty, side).withProperty(colorProperty, colour);
|
||||
state = state.withProperty(BlockStateFacing.facingProperty, side);
|
||||
|
||||
if(state instanceof IExtendedBlockState)
|
||||
{
|
||||
return ((IExtendedBlockState)state).withProperty(GlowPanelBlockState.colorState, new GlowPanelColorProperty(colour));
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
public static int hash(IExtendedBlockState state)
|
||||
{
|
||||
int hash = 1;
|
||||
hash = 31 * hash + state.getValue(GlowPanelBlockState.colorState).color.ordinal();
|
||||
hash = 31 * hash + state.getValue(BlockStateFacing.facingProperty).ordinal();
|
||||
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user