Skip to content

Commit

Permalink
Add blur
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack253-png committed Sep 21, 2024
1 parent 1dcbb24 commit 13b0642
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.primogemstudio.advancedfmk.flutter

import com.mojang.blaze3d.pipeline.TextureTarget
import com.mojang.blaze3d.platform.GlStateManager
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.BufferUploader
import com.mojang.blaze3d.vertex.DefaultVertexFormat
import com.mojang.blaze3d.vertex.VertexFormat
import com.primogemstudio.advancedfmk.flutter.Shaders.POST_BLUR
import net.minecraft.client.Minecraft
import java.lang.ref.Cleaner

class FlutterInstance(assets: String?, val rect: FlutterRect, var width: Int, var height: Int) :
AutoCloseable {
class FlutterInstance(assets: String?, val rect: FlutterRect, var width: Int, var height: Int): AutoCloseable {
companion object {
val ITARGET = TextureTarget(1, 1, false, false).apply {
setClearColor(0f, 0f, 0f, 0f)
clear(false)
}
}
private val cleaner: Cleaner.Cleanable
val handle: Long = FlutterNative.createInstance(assets)
var pressed: Boolean = false
Expand Down Expand Up @@ -47,13 +54,16 @@ class FlutterInstance(assets: String?, val rect: FlutterRect, var width: Int, va
get() = FlutterNative.getTexture(handle)

fun renderToScreen() {
ITARGET.clear(false)
ITARGET.bindWrite(true)

val x = rect.left
val y = Minecraft.getInstance().window.height - rect.bottom
val width = rect.right - rect.left
val height = rect.bottom - rect.top
val w = Minecraft.getInstance().window.width
val h = Minecraft.getInstance().window.height
GlStateManager._colorMask(true, true, true, false)
GlStateManager._colorMask(true, true, true, true)
GlStateManager._disableDepthTest()
GlStateManager._depthMask(false)
GlStateManager._viewport(x, y, width, height)
Expand All @@ -72,6 +82,10 @@ class FlutterInstance(assets: String?, val rect: FlutterRect, var width: Int, va
GlStateManager._disableBlend()
GlStateManager._depthMask(true)
GlStateManager._colorMask(true, true, true, true)

POST_BLUR.setSamplerUniform("InputSampler", ITARGET)
POST_BLUR.render(0f)
// -Dorg.lwjgl.glfw.libname=/usr/lib/libglfw.so
}

override fun close() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.primogemstudio.advancedfmk.flutter

import com.primogemstudio.advancedfmk.flutter.FlutterInstance.Companion.ITARGET
import java.util.function.Consumer

object FlutterViewEvent {
Expand All @@ -11,5 +12,6 @@ object FlutterViewEvent {
i.rect.bottom = (height + i.height) / 2
i.sendSizeEvent()
})
ITARGET.resize(width, height, false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package com.primogemstudio.advancedfmk.flutter
import com.mojang.blaze3d.vertex.DefaultVertexFormat
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.ShaderInstance
import net.minecraft.resources.ResourceLocation
import org.ladysnake.satin.api.managed.ShaderEffectManager


object Shaders {
@JvmField
val BLIT_NO_FLIP = ShaderInstance(
Minecraft.getInstance().resourceManager, "blit_no_flip", DefaultVertexFormat.POSITION_COLOR
)

@JvmField
val POST_BLUR = ShaderEffectManager.getInstance().manage(ResourceLocation.withDefaultNamespace("shaders/filter/gaussian_blur.json"))
}

Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.primogemstudio.advancedfmk.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import com.primogemstudio.advancedfmk.flutter.FlutterInstance;
import com.primogemstudio.advancedfmk.flutter.FlutterRect;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.renderer.GameRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(RenderSystem.class)
@Mixin(GameRenderer.class)
public abstract class RenderSystemMixin {
@Unique
private static FlutterInstance instance;

@Inject(method = "flipFrame", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwSwapBuffers(J)V"), remap = false)
private static void flipFrame(long l, CallbackInfo ci) {
@Inject(method = "render", at = @At(value = "RETURN"))
private void flipFrame(DeltaTracker deltaTracker, boolean renderLevel, CallbackInfo ci) {
if (instance == null) {
instance = new FlutterInstance("/home/coder2/flutter/flutter_demo/build/linux/x64/release/bundle/data/flutter_assets", new FlutterRect(), 600, 800);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 150

uniform sampler2D DiffuseSampler;

in vec2 texCoord;

out vec4 fragColor;

void main() {
fragColor = texture(DiffuseSampler, texCoord);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"vertex": "blit_no_flip",
"fragment": "blit_screen",
"fragment": "blit_no_flip",
"samplers": [
{
"name": "DiffuseSampler"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"targets": [
"swap"
],
"passes": [
{
"name": "gaussian_blur",
"intarget": "minecraft:main",
"outtarget": "swap",
"uniforms": [
]
},
{
"name": "blit",
"intarget": "swap",
"outtarget": "minecraft:main"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#version 150

uniform sampler2D DiffuseSampler;
uniform sampler2D InputSampler;

uniform vec4 ColorModulate;
uniform int Radius;
uniform int DigType;
uniform float NoiseStrength;

in vec2 texCoord;
in vec2 oneTexel;

out vec4 fragColor;

#define PI2 6.2831853072

vec4 blur(int samples) {
vec4 O = vec4(0.0);
float r = float(samples)*0.5;
float sigma = r*0.5;
float f = 1./(6.28318530718*sigma*sigma);

int s2 = samples*samples;
for (int i = 0; i<s2; i++) {
vec2 d = vec2(i%samples, i/samples) - r;
O += texture(DiffuseSampler, texCoord + oneTexel * d) * exp(-0.5 * dot(d/=sigma, d)) * f;
}
// use pre-multiplied alpha
return O/O.a;
}

vec4 blur_dig2(int samples) {
float count = 1.0;
vec4 color = texture(DiffuseSampler, texCoord);
float directionStep = PI2 / 48;

vec2 off;
float c, s, dist, dist2, weight;
for (float d = 0.0; d < PI2; d += directionStep) {
c = cos(d);
s = sin(d);
dist = 1.0 / max(abs(c), abs(s));
dist2 = dist * 3.0;
off = vec2(c, s);
for (float i = dist2; i <= 32.0; i += dist2) {
weight = i / samples;
count += weight;
color += texture(DiffuseSampler, texCoord + off * oneTexel * i) * weight;
}
}

return color / count;
}

float random(vec2 st) {
return (fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123) - 0.5) * 2;
}

void main() {
vec4 col = texture(InputSampler, texCoord);
vec4 dst = texture(DiffuseSampler, texCoord);
if (col.a <= 0.01)
{
fragColor = dst * ColorModulate;
return;
}

col.a = 0.88;

fragColor = mix(DigType == 0 ? blur(Radius) : blur_dig2(Radius), vec4(col.xyz, 1.0), col.a);
fragColor.x += random(dst.xy * 0.03125 * texCoord) * NoiseStrength;
fragColor.y += random(dst.xz * 0.03125 * texCoord) * NoiseStrength;
fragColor.z += random(dst.yz * 0.03125 * texCoord) * NoiseStrength;
fragColor.a = dst.a;
fragColor *= ColorModulate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"blend": {
"func": "add",
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "gaussian_blur",
"fragment": "gaussian_blur",
"attributes": [
"Position"
],
"samplers": [
{
"name": "DiffuseSampler"
},
{
"name": "InputSampler"
}
],
"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": "InSize",
"type": "float",
"count": 2,
"values": [
1.0,
1.0
]
},
{
"name": "OutSize",
"type": "float",
"count": 2,
"values": [
1.0,
1.0
]
},
{
"name": "ColorModulate",
"type": "float",
"count": 4,
"values": [
1.0,
1.0,
1.0,
1.0
]
},
{
"name": "Radius",
"type": "int",
"count": 1,
"values": [
8
]
},
{
"name": "DigType",
"type": "int",
"count": 1,
"values": [
0
]
},
{
"name": "NoiseStrength",
"type": "float",
"count": 1,
"values": [
0
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 150

in vec4 Position;

uniform mat4 ProjMat;
uniform vec2 InSize;
uniform vec2 OutSize;

out vec2 texCoord;
out vec2 oneTexel;

void main(){
vec4 outPos = ProjMat * vec4(Position.xy, 0.0, 1.0);
gl_Position = vec4(outPos.xy, 0.2, 1.0);

oneTexel = 1.0 / InSize;

texCoord = Position.xy / OutSize;
}

0 comments on commit 13b0642

Please sign in to comment.