help me amo-wan kenobi

This commit is contained in:
petrak@ 2023-07-21 11:30:33 -05:00
parent d385189a97
commit 2e3264f037
7 changed files with 66 additions and 95 deletions

View file

@ -1,18 +1,15 @@
package at.petrak.hexcasting.client.render.overlays;
import at.petrak.hexcasting.client.render.shader.HexShaders;
import at.petrak.hexcasting.mixin.accessor.client.AccessorGameRenderer;
import com.mojang.blaze3d.pipeline.TextureTarget;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import org.joml.Matrix4f;
import static at.petrak.hexcasting.api.HexAPI.modLoc;
/**
* How eigengrau works
* - EIGENGRAU_BZ_SIMULATION stores the state of the BZ cellular automata (thanks Acerola). It's updated on the GPU
@ -23,21 +20,12 @@ import static at.petrak.hexcasting.api.HexAPI.modLoc;
public class EigengrauOverlay {
public static TextureTarget EIGENGRAU_BZ_SIMULATION;
public static TextureTarget EIGENGRAU_VEIL;
public static final ResourceLocation SPECIAL_BZ_SIM_ID = modLoc("eigengrau/simulation");
public static final ResourceLocation SPECIAL_BZ_VEIL_ID = modLoc("eigengrau/veil");
/**
* "Radius" of a hex cell in pixels
*/
private static final int RESOLUTION = 5;
public static void renderEigengrau() {
// Post-processing
if (Minecraft.getInstance().gameRenderer.currentEffect() == null) {
var mogrwbjah = (AccessorGameRenderer) Minecraft.getInstance().gameRenderer;
mogrwbjah.hex$loadEffect(new ResourceLocation(
"shaders/post/hexcasting__eigengrau_presenter_entrypoint.json"));
}
var window = Minecraft.getInstance().getWindow();
int w = window.getWidth();
int h = window.getHeight();
@ -54,6 +42,20 @@ public class EigengrauOverlay {
var tess = Tesselator.getInstance();
var buf = tess.getBuilder();
renderOverAll(w, h, mat, tess, buf);
RenderSystem.setShader(() -> HexShaders.EIGENGRAU_PRESENTER);
RenderSystem.setShaderTexture(0, Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
RenderSystem.setShaderTexture(1, EIGENGRAU_VEIL.getColorTextureId());
RenderSystem.setShaderTexture(2, EIGENGRAU_BZ_SIMULATION.getColorTextureId());
HexShaders.EIGENGRAU_PRESENTER.getUniform("ScreenSize").set((float) w, (float) h);
HexShaders.EIGENGRAU_PRESENTER.getUniform("Resolution").set((float) RESOLUTION);
renderOverAll(w, h, mat, tess, buf);
RenderSystem.setShader(() -> prevShader);
}
private static void renderOverAll(int w, int h, Matrix4f mat, Tesselator tess, BufferBuilder buf) {
buf.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
buf.vertex(mat, 0, 0, 0)
.uv(0, 0)
@ -68,8 +70,6 @@ public class EigengrauOverlay {
.uv(1, 0)
.endVertex();
tess.end();
RenderSystem.setShader(() -> prevShader);
}
public static void initTextures() {

View file

@ -17,6 +17,7 @@ public class HexShaders {
* Spins the BZ simulation in the background
*/
public static ShaderInstance EIGENGRAU_BZ;
public static ShaderInstance EIGENGRAU_PRESENTER;
public static void init(ResourceProvider resourceProvider,
Consumer<Pair<ShaderInstance, Consumer<ShaderInstance>>> registrations) throws IOException {
@ -28,6 +29,10 @@ public class HexShaders {
new ShaderInstance(resourceProvider, "hexcasting__eigengrau_bz", DefaultVertexFormat.POSITION_TEX),
inst -> EIGENGRAU_BZ = inst
));
registrations.accept(Pair.of(
new ShaderInstance(resourceProvider, "hexcasting__eigengrau_presenter", DefaultVertexFormat.POSITION_TEX),
inst -> EIGENGRAU_PRESENTER = inst
));
// Good a time as any I guess?
EigengrauOverlay.initTextures();

View file

@ -1,21 +0,0 @@
package at.petrak.hexcasting.mixin.client;
import at.petrak.hexcasting.client.render.overlays.EigengrauOverlay;
import com.mojang.blaze3d.pipeline.RenderTarget;
import net.minecraft.client.renderer.PostChain;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(PostChain.class)
public class MixinPostChain {
@Inject(method = "getRenderTarget", at = @At("RETURN"), cancellable = true)
void hex$useOurTargets(String target, CallbackInfoReturnable<RenderTarget> cir) {
if (EigengrauOverlay.SPECIAL_BZ_SIM_ID.toString().equals(target)) {
cir.setReturnValue(EigengrauOverlay.EIGENGRAU_BZ_SIMULATION);
} else if (EigengrauOverlay.SPECIAL_BZ_VEIL_ID.toString().equals(target)) {
cir.setReturnValue(EigengrauOverlay.EIGENGRAU_VEIL);
}
}
}

View file

@ -1,22 +1,19 @@
#version 150
// see PostPass.java: the game texture has a defined name
uniform sampler2D DiffuseSampler;
uniform sampler2D veil;
uniform sampler2D bz;
uniform vec2 ScreenSize;
const float resolution = 5.0;
uniform float Resolution;
uniform float ScreenSize;
in vec2 texCoord0;
out vec4 fragColor;
ivec2 pxToHex(vec2 px) {
float sqrt3 = sqrt(3.0);
float x = px.x / (resolution * sqrt3);
float y = px.y / (resolution * sqrt3);
float x = px.x / (Resolution * sqrt3);
float y = px.y / (Resolution * sqrt3);
float tmp = floor(x + sqrt3 * y + 1.0);
int q = int((floor(2.0 * x + 1.0) + tmp) / 3.0);
@ -33,7 +30,7 @@ vec2 hexToPx(ivec2 hex) {
sqrt3 * float(hex.x - hex.y) + sqrt3 / 2.0 * float(hex.y),
1.5 * float(hex.y)
);
return xy * resolution;
return xy * Resolution;
}
void main() {
@ -47,7 +44,7 @@ void main() {
float brightness = (worldSample.r + worldSample.g + worldSample.b) / 3.0;
float eigengrauAmount = clamp(veil.r + mix(0.0, veil.g, 1.0 - brightness), 0.5, 1.0);
vec2 st = vec2(float(hex.x), float(hex.y)) / resolution;
vec2 st = vec2(float(hex.x), float(hex.y)) / Resolution;
float eigengrauSample = texture(bz, st).r / 100.0;
// vec3 col = smoothstep(worldSample, vec3(eigengrauSample), vec3(eigengrauAmount));
vec3 col = worldSample;

View file

@ -1,13 +1,14 @@
{
"blend": {
"func": "add",
"srcrgb": "one",
"dstrgb": "zero"
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "screenquad",
"vertex": "position_tex",
"fragment": "hexcasting__eigengrau_presenter",
"attributes": [
"Position"
"Position",
"UV0"
],
"samplers": [
{
@ -22,7 +23,24 @@
],
"uniforms": [
{
"name": "ProjMat",
"name": "ScreenSize",
"type": "float",
"count": 2,
"values": [
640.0,
480.0
]
},
{
"name": "Resolution",
"type": "float",
"count": 1,
"values": [
5.0
]
},
{
"name": "ModelViewMat",
"type": "matrix4x4",
"count": 16,
"values": [
@ -45,20 +63,25 @@
]
},
{
"name": "InSize",
"type": "float",
"count": 2,
"name": "ProjMat",
"type": "matrix4x4",
"count": 16,
"values": [
1.0,
1.0
]
},
{
"name": "OutSize",
"type": "float",
"count": 2,
"values": [
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
]
}

View file

@ -1,32 +0,0 @@
// This is the ENTRYPOINT for the presenter.
{
targets: [
"swap",
"hexcasting:eigengrau/simulation",
"hexcasting:eigengrau/veil"
],
passes: [
{
// Due to 1984, we can't have this in the hexcasting namespace.
// This is minecraft:shaders/program/hexcasting__eigengray_presenter.json, which loads the .fsh
name: "hexcasting__eigengrau_presenter",
intarget: "minecraft:main",
outtarget: "swap",
auxtargets: [
{
name: "bz",
id: "hexcasting:eigengrau/simulation",
},
{
name: "veil",
id: "hexcasting:eigengrau/veil",
}
],
},
{
"name": "blit",
"intarget": "swap",
"outtarget": "minecraft:main"
}
]
}

View file

@ -28,7 +28,6 @@
"accessor.client.AccessorRenderStateShard",
"accessor.client.AccessorRenderType",
"client.MixinClientLevel",
"client.MixinPlayerRenderer",
"client.MixinPostChain"
"client.MixinPlayerRenderer"
]
}