Added Fray Screen Effect

This commit is contained in:
Waterpicker 2021-06-16 04:53:37 -05:00
parent 0046a0f137
commit 1e26de6a00
10 changed files with 250 additions and 70 deletions

View file

@ -2,6 +2,8 @@ package org.dimdev.dimdoors.client;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gl.Uniform;
import net.minecraft.client.render.Shader;
@Environment(EnvType.CLIENT)
@ -15,14 +17,4 @@ public class ModShaders {
public static Shader getDimensionalPortal() {
return DIMENSIONAL_PORTAL;
}
private static Shader STATIC = null;
public static void setStaticShader(Shader dimensionalPortal) {
STATIC = dimensionalPortal;
}
public static Shader getStaticShader() {
return STATIC;
}
}

View file

@ -63,23 +63,10 @@ public class MyRenderLayer extends RenderLayer {
false,
false)
)
// .alpha(Alpha.HALF_ALPHA)
.shader(RenderPhase.POSITION_COLOR_TEXTURE_SHADER)
.build(false)
);
public static RenderLayer STATIC = RenderLayerFactory.create(
"static",
VertexFormats.POSITION_TEXTURE,
VertexFormat.DrawMode.QUADS,
2097152,
true,
false,
RenderLayer.MultiPhaseParameters.builder()
.shader(new Shader(ModShaders::getStaticShader))
.build(true));
public static RenderLayer getMonolith(Identifier texture) {
RenderLayer.MultiPhaseParameters multiPhaseParameters = RenderLayer.MultiPhaseParameters.builder().texture(new RenderPhase.Texture(texture, false, false)).shader(new Shader(GameRenderer::getRenderTypeEntityTranslucentShader)).transparency(RenderPhase.TRANSLUCENT_TRANSPARENCY).cull(DISABLE_CULLING).lightmap(ENABLE_LIGHTMAP).depthTest(RenderPhase.ALWAYS_DEPTH_TEST).overlay(ENABLE_OVERLAY_COLOR).build(false);
return RenderLayerFactory.create("monolith", VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL, VertexFormat.DrawMode.QUADS, 256, true, true, multiPhaseParameters);

View file

@ -1,7 +1,6 @@
package org.dimdev.dimdoors.mixin.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
@ -33,6 +32,5 @@ public abstract class GameRendererMixin {
@Inject(method = "loadShaders", at = @At(value = "INVOKE", shift = At.Shift.AFTER, ordinal = 1, target = "java/util/List.add(Ljava/lang/Object;)Z"), locals = LocalCapture.CAPTURE_FAILSOFT)
public void onReload(ResourceManager manager, CallbackInfo ci, List list, List list2) throws IOException {
list2.add(Pair.of(new Shader(manager, "dimensional_portal", VertexFormats.POSITION), (Consumer<Shader>) ModShaders::setDimensionalPortal));
list2.add(Pair.of(new Shader(manager, "static", VertexFormats.POSITION), (Consumer<Shader>) ModShaders::setStaticShader));
}
}

View file

@ -8,6 +8,7 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.ModConfig;
import org.dimdev.dimdoors.client.ModShaders;
import org.dimdev.dimdoors.world.ModBiomes;
import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.level.component.PlayerModifiersComponent;
@ -45,49 +46,5 @@ public abstract class InGameHudMixin{
// }
@Inject(method = "render", at = @At("HEAD"), cancellable = true)
public void renderOverlayMixin(MatrixStack matrices, float tickDelta, CallbackInfo ci) {
float overlayOpacity = (config.fray.grayScreenFray - PlayerModifiersComponent.getFray(getCameraPlayer()))/(config.fray.grayScreenFray - (float)config.fray.maxFray);
if (PlayerModifiersComponent.getFray(getCameraPlayer()) > config.fray.grayScreenFray) {
System.out.println(overlayOpacity);
this.renderOverlay(new Identifier("dimdoors", "textures/other/static.png"), overlayOpacity);
}
}
private void renderOverlay(Identifier texture, float opacity) {
frame++;
if(frame > 6)
frame = 0;
float frameAdjustment = (opacity);
opacity /= OVERLAY_OPACITY_ADJUSTEMENT;
float amountMoved = ((float)frame)/6F;
float up = amountMoved;
float down = 1*amountMoved + 1f/6f;
float left = frameAdjustment;
float right = 1-frameAdjustment;
/*
up = up+frameAdjustment;
down = down-frameAdjustment;
*/
RenderSystem.disableDepthTest();
RenderSystem.depthMask(false);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, opacity);
RenderSystem.setShaderTexture(0, texture);
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferBuilder = tessellator.getBuffer();
bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
bufferBuilder.vertex(0.0D, (double)this.scaledHeight, -90.0D).texture(left, up).next(); //Upper left hand corner
bufferBuilder.vertex((double)this.scaledWidth, (double)this.scaledHeight, -90.0D).texture(right, up).next(); //Upper right hand corner
bufferBuilder.vertex((double)this.scaledWidth, 0.0D, -90.0D).texture(right, down).next(); //Lower left hand corner
bufferBuilder.vertex(0.0D, 0.0D, -90.0D).texture(left, down).next();//Lower right hand corner.
tessellator.draw();
RenderSystem.depthMask(true);
RenderSystem.enableDepthTest();
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.disableBlend();
}
}

View file

@ -0,0 +1,42 @@
package org.dimdev.dimdoors.mixin.client;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.JsonEffectGlShader;
import net.minecraft.client.gl.PostProcessShader;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.ModConfig;
import org.dimdev.dimdoors.world.level.component.PlayerModifiersComponent;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(PostProcessShader.class)
public class PostProcessShaderMixin {
private ModConfig.Player config = DimensionalDoorsInitializer.getConfig().getPlayerConfig();
@Shadow @Final private JsonEffectGlShader program;
@Inject(method = "Lnet/minecraft/client/gl/PostProcessShader;render(F)V", at = @At("HEAD"), cancellable = true)
public void render(float time, CallbackInfo cir) {
if(program.getName().equals("fray")) {
if (PlayerModifiersComponent.getFray(getCameraPlayer()) > config.fray.grayScreenFray) {
float overlayOpacity = (config.fray.grayScreenFray - PlayerModifiersComponent.getFray(getCameraPlayer())) / (config.fray.grayScreenFray - (float) config.fray.maxFray);
program.getUniformByNameOrDummy("FrayIntensity").set(overlayOpacity);
} else {
program.getUniformByNameOrDummy("FrayIntensity").set(0.0f);
}
}
}
private PlayerEntity getCameraPlayer() {
return !(MinecraftClient.getInstance().getCameraEntity() instanceof PlayerEntity) ? null : (PlayerEntity)MinecraftClient.getInstance().getCameraEntity();
}
}

View file

@ -0,0 +1,69 @@
{
"targets": [
"water",
"translucent",
"itemEntity",
"particles",
"clouds",
"weather",
"final"
],
"passes": [
{
"name": "transparency",
"intarget": "minecraft:main",
"outtarget": "final",
"auxtargets": [
{
"name": "DiffuseDepthSampler",
"id": "minecraft:main:depth"
},
{
"name": "TranslucentSampler",
"id": "translucent"
},
{
"name": "TranslucentDepthSampler",
"id": "translucent:depth"
},
{
"name": "ItemEntitySampler",
"id": "itemEntity"
},
{
"name": "ItemEntityDepthSampler",
"id": "itemEntity:depth"
},
{
"name": "ParticlesSampler",
"id": "particles"
},
{
"name": "ParticlesDepthSampler",
"id": "particles:depth"
},
{
"name": "CloudsSampler",
"id": "clouds"
},
{
"name": "CloudsDepthSampler",
"id": "clouds:depth"
},
{
"name": "WeatherSampler",
"id": "weather"
},
{
"name": "WeatherDepthSampler",
"id": "weather:depth"
}
]
},
{
"name": "fray",
"intarget": "final",
"outtarget": "minecraft:main"
}
]
}

View file

@ -0,0 +1,105 @@
#version 150
//make this a uniform when implementing in practice.
//ranges from 0 to 1.
//note: the shader should not be used when the effect strength is 0,
//because this will cause division-by-zero issues inside the shader.
uniform float FrayIntensity;
uniform sampler2D DiffuseSampler;
uniform vec2 OutSize;
in vec2 texcoord;
out vec4 fragColor;
float square(float f) { return f * f; }
vec2 square(vec2 v) { return v * v; }
float smoothify(float f) { return f * f * (f * -2.0 + 3.0); }
vec2 smoothify(vec2 v) { return v * v * (v * -2.0 + 3.0); }
float unmix(float low, float high, float frac) { return (frac - low) / (high - low); }
vec2 unmix(vec2 low, vec2 high, vec2 frac) { return (frac - low) / (high - low); }
float hash11(float p) { //from https://www.shadertoy.com/view/4djSRW
p = fract(p * 10.31);
p *= p + 33.33;
p *= p + p;
return fract(p);
}
float noise11(float coord, float wrap) {
float fracCoord = fract(coord);
vec2 corners;
corners.x = mod(coord - fracCoord, wrap);
corners.y = mod(corners.x + 1.0, wrap);
fracCoord = smoothify(fracCoord);
return mix(hash11(corners.x), hash11(corners.y), fracCoord);
}
vec2 hash22(vec2 p) { //from https://www.shadertoy.com/view/4djSRW
vec3 p3 = fract(vec3(p.xyx) * vec3(10.31, 10.30, 9.73));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec2 noise22(vec2 coord) {
vec2 fracCoord = fract(coord);
vec4 corners;
corners.xy = coord - fracCoord;
corners.zw = corners.xy + vec2(1.0);
fracCoord = smoothify(fracCoord);
return mix(
mix(
hash22(corners.xy),
hash22(corners.xw),
fracCoord.y
),
mix(
hash22(corners.zy),
hash22(corners.zw),
fracCoord.y
),
fracCoord.x
);
}
void main(){
//-1 to +1 instead of 0 to 1.
vec2 signedTexcoord = texcoord * 2.0 - 1.0;
//0 in the center, 1 on the edges.
//this is not a euclidean distance.
float centerDistance = sqrt(1.0 - (1.0 - signedTexcoord.x * signedTexcoord.x) * (1.0 - signedTexcoord.y * signedTexcoord.y));
//offset the texcoord a bit so the lines are more wiggley.
vec2 offset = noise22(texcoord * 4.0);
offset += noise22(texcoord * 8.0) * 0.5;
offset += noise22(texcoord * 16.0) * 0.25;
offset /= 1.0 + 0.5 + 0.25; //0 to 1
offset = offset * 2.0 - 1.0; //-1 to +1
//don't offset the texcoord in the exact middle of the screen,
//because we want the lines to converge on the center.
offset *= centerDistance;
//0.03125 is the amount of wiggleyness to apply.
vec2 tc = texcoord + offset * 0.03125;
//texcoord, in polar coordinates, ranging from 0 to 1 instead of -pi to +pi.
float angle = atan(tc.y - 0.5, tc.x - 0.5) * (0.5 / 3.14159265359) + 0.5;
//noise used for the lines.
//this is one-dimensional noise, where the angle is the
float noise = noise11(angle * 64.0, 64.0);
noise += noise11(angle * 128.0, 128.0) * 0.5;
noise += noise11(angle * 256.0, 256.0) * 0.25;
noise /= 1.0 + 0.5 + 0.25; //0 to 1
//4.0 controls the "initial" thickness of the lines.
//higher numbers will be thinner, and lower numbers will be thicker.
noise = abs(noise * 2.0 - 1.0) * 4.0;
//0 at the edges, and... more than 0 at the center.
float distanceFactor = unmix(1.0, 1.0 - FrayIntensity, centerDistance);
float finalMultiplier = (noise + distanceFactor) * (1.0 - FrayIntensity);
fragColor = texture(DiffuseSampler, texcoord);
fragColor.rgb *= min(finalMultiplier, 1.0);
}

View file

@ -0,0 +1,14 @@
{
"blend": { "func": "add", "srcrgb": "one", "dstrgb": "zero" },
"vertex": "fray",
"fragment": "fray",
"attributes": [ "Position" ],
"samplers": [
{ "name": "DiffuseSampler" }
],
"uniforms": [
{ "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": "OutSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] },
{ "name": "FrayIntensity", "type": "float", "count": 1, "values": [ 0.1 ] }
]
}

View file

@ -0,0 +1,15 @@
#version 150
in vec4 Position;
uniform mat4 ProjMat;
uniform vec2 OutSize;
out vec2 texcoord;
void main(){
vec4 outPos = ProjMat * vec4(Position.xy, 0.0, 1.0);
gl_Position = vec4(outPos.xy, 0.2, 1.0);
texcoord = Position.xy / OutSize;
}

View file

@ -34,7 +34,8 @@
"client.GameRendererMixin",
"client.InGameHudMixin",
"client.WorldRendererMixin",
"client.accessor.RenderLayerAccessor"
"client.accessor.RenderLayerAccessor",
"client.PostProcessShaderMixin"
],
"injectors": {
"defaultRequire": 1