Skip to content

Instantly share code, notes, and snippets.

@antoinefortin
Created August 25, 2021 05:10
Show Gist options
  • Select an option

  • Save antoinefortin/f5eac28a1e5cfb1fb629c576f8bcfca5 to your computer and use it in GitHub Desktop.

Select an option

Save antoinefortin/f5eac28a1e5cfb1fb629c576f8bcfca5 to your computer and use it in GitHub Desktop.
kernel UVmap : ImageComputationKernel<ePixelWise>
{
Image<eRead,eAccessRandom, eEdgeClamped> src; //I
Image<eWrite> dst; //O
local:
float width;
float height;
void init() {
width = src.bounds.x2; //function to find right edge
height = src.bounds.y2; //function to find top edge
}
float sphere(float3 pos, float r)
{
return length(pos) - r;
}
float map(float3 pos)
{
float3 q = pos;
q.x = fmod(q.x + 4.0f, 8.0f) - 4.0f;
q.z = fmod(q.z + 4.0f, 8.0f) - 4.0f;
q.y = fmod(q.y + 4.0f, 8.0f) - 4.0f;
float d = sphere(q, 1.5f);
return d;
}
float3 computeNormal(float3 pos)
{
return normalize(float3(
map(pos + float3(0.1f, 0.0f, 0.0f)) - map(pos - float3(0.1f, 0.0f, 0.0f)),
map(pos + float3(0.0f, 0.1f, 0.0f)) - map(pos - float3(0.0f, 0.1f, 0.0f)),
map(pos + float3(0.0f, 0.0f, 0.1f)) - map(pos - float3(0.0f, 0.0f, 0.1f))
));
}
void process(int2 pos) {
float2 fg = float2(pos.x, pos.y);
float2 ires = float2(width, height);
// (fragCoord -.5 * iResolution.xy) / iResolution.y;
float2 uv = (fg - .5f * ires)/ires.y;
float3 rd = normalize(float3(uv,1.0));
float3 pos2 = float3(25.0f, 5.0f, -15.0f);
float3 color = float3(1.0f,0.0f, 0.0f);
color = float3(uv.x, uv.y, uv.x * uv.y);
for(int i=0; i < 512; i++)
{
float d = map(pos2);
if(d < 0.01)
{
color = float3(1.0f);
float3 nws = computeNormal(pos2);
float3 sunPos = float3(-2.f, 2.f, -4.f);
float sunFactor = dot(normalize(nws), normalize(sunPos));
float3 basicColor = float3(.2f, .3f, .88f);
color = basicColor * sunFactor * normalize(pos2);
}
pos2 += d * rd;
}
//rendering with a fog calculation (further is darker)
dst(0) = color.x;
dst(1) = color.y;
dst(2) = color.z;
dst(3) = 1.0f;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment