From 133c703160e29bb335c99997812d2db3f6598e93 Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Sat, 13 May 2023 14:49:00 +0200 Subject: [PATCH] Intersection shader --- .../raytracingintersection/intersection.rint | 39 ++++++++++++++++++ .../intersection.rint.spv | Bin 0 -> 2444 bytes 2 files changed, 39 insertions(+) create mode 100644 shaders/glsl/raytracingintersection/intersection.rint create mode 100644 shaders/glsl/raytracingintersection/intersection.rint.spv diff --git a/shaders/glsl/raytracingintersection/intersection.rint b/shaders/glsl/raytracingintersection/intersection.rint new file mode 100644 index 00000000..e2d4c410 --- /dev/null +++ b/shaders/glsl/raytracingintersection/intersection.rint @@ -0,0 +1,39 @@ +/* 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); + } +} \ No newline at end of file diff --git a/shaders/glsl/raytracingintersection/intersection.rint.spv b/shaders/glsl/raytracingintersection/intersection.rint.spv new file mode 100644 index 0000000000000000000000000000000000000000..29e50d65273f3d3819c2fc3fbf105b963bc03a42 GIT binary patch literal 2444 zcmZ9M+iz4=6o+>^(^76#q<|MFQ|bkURvOC1RM6q#v{I$C2nY|8VLG%YX*szoL+6{JwL}4hQyRWv}0FUG`dg?Kzdf$sM6O5O#-=a4+O*Ul#$`DwVJ~~yHD85{=&QNT6^3p**WX6JkLYul9XiuLU|-MlYWSR; zTC%?E^8&csr`T2QQ||RXuIulD2$UcnJ5Pj_56>~M1gPd{t*V&IDu81el zD+RvHz8l$5VBfI#ciy=E_A34Zyq~P*ogmtH^@C`!oGFxU$Q$txw!(@3r_nbbnCGf?P6yjr~N9s@#Yp^ zW7p>S`iq^BT*S^u>>b@!u5WBhT+y81j%lq_y5wUad zZI`icTYQ0dd*bhu^;73o?d2<^jyNZ6`q^6p(e~p&{@t4|xQkR}4Pt$r!PkhK*t4!9 z-hsZ>BaXHk=-!7mYZHG)lrvsN{4Sc$IPLn`qc(Hsw}Qz1!p-(jKNNnGaQbSq zA7kG~%&E{}Mp%Z(VMZ6>X)fQ`!k1=P^?e|I1|5|bbaKlL+qRWA1tZCc>n+a literal 0 HcmV?d00001