ForgePatch/src/main/java/net/minecraftforge/client/model/ModelFluid.java

404 lines
15 KiB
Java
Raw Normal View History

2015-06-18 11:14:46 +00:00
package net.minecraftforge.client.model;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import javax.vecmath.Vector4f;
2015-06-18 11:14:46 +00:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms.TransformType;
2015-06-18 11:14:46 +00:00
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.model.IBakedModel;
import net.minecraft.client.resources.model.ModelRotation;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.pipeline.LightUtil;
import net.minecraftforge.client.model.pipeline.UnpackedBakedQuad;
2015-06-18 11:14:46 +00:00
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.fluids.BlockFluidBase;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.common.FMLLog;
import com.google.common.base.Function;
import com.google.common.base.Optional;
2015-12-30 10:31:36 +00:00
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
2015-06-18 11:14:46 +00:00
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
@SuppressWarnings("deprecation")
2015-06-18 11:14:46 +00:00
public class ModelFluid implements IModelCustomData
{
public static final ModelFluid waterModel = new ModelFluid(FluidRegistry.WATER);
public static final ModelFluid lavaModel = new ModelFluid(FluidRegistry.LAVA);
private final Fluid fluid;
public ModelFluid(Fluid fluid)
{
this.fluid = fluid;
}
public Collection<ResourceLocation> getDependencies()
{
return Collections.emptySet();
}
public Collection<ResourceLocation> getTextures()
{
return ImmutableSet.of(fluid.getStill(), fluid.getFlowing());
}
public IFlexibleBakedModel bake(IModelState state, VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter)
{
ImmutableMap<TransformType, TRSRTransformation> map = IPerspectiveAwareModel.MapWrapper.getTransforms(state);
IFlexibleBakedModel ret = new BakedFluid(state.apply(Optional.<IModelPart>absent()), format, fluid.getColor(), bakedTextureGetter.apply(fluid.getStill()), bakedTextureGetter.apply(fluid.getFlowing()), fluid.isGaseous());
if(map.isEmpty())
{
return ret;
}
return new IPerspectiveAwareModel.MapWrapper(ret, map);
2015-06-18 11:14:46 +00:00
}
public IModelState getDefaultState()
{
return ModelRotation.X0_Y0;
}
public static enum FluidLoader implements ICustomModelLoader
{
instance;
public void onResourceManagerReload(IResourceManager resourceManager) {}
public boolean accepts(ResourceLocation modelLocation)
{
return modelLocation.getResourceDomain().equals("forge") && (
modelLocation.getResourcePath().equals("fluid") ||
modelLocation.getResourcePath().equals("models/block/fluid") ||
modelLocation.getResourcePath().equals("models/item/fluid"));
}
public IModel loadModel(ResourceLocation modelLocation)
{
return waterModel;
}
}
public static class BakedFluid implements IFlexibleBakedModel, ISmartBlockModel
{
private static final int x[] = { 0, 0, 1, 1 };
private static final int z[] = { 0, 1, 1, 0 };
private static final float eps = 1e-3f;
2015-12-30 10:31:36 +00:00
private final LoadingCache<Long, BakedFluid> modelCache = CacheBuilder.newBuilder().maximumSize(200).build(new CacheLoader<Long, BakedFluid>()
{
public BakedFluid load(Long key) throws Exception
{
boolean statePresent = (key & 1) != 0;
key >>>= 1;
int[] cornerRound = new int[4];
for(int i = 0; i < 4; i++)
{
cornerRound[i] = (int)(key & 0x3FF);
key >>>= 10;
}
int flowRound = (int)(key & 0x7FF) - 1024;
return new BakedFluid(transformation, format, color, still, flowing, gas, statePresent, cornerRound, flowRound);
}
});
private final Optional<TRSRTransformation> transformation;
2015-06-18 11:14:46 +00:00
private final VertexFormat format;
private final int color;
private final TextureAtlasSprite still, flowing;
private final boolean gas;
private final EnumMap<EnumFacing, List<BakedQuad>> faceQuads;
public BakedFluid(Optional<TRSRTransformation> transformation, VertexFormat format, int color, TextureAtlasSprite still, TextureAtlasSprite flowing, boolean gas)
2015-06-18 11:14:46 +00:00
{
this(transformation, format, color, still, flowing, gas, Optional.<IExtendedBlockState>absent());
}
public BakedFluid(Optional<TRSRTransformation> transformation, VertexFormat format, int color, TextureAtlasSprite still, TextureAtlasSprite flowing, boolean gas, Optional<IExtendedBlockState> stateOption)
2015-12-30 10:31:36 +00:00
{
this(transformation, format, color, still, flowing, gas, stateOption.isPresent(), getCorners(stateOption), getFlow(stateOption));
}
private static int[] getCorners(Optional<IExtendedBlockState> stateOption)
{
int[] cornerRound = new int[]{0, 0, 0, 0};
if(stateOption.isPresent())
{
IExtendedBlockState state = stateOption.get();
for(int i = 0; i < 4; i++)
{
cornerRound[i] = Math.round(state.getValue(BlockFluidBase.LEVEL_CORNERS[i]) * 768);
}
}
return cornerRound;
}
private static int getFlow(Optional<IExtendedBlockState> stateOption)
{
float flow = -1000;
2015-12-30 10:31:36 +00:00
if(stateOption.isPresent())
{
flow = stateOption.get().getValue(BlockFluidBase.FLOW_DIRECTION);
2015-12-30 10:31:36 +00:00
}
int flowRound = (int)Math.round(Math.toDegrees(flow));
flowRound = MathHelper.clamp_int(flowRound, -1000, 1000);
2015-12-30 10:31:36 +00:00
return flowRound;
}
public BakedFluid(Optional<TRSRTransformation> transformation, VertexFormat format, int color, TextureAtlasSprite still, TextureAtlasSprite flowing, boolean gas, boolean statePresent, int[] cornerRound, int flowRound)
2015-06-18 11:14:46 +00:00
{
this.transformation = transformation;
this.format = format;
this.color = color;
this.still = still;
this.flowing = flowing;
this.gas = gas;
faceQuads = Maps.newEnumMap(EnumFacing.class);
for(EnumFacing side : EnumFacing.values())
{
faceQuads.put(side, ImmutableList.<BakedQuad>of());
}
2015-12-30 10:31:36 +00:00
if(statePresent)
2015-06-18 11:14:46 +00:00
{
float[] y = new float[4];
for(int i = 0; i < 4; i++)
{
if(gas)
{
2015-12-30 10:31:36 +00:00
y[i] = 1 - cornerRound[i] / 768f;
2015-06-18 11:14:46 +00:00
}
else
{
2015-12-30 10:31:36 +00:00
y[i] = cornerRound[i] / 768f;
2015-06-18 11:14:46 +00:00
}
}
2015-12-30 10:31:36 +00:00
float flow = (float)Math.toRadians(flowRound);
2015-06-18 11:14:46 +00:00
// top
TextureAtlasSprite topSprite = flowing;
float scale = 4;
if(flow < -17F)
2015-06-18 11:14:46 +00:00
{
flow = 0;
scale = 8;
topSprite = still;
}
float c = MathHelper.cos(flow) * scale;
float s = MathHelper.sin(flow) * scale;
EnumFacing side = gas ? EnumFacing.DOWN : EnumFacing.UP;
UnpackedBakedQuad.Builder builder = new UnpackedBakedQuad.Builder(format);
builder.setQuadOrientation(side);
builder.setQuadColored();
2015-06-18 11:14:46 +00:00
for(int i = gas ? 3 : 0; i != (gas ? -1 : 4); i+= (gas ? -1 : 1))
{
putVertex(
builder, side,
2015-06-18 11:14:46 +00:00
x[i], y[i], z[i],
topSprite.getInterpolatedU(8 + c * (x[i] * 2 - 1) + s * (z[i] * 2 - 1)),
topSprite.getInterpolatedV(8 + c * (x[(i + 1) % 4] * 2 - 1) + s * (z[(i + 1) % 4] * 2 - 1)));
}
faceQuads.put(side, ImmutableList.<BakedQuad>of(builder.build()));
2015-06-18 11:14:46 +00:00
// bottom
side = side.getOpposite();
builder = new UnpackedBakedQuad.Builder(format);
builder.setQuadOrientation(side);
builder.setQuadColored();
2015-06-18 11:14:46 +00:00
for(int i = gas ? 3 : 0; i != (gas ? -1 : 4); i+= (gas ? -1 : 1))
{
putVertex(
builder, side,
2015-06-18 11:14:46 +00:00
z[i], gas ? 1 : 0, x[i],
still.getInterpolatedU(z[i] * 16),
still.getInterpolatedV(x[i] * 16));
}
faceQuads.put(side, ImmutableList.<BakedQuad>of(builder.build()));
2015-06-18 11:14:46 +00:00
// sides
for(int i = 0; i < 4; i++)
{
side = EnumFacing.getHorizontal((5 - i) % 4);
BakedQuad q[] = new BakedQuad[2];
for(int k = 0; k < 2; k++)
{
builder = new UnpackedBakedQuad.Builder(format);
builder.setQuadOrientation(side);
builder.setQuadColored();
2015-06-18 11:14:46 +00:00
for(int j = 0; j < 4; j++)
{
int l = (k * 3) + (1 - 2 * k) * j;
float yl = z[l] * y[(i + x[l]) % 4];
if(gas && z[l] == 0) yl = 1;
putVertex(
builder, side,
2015-06-18 11:14:46 +00:00
x[(i + x[l]) % 4], yl, z[(i + x[l]) % 4],
flowing.getInterpolatedU(x[l] * 8),
flowing.getInterpolatedV((gas ? yl : 1 - yl) * 8));
}
q[k] = builder.build();
2015-06-18 11:14:46 +00:00
}
faceQuads.put(side, ImmutableList.of(q[0], q[1]));
}
}
else
{
// 1 quad for inventory
UnpackedBakedQuad.Builder builder = new UnpackedBakedQuad.Builder(format);
builder.setQuadOrientation(EnumFacing.UP);
builder.setQuadColored();
2015-06-18 11:14:46 +00:00
for(int i = 0; i < 4; i++)
{
putVertex(
builder, EnumFacing.UP,
2015-06-18 11:14:46 +00:00
z[i], x[i], 0,
still.getInterpolatedU(x[i] * 16),
still.getInterpolatedV(z[i] * 16));
}
faceQuads.put(EnumFacing.SOUTH, ImmutableList.<BakedQuad>of(builder.build()));
2015-06-18 11:14:46 +00:00
}
}
private void putVertex(UnpackedBakedQuad.Builder builder, EnumFacing side, float x, float y, float z, float u, float v)
2015-06-18 11:14:46 +00:00
{
for(int e = 0; e < format.getElementCount(); e++)
2015-06-18 11:14:46 +00:00
{
switch(format.getElement(e).getUsage())
2015-06-18 11:14:46 +00:00
{
case POSITION:
float[] data = new float[]{ x - side.getDirectionVec().getX() * eps, y, z - side.getDirectionVec().getZ() * eps, 1 };
if(transformation.isPresent())
{
Vector4f vec = new Vector4f(data);
transformation.get().getMatrix().transform(vec);
vec.get(data);
}
builder.put(e, data);
2015-06-18 11:14:46 +00:00
break;
case COLOR:
float d = LightUtil.diffuseLight(side);
builder.put(e,
2015-06-18 11:14:46 +00:00
d * ((color >> 16) & 0xFF) / 255f,
d * ((color >> 8) & 0xFF) / 255f,
d * (color & 0xFF) / 255f,
((color >> 24) & 0xFF) / 255f);
break;
case UV: if(format.getElement(e).getIndex() == 0)
{
builder.put(e, u, v, 0f, 1f);
2015-06-18 11:14:46 +00:00
break;
}
2015-06-18 11:14:46 +00:00
case NORMAL:
builder.put(e, (float)side.getFrontOffsetX(), (float)side.getFrontOffsetY(), (float)side.getFrontOffsetZ(), 0f);
2015-06-18 11:14:46 +00:00
break;
default:
builder.put(e);
2015-06-18 11:14:46 +00:00
break;
}
}
}
public boolean isAmbientOcclusion()
{
2015-12-30 10:31:36 +00:00
return false;
2015-06-18 11:14:46 +00:00
}
public boolean isGui3d()
{
return false;
}
public boolean isBuiltInRenderer()
{
return false;
}
2015-12-30 04:15:03 +00:00
public TextureAtlasSprite getParticleTexture()
2015-06-18 11:14:46 +00:00
{
return still;
}
public ItemCameraTransforms getItemCameraTransforms()
{
return ItemCameraTransforms.DEFAULT;
}
public List<BakedQuad> getFaceQuads(EnumFacing side)
{
return faceQuads.get(side);
}
public List<BakedQuad> getGeneralQuads()
{
return ImmutableList.of();
}
public VertexFormat getFormat()
{
return format;
}
public IBakedModel handleBlockState(IBlockState state)
{
2015-12-30 10:31:36 +00:00
if(state instanceof IExtendedBlockState)
{
IExtendedBlockState exState = (IExtendedBlockState)state;
int[] cornerRound = getCorners(Optional.of(exState));
int flowRound = getFlow(Optional.of(exState));
long key = flowRound + 1024;
for(int i = 3; i >= 0; i--)
{
key <<= 10;
key |= cornerRound[i];
}
key <<= 1;
key |= 1;
return modelCache.getUnchecked(key);
}
return this;
2015-06-18 11:14:46 +00:00
}
}
@Override
public IModel process(ImmutableMap<String, String> customData)
{
if(!customData.containsKey("fluid")) return this;
String fluidStr = customData.get("fluid");
JsonElement e = new JsonParser().parse(fluidStr);
String fluid = e.getAsString();
if(!FluidRegistry.isFluidRegistered(fluid))
{
FMLLog.severe("fluid '%s' not found", fluid);
return waterModel;
}
return new ModelFluid(FluidRegistry.getFluid(fluid));
}
}