From 6f6af535536b96d5accecfc758bdbe625442e762 Mon Sep 17 00:00:00 2001 From: Imitater967 Date: Mon, 8 Apr 2024 00:00:40 +0800 Subject: [PATCH] refactor(vignette): use shader to create vignette instead of texture (#80) * adjust vignette value to improve effect --- assets/shaders/post_frag.glsl | 14 ++++++++--- .../dag/nodes/FinalPostProcessingNode.java | 25 ++++++++----------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/assets/shaders/post_frag.glsl b/assets/shaders/post_frag.glsl index 18f1997..1220486 100644 --- a/assets/shaders/post_frag.glsl +++ b/assets/shaders/post_frag.glsl @@ -30,8 +30,9 @@ uniform mat4 prevViewProjMatrix; #endif #ifdef VIGNETTE -uniform sampler2D texVignette; -uniform vec3 tint = vec3(1.0,1.0,1.0); +uniform float vignetteRadius = 0.8f; +uniform float vignetteFeather = 0.5f; +uniform vec3 vignetteTint = vec3(1.0,0.0,0.0); #endif layout(location = 0) out vec4 outColor; @@ -120,8 +121,13 @@ void main() { finalColor.rgb = texture(texColorGradingLut, lutScale * finalColor.rgb + lutOffset).rgb; #ifdef VIGNETTE - float vig = texture(texVignette, v_uv0.xy).x; - finalColor.rgb *= vec3(vig, vig, vig) + (1 - vig) * tint.rgb; + vec2 newUV = v_uv0.xy * 2 - 1; + float circle = length(newUV); + float mask = 1 - smoothstep(vignetteRadius, vignetteRadius + vignetteFeather, circle); + float invertMask = 1 - mask; + vec3 displayColor = finalColor.rgb * mask; + vec3 vignetteColor = (finalColor.rgb) * vignetteTint * invertMask; + finalColor.rgb = displayColor + vignetteColor; #endif outColor.rgba = finalColor; diff --git a/src/main/java/org/terasology/corerendering/rendering/dag/nodes/FinalPostProcessingNode.java b/src/main/java/org/terasology/corerendering/rendering/dag/nodes/FinalPostProcessingNode.java index 776c7f8..fb65427 100644 --- a/src/main/java/org/terasology/corerendering/rendering/dag/nodes/FinalPostProcessingNode.java +++ b/src/main/java/org/terasology/corerendering/rendering/dag/nodes/FinalPostProcessingNode.java @@ -70,14 +70,20 @@ public class FinalPostProcessingNode extends AbstractNode implements PropertyCha @Range(min = 0.0f, max = 1.0f) private float filmGrainIntensity = 0.05f; + @SuppressWarnings("FieldCanBeLocal") + @Range(min = 0.0f, max = 1.0f) + private float vignetteFeather = 0.6f; + @SuppressWarnings("FieldCanBeLocal") + @Range(min = 0.0f, max = 1.0f) + private float vignetteRadius = 0.9f; + private Vector3f vignetteTint = new Vector3f(.0f, .0f, .0f); + private FBO lastUpdatedGBuffer; private StateChange setBlurTexture; private StateChange setNoiseTexture; - private StateChange setVignetteInputTexture; private final Mesh renderQuad; - private final Vector3f tint = new Vector3f(.0f, .0f, .0f); public FinalPostProcessingNode(String nodeUri, Name providingModule, Context context) { @@ -128,17 +134,11 @@ public void setDependencies(Context context) { // TODO: evaluate the possibility to use GPU-based noise algorithms instead of CPU-generated textures. setNoiseTexture = new SetInputTexture2D(texId++, TextureUtil.getTextureUriForWhiteNoise(NOISE_TEXTURE_SIZE, 0x1234, 0, 512).toString(), POST_MATERIAL_URN, "texNoise"); - setVignetteInputTexture = new SetInputTexture2D(texId++, "engine:vignette", POST_MATERIAL_URN, "texVignette"); - if (renderingConfig.getBlurIntensity() != 0) { addDesiredStateChange(setBlurTexture); } - if (renderingConfig.isVignette()) { - addDesiredStateChange(setVignetteInputTexture); - } - if (renderingConfig.isFilmGrain()) { addDesiredStateChange(setNoiseTexture); } @@ -169,7 +169,9 @@ public void process() { } if (renderingConfig.isVignette()) { - postMaterial.setFloat3("tint", tint); + postMaterial.setFloat3("vignetteTint", vignetteTint); + postMaterial.setFloat("vignetteRadius", vignetteRadius); + postMaterial.setFloat("vignetteFeather", vignetteFeather); } this.renderQuad.render(); @@ -193,11 +195,6 @@ public void propertyChange(PropertyChangeEvent event) { } break; case RenderingConfig.VIGNETTE: - if (renderingConfig.isVignette()) { - addDesiredStateChange(setVignetteInputTexture); - } else { - removeDesiredStateChange(setVignetteInputTexture); - } break; case RenderingConfig.BLUR_INTENSITY: if (renderingConfig.getBlurIntensity() != 0) {