Skip to content

Commit

Permalink
chore: prepare changes for wires rendering in distance
Browse files Browse the repository at this point in the history
See #224
  • Loading branch information
klikli-dev committed Feb 5, 2025
1 parent 27bf21a commit 504b6f4
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/com/klikli_dev/theurgy/Theurgy.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public Theurgy(IEventBus modEventBus, ModContainer modContainer) {
modEventBus.addListener(Client::onRegisterMenuScreens);
modEventBus.addListener(BlockOverlays::onTextureAtlasStitched);
modEventBus.addListener(KeyMappingsRegistry::onRegisterKeyMappings);
modEventBus.addListener(ShaderRegistry::onRegisterShaders);
NeoForge.EVENT_BUS.addListener(Client::onRenderLevelStage);
NeoForge.EVENT_BUS.addListener(Client::onClientTick);
NeoForge.EVENT_BUS.addListener(Client::onRecipesUpdated);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,36 @@
import net.minecraft.Util;
import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation;

import java.util.OptionalDouble;
import java.util.function.Function;

public class RenderTypes extends RenderStateShard {

public static ShaderInstance rendertypeDistanceLines;
protected static final ShaderStateShard RENDERTYPE_DISTANCE_LINES_SHADER = new ShaderStateShard(() -> rendertypeDistanceLines);

protected static final RenderType DISTANCE_LINES = RenderType.create(
"distance_lines",
DefaultVertexFormat.POSITION_COLOR_NORMAL,
VertexFormat.Mode.LINES,
1536,
false,
false,
RenderType.CompositeState.builder()
.setShaderState(RENDERTYPE_DISTANCE_LINES_SHADER)
.setLineState(new RenderStateShard.LineStateShard(OptionalDouble.empty()))
.setLayeringState(VIEW_OFFSET_Z_LAYERING)
.setTransparencyState(TRANSLUCENT_TRANSPARENCY)
.setOutputState(ITEM_ENTITY_TARGET)
.setWriteMaskState(COLOR_DEPTH_WRITE)
.setCullState(NO_CULL)
.createCompositeState(false)
);

protected static final Function<ResourceLocation, RenderType> ENTITY_TRANSLUCENT_CULL_NO_DEPTH = Util.memoize(
p_286165_ -> {
RenderType.CompositeState rendertype$compositestate = RenderType.CompositeState.builder()
Expand Down Expand Up @@ -114,4 +137,8 @@ public static RenderType fluid() {
public static RenderType srcMinusOne(ResourceLocation location) {
return SRC_MINUS_ONE.apply(location);
}

public static RenderType distanceLines() {
return DISTANCE_LINES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.klikli_dev.theurgy.logistics;

import com.klikli_dev.theurgy.content.render.RenderTypes;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
Expand Down Expand Up @@ -43,7 +44,7 @@ public void onRenderLevelStage(RenderLevelStageEvent event) {
poseStack.translate(-renderPosX, -renderPosY, -renderPosZ);

//we use lines() to avoid all the wires getting connected as it would happen with linestrip
var buffer = bufferSource.getBuffer(RenderType.lines());
var buffer = bufferSource.getBuffer(RenderTypes.distanceLines());
for (var wire : this.wires) {
poseStack.pushPose();
poseStack.translate(wire.from().getX(), wire.from().getY(), wire.from().getZ());
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/klikli_dev/theurgy/registry/ShaderRegistry.java
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);
}
}
}
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);
}
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 ] }
]
}
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;
}

0 comments on commit 504b6f4

Please sign in to comment.