• April 14, 2021, 03:51:17 PM

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

0 Members and 1 Guest are viewing this topic.

#### 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

(
texture2d<float, access::write> outTexture  [[texture(1)]],
device Control &control  [[buffer(0)]],
)
{
const int2 oList[] = { {-1,-1}, {0,-1}, {1,-1}, {-1,0}, {+1,0}, {-1,+1}, {0,+1}, {+1,+1} };

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);

bCount += 1;
}
}

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

« Last Edit: January 27, 2021, 03:57:22 PM by kosalos, Reason: spirce code not displayed correctly »

#### kosalos

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

### Similar Topics

###### depth of field problem

Started by Tsarvek on Mandelbulb3d

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

Started by Micha1982 on Mandelbulber

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

Started by mclarekin on Fractal Image Gallery

1 Replies
393 Views
January 29, 2018, 06:35:32 AM
by M Benesi
###### Rendering depth buffer from DE

Started by utak3r on Synthclipse

6 Replies
530 Views
March 05, 2018, 08:43:57 AM
by utak3r
###### Moire effect

Started by alber on Fractal Mathematics And New Theories

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