ModelHelper expansion. Add a transformer to color quads.

This commit is contained in:
Bernhard Bonigl committed 2016-03-19 19:27:38 +01:00
1 parent 6e287e5745
commit ee03b42d71
2 files changed
+59 -83

No files matched your search

@@ -1,17 +1,7 @@
package slimeknights.mantle.client;
import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
@@ -19,27 +9,21 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.IResource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraftforge.client.model.IModelState;
import net.minecraftforge.client.model.SimpleModelState;
import net.minecraftforge.client.model.TRSRTransformation;
import net.minecraftforge.client.model.pipeline.UnpackedBakedQuad;
import net.minecraftforge.client.model.pipeline.VertexTransformer;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.Map;
import javax.vecmath.Matrix3f;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector4f;
public class ModelHelper {
static final Type maptype = new TypeToken<Map<String, String>>() {}.getType();
private static final Gson
GSON =
new GsonBuilder().registerTypeAdapter(maptype, ModelTextureDeserializer.INSTANCE).create();
public static final Optional<IModelState> DEFAULT_ITEM_STATE;
public static final Optional<IModelState> DEFAULT_TOOL_STATE;
public static final TRSRTransformation BLOCK_THIRD_PERSON_RIGHT;
@@ -55,50 +39,9 @@ public class ModelHelper {
}
public static BakedQuad colorQuad(int color, BakedQuad quad) {
int[] data = quad.getVertexData();
int a = (color >> 24);
if(a == 0) {
a = 255;
}
int c = 0;
c |= ((color >> 16) & 0xFF) << 0; // red
c |= ((color >> 8) & 0xFF) << 8; // green
c |= ((color >> 0) & 0xFF) << 16; // blue
c |= (a & 0xFF) << 24; // alpha
// update color in the data. all 4 Vertices.
for(int i = 0; i < 4; i++) {
data[i * 7 + 3] = c;
}
return new BakedQuad(data, quad.getTintIndex(), quad.getFace(), quad.getSprite(), quad
.shouldApplyDiffuseLighting(), quad.getFormat());
}
public static Map<String, String> loadTexturesFromJson(ResourceLocation location) throws IOException {
// get the json
IResource
iresource =
Minecraft.getMinecraft().getResourceManager()
.getResource(new ResourceLocation(location.getResourceDomain(), location.getResourcePath() + ".json"));
Reader reader = new InputStreamReader(iresource.getInputStream(), Charsets.UTF_8);
return GSON.fromJson(reader, maptype);
}
public static ImmutableList<ResourceLocation> loadTextureListFromJson(ResourceLocation location) throws IOException {
ImmutableList.Builder<ResourceLocation> builder = ImmutableList.builder();
for(String s : loadTexturesFromJson(location).values()) {
builder.add(new ResourceLocation(s));
}
return builder.build();
}
public static ResourceLocation getModelLocation(ResourceLocation location) {
return new ResourceLocation(location.getResourceDomain(), "models/" + location.getResourcePath() + ".json");
ColorTransformer transformer = new ColorTransformer(color, quad.getFormat());
quad.pipe(transformer);
return transformer.build();
}
private static TRSRTransformation get(float tx, float ty, float tz, float ax, float ay, float az, float s) {
@@ -132,28 +75,44 @@ public class ModelHelper {
}
}
/**
* Deseralizes a json in the format of { "textures": { "foo": "texture",... }}
* Ignores all invalid json
*/
public static class ModelTextureDeserializer implements JsonDeserializer<Map<String, String>> {
public static final ModelTextureDeserializer INSTANCE = new ModelTextureDeserializer();
private static class ColorTransformer extends VertexTransformer {
private static final Gson GSON = new Gson();
private final float r,g,b,a;
public ColorTransformer(int color, VertexFormat format) {
super(new UnpackedBakedQuad.Builder(format));
int a = (color >> 24);
if(a == 0) {
a = 255;
}
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;
this.r = (float)r/255f;
this.g = (float)g/255f;
this.b = (float)b/255f;
this.a = (float)a/255f;
}
@Override
public Map<String, String> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
public void put(int element, float... data) {
VertexFormatElement.EnumUsage usage = parent.getVertexFormat().getElement(element).getUsage();
JsonObject obj = json.getAsJsonObject();
JsonElement texElem = obj.get("textures");
if(texElem == null) {
throw new JsonParseException("Missing textures entry in json");
// transform normals and position
if(usage == VertexFormatElement.EnumUsage.COLOR && data.length >= 4) {
data[0] = r;
data[1] = g;
data[2] = b;
data[3] = a;
}
super.put(element, data);
}
return GSON.fromJson(texElem, maptype);
public UnpackedBakedQuad build() {
return ((UnpackedBakedQuad.Builder) parent).build();
}
}
}