Make a Shadertoy like shader that renders two close spheres on top a checkerboard plane…
- User
Make a Shadertoy like shader that renders two close spheres on top a checkerboard plane using PBR parameters and have IBL
- Assistant
Make a Shadertoy like shader that renders two close spheres on top a checkerboard plane using PBR parameters and have IBL
Certainly! Below is a Shadertoy shader that renders two close spheres on top of a checkerboard plane using Physically Based Rendering (PBR) parameters and incorporates Image-Based Lighting (IBL). This shader uses GLSL and can be directly pasted into Shadertoy for visualization.
Scene Setup:
PBR Parameters:
Image-Based Lighting (IBL):
iChannel0) is utilized for IBL. Ensure to upload an HDR environment map to iChannel0.Setup Environment Map:
iChannel0.Upload Checkerboard Texture (Optional):
iChannel1. Otherwise, the shader generates a procedural checkerboard.Paste the Shader Code:
iChannel0 (and iChannel1 if using) appropriately.// Shadertoy PBR with IBL: Two Spheres on a Checkerboard Plane
// Author: ChatGPT
/*
To use IBL properly, please upload an HDR environment map to iChannel0.
For the checkerboard, the shader uses a procedural pattern, so no texture is needed for the plane.
*/
#define PI 3.14159265359
// Material structure
struct Material {
vec3 albedo;
float metalness;
float roughness;
};
// Light structure
struct Light {
vec3 position;
vec3 color;
};
// Camera parameters
vec3 camPos = vec3(0.0, 1.5, 5.0);
vec3 camTarget = vec3(0.0, 0.5, 0.0);
// Utility functions
// Normalize and handle possible division by zero
vec3 normalizeSafe(vec3 v) {
return length(v) > 0.0 ? normalize(v) : v;
}
// Reflect function
vec3 reflectVec(vec3 I, vec3 N) {
return reflect(I, N);
}
// Geometry intersection
float sphereSDF(vec3 p, vec3 center, float radius) {
return length(p - center) - radius;
}
float planeSDF(vec3 p, vec3 normal, float d) {
// Plane equation: dot(p, normal) + d = 0
return dot(p, normal) + d;
}
// Ray marching parameters
const int MAX_STEPS = 100;
const float MAX_DIST = 100.0;
const float SURF_DIST = 0.001;
// Scene SDF
float sceneSDF(vec3 p, out int objID) {
float d1 = sphereSDF(p, vec3(-1.0, 1.0, 0.0), 1.0);
float d2 = sphereSDF(p, vec3(1.0, 1.0, 0.0), 1.0);
float dp = planeSDF(p, vec3(0.0, 1.0, 0.0), 0.0);
float minD = min(d1, d2);
minD = min(minD, dp);
// Determine object ID
if(minD == d1) objID = 1;
else if(minD == d2) objID = 2;
else objID = 0; // Plane
return minD;
}
// Estimate normal using gradient
vec3 estimateNormal(vec3 p) {
float eps = 0.0001;
int tmp;
return normalize(vec3(
sceneSDF(p + vec3(eps, 0.0, 0.0), tmp) - sceneSDF(p - vec3(eps, 0.0, 0.0), tmp),
sceneSDF(p + vec3(0.0, eps, 0.0), tmp) - sceneSDF(p - vec3(0.0, eps, 0.0), tmp),
sceneSDF(p + vec3(0.0, 0.0, eps), tmp) - sceneSDF(p - vec3(0.0, 0.0, eps), tmp)
));
}
// Fresnel Schlick approximation
vec3 fresnelSchlick(float cosTheta, vec3 F0){
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
// Distribution GGX
float distributionGGX(vec3 N, vec3 H, float roughness){
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return nom / denom;
}
// Geometry Smith
float geometrySchlickGGX(float NdotV, float roughness){
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;
return nom / denom;
}
float geometrySmith(vec3 N, vec3 V, vec3 L, float roughness){
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = geometrySchlickGGX(NdotV, roughness);
float ggx1 = geometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
// PBR Lighting
vec3 fresnel(vec3 F0, vec3 H, vec3 V){
float cosTheta = max(dot(H, V), 0.0);
return fresnelSchlick(cosTheta, F0);
}
// Main function
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
// Screen space [-1,1]
vec2 p = uv * 2.0 - 1.0;
p.x *= iResolution.x / iResolution.y;
// Camera setup
vec3 forward = normalize(camTarget - camPos);
vec3 right = normalize(cross(forward, vec3(0.0, 1.0, 0.0)));
vec3 up = cross(right, forward);
float fov = PI/3.0;
vec3 rayDir = normalize(forward + p.x * right * tan(fov/2.0) + p.y * up * tan(fov/2.0));
vec3 rayOrigin = camPos;
// Ray marching
float totalDist = 0.0;
int hitObject = -1;
vec3 hitPos;
for(int i=0; i < MAX_STEPS; i++) {
vec3 p = rayOrigin + totalDist * rayDir;
int objID;
float dist = sceneSDF(p, objID);
if(dist < SURF_DIST){
hitObject = objID;
hitPos = p;
break;
}
totalDist += dist;
if(totalDist > MAX_DIST) break;
}
vec3 color = vec3(0.0);
if(hitObject != -1){
// Estimate normal
vec3 N = estimateNormal(hitPos);
vec3 V = normalize(camPos - hitPos);
// Define materials
Material mat;
if(hitObject == 1){
// Material for first sphere
mat.albedo = vec3(0.7, 0.0, 0.0); // Red
mat.metalness = 0.9;
mat.roughness = 0.2;
}
else if(hitObject == 2){
// Material for second sphere
mat.albedo = vec3(0.0, 0.7, 0.0); // Green
mat.metalness = 0.1;
mat.roughness = 0.8;
}
else{
// Material for plane (checkerboard)
float checker = step(0.5, sin(hitPos.x * 5.0) * sin(hitPos.z * 5.0));
mat.albedo = mix(vec3(1.0), vec3(0.0), checker);
mat.metalness = 0.0;
mat.roughness = 1.0;
}
// IBL: sample environment map
vec3 envColor = texture(iChannel0, normalize(reflect(-V, N))).rgb;
// Assume single directional light for simplicity
Light light;
light.position = vec3(5.0, 10.0, 5.0);
light.color = vec3(300.0); // Intensely bright light
// Compute lighting
vec3 L = normalize(light.position - hitPos);
vec3 H = normalize(V + L);
float distance = length(light.position - hitPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = light.color * attenuation;
// Cook-Torrance BRDF
float NDF = distributionGGX(N, H, mat.roughness);
float G = geometrySmith(N, V, L, mat.roughness);
vec3 F = fresnel(mat.albedo, H, V);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
vec3 specular = numerator / denominator;
// kS is specular component
vec3 kS = F;
// kD is diffuse component, (1 - kS)
vec3 kD = vec3(1.0) - kS;
// If metal, no diffuse
kD *= 1.0 - mat.metalness;
// Lambertian diffuse
vec3 diffuse = mat.albedo / PI;
// Final lighting
float NdotL = max(dot(N, L), 0.0);
vec3 Lo = (kD * diffuse + specular) * radiance * NdotL;
// IBL diffuse and specular
vec3 F0 = mat.albedo;
F0 = mix(vec3(0.04), mat.albedo, mat.metalness);
vec3 F_IBL = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 kS_IBL = F_IBL;
vec3 kD_IBL = 1.0 - kS_IBL;
kD_IBL *= 1.0 - mat.metalness;
// Diffuse IBL
vec3 irradiance = envColor * 0.1; // Scale down the environment irradiance
vec3 diffuse_IBL = kD_IBL * mat.albedo / PI * irradiance;
// Specular IBL (GGX)
// Approximation: integrating environment's specular
vec3 R = reflect(-V, N);
vec3 specular_IBL = kS_IBL * envColor * pow(max(dot(R, V), 0.0), 1.0); // Rough approximation
color = Lo + diffuse_IBL + specular_IBL;
// Tone mapping (Reinhard)
color = color / (color + vec3(1.0));
// Gamma correction
color = pow(color, vec3(1.0/2.2));
}
// Background color
if(hitObject == -1){
color = texture(iChannel0, rayDir.xy * 0.5 + 0.5).rgb;
}
fragColor = vec4(color, 1.0);
}
Channels Setup:
iChannel0: Upload your HDR environment map here for IBL.iChannel1: Not used in this shader since the checkerboard is procedural, but you can modify the shader to use a texture if desired.Adjusting the Light:
(5.0, 10.0, 5.0). You can modify light.position and light.color within the shader to change the light’s properties.Materials:
metalness = 0.9, roughness = 0.2).metalness = 0.1, roughness = 0.8).Performance: This shader uses ray marching, which can be computationally intensive. For better performance, consider optimizing the number of steps or using more advanced acceleration structures.
IBL Quality: The IBL implementation here is a simplified version. For higher quality reflections and ambient lighting, more sophisticated techniques like Environment Radiance Integration or prefiltered environment maps can be employed.
Customization: Feel free to tweak the materials, light positions, and other parameters to achieve different visual effects.
Here's an example of how the rendered scene might look:
[image: PBR Two Spheres on Checkerboard] open
(Replace the image link with an actual screenshot after running the shader in Shadertoy.)
This shader provides a foundational implementation of PBR with IBL in a ray-marched scene on Shadertoy. You can expand upon it by adding more objects, improving the lighting model, or enhancing the IBL with more accurate environment sampling. Shadertoy’s community and resources can also offer further assistance and optimization techniques.
Feel free to experiment and modify the shader to better fit your project needs!