Skip to content

Commit 4926d59

Browse files
committed
Get rid of some hacky stuff.
In resizeDistanceField(), there were some ad-hoc adjustments to MSDF and SDF crispness, and they were never really quite right. They're much simpler now. In enableShader() and enableDistanceFieldShader(), it was failing to set uniforms if the u_smoothing value was the same as the value used for the last thing, even if the shader was different (and so the stored value couldn't be the same).
1 parent 93b237d commit 4926d59

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

src/main/java/com/github/tommyettinger/textra/Font.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,19 +2925,13 @@ public void enableShader(Batch batch) {
29252925
if (distanceField == DistanceFieldType.MSDF) {
29262926
batch.setShader(shader);
29272927
float smoothing = 7f * actualCrispness * Math.max(cellHeight / originalCellHeight, cellWidth / originalCellWidth);
2928-
Float old = smoothingValues.get(batch);
2929-
if(old != null && MathUtils.isEqual(old, smoothing))
2930-
return;
29312928
batch.flush();
29322929
shader.setUniformf("u_smoothing", smoothing);
29332930
smoothingValues.put(batch, smoothing);
29342931
} else if (distanceField == DistanceFieldType.SDF || getDistanceField() == DistanceFieldType.SDF_OUTLINE) {
29352932
batch.setShader(shader);
29362933
float smoothing = (actualCrispness / (Math.max(cellHeight / originalCellHeight,
29372934
cellWidth / originalCellWidth) * 0.5f + 0.125f));
2938-
Float old = smoothingValues.get(batch);
2939-
if(old != null && MathUtils.isEqual(old, smoothing))
2940-
return;
29412935
batch.flush();
29422936
shader.setUniformf("u_smoothing", smoothing);
29432937
smoothingValues.put(batch, smoothing);
@@ -2964,18 +2958,12 @@ public void enableDistanceFieldShader(Batch batch) {
29642958
if (batch.getShader() == shader) {
29652959
if (distanceField == DistanceFieldType.MSDF) {
29662960
float smoothing = 7f * actualCrispness * Math.max(cellHeight / originalCellHeight, cellWidth / originalCellWidth);
2967-
Float old = smoothingValues.get(batch);
2968-
if(old != null && MathUtils.isEqual(old, smoothing))
2969-
return;
29702961
batch.flush();
29712962
shader.setUniformf("u_smoothing", smoothing);
29722963
smoothingValues.put(batch, smoothing);
29732964
} else if (distanceField == DistanceFieldType.SDF || getDistanceField() == DistanceFieldType.SDF_OUTLINE) {
29742965
float smoothing = (actualCrispness / (Math.max(cellHeight / originalCellHeight,
29752966
cellWidth / originalCellWidth) * 0.5f + 0.125f));
2976-
Float old = smoothingValues.get(batch);
2977-
if(old != null && MathUtils.isEqual(old, smoothing))
2978-
return;
29792967
batch.flush();
29802968
shader.setUniformf("u_smoothing", smoothing);
29812969
smoothingValues.put(batch, smoothing);
@@ -6608,31 +6596,26 @@ public void removeStoredState(String name) {
66086596
storedStates.remove(name, 0L);
66096597
}
66106598
/**
6599+
* Important; must be called in {@link com.badlogic.gdx.ApplicationListener#resize(int, int)} on each
6600+
* SDF, MSDF, or SDF_OUTLINE font you have currently rendering! This allows the distance field to appear
6601+
* as correct and crisp-outlined as it should be; without this, the distance field will probably not look
6602+
* very sharp at all. This doesn't need to be called every frame, only in resize().
6603+
* <br>
66116604
* Given the new width and height for a window, this attempts to adjust the {@link #actualCrispness} of an
66126605
* SDF/MSDF/SDF_OUTLINE font so that it will display cleanly at a different size. This uses this font's
66136606
* {@link #distanceFieldCrispness} as a multiplier applied after calculating the initial crispness.
6614-
* This is a suggestion for what to call in your {@link com.badlogic.gdx.ApplicationListener#resize(int, int)}
6615-
* method for each SDF, MSDF, or SDF_OUTLINE font you have currently rendering.
66166607
*
66176608
* @param width the new window width; usually a parameter in {@link com.badlogic.gdx.ApplicationListener#resize(int, int)}
66186609
* @param height the new window height; usually a parameter in {@link com.badlogic.gdx.ApplicationListener#resize(int, int)}
66196610
*/
66206611
public void resizeDistanceField(int width, int height) {
6621-
if (getDistanceField() == DistanceFieldType.MSDF) {
6622-
if (Gdx.graphics.getBackBufferWidth() == 0 || Gdx.graphics.getBackBufferHeight() == 0) {
6623-
actualCrispness = distanceFieldCrispness;
6624-
} else {
6625-
actualCrispness = distanceFieldCrispness * (float) Math.pow(8f,
6626-
Math.max((float) width / Gdx.graphics.getBackBufferWidth(),
6627-
(float) height / Gdx.graphics.getBackBufferHeight()) * 1.9f - 2.15f + cellHeight * 0.01f);
6628-
}
6629-
} else if (getDistanceField() == DistanceFieldType.SDF || getDistanceField() == DistanceFieldType.SDF_OUTLINE) {
6612+
if (getDistanceField() != DistanceFieldType.STANDARD) {
66306613
if (Gdx.graphics.getBackBufferWidth() == 0 || Gdx.graphics.getBackBufferHeight() == 0) {
66316614
actualCrispness = distanceFieldCrispness;
66326615
} else {
6633-
actualCrispness = distanceFieldCrispness * (float) Math.pow(4f,
6634-
Math.max((float) width / Gdx.graphics.getBackBufferWidth(),
6635-
(float) height / Gdx.graphics.getBackBufferHeight()) * 1.9f - 2f + cellHeight * 0.005f);
6616+
actualCrispness = distanceFieldCrispness *
6617+
Math.max((float) width / Gdx.graphics.getBackBufferWidth(),
6618+
(float) height / Gdx.graphics.getBackBufferHeight());
66366619
}
66376620
}
66386621
}

src/test/java/com/github/tommyettinger/textra/IncongruityDistanceFieldTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public void render() {
7171

7272
stage.act();
7373
stage.draw();
74+
// System.out.println("A Starry (MSDF) uses shader " + msdfFonts[0].shader);
75+
// System.out.println("Cascadia Mono (MSDF) uses shader " + msdfFonts[1].shader);
76+
// System.out.println("Iosevka (MSDF) uses shader " + msdfFonts[6].shader);
7477
}
7578

7679
@Override

0 commit comments

Comments
 (0)