procedural-3d-engine/shaders/glsl/raytracingintersection/intersection.rint
2023-05-13 14:49:00 +02:00

39 lines
No EOL
850 B
Text

/* Copyright (c) 2023, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
#version 460
#extension GL_EXT_ray_tracing : require
struct Sphere {
vec3 center;
float radius;
vec4 color;
};
layout(binding = 3, set = 0) buffer Spheres { Sphere s[]; } spheres;
// Ray-sphere intersection
// By Inigo Quilez, from https://iquilezles.org/articles/spherefunctions/
float sphIntersect(const Sphere s, vec3 ro, vec3 rd)
{
vec3 oc = ro - s.center;
float b = dot(oc, rd);
float c = dot(oc, oc) - s.radius * s.radius;
float h = b * b - c;
if (h < 0.0) {
return -1.0;
}
h = sqrt(h);
return -b - h;
}
void main() {
Sphere sphere = spheres.s[gl_PrimitiveID];
float hit = sphIntersect(sphere, gl_WorldRayOriginEXT, gl_WorldRayDirectionEXT);
if (hit > 0) {
reportIntersectionEXT(hit, 0);
}
}