Skip to content

Instantly share code, notes, and snippets.

@Runemoro
Created October 6, 2019 10:58
Show Gist options
  • Save Runemoro/b5ad7cc0bd46635809fb72c51ac1eda7 to your computer and use it in GitHub Desktop.
Save Runemoro/b5ad7cc0bd46635809fb72c51ac1eda7 to your computer and use it in GitHub Desktop.
package net.minecraft.client.render;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.util.math.Matrix4f;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.client.util.math.Vector4f;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3i;
import net.minecraft.util.math.Matrix3f;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.system.MemoryStack;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
@Environment(EnvType.CLIENT)
public interface VertexConsumer {
Logger LOGGER = LogManager.getLogger();
VertexConsumer vertex(double x, double y, double z);
VertexConsumer color(int red, int green, int blue, int alpha);
VertexConsumer texture0(float u, float v);
VertexConsumer texture1(int u, int v);
VertexConsumer texture2(int u, int v);
VertexConsumer normal(float x, float y, float z);
void next();
void method_22922(int int_1, int int_2);
void method_22923();
default VertexConsumer color(float red, float green, float blue, float alpha) {
return color((int) (red * 255.0f), (int) (green * 255.0f), (int) (blue * 255.0f), (int) (alpha * 255.0f));
}
default VertexConsumer texture2(int uv) {
return texture2(uv & 0xFFFF, uv >> 16 & 0xFFFF);
}
default void quad(Matrix4f matrix, BakedQuad quad, float red, float green, float blue, int texture2) {
quad(matrix, quad, new float[]{1.0f, 1.0f, 1.0f, 1.0f}, red, green, blue, new int[]{texture2, texture2, texture2, texture2}, false);
}
default void quad(Matrix4f matrix, BakedQuad quad, float[] colorMultipliers, float red, float green, float blue, int[] texture2, boolean boolean_1) {
int[] vertexData = quad.getVertexData();
Vec3i normal = quad.getFace().getVector();
Vector3f normal2 = new Vector3f((float) normal.getX(), (float) normal.getY(), (float) normal.getZ());
Matrix3f matrix3f = new Matrix3f(matrix);
matrix3f.transpose();
float det = matrix3f.determinantAndAdjugate();
if (det < 1.0E-5f) {
LOGGER.warn("Could not invert matrix while baking vertex: " + matrix);
} else {
float determinantOfAdjugate = matrix3f.determinant(); // = |A|^2
matrix3f.multiply(MathHelper.fastInverseCubeRootf(determinantOfAdjugate));
// so matrix is now A^(-1) * |A|^(-1/3)...
}
normal2.multiply(matrix3f);
int integer14 = 8;
int l = vertexData.length / 8;
try (MemoryStack mstack = MemoryStack.stackPush()) {
ByteBuffer uvn = mstack.malloc(VertexFormats.POSITION_COLOR_UV_NORMAL.getVertexSize());
IntBuffer asInt = uvn.asIntBuffer();
for (int i = 0; i < l; ++i) {
asInt.clear();
asInt.put(vertexData, i * 8, 8);
float float15 = uvn.getFloat(0);
float float16 = uvn.getFloat(4);
float float17 = uvn.getFloat(8);
byte r;
byte g;
byte b;
if (boolean_1) {
int integer17 = uvn.get(12) & 0xFF;
int integer18 = uvn.get(13) & 0xFF;
int integer19 = uvn.get(14) & 0xFF;
r = (byte) (integer17 * colorMultipliers[i] * red);
g = (byte) (integer18 * colorMultipliers[i] * green);
b = (byte) (integer19 * colorMultipliers[i] * blue);
} else {
r = (byte) (255.0f * colorMultipliers[i] * red);
g = (byte) (255.0f * colorMultipliers[i] * green);
b = (byte) (255.0f * colorMultipliers[i] * blue);
}
int t2 = texture2[i];
float u = uvn.getFloat(16);
float v = uvn.getFloat(20);
vertex(matrix, float15, float16, float17);
color(r, g, b, 255);
texture0(u, v);
texture2(t2);
normal(normal2.getX(), normal2.getY(), normal2.getZ());
next();
}
}
}
default VertexConsumer vertex(Matrix4f matrix, float x, float y, float z) {
Vector4f vec = new Vector4f(x, y, z, 1.0f);
vec.multiply(matrix);
return vertex(vec.getX(), vec.getY(), vec.getZ());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment