Skip to content

Commit

Permalink
Change texture encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Dec 21, 2023
1 parent d922d90 commit da9011d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
4 changes: 2 additions & 2 deletions buildscript/src/main/java/Buildscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ public void getModDependencies(ModDependencyCollector d) {
if (CUSTOM_SODIUM) {
d.add(new JavaJarDependency(getProjectDir().resolve("custom_sodium").resolve(customSodiumName).toAbsolutePath(), null, new MavenId("me.jellysquid.mods", "sodium-fabric", customSodiumName.replace("sodium-fabric-", ""))), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
} else {
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "sodium", "mc1.20.3-0.5.6"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "sodium", "mc1.20.3-0.5.5"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME);
}
} else {
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "sodium", "mc1.20.3-0.5.6"), ModDependencyFlag.COMPILE);
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "sodium", "mc1.20.3-0.5.5"), ModDependencyFlag.COMPILE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void transform(
SodiumParameters parameters) {
CommonTransformer.transform(t, tree, root, parameters, false);

replaceMidTexCoord(t, tree, root, 1.0f / 65536.0f);
replaceMidTexCoord(t, tree, root, 1.0f / 32768.0f);

root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix0, "mat4(1.0)");
root.replaceExpressionMatches(t, CommonTransformer.glTextureMatrix1, "iris_LightmapTextureMatrix");
Expand Down Expand Up @@ -150,7 +150,7 @@ public static void injectVertInit(
"void _vert_init() {" +
"_vert_position = (vec3(a_PosId.xyz) * 0.00048828125 + -8.0"
+ ");" +
"_vert_tex_diffuse_coord = (a_TexCoord * 1.52587891E-5);" +
"_vert_tex_diffuse_coord = (a_TexCoord * " + (1.0f / 32768.0f) + ");" +
"_vert_tex_light_coord = a_LightCoord;" +
"_vert_color = " + separateAo + ";" +
"_draw_id = (a_PosId.w >> 8u) & 0xFFu; }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class XHFPModelVertexType implements ChunkVertexType {
.build();

private static final int POSITION_MAX_VALUE = 65536;
private static final int TEXTURE_MAX_VALUE = 65536;
private static final int TEXTURE_MAX_VALUE = 32768;

private static final float MODEL_ORIGIN = 8.0f;
private static final float MODEL_RANGE = 32.0f;
Expand All @@ -47,8 +47,9 @@ public ChunkVertexEncoder getEncoder() {
return new XHFPTerrainVertex();
}

static short encodeBlockTexture(float value) {
return (short) (Math.min(0.99999997F, value) * TEXTURE_MAX_VALUE);
public static int encodeTexture(float u, float v) {
return ((Math.round(u * TEXTURE_MAX_VALUE) & 0xFFFF) << 0) |
((Math.round(v * TEXTURE_MAX_VALUE) & 0xFFFF) << 16);
}

static float decodeBlockTexture(short raw) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public long write(long ptr,

MemoryUtil.memPutInt(ptr + 8, vertex.color);

MemoryUtil.memPutShort(ptr + 12, XHFPModelVertexType.encodeBlockTexture(vertex.u));
MemoryUtil.memPutShort(ptr + 14, XHFPModelVertexType.encodeBlockTexture(vertex.v));
MemoryUtil.memPutInt(ptr + 12, XHFPModelVertexType.encodeTexture(vertex.u, vertex.v));

MemoryUtil.memPutInt(ptr + 16, vertex.light);

Expand Down Expand Up @@ -116,18 +115,12 @@ public long write(long ptr,
uSum *= 0.25f;
vSum *= 0.25f;

short midU = XHFPModelVertexType.encodeBlockTexture(uSum);
short midV = XHFPModelVertexType.encodeBlockTexture(vSum);
int midUV = XHFPModelVertexType.encodeTexture(uSum, vSum);

MemoryUtil.memPutShort(ptr + 20, midU);
MemoryUtil.memPutShort(ptr + 20 - STRIDE, midU);
MemoryUtil.memPutShort(ptr + 20 - STRIDE * 2, midU);
MemoryUtil.memPutShort(ptr + 20 - STRIDE * 3, midU);

MemoryUtil.memPutShort(ptr + 22, midV);
MemoryUtil.memPutShort(ptr + 22 - STRIDE, midV);
MemoryUtil.memPutShort(ptr + 22 - STRIDE * 2, midV);
MemoryUtil.memPutShort(ptr + 22 - STRIDE * 3, midV);
MemoryUtil.memPutInt(ptr + 20, midUV);
MemoryUtil.memPutInt(ptr + 20 - STRIDE, midUV);
MemoryUtil.memPutInt(ptr + 20 - STRIDE * 2, midUV);
MemoryUtil.memPutInt(ptr + 20 - STRIDE * 3, midUV);

uSum = 0;
vSum = 0;
Expand Down

0 comments on commit da9011d

Please sign in to comment.