-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: prepare changes for wires rendering in distance
See #224
- Loading branch information
1 parent
27bf21a
commit 504b6f4
Showing
7 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/klikli_dev/theurgy/registry/ShaderRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.klikli_dev.theurgy.registry; | ||
|
||
import com.klikli_dev.theurgy.Theurgy; | ||
import com.klikli_dev.theurgy.content.render.RenderTypes; | ||
import com.mojang.blaze3d.vertex.DefaultVertexFormat; | ||
import net.minecraft.client.renderer.ShaderInstance; | ||
import net.neoforged.neoforge.client.event.RegisterShadersEvent; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
public class ShaderRegistry { | ||
|
||
public static void onRegisterShaders(RegisterShadersEvent event) { | ||
try { | ||
event.registerShader(new ShaderInstance(event.getResourceProvider(), Theurgy.loc("rendertype_distance_lines"), DefaultVertexFormat.POSITION_COLOR_NORMAL), shaderInstance -> { | ||
RenderTypes.rendertypeDistanceLines = shaderInstance; | ||
}); | ||
} catch (IOException e) { | ||
Theurgy.LOGGER.error("Failed to register shader", e); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/resources/assets/theurgy/shaders/core/rendertype_distance_lines.fsh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#version 150 | ||
|
||
#moj_import <fog.glsl> | ||
|
||
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); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/resources/assets/theurgy/shaders/core/rendertype_distance_lines.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"vertex": "rendertype_lines", | ||
"fragment": "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 ] } | ||
] | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/resources/assets/theurgy/shaders/core/rendertype_distance_lines.vsh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#version 150 | ||
|
||
#moj_import <fog.glsl> | ||
|
||
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() { | ||
vertexDistance = fog_distance(Position, FogShape); | ||
|
||
|
||
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; | ||
|
||
|
||
vec2 lineScreenDirection = normalize((ndc2.xy - ndc1.xy) * ScreenSize); | ||
|
||
//divide by distance to scale line thickness based on distance to camera, see https://github.com/klikli-dev/theurgy/issues/224 | ||
vec2 lineOffset = (vec2(-lineScreenDirection.y, lineScreenDirection.x) * LineWidth / ScreenSize) / vertexDistance; | ||
|
||
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); | ||
} | ||
|
||
vertexColor = Color; | ||
} |