From f6f8fef556e74f24187ad2a6296f573024a378b3 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Fri, 8 Nov 2024 17:52:10 +0100 Subject: Fix line renderer --- .../firmament/mixins/MainWindowFirstLoadPatch.java | 31 +++++++++++ src/main/kotlin/events/DebugInstantiateEvent.kt | 9 ++++ src/main/kotlin/util/render/FirmamentShaders.kt | 20 +++++-- .../firmament/shaders/code/rendertype_lines.fsh | 18 ------- .../firmament/shaders/code/rendertype_lines.json | 17 ------ .../firmament/shaders/code/rendertype_lines.vsh | 62 ---------------------- .../firmament/shaders/core/rendertype_lines.fsh | 18 +++++++ .../firmament/shaders/core/rendertype_lines.json | 17 ++++++ .../firmament/shaders/core/rendertype_lines.vsh | 62 ++++++++++++++++++++++ src/main/resources/fabric.mod.json | 3 ++ 10 files changed, 157 insertions(+), 100 deletions(-) create mode 100644 src/main/java/moe/nea/firmament/mixins/MainWindowFirstLoadPatch.java create mode 100644 src/main/kotlin/events/DebugInstantiateEvent.kt delete mode 100644 src/main/resources/assets/firmament/shaders/code/rendertype_lines.fsh delete mode 100644 src/main/resources/assets/firmament/shaders/code/rendertype_lines.json delete mode 100644 src/main/resources/assets/firmament/shaders/code/rendertype_lines.vsh create mode 100644 src/main/resources/assets/firmament/shaders/core/rendertype_lines.fsh create mode 100644 src/main/resources/assets/firmament/shaders/core/rendertype_lines.json create mode 100644 src/main/resources/assets/firmament/shaders/core/rendertype_lines.vsh (limited to 'src') diff --git a/src/main/java/moe/nea/firmament/mixins/MainWindowFirstLoadPatch.java b/src/main/java/moe/nea/firmament/mixins/MainWindowFirstLoadPatch.java new file mode 100644 index 0000000..0a90b35 --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/MainWindowFirstLoadPatch.java @@ -0,0 +1,31 @@ +package moe.nea.firmament.mixins; + +import moe.nea.firmament.Firmament; +import moe.nea.firmament.events.DebugInstantiateEvent; +import net.minecraft.client.gui.LogoDrawer; +import net.minecraft.client.gui.screen.TitleScreen; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(TitleScreen.class) +public class MainWindowFirstLoadPatch { + @Unique + private static boolean hasInited = false; + + @Inject(method = "(ZLnet/minecraft/client/gui/LogoDrawer;)V", at = @At("RETURN")) + private void onCreate(boolean doBackgroundFade, LogoDrawer logoDrawer, CallbackInfo ci) { + if (!hasInited) { + try { + DebugInstantiateEvent.Companion.publish(new DebugInstantiateEvent()); + } catch (Throwable t) { + Firmament.INSTANCE.getLogger().error("Failed to instantiate debug instances", t); + System.exit(1); + throw t; + } + } + hasInited = true; + } +} diff --git a/src/main/kotlin/events/DebugInstantiateEvent.kt b/src/main/kotlin/events/DebugInstantiateEvent.kt new file mode 100644 index 0000000..3470a8c --- /dev/null +++ b/src/main/kotlin/events/DebugInstantiateEvent.kt @@ -0,0 +1,9 @@ +package moe.nea.firmament.events + +/** + * Called in a devenv after minecraft has been initialized. This event should be used to force instantiation of lazy + * variables (and similar late init) to cause any possible issues to materialize. + */ +class DebugInstantiateEvent : FirmamentEvent() { + companion object : FirmamentEventBus() +} diff --git a/src/main/kotlin/util/render/FirmamentShaders.kt b/src/main/kotlin/util/render/FirmamentShaders.kt index 5147088..ba67dbb 100644 --- a/src/main/kotlin/util/render/FirmamentShaders.kt +++ b/src/main/kotlin/util/render/FirmamentShaders.kt @@ -3,14 +3,28 @@ package moe.nea.firmament.util.render import net.minecraft.client.gl.Defines import net.minecraft.client.gl.ShaderProgramKey import net.minecraft.client.render.RenderPhase +import net.minecraft.client.render.VertexFormat import net.minecraft.client.render.VertexFormats import moe.nea.firmament.Firmament +import moe.nea.firmament.annotations.Subscribe +import moe.nea.firmament.events.DebugInstantiateEvent +import moe.nea.firmament.util.MC object FirmamentShaders { + val shaders = mutableListOf() + private fun shader(name: String, format: VertexFormat, defines: Defines): ShaderProgramKey { + val key = ShaderProgramKey(Firmament.identifier(name), format, defines) + shaders.add(key) + return key + } - val _LINES: ShaderProgramKey = - ShaderProgramKey(Firmament.identifier("rendertype_lines"), VertexFormats.LINES, Defines.EMPTY) - val LINES = RenderPhase.ShaderProgram(_LINES) + val LINES = RenderPhase.ShaderProgram(shader("core/rendertype_lines", VertexFormats.LINES, Defines.EMPTY)) + @Subscribe + fun debugLoad(event: DebugInstantiateEvent) { + shaders.forEach { + MC.instance.shaderLoader.getOrCreateProgram(it) + } + } } diff --git a/src/main/resources/assets/firmament/shaders/code/rendertype_lines.fsh b/src/main/resources/assets/firmament/shaders/code/rendertype_lines.fsh deleted file mode 100644 index 057f31f..0000000 --- a/src/main/resources/assets/firmament/shaders/code/rendertype_lines.fsh +++ /dev/null @@ -1,18 +0,0 @@ -#version 150 - -#moj_import - -uniform vec4 ColorModulator; -uniform float FogStart; -uniform float FogEnd; -uniform vec4 FogColor; - -in float vertexDistance; -in vec4 vertexColor; - -out vec4 fragColor; - -void main() { - vec4 color = vertexColor * ColorModulator; - fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor); -} diff --git a/src/main/resources/assets/firmament/shaders/code/rendertype_lines.json b/src/main/resources/assets/firmament/shaders/code/rendertype_lines.json deleted file mode 100644 index f276639..0000000 --- a/src/main/resources/assets/firmament/shaders/code/rendertype_lines.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "vertex": "firmament:rendertype_lines", - "fragment": "firmament:rendertype_lines", - "samplers": [ - ], - "uniforms": [ - { "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, - { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, - { "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] }, - { "name": "LineWidth", "type": "float", "count": 1, "values": [ 1.0 ] }, - { "name": "ScreenSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, - { "name": "FogStart", "type": "float", "count": 1, "values": [ 0.0 ] }, - { "name": "FogEnd", "type": "float", "count": 1, "values": [ 1.0 ] }, - { "name": "FogColor", "type": "float", "count": 4, "values": [ 0.0, 0.0, 0.0, 0.0 ] }, - { "name": "FogShape", "type": "int", "count": 1, "values": [ 0 ] } - ] -} diff --git a/src/main/resources/assets/firmament/shaders/code/rendertype_lines.vsh b/src/main/resources/assets/firmament/shaders/code/rendertype_lines.vsh deleted file mode 100644 index b2d0f99..0000000 --- a/src/main/resources/assets/firmament/shaders/code/rendertype_lines.vsh +++ /dev/null @@ -1,62 +0,0 @@ -#version 150 - -#moj_import - -in vec3 Position; -in vec4 Color; -in vec3 Normal; - -uniform mat4 ModelViewMat; -uniform mat4 ProjMat; -uniform float LineWidth; -uniform vec2 ScreenSize; -uniform int FogShape; - -out float vertexDistance; -out vec4 vertexColor; - -const float VIEW_SHRINK = 1.0 - (1.0 / 256.0); -const mat4 VIEW_SCALE = mat4( - VIEW_SHRINK, 0.0, 0.0, 0.0, - 0.0, VIEW_SHRINK, 0.0, 0.0, - 0.0, 0.0, VIEW_SHRINK, 0.0, - 0.0, 0.0, 0.0, 1.0 -); - -void main() { - vec4 linePosStart = ProjMat * VIEW_SCALE * ModelViewMat * vec4(Position, 1.0); - vec4 linePosEnd = ProjMat * VIEW_SCALE * ModelViewMat * vec4(Position + Normal, 1.0); - - vec3 ndc1 = linePosStart.xyz / linePosStart.w; - vec3 ndc2 = linePosEnd.xyz / linePosEnd.w; - - bool linePosStartBehind = ndc1.z <= -1; - bool linePosEndBehind = ndc2.z <= -1; - - if ((linePosStartBehind && linePosEndBehind)) { - gl_Position = vec4(-2.0, -2.0, -2.0, 1.0); - return; // I don't care for these people - } - if ((linePosStartBehind || linePosEndBehind) && false) { - ndc1.z = 0.0; - ndc2.z = 0.0; - linePosStart.w = 1.0; - // TODO: use mx + b to find move the two coordinates around to extend lines - } - - vec2 lineScreenDirection = normalize((ndc2.xy - ndc1.xy) * ScreenSize); - vec2 lineOffset = vec2(-lineScreenDirection.y, lineScreenDirection.x) * LineWidth / ScreenSize; - - if (lineOffset.x < 0.0) { - lineOffset *= -1.0; - } - - if (gl_VertexID % 2 == 0) { - gl_Position = vec4((ndc1 + vec3(lineOffset, 0.0)) * linePosStart.w, linePosStart.w); - } else { - gl_Position = vec4((ndc1 - vec3(lineOffset, 0.0)) * linePosStart.w, linePosStart.w); - } - - vertexDistance = fog_distance(Position, FogShape); - vertexColor = Color; -} diff --git a/src/main/resources/assets/firmament/shaders/core/rendertype_lines.fsh b/src/main/resources/assets/firmament/shaders/core/rendertype_lines.fsh new file mode 100644 index 0000000..057f31f --- /dev/null +++ b/src/main/resources/assets/firmament/shaders/core/rendertype_lines.fsh @@ -0,0 +1,18 @@ +#version 150 + +#moj_import + +uniform vec4 ColorModulator; +uniform float FogStart; +uniform float FogEnd; +uniform vec4 FogColor; + +in float vertexDistance; +in vec4 vertexColor; + +out vec4 fragColor; + +void main() { + vec4 color = vertexColor * ColorModulator; + fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor); +} diff --git a/src/main/resources/assets/firmament/shaders/core/rendertype_lines.json b/src/main/resources/assets/firmament/shaders/core/rendertype_lines.json new file mode 100644 index 0000000..e4537ca --- /dev/null +++ b/src/main/resources/assets/firmament/shaders/core/rendertype_lines.json @@ -0,0 +1,17 @@ +{ + "vertex": "firmament:core/rendertype_lines", + "fragment": "firmament:core/rendertype_lines", + "samplers": [ + ], + "uniforms": [ + { "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, + { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, + { "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] }, + { "name": "LineWidth", "type": "float", "count": 1, "values": [ 1.0 ] }, + { "name": "ScreenSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, + { "name": "FogStart", "type": "float", "count": 1, "values": [ 0.0 ] }, + { "name": "FogEnd", "type": "float", "count": 1, "values": [ 1.0 ] }, + { "name": "FogColor", "type": "float", "count": 4, "values": [ 0.0, 0.0, 0.0, 0.0 ] }, + { "name": "FogShape", "type": "int", "count": 1, "values": [ 0 ] } + ] +} diff --git a/src/main/resources/assets/firmament/shaders/core/rendertype_lines.vsh b/src/main/resources/assets/firmament/shaders/core/rendertype_lines.vsh new file mode 100644 index 0000000..b2d0f99 --- /dev/null +++ b/src/main/resources/assets/firmament/shaders/core/rendertype_lines.vsh @@ -0,0 +1,62 @@ +#version 150 + +#moj_import + +in vec3 Position; +in vec4 Color; +in vec3 Normal; + +uniform mat4 ModelViewMat; +uniform mat4 ProjMat; +uniform float LineWidth; +uniform vec2 ScreenSize; +uniform int FogShape; + +out float vertexDistance; +out vec4 vertexColor; + +const float VIEW_SHRINK = 1.0 - (1.0 / 256.0); +const mat4 VIEW_SCALE = mat4( + VIEW_SHRINK, 0.0, 0.0, 0.0, + 0.0, VIEW_SHRINK, 0.0, 0.0, + 0.0, 0.0, VIEW_SHRINK, 0.0, + 0.0, 0.0, 0.0, 1.0 +); + +void main() { + vec4 linePosStart = ProjMat * VIEW_SCALE * ModelViewMat * vec4(Position, 1.0); + vec4 linePosEnd = ProjMat * VIEW_SCALE * ModelViewMat * vec4(Position + Normal, 1.0); + + vec3 ndc1 = linePosStart.xyz / linePosStart.w; + vec3 ndc2 = linePosEnd.xyz / linePosEnd.w; + + bool linePosStartBehind = ndc1.z <= -1; + bool linePosEndBehind = ndc2.z <= -1; + + if ((linePosStartBehind && linePosEndBehind)) { + gl_Position = vec4(-2.0, -2.0, -2.0, 1.0); + return; // I don't care for these people + } + if ((linePosStartBehind || linePosEndBehind) && false) { + ndc1.z = 0.0; + ndc2.z = 0.0; + linePosStart.w = 1.0; + // TODO: use mx + b to find move the two coordinates around to extend lines + } + + vec2 lineScreenDirection = normalize((ndc2.xy - ndc1.xy) * ScreenSize); + vec2 lineOffset = vec2(-lineScreenDirection.y, lineScreenDirection.x) * LineWidth / ScreenSize; + + if (lineOffset.x < 0.0) { + lineOffset *= -1.0; + } + + if (gl_VertexID % 2 == 0) { + gl_Position = vec4((ndc1 + vec3(lineOffset, 0.0)) * linePosStart.w, linePosStart.w); + } else { + gl_Position = vec4((ndc1 - vec3(lineOffset, 0.0)) * linePosStart.w, linePosStart.w); + } + + vertexDistance = fog_distance(Position, FogShape); + vertexColor = Color; +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 9997018..9b00adf 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -33,6 +33,9 @@ "rei_client": [ "moe.nea.firmament.compat.rei.FirmamentReiPlugin" ], + "rei_common": [ + "moe.nea.firmament.compat.rei.FirmamentReiCommonPlugin" + ], "modmenu": [ "moe.nea.firmament.compat.modmenu.FirmamentModMenuPlugin" ], -- cgit