Skip to content

Commit

Permalink
A fine solution
Browse files Browse the repository at this point in the history
- Rely on transform matrix for all font vertex position stuffs
- Use a skew matrix for italics
  • Loading branch information
Jozufozu committed Sep 26, 2024
1 parent bf15b20 commit 9110e6e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@
import net.minecraft.util.FastColor;

public class GlyphInstance extends AbstractInstance {
// Skew x by 1 - 0.25 * y
// Note that columns are written as rows.
private static final Matrix4f ITALIC_SKEW = new Matrix4f(1, 0, 0, 0, -0.25f, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);

public final Matrix4f pose = new Matrix4f();

public float u0;
public float u1;
public float v0;
public float v1;

public float x0;
public float x1;
public float x2;
public float x3;

public float y0;
public float y1;

public byte red = (byte) 0xFF;
public byte green = (byte) 0xFF;
public byte blue = (byte) 0xFF;
Expand All @@ -47,22 +43,26 @@ public GlyphInstance setGlyph(BakedGlyph glyph, float x, float y, boolean italic
float up = glyphReader.flywheel$up();
float down = glyphReader.flywheel$down();

float f = x + left;
float g = x + right;
float h = up - 3.0f;
float j = down - 3.0f;
float k = y + h;
float l = y + j;
float m = italic ? 1.0f - 0.25f * h : 0.0f;
float n = italic ? 1.0f - 0.25f * j : 0.0f;

y0 = k;
y1 = l;

x0 = f + m;
x1 = f + n;
x2 = g + n;
x3 = g + m;
pose.translate(x + left, y + up - 3.0f, 0.0f);
pose.scale(right - left, down - up, 1.0f);

if (italic) {
pose.mul(ITALIC_SKEW);
}

return this;
}

public GlyphInstance setEffect(BakedGlyph glyph, float x0, float y0, float x1, float y1, float depth) {
var glyphReader = FlwLibLink.INSTANCE.getGlyphExtension(glyph);

u0 = glyphReader.flywheel$u0();
u1 = glyphReader.flywheel$u1();
v0 = glyphReader.flywheel$v0();
v1 = glyphReader.flywheel$v1();

pose.translate(x0, y0, depth);
pose.scale(x1 - x0, y1 - y0, 1.0f);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ public final class InstanceTypes {
.layout(LayoutBuilder.create()
.matrix("pose", FloatRepr.FLOAT, 4)
.vector("u0u1v0v1", FloatRepr.FLOAT, 4)
.vector("x", FloatRepr.FLOAT, 4)
.vector("y", FloatRepr.FLOAT, 2)
.vector("color", FloatRepr.UNSIGNED_BYTE, 4)
.vector("light", FloatRepr.UNSIGNED_SHORT, 2)
.build())
Expand All @@ -119,17 +117,11 @@ public final class InstanceTypes {
MemoryUtil.memPutFloat(ptr + 68, instance.u1);
MemoryUtil.memPutFloat(ptr + 72, instance.v0);
MemoryUtil.memPutFloat(ptr + 76, instance.v1);
MemoryUtil.memPutFloat(ptr + 80, instance.x0);
MemoryUtil.memPutFloat(ptr + 84, instance.x1);
MemoryUtil.memPutFloat(ptr + 88, instance.x2);
MemoryUtil.memPutFloat(ptr + 92, instance.x3);
MemoryUtil.memPutFloat(ptr + 96, instance.y0);
MemoryUtil.memPutFloat(ptr + 100, instance.y1);
MemoryUtil.memPutByte(ptr + 104, instance.red);
MemoryUtil.memPutByte(ptr + 105, instance.green);
MemoryUtil.memPutByte(ptr + 106, instance.blue);
MemoryUtil.memPutByte(ptr + 107, instance.alpha);
ExtraMemoryOps.put2x16(ptr + 108, instance.light);
MemoryUtil.memPutByte(ptr + 80, instance.red);
MemoryUtil.memPutByte(ptr + 81, instance.green);
MemoryUtil.memPutByte(ptr + 82, instance.blue);
MemoryUtil.memPutByte(ptr + 83, instance.alpha);
ExtraMemoryOps.put2x16(ptr + 84, instance.light);
})
.vertexShader(Flywheel.rl("instance/glyph.vert"))
.cullShader(Flywheel.rl("instance/cull/glyph.glsl"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* A visual that renders a single line of text.
*/
public class TextVisual {
public static final float ONE_PIXEL = 0.125f;

public boolean dropShadow;
public boolean with8xOutline;
public int backgroundColor = 0;
Expand Down Expand Up @@ -120,9 +122,9 @@ public boolean accept(int i, Style style, int j) {

GlyphInstance glyph = recycler.get(new GlyphModelKey(glyphExtension.flywheel$texture(), new GlyphSettings(bold, dropShadow, with8xOutline)));

glyph.pose.set(pose);
glyph.setGlyph(bakedGlyph, this.x, this.y, style.isItalic());
glyph.color(r, g, b, this.a);
glyph.pose.set(pose);
glyph.light = light;
glyph.setChanged();
}
Expand All @@ -146,20 +148,9 @@ private void addEffect(float x0, float y0, float x1, float y1, float depth, floa

GlyphInstance glyph = recycler.get(new GlyphModelKey(glyphExtension.flywheel$texture(), new GlyphSettings(false, dropShadow, with8xOutline)));

glyph.u0 = glyphExtension.flywheel$u0();
glyph.u1 = glyphExtension.flywheel$u1();
glyph.v0 = glyphExtension.flywheel$v0();
glyph.v1 = glyphExtension.flywheel$v1();

glyph.y0 = y0;
glyph.y1 = y1;

glyph.x0 = x0;
glyph.x1 = x1;
glyph.x2 = x1;
glyph.x3 = x0;
glyph.color(r, g, b, this.a);
glyph.pose.set(pose);
glyph.setEffect(bakedGlyph, x0, y0, x1, y1, depth);
glyph.color(r, g, b, this.a);
glyph.light = light;
glyph.setChanged();
}
Expand All @@ -181,7 +172,6 @@ public float finish(int backgroundColor, float x) {

private static final Material GLYPH_MATERIAL = SimpleMaterial.builder()
.cutout(CutoutShaders.ONE_TENTH)
.backfaceCulling(false)
.build();

private record GlyphModelKey(ResourceLocation font, GlyphSettings settings) {
Expand All @@ -207,19 +197,19 @@ public GlyphMesh into() {
continue;
}

out.add(new Vector3f(x, y, 0));
out.add(new Vector3f(x * ONE_PIXEL, y * ONE_PIXEL, 0));
}
}
} else {
out.add(new Vector3f(0, 0, 0));
}

if (bold) {
out.add(new Vector3f(1, 0, 0));
out.add(new Vector3f(ONE_PIXEL, 0, 0));
}

if (dropShadow) {
out.add(new Vector3f(1, 1, 0));
out.add(new Vector3f(ONE_PIXEL, ONE_PIXEL, 0));
}

return new GlyphMesh(out.toArray(new Vector3f[0]));
Expand All @@ -232,6 +222,8 @@ public GlyphMesh into() {
* @param quads Each quad will be expanded into 4 vertices.
*/
private record GlyphMesh(Vector3f[] quads) implements QuadMesh {
private static final float[] X = new float[]{0, 0, 1, 1};
private static final float[] Y = new float[]{0, 1, 1, 0};

@Override
public int vertexCount() {
Expand All @@ -245,8 +237,8 @@ public void write(MutableVertexList vertexList) {
var quadStart = i * 4;

for (int j = 0; j < 4; j++) {
vertexList.x(quadStart + j, quad.x);
vertexList.y(quadStart + j, quad.y);
vertexList.x(quadStart + j, quad.x + X[j]);
vertexList.y(quadStart + j, quad.y + Y[j]);
vertexList.z(quadStart + j, quad.z);
vertexList.normalX(quadStart + j, 0);
vertexList.normalY(quadStart + j, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "flywheel:util/matrix.glsl"

void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) {

radius += abs(i.x[2] - i.x[0]) + abs(i.y[1] - i.y[0]);
center += vec3((i.x[0] + i.x[2]) * 0.5, (i.y[0] + i.y[1]) * 0.5, 0.);

transformBoundingSphere(i.pose, center, radius);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ void flw_instanceVertex(in FlwInstance i) {

uint yIndex = ((vertexInGlyph + 1u) >> 1u) & 1u;

flw_vertexPos.x += i.x[vertexInGlyph];
flw_vertexPos.y += i.y[yIndex];

flw_vertexPos = i.pose * flw_vertexPos;

flw_vertexTexCoord.s = i.u0u1v0v1[(vertexInGlyph & 2u) >> 1u];
Expand Down

0 comments on commit 9110e6e

Please sign in to comment.