Fractalforums

Fractal Software => Programming => Topic started by: kosalos on June 27, 2020, 06:42:31 AM

Title: Shadow Lines
Post by: kosalos on June 27, 2020, 06:42:31 AM
Add shadow lines to your 2D fractals.
1. calculate your fractal as usual, storing to a texture.
2. call a second compute shader that creates an altered copy with shadow lines added.

Here's an Apple Metal shader, but you get the idea...
--------------------------------------------------------------

kernel void shadowShader
(
 texture2d<float, access::read> src [[texture(0)]],        // your 2D fractal
 texture2d<float, access::write> dst [[texture(1)]],       // copy with shadow lines added
 constant Control &control [[buffer(0)]],                       // merely holds the size of the texture
 uint2 p [[thread_position_in_grid]])
{
    if(p.x > uint(control.xSize)) return; // screen size not evenly divisible by threadGroups
    if(p.y > uint(control.ySize)) return;
   
    float4 v = src.read(p);
   
    if(p.x > 1 && p.y > 1) {
        bool shadow = false;
       
        {
            uint2 p2 = p;
            p2.x -= 1;
            float4 vx = src.read(p2);
            if(v.x < vx.x || v.y < vx.y) shadow = true;
        }
       
        if(!shadow)
        {
            uint2 p2 = p;
            p2.y -= 1;
            float4 vx = src.read(p2);
            if(v.x < vx.x || v.y < vx.y) shadow = true;
        }
       
        if(!shadow)
        {
            uint2 p2 = p;
            p2.x -= 1;
            p2.y -= 1;
            float4 vx = src.read(p2);
            if(v.x < vx.x || v.y < vx.y) shadow = true;
        }
       
        if(shadow) {
            v.x /= 4;
            v.y /= 4;
            v.z /= 4;
        }
    }
   
    dst.write(v,p);
}