From 3b12a88a76039e9761add05af2805931f4468ba6 Mon Sep 17 00:00:00 2001 From: Valerie Date: Sun, 14 Apr 2024 13:28:52 -0400 Subject: [PATCH] initial implementation of DoF --- shaders/composite.fsh | 10 +++++++--- shaders/lang/en_US.lang | 2 ++ shaders/module/dof.frag | 19 +++++++++++++++++++ shaders/shaders.properties | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 shaders/module/dof.frag diff --git a/shaders/composite.fsh b/shaders/composite.fsh index 345991b..8173208 100644 --- a/shaders/composite.fsh +++ b/shaders/composite.fsh @@ -1,5 +1,7 @@ #version 120 +#define pixelSize 2 // the size of pixels [1 2 4 8 16] + #define dithering // whether or not to apply dithering #define colorMode 0 // hsv/rgb [0 1] @@ -9,14 +11,14 @@ #define rgbSteps 4 // the number of rgb values to use [2 4 8 16 32 64] -#define pixelSize 2 // the size of pixels [1 2 4 8 16] - uniform sampler2D gcolor; uniform sampler2D colortex1; uniform float viewWidth, viewHeight; varying vec2 texcoord; +#include "/module/dof.frag" + // All components are in the range [0…1], including hue. vec3 rgb2hsv(vec3 c) { @@ -116,7 +118,9 @@ float dither(float color, float dithersteps) { void main() { // adjust texture coordinate based on pixel size if needed vec2 newcoord = texcoord; - #if pixelSize > 1 + #ifdef dof + newcoord = depthOfField(); + #elif pixelSize > 1 vec2 view = vec2(viewWidth, viewHeight) / float(pixelSize); float offset = (ceil(pixelSize * 0.5) - 0.5) / float(pixelSize); newcoord = (floor(newcoord * view) + offset) / view; diff --git a/shaders/lang/en_US.lang b/shaders/lang/en_US.lang index f06b118..b4ab046 100644 --- a/shaders/lang/en_US.lang +++ b/shaders/lang/en_US.lang @@ -36,6 +36,8 @@ option.tWarp=Texture Warping option.tWarp.comment=Emulates affine texture mapping responsible for warping textures on the PSX screen.FX=FX +option.dof=Depth of Field +option.dof.comment=Downscale out-of-focus objects option.worldCurvature=World Curvature value.worldCurvature.0=Off value.worldCurvature.1=Chunk diff --git a/shaders/module/dof.frag b/shaders/module/dof.frag new file mode 100644 index 0000000..18e830f --- /dev/null +++ b/shaders/module/dof.frag @@ -0,0 +1,19 @@ + +//#define dof + +uniform float centerDepthSmooth; +uniform sampler2D depthtex1; + +const float centerDepthSmoothHalfLife = 16f; + +vec2 depthOfField() { + float depth = texture2D(depthtex1, texcoord).x; + float distance = depth - centerDepthSmooth; + int stops = min(int(distance * 96), 5); + + float virtualSize = pow(float(pixelSize), 1 + stops);//1 + stops); + vec2 view = vec2(viewWidth, viewHeight) / virtualSize; + float offset = (ceil(virtualSize * 0.5) - 0.5) / virtualSize; + return (floor(texcoord * view) + offset) / view; +} + diff --git a/shaders/shaders.properties b/shaders/shaders.properties index 8ac1888..5aea619 100644 --- a/shaders/shaders.properties +++ b/shaders/shaders.properties @@ -19,7 +19,7 @@ screen.COLOR=colorMode dithering rgbSteps hueSteps satSt screen.PSX=vWarp tWarp # fx -screen.FX=worldCurvature +screen.FX=dof worldCurvature # -- CONDITIONALS -- gbuffers_hand.enabled=tWarp