Depth of field effect in VEX




VEX, Rendering


Houdini 20.0


Using two grids as sensor and focus plane along with VEX to render primitive colors to a grid with depth blur.

1 Code

The point wrangle sets random samples around each sensor pixel defined by sample_circle_uniform within an aperture radius. The intersect function shoots rays at the corresponding point on the focus plane. Once all primitive colors are stored inside the clr[] array, their average avg is exported as v@Cd.

int samples = chi('samples');
float aper = chf('aperture');
int stoch = chi('stochastic');

vector clr[] = {};
for(int i = 0; i < samples; i++){
    float c = i / float(samples-1);
    vector2 u = set(1.0 - c, c);
    if(stoch == 1) u = rand(i@ptnum, i);
    vector2 a = sample_circle_uniform(u);
    vector pos = v@P + vector(a) * aper;
    vector dir = normalize(v@opinput1_P - pos);
    int prim_hit = intersect(2, pos, dir * 10.0, set(0), set(0));
    if(prim_hit >= 0){
        vector clr_hit = prim(2, 'Cd', prim_hit);
        append(clr, clr_hit);

v@Cd = avg(clr);