-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalyticPrimitives.hlsli
244 lines (211 loc) · 8.47 KB
/
AnalyticPrimitives.hlsli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
//**********************************************************************************************
//
// AnalyticPrimitives.hlsli
//
// Set of ray vs analytic primitive intersection tests.
//
//**********************************************************************************************
#ifndef ANALYTICPRIMITIVES_H
#define ANALYTICPRIMITIVES_H
#include "RaytracingShaderHelper.hlsli"
// Solve a quadratic equation.
// Ref: https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
bool SolveQuadraticEqn(float a, float b, float c, out float x0, out float x1)
{
float discr = b * b - 4 * a * c;
if (discr < 0) return false;
else if (discr == 0) x0 = x1 = -0.5 * b / a;
else {
float q = (b > 0) ?
-0.5 * (b + sqrt(discr)) :
-0.5 * (b - sqrt(discr));
x0 = q / a;
x1 = c / q;
}
if (x0 > x1) swap(x0, x1);
return true;
}
// Calculate a normal for a hit point on a sphere.
float3 CalculateNormalForARaySphereHit(in Ray ray, in float thit, float3 center)
{
float3 hitPosition = ray.origin + thit * ray.direction;
return normalize(hitPosition - center);
}
// Analytic solution of an unbounded ray sphere intersection points.
// Ref: https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
bool SolveRaySphereIntersectionEquation(in Ray ray, out float tmin, out float tmax, in float3 center, in float radius)
{
float3 L = ray.origin - center;
float a = dot(ray.direction, ray.direction);
float b = 2 * dot(ray.direction, L);
float c = dot(L, L) - radius * radius;
return SolveQuadraticEqn(a, b, c, tmin, tmax);
}
bool NewSolveRaySphereIntersection(in Ray ray, out float tmin, out float tmax, in float3 c, in float r)
{
float3 l = c - ray.origin;
float s = dot(l, ray.direction);
float l_square = dot(l, l);
float r_square = r * r;
if (s < 0 && l_square > r_square)
{
return false;
}
float m_square = l_square - s * s;
if (m_square > r_square)
{
return false;
}
float q = sqrt(r_square - m_square);
tmin = s - q;
tmax = s + q;
return true;
}
// Test if a ray with RayFlags and segment <RayTMin(), RayTCurrent()> intersects a hollow sphere.
bool RaySphereIntersectionTest(in Ray ray, out float thit, out float tmax, out ProceduralPrimitiveAttributes attr, in float3 center = float3(0, 0, 0), in float radius = 1)
{
float t0, t1; // solutions for t if the ray intersects
if (!SolveRaySphereIntersectionEquation(ray, t0, t1, center, radius)) return false;
tmax = t1;
if (t0 < RayTMin())
{
// t0 is before RayTMin, let's use t1 instead .
if (t1 < RayTMin()) return false; // both t0 and t1 are before RayTMin
attr.normal = CalculateNormalForARaySphereHit(ray, t1, center);
if (IsAValidHit(ray, t1, attr.normal))
{
thit = t1;
return true;
}
}
else
{
attr.normal = CalculateNormalForARaySphereHit(ray, t0, center);
if (IsAValidHit(ray, t0, attr.normal))
{
thit = t0;
return true;
}
attr.normal = CalculateNormalForARaySphereHit(ray, t1, center);
if (IsAValidHit(ray, t1, attr.normal))
{
thit = t1;
return true;
}
}
return false;
}
// Test if a ray segment <RayTMin(), RayTCurrent()> intersects a solid sphere.
// Limitation: this test does not take RayFlags into consideration and does not calculate a surface normal.
bool RaySolidSphereIntersectionTest(in Ray ray, out float thit, out float tmax, in float3 center = float3(0, 0, 0), in float radius = 1)
{
float t0, t1; // solutions for t if the ray intersects
if (!SolveRaySphereIntersectionEquation(ray, t0, t1, center, radius))
return false;
// Since it's a solid sphere, clip intersection points to ray extents.
thit = max(t0, RayTMin());
tmax = min(t1, RayTCurrent());
return true;
}
// Test if a ray with RayFlags and segment <RayTMin(), RayTCurrent()> intersects multiple hollow spheres.
bool RaySpheresIntersectionTest(in Ray ray, out float thit, out ProceduralPrimitiveAttributes attr)
{
const int N = 3;
float3 centers[N] =
{
float3(-0.3, -0.3, -0.3),
float3(0.1, 0.1, 0.4),
float3(0.35,0.35, 0.0)
};
float radii[N] = { 0.6, 0.3, 0.15 };
bool hitFound = false;
//
// Test for intersection against all spheres and take the closest hit.
//
thit = RayTCurrent();
// test against all spheres
for (int i = 0; i < N; i++)
{
float _thit;
float _tmax;
ProceduralPrimitiveAttributes _attr = (ProceduralPrimitiveAttributes)0;
if (RaySphereIntersectionTest(ray, _thit, _tmax, _attr, centers[i], radii[i]))
{
if (_thit < thit)
{
thit = _thit;
attr = _attr;
hitFound = true;
}
}
}
return hitFound;
}
// Test if a ray segment <RayTMin(), RayTCurrent()> intersects an AABB.
// Limitation: this test does not take RayFlags into consideration and does not calculate a surface normal.
// Ref: https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-box-intersection
bool RayAABBIntersectionTest(Ray ray, float3 aabb[2], out float tmin, out float tmax)
{
float3 tmin3, tmax3;
int3 sign3 = ray.direction > 0;
// Handle rays parallel to any x|y|z slabs of the AABB.
// If a ray is within the parallel slabs,
// the tmin, tmax will get set to -inf and +inf
// which will get ignored on tmin/tmax = max/min.
// If a ray is outside the parallel slabs, -inf/+inf will
// make tmax > tmin fail (i.e. no intersection).
// TODO: handle cases where ray origin is within a slab
// that a ray direction is parallel to. In that case
// 0 * INF => NaN
const float FLT_INFINITY = 1.#INF;
float3 invRayDirection = ray.direction != 0
? 1 / ray.direction
: (ray.direction > 0) ? FLT_INFINITY : -FLT_INFINITY;
tmin3.x = (aabb[1 - sign3.x].x - ray.origin.x) * invRayDirection.x;
tmax3.x = (aabb[sign3.x].x - ray.origin.x) * invRayDirection.x;
tmin3.y = (aabb[1 - sign3.y].y - ray.origin.y) * invRayDirection.y;
tmax3.y = (aabb[sign3.y].y - ray.origin.y) * invRayDirection.y;
tmin3.z = (aabb[1 - sign3.z].z - ray.origin.z) * invRayDirection.z;
tmax3.z = (aabb[sign3.z].z - ray.origin.z) * invRayDirection.z;
tmin = max(max(tmin3.x, tmin3.y), tmin3.z);
tmax = min(min(tmax3.x, tmax3.y), tmax3.z);
return tmax > tmin && tmax >= RayTMin() && tmin <= RayTCurrent();
}
// Test if a ray with RayFlags and segment <RayTMin(), RayTCurrent()> intersects a hollow AABB.
bool RayAABBIntersectionTest(Ray ray, float3 aabb[2], out float thit, out ProceduralPrimitiveAttributes attr)
{
float tmin, tmax;
if (RayAABBIntersectionTest(ray, aabb, tmin, tmax))
{
// Only consider intersections crossing the surface from the outside.
if (tmin < RayTMin() || tmin > RayTCurrent())
return false;
thit = tmin;
// Set a normal to the normal of a face the hit point lays on.
float3 hitPosition = ray.origin + thit * ray.direction;
float3 distanceToBounds[2] = {
abs(aabb[0] - hitPosition),
abs(aabb[1] - hitPosition)
};
const float eps = 0.0001;
if (distanceToBounds[0].x < eps) attr.normal = float3(-1, 0, 0);
else if (distanceToBounds[0].y < eps) attr.normal = float3(0, -1, 0);
else if (distanceToBounds[0].z < eps) attr.normal = float3(0, 0, -1);
else if (distanceToBounds[1].x < eps) attr.normal = float3(1, 0, 0);
else if (distanceToBounds[1].y < eps) attr.normal = float3(0, 1, 0);
else if (distanceToBounds[1].z < eps) attr.normal = float3(0, 0, 1);
return IsAValidHit(ray, thit, attr.normal);
}
return false;
}
#endif // ANALYTICPRIMITIVES_H