• April 14, 2021, 03:10:20 PM

Login with username, password and session length

Author Topic:  simple depth of field effect  (Read 136 times)

0 Members and 1 Guest are viewing this topic.

Offline kosalos

  • Fractal Friar
  • *
  • Posts: 142
simple depth of field effect
« on: January 27, 2021, 12:31:06 AM »
Simple depth of field blur effect.

My ray marching code stores pixel colors to a texture of RGBA values.
The alpha channel is unused, so the pixel depth can be stored there with no visual effect.
A second shader takes this original texture as input and creates an output texture with depth effect added.

Here is the Apple Metal rendition:

// add depth of field effect
// input pixel (rgba) :  rgb = color, a = distance from camera
// control.blurFocalDistance = focal distance;   0.001 ... 4
// control.blurStrength = size of color averaging effect;  1 ... 500
// control.xSize, ySize = dimensions of input and output textures

kernel void effectsShader
(
 texture2d<float, access::read> inTexture  [[texture(0)]],
 texture2d<float, access::write> outTexture  [[texture(1)]],
 device Control &control  [[buffer(0)]],
 uint2 p [[thread_position_in_grid]]
 )
{
    const int2 oList[] = { {-1,-1}, {0,-1}, {1,-1}, {-1,0}, {+1,0}, {-1,+1}, {0,+1}, {+1,+1} };

    float4 color = inTexture.read(p);
    int maxWidth = int(abs(color.a - control.blurFocalDistance) * control.blurStrength);
   
    if(maxWidth < 2) {
        outTexture.write(color,p);
        return;
    }
   
    float4 total = color;
    int bCount = 1;
    int2 q;

    for(int w = 1;w <= maxWidth; ++w) {
        for(int i=0;i<8;++i) {
            q = int2(p);
            q += oList * w;  // << not displayed correctly. Use "oList [ i ]"
            q.x = max(q.x,0);   q.x = min(q.x,control.xSize-1); // ensure we stay in bounds
            q.y = max(q.y,0);   q.y = min(q.y,control.ySize-1);

            total += inTexture.read(uint2(q));
            bCount += 1;
        }
    }

    total /= float(bCount);
    outTexture.write(total,p);
}


Linkback: https://fractalforums.org/index.php?topic=4005.0
« Last Edit: January 27, 2021, 03:57:22 PM by kosalos, Reason: spirce code not displayed correctly »

Offline kosalos

  • Fractal Friar
  • *
  • Posts: 142
Re: simple depth of field effect
« Reply #1 on: January 27, 2021, 03:58:06 PM »
another example


xx
depth of field problem

Started by Tsarvek on Mandelbulb3d

1 Replies
559 Views
Last post August 17, 2018, 06:36:21 PM
by muinami
xx
Depth of field usage

Started by Micha1982 on Mandelbulber

8 Replies
806 Views
Last post April 07, 2018, 07:44:31 AM
by Micha1982
xx
color by depth

Started by mclarekin on Fractal Image Gallery

1 Replies
392 Views
Last post January 29, 2018, 06:35:32 AM
by M Benesi
clip
Rendering depth buffer from DE

Started by utak3r on Synthclipse

6 Replies
529 Views
Last post March 05, 2018, 08:43:57 AM
by utak3r
clip
Moire effect

Started by alber on Fractal Mathematics And New Theories

4 Replies
436 Views
Last post February 22, 2019, 05:22:50 PM
by claude