Skip to content

Commit

Permalink
Fix highlight rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
covers1624 committed Jan 23, 2024
1 parent 36923ce commit bb57066
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/main/java/codechicken/microblock/client/MicroblockRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import codechicken.microblock.part.PlacementGrid;
import codechicken.microblock.part.StandardMicroFactory;
import codechicken.microblock.util.MaskedCuboid;
import codechicken.multipart.client.Shaders;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
Expand Down Expand Up @@ -65,7 +66,7 @@ public class MicroblockRender {
private static final ThreadLocal<PipelineState> PIPELINES = ThreadLocal.withInitial(PipelineState::create);

public static final RenderType HIGHLIGHT_RENDER_TYPE = RenderType.create(MOD_ID + ":highlight", DefaultVertexFormat.BLOCK, VertexFormat.Mode.QUADS, 255, RenderType.CompositeState.builder()
.setShaderState(RenderType.RENDERTYPE_SOLID_SHADER)
.setShaderState(new RenderStateShard.ShaderStateShard(Shaders::highlightShader))
.setTextureState(RenderType.BLOCK_SHEET)
.setWriteMaskState(RenderType.COLOR_DEPTH_WRITE)
.setTransparencyState(RenderType.TRANSLUCENT_TRANSPARENCY)
Expand Down Expand Up @@ -259,19 +260,8 @@ public static PipelineState create() {
}
}

private static class MicroblockLevelProxy implements BlockAndTintGetter {

private final BlockAndTintGetter other;
private final BlockPos pos;
private final BlockState state;

private MicroblockLevelProxy(BlockAndTintGetter other, BlockPos pos, BlockState state) {
this.other = other;
this.pos = pos;
this.state = state;
}

// @formatter:off
// @formatter:off
private record MicroblockLevelProxy(BlockAndTintGetter other, BlockPos pos, BlockState state) implements BlockAndTintGetter {
@Override public float getShade(Direction face, boolean shade) { return other.getShade(face, shade); }
@Override public LevelLightEngine getLightEngine() { return other.getLightEngine(); }
@Override public int getBlockTint(BlockPos pos, ColorResolver resolver) { return other.getBlockTint(pos, resolver); }
Expand All @@ -280,6 +270,6 @@ private MicroblockLevelProxy(BlockAndTintGetter other, BlockPos pos, BlockState
@Override public FluidState getFluidState(BlockPos pos) { return other.getFluidState(pos); }
@Override public int getHeight() { return other.getHeight(); }
@Override public int getMinBuildHeight() { return other.getMinBuildHeight(); }
// @formatter:on
}
// @formatter:on
}
39 changes: 39 additions & 0 deletions src/main/java/codechicken/multipart/client/Shaders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package codechicken.multipart.client;

import codechicken.lib.render.shader.CCShaderInstance;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import net.covers1624.quack.util.CrashLock;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.event.RegisterShadersEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

import static codechicken.multipart.CBMultipart.MOD_ID;

/**
* Created by covers1624 on 23/1/24.
*/
public class Shaders {

public static final CrashLock LOCK = new CrashLock("Already Initialised");

private static @Nullable CCShaderInstance highlightShader;

public static void init() {
LOCK.lock();
FMLJavaModLoadingContext.get().getModEventBus().addListener(Shaders::onRegisterShaders);
}

private static void onRegisterShaders(RegisterShadersEvent event) {
event.registerShader(
CCShaderInstance.create(event.getResourceProvider(), new ResourceLocation(MOD_ID, "highlight"), DefaultVertexFormat.BLOCK),
e -> highlightShader = (CCShaderInstance) e
);
}

public static CCShaderInstance highlightShader() {
return Objects.requireNonNull(highlightShader, "Highlight shader not loaded yet.");
}
}
2 changes: 2 additions & 0 deletions src/main/java/codechicken/multipart/init/ClientInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import codechicken.multipart.client.ClientEventHandler;
import codechicken.multipart.client.MultipartBlockRenderer;
import codechicken.multipart.client.MultipartTileRenderer;
import codechicken.multipart.client.Shaders;
import codechicken.multipart.handler.ControlKeyHandler;
import net.covers1624.quack.util.CrashLock;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
Expand All @@ -29,6 +30,7 @@ public static void init() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(ClientInit::onClientInit);
bus.addListener(ClientInit::onRegisterRenderers);
Shaders.init();
}

private static void onClientInit(FMLClientSetupEvent event) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/assets/cb_multipart/shaders/core/highlight.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#version 150

uniform sampler2D Sampler0;

uniform vec4 ColorModulator;

in vec4 vertexColor;
in vec2 texCoord0;
in vec2 texCoord2;
in vec4 normal;

out vec4 fragColor;

void main() {
vec4 color = texture(Sampler0, texCoord0) * vertexColor;
fragColor = color * ColorModulator;
}
24 changes: 24 additions & 0 deletions src/main/resources/assets/cb_multipart/shaders/core/highlight.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"blend": {
"func": "add",
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "cb_multipart:highlight",
"fragment": "cb_multipart:highlight",
"attributes": [
"Position",
"Color",
"UV0",
"UV2",
"Normal"
],
"samplers": [
{ "name": "Sampler0" }
],
"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 ] }
]
}
24 changes: 24 additions & 0 deletions src/main/resources/assets/cb_multipart/shaders/core/highlight.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#version 150

in vec3 Position;
in vec4 Color;
in vec2 UV0;
in vec2 UV2;
in vec3 Normal;

uniform mat4 ModelViewMat;
uniform mat4 ProjMat;

out vec4 vertexColor;
out vec2 texCoord0;
out vec2 texCoord2;
out vec4 normal;

void main() {
gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0);

vertexColor = Color;
texCoord0 = UV0;
texCoord2 = UV2;
normal = ProjMat * ModelViewMat * vec4(Normal, 0.0);
}

0 comments on commit bb57066

Please sign in to comment.