• April 20, 2021, 09:31:05 AM

Login with username, password and session length

Author Topic:  Add texture to any 3D fractal  (Read 166 times)

0 Members and 1 Guest are viewing this topic.

Offline kosalos

  • Fractal Friar
  • *
  • Posts: 142
Add texture to any 3D fractal
« on: June 30, 2020, 08:34:33 PM »
I'm sure there are better mapping algorithms, but this works well

1. supply an image to the compute shader
(Apple Metal shader, but you get the idea)

bool   c.txtOnOff                       // use texture?
float2 c.txtSize                        // size of texture image in pixels
float   c.tCenterX,c.tCenterY     // scroll amount    0.01 ... 1
float   c.tScale                        // texture scaling   0.01 ... 1

in your ray marching shader, after the DE() call,  where you have the DE() distance for the current pixel:

kernel void rayMarchShader
(
 texture2d<float, access::write> outTexture     [[texture(0)]],
 texture2d<float, access::read> coloringTexture [[texture(1)]],
 device Control &c      [[ buffer(0) ]],
 uint2 p [[thread_position_in_grid]]   )
{
...    float distance = DE() for current pixel


        float3 position = camera + distance * direction;
        float3 normal = calcNormal(position,0.057,c);

        // use texture
        if(c.txtOnOff) {
            float scale = c.tScale;
            float len = length(position) / distance;
            float x = normal.x / len;
            float y = normal.z / len;
            float w = c.txtSize.x;
            float h = c.txtSize.y;
            float xx = w + (c.tCenterX * 4 + x * scale) * (w + len);
            float yy = h + (c.tCenterY * 4 + y * scale) * (h + len);
           
            uint2 pt;
            pt.x = uint(fmod(xx,w));
            pt.y = uint(c.txtSize.y - fmod(yy,h));    // flip Y coord
            color = coloringTexture.read(pt).xyz;    //  extract pixel color from texture
        }


Linkback: https://fractalforums.org/index.php?topic=3634.0


There are no comments for this topic. Do you want to be the first?