Skip to main content

Featured

Learning To Work With Python Frameworks: FLASK vs DJANGO

                 Image:  https://www.bitcot.com/flask-vs-django/ Breakdown I had experience building with Python and other APIs, but I’d never worked with a web framework. I wanted to learn how a full-stack web application fits together, so I learned two of the most popular Python ones: Flask first, as the small and simple option, then Django, as the larger one with more built in. This post breaks down what I learned from each, and how they compare. Tools Python 3.14 VS Code Virtual Environments Flask Django The Use Cases For Each Flask Built for simple projects, with a small core and a minimal set of features. Easy to install and quick to get started with. Fast and lightweight, but not designed for high-workload projects. Built on a synchronous model, so each worker handles one request at a time. Handling several requests at once means running multiple workers or threads. Best for quick projects and prototyping, like getting a ...

Converting GLSL to HLSL For Custom Nodes

The Breakdown

Back In 2023 I started writing shaders on ShaderToy to better understand the GPU pipeline & how things render on screen. I got some cool results, grasped the fundamentals of GLSL and learned some graphics programming principles. Only had two problems though, GLSL didn't feel modern and it felt like my work was stuck on ShaderToy. What do I do now? Well, modern engines use HLSL, and GLSL and HLSL share a lot of commonalities in what they do, while having a few differences like data types, functions & entry points. The structure exists, all I needed to do was make it run in unreal engine. Should be easy enough, right?


The Problem

Data Types, Variables & Functions

The very first step was to convert all the data types & variables, in VS Code, then pasting code in the custom node and intentionally get errors. I figured, if I break it, then I know everything wrong with it and what to debug. If I don't know what an error means, I get to learn that too. A little brute force-ish? Maybe, but I know what to look for now. Big brain. The conversions were as follows:

vec2 => float2
vec3 => float3
vec4 => vec4
iTime => Time
iResolution => Resolution
fract => frac

Entry Points

Next, I removed main () function & it's global variables. All GLSL shaders had void mainImage( out vec4 fragColor, in vec2 fragCoord). All I needed to do, was get rid of that, fix my indentation, then let Unreal Engine handle that by creating nodes for those variables and connect them to the custom node. I also then defined all the variables within the main () function their respective nodes. All the important nodes are as follows:

TexCoord - To define the UV coordinates. These were then multiplied with a scalar and then added, so I could manipulate the shader look.
float2 - To define the 0 to 1 U and V space.
float3 - To define the colour.
Time - To define the animation over time.


#define macro

This was the first time I had ever encountered a #define macro, more specifically, Function-Like macros. What are Function-Like macros? Well, these work like normal functions and accept relevant parameters. They can only work when the arguments follow the macro name in parenthesis. This makes it one single line of legible code. Macros play really nice with custom nodes, instead of having 2 different custom nodes containing functions & the main body, respectively.



Code Examples:

GLSL
//Sphere funtion
float sdSphere(vec3 p, float s)
{
    return length(p) - s;
}

//BoxFunction
float sdBox( vec3 p, vec3 b )
{
  vec3 q = abs(p) - b;
  return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}

//SmoothFunction
float smin (float a, float b, float k)
{
    float h = max(k-abs(a-b), .5)/k;
    return min(a, b) - h*h*h*k*(1.0/6.0);
}

//RotationFunction
mat2 rot2D(float angle)
{
    float s = sin(angle);
    float c = cos(angle);
    return mat2(c, -s, s, c);
}


//Cosine based palette function, 4 vec3 params
vec3 palette(float t)

{
    vec3 a = vec3(.5, .5, .5);
    vec3 b = vec3(.8, .5, .5);
    vec3 c = vec3(1., 1., 1.);
    vec3 d = vec3(.8, .6, .5);
    return a + b*cos( 6.28318*(c*t+d) );
}


float map(vec3 Point)
{
    vec3 SpherePos = vec3(0., 0 , sin(iTime)* 1.);
    float Sphere = sdSphere(Point - SpherePos, .8);
   
   
    vec3 q = Point;
    //Whatever axis you omit, it will rotate by that axis
    q.xy *= rot2D(iTime);
   
    //Foward movement
    Point.z += iTime * .4;
   
    //Space repetition
    q = fract(Point) - .5;
   
   
    //Multiplying the point by a value distorts the shape. Divide the output to reduce
    artifacts
    //float Box = sdBox(Point* 1., vec3(.75))/ 4.;
                            //Scaling cube size
    float Box = sdBox(q, vec3(.05));
   
    //Adding the ground for blending
    //Positive value determine how much we push the ground down
    float Ground = Point.z + 1.;
   
    return smin(Ground, smin(Sphere, Box, 5.5), 1.);
 
}

HLSL
#define sdSphere(p, s) (length(p) - s)
#define sdBox(p, b) (length(max(abs(p)-b, 0.0)) + min(max((abs(p)-b).x,
max((abs(p)-b).y, (abs(p)-b).z)), 0.0))
#define smin(a, b, k) (min(a,b) - pow(max(k-abs((a)-(b)), 0.5)/k, 3.0) * k * (1.0/6.0))
#define rot2D(angle) (float2x2(cos(angle), -sin(angle), sin(angle), cos(angle)))
#define Palette(t) (float3(.5, .5, .5) + float3(.8, .5, .5) * -cos(6.28318 *
(float3(1.0, 1.0, 1.0) * (t) + float3(.8, .6, .5))))
#define map(Point) (smin(sdSphere((Point) - float3(0., 0., sin(Time) * 1.), .8),
sdBox(frac((Point) + float3(0., 0., Time * .4)) - .5, float3(.05, .05, .05)), 5.5))


What I Learned

#define macros are an easier way to wrap a function into a simple, readable single line of code. Understanding order of operations in macros is very important for translation. Macros are much more reliable when working with custom nodes.

Avoid naming variables similar to built-in HLSL functions. Lastly, knowing the difference between how data types & functions are handled between GLSL & HLSL is very important, especially for main () functions & global variables.

Additional Shaders





Conclusion

Surprisingly, it was a bit easier than the assumed difficulty. The biggest lesson was learning how to work with a #define macro, as it was a great way to simplify functions into one line for readability. Having a surface level understanding of C/C++, it was definitely a new concept. Not too bad of an experience.


Final code

#define sdSphere(p, s) (length(p) - s)
#define sdBox(p, b) (length(max(abs(p)-b, 0.0)) + min(max((abs(p)-b).x, max((abs(p)-b).y, (abs(p)-b).z)), 0.0))
#define smin(a, b, k) (min(a,b) - pow(max(k-abs((a)-(b)), 0.5)/k, 3.0) * k * (1.0/6.0))
#define rot2D(angle) (float2x2(cos(angle), -sin(angle), sin(angle), cos(angle)))
#define Palette(t) (float3(.5, .5, .5) + float3(.8, .5, .5) * -cos(6.28318 * (float3(1.0, 1.0, 1.0) * (t) + float3(.8, .6, .5))))
#define map(Point) (smin(sdSphere((Point) - float3(0., 0., sin(Time) * 1.), .8), sdBox(frac((Point) + float3(0., 0., Time * .4)) - .5, float3(.05, .05, .05)), 5.5))


// Normalized pixel coordinates (from 0 to 1)
    float2 uv =(fragCoord * 2. - Resolution.xy) / Resolution.y;

    //Intialization
    // Ray origin
    float3 RayOrigin = float3(0, 0, -4);

    // Ray direction    //This will allow us to control the perspective
    float3 RayDirection = normalize(float3(uv * .9, 1));
    // Pixel colour
    float3 Colour = float3(0.0, 0.0, 0.0);

    // Total distance travelled
    float TotalDistance = 0.;

    int i = 0;
    for (int i = 0; i < 85; i++)
    {
        // Raymarching
        float3 Point = RayOrigin + RayDirection * TotalDistance;

        //Wiggle the ray
        Point.y += sin(TotalDistance)* .1;
        // Current distance
        float Distance = map(Point);
        TotalDistance += Distance;

        // Breaks so the GPU doesn't calculator more than it needs to
        if (Distance < .001) break;
        if (TotalDistance > 100.) break;
    }

   //Colour = palette(TotalDistance *.1);
   Colour = Palette(TotalDistance* .04 + float(i)* .005);
return float4(Colour, 1);


GitHub Repo


ShaderToy


Comments