Recreating GLSL code in TOPs (excluding the GLSL TOP)

I’m trying to recreate GLSL code in TOPs.

// Uniforms
uniform vec2 iResolution;             // Output resolution      

// Sample the luminance-like value: length of RGB vector at a UV coordinate
float getLuminanceMagnitude(vec2 uv)
{
    vec3 rgb = texture(sTD2DInputs[0], uv).rgb;
    return length(rgb); // Gives a single scalar representing intensity
}

// Compute the gradient of luminance using central differences
vec2 computeLuminanceGradient(vec2 uv, float delta)
{
    vec2 offset = vec2(delta, 0.0);
    float dx = getLuminanceMagnitude(uv + offset.xy) - getLuminanceMagnitude(uv - offset.xy);
    float dy = getLuminanceMagnitude(uv + offset.yx) - getLuminanceMagnitude(uv - offset.yx);
    return vec2(dx, dy) / (delta*2.);
}

out vec4 fragColor;

void main()
{
    vec2 uv = gl_FragCoord.xy / iResolution.xy;
    
    

    // Compute luminance gradient
    float pixel_delta = 1.0 / iResolution.y;
    vec2 luminance_gradient = computeLuminanceGradient(uv, pixel_delta);
    
    fragColor = vec4(luminance_gradient,1.,1.);
}

I created a TOPs network where I use transforms to offset the image in the x direction (1 and -1) and subtract them to create dx, and the same for dy. But the output is very different. The output of the GLSL code has more detail. How do I get the TOPs network to do something similar or closer to the GLSL code? Here is a toe with the TOPs network and the GLSL TOP.
normal_difference.5.toe (5.2 KB)

Hi there! :smiley:

There are a few things happening. One thing that’s important when working with data besides colors inside TOPs is to set the bits to more than 8bit. Right now your length calculation are 8 bit which makes the values clamp between 0 and 1.
Other thing is that you multiply the derivatives with the res/2 inside your TOP network. This should be just 1/2 since your derivative is also in pixel space. You can validate how it looks by looking at the ‘normalize view’ (pressing ‘n’ when a top is selected).

See toe file for what I changed.

(side node, the vec2 uv you calculate in the shader is by default supplied by touchdesigner as vUV.st. And the iResolution.xy as uTDOutputInfo.res.zw).

Cheers,
tim

ps. also I switched the subtraction in the shader

normal_difference.6.toe (5.5 KB)

1 Like