Skip to content

Instantly share code, notes, and snippets.

@abesmon
Created February 21, 2021 21:37
Show Gist options
  • Save abesmon/5701e55e431f2ece2f018371cfebb0f6 to your computer and use it in GitHub Desktop.
Save abesmon/5701e55e431f2ece2f018371cfebb0f6 to your computer and use it in GitHub Desktop.
SDF but that is calculated to the origin of mesh. It can be used to create virtual space with different sdf shaped inside of any mesh
uniform float uTime;
uniform sampler2D frontTex;
in v2f {
flat int cameraIndex;
vec4 pos;
vec3 originalP;
vec2 uv;
mat3 tbn;
} iVert;
out vec4 fragColor;
vec4 over(vec4 front, vec4 backv) {
return mix(backv, front, front.a);
}
float distance_from_sphere(in vec3 p, in vec3 c, float r)
{
return length(p - c) - r;
}
float map_the_world(in vec3 p)
{
float displacement = sin(5.0 * p.x + uTime) * sin(5.0 * p.y + uTime) * sin(5.0 * p.z + + uTime) * 0.25;
float sphere_0 = distance_from_sphere(p, vec3(0.0), 0.5);
return sphere_0 + displacement;
}
vec3 calculate_normal(in vec3 p)
{
const vec3 small_step = vec3(0.001, 0.0, 0.0);
float gradient_x = map_the_world(p + small_step.xyy) - map_the_world(p - small_step.xyy);
float gradient_y = map_the_world(p + small_step.yxy) - map_the_world(p - small_step.yxy);
float gradient_z = map_the_world(p + small_step.yyx) - map_the_world(p - small_step.yyx);
vec3 normal = vec3(gradient_x, gradient_y, gradient_z);
return normalize(normal);
}
vec3 ray_march(in vec3 ro, in vec3 rd)
{
float total_distance_traveled = 0.0;
const int NUMBER_OF_STEPS = 32;
const float MINIMUM_HIT_DISTANCE = 0.001;
const float MAXIMUM_TRACE_DISTANCE = 1000.0;
for (int i = 0; i < NUMBER_OF_STEPS; ++i)
{
vec3 current_position = ro + total_distance_traveled * rd;
float distance_to_closest = map_the_world(current_position);
if (distance_to_closest < MINIMUM_HIT_DISTANCE)
{
vec3 normal = calculate_normal(current_position);
vec3 light_position = vec3(2.0, -5.0, -3.0);
light_position = mat3(uTDMats[iVert.cameraIndex].cam) * light_position;
vec3 direction_to_light = normalize(current_position - light_position);
float diffuse_intensity = max(0.0, dot(normal, direction_to_light));
return vec3(1.0, 0.0, 0.0) * diffuse_intensity;
}
if (total_distance_traveled > MAXIMUM_TRACE_DISTANCE)
{
break;
}
total_distance_traveled += distance_to_closest;
}
return vec3(0.0);
}
void main()
{
TDCheckDiscard();
vec4 color = vec4(1.0);
vec2 centeredUV = iVert.uv * 2.0 - 1.0;
vec3 camera_position = vec3(0.0, 0.0, 2.0);
vec3 ro = camera_position;
vec3 rd = mat3(uTDMats[iVert.cameraIndex].cam) * iVert.originalP.xyz;
rd.z *= -1;
vec3 shaded_color = ray_march(ro, rd);
color = vec4(shaded_color, 1.0);
color = over(texture(frontTex, iVert.uv), color);
TDAlphaTest(color.a);
fragColor = TDOutputSwizzle(color);
}
out v2f {
flat int cameraIndex;
vec4 pos;
vec3 originalP;
vec2 uv;
mat3 tbn;
} oVert;
in vec4 T;
void main()
{
oVert.originalP = P;
vec4 worldSpacePos = TDDeform(P);
oVert.pos = worldSpacePos;
vec3 texcoord = TDInstanceTexCoord(uv[0]);
oVert.uv.st = texcoord.st;
oVert.cameraIndex = TDCameraIndex();
vec3 worldSpaceT = normalize(TDDeformNorm(T.xyz));
vec3 worldSpaceN = normalize(TDDeformNorm(N));
oVert.tbn = TDCreateTBNMatrix(worldSpaceN, worldSpaceT, T.w);
gl_Position = TDWorldToProj(worldSpacePos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment