Make a Loading Circle using Shaders

Hey guys,

In this video, I explain how to implement a Loading Circle using Shaders. I use the Metal Shading Language, but the concept here applies to the OpenGL Shading Language as well.

Here is the video and the code for your convenience.

 
 

Code Snippet.

#include <metal_stdlib> using namespace metal;

//rotate space by angle float2x2 rotate2d(float _angle){ return float2x2(cos(_angle),-sin(_angle), sin(_angle),cos(_angle)); }

//Sign distance function of circle/ring float sdfCircle(float2 p, float2 c, float r) { return abs(r - length(p - c)); }

//Sign distance function of line float sdfLine( float2 p, float2 a, float2 b) { float2 pa = p - a, ba = b - a; float h = clamp(dot(pa,ba) / dot(ba,ba), 0., 1.);
return length(pa - ba * h); }

//sharpens the object float sharpen(float d, float w, float2 resolution) { float e = 1. / min(resolution.y , resolution.x); return 1. - smoothstep(-e, e, d - w); }

//computes the modulus float mod(float x, float y){ return x-y*floor(x/y); }

#define M_PI 3.1415926535897932384626433832795

fragment float4 fs_main( FS_INPUT In [[stage_in]], constant FS_UNIFORM& uniform [[buffer(16)]], texture2d texture0 [[texture(2)]], sampler texture0Smplr [[sampler(2)]]) { float2 st = -1. + 2. * In.v_texcoord; float3 color=float3(0.0); float m=0.0;

//scale and translate the space    
st*=3.0;
st.y+=2.0;

//Sample the texture
float4 myTexture=texture0.sample(texture0Smplr,In.v_texcoord);

//step 1. create a circle
float c=sdfCircle(st,float2(0.0,0.0),0.5);
c=sharpen(c,0.06,uniform.resolution);


//step 2. create a line and rotate the space
for(int i=0; i<15; i++){

    //make a copy of the space
    float2 st2=st;

    //rotate the space by 24 degrees
    st2=st2*rotate2d((i*24.0)*M_PI/180.0);

    //create line with the current space
    float l=sdfLine(st2,float2(0.0,0.0),float2(0.0,0.6));

    //sharpen the line
    l=sharpen(l,0.06,uniform.resolution);

    m+=min(l,c);

}


float a=-atan2(st.y,st.x);

a=mod(-uniform.time+0.5*(a/M_PI),1.0);

m*=a;

float4 finalColor=max(myTexture,m);

return finalColor;

}

Thanks for watching.

40 commonly asked questions by new Game Engine Dev

In this video, I answer 40 commonly asked questions by new Game Engine Developers.

 
 

Thanks for watching.

Game Engine Dev tips: Twitch Highlights #2

Hey guys, in this video, I share more game engine dev tips I shared during my live streams on Twitch. I hope you find them helpful.

 
 

Thanks for watching.

BTW, if you are an indie game engine developer and you are looking for a community of like-minded individuals, join my Discord Server. We share tips, insights, and help each other.

If you want to watch the development of the game engine live, join my Twitch Channel.

Game Engine Dev tips: Twitch Highlights

In this video, I share several game engine dev tips I shared during my live streams on Twitch.

 
 

Hope these tips are helpful.

BTW, join my Twitch Channel: https://www.twitch.tv/haroldserrano

Untold Engine Beta v0.0.13 - Release Notes

In this beta release, several issues with the Untold Engine were fixed. The major issue was the loading speed of 3D models. The Untold Engine no longer imports models from XML file types. Instead, a binary file format was developed and all 3D model is now imported from binary files. Doing so improved the loading speed significantly. The navigation path computed using the A* algorithm was also improved. The I/O controllers were also improved.

 
 

Join our Discord If you are an indie game engine developer and you are looking for a community of like-minded individuals, join my Discord. We share tips, insights, and help each other:

https://discordapp.com/invite/VwxyAMp

Thanks for reading.