This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
intersect.glsl
275 lines (224 loc) · 7.85 KB
/
intersect.glsl
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
export default `
uniform sampler2D positionBuffer;
uniform sampler2D normalBuffer;
uniform sampler2D uvBuffer;
uniform sampler2D bvhBuffer;
struct Triangle {
vec3 p0;
vec3 p1;
vec3 p2;
};
void surfaceInteractionFromBVH(inout SurfaceInteraction si, Triangle tri, vec3 barycentric, ivec3 index, vec3 faceNormal, int materialIndex) {
si.hit = true;
si.faceNormal = faceNormal;
si.position = barycentric.x * tri.p0 + barycentric.y * tri.p1 + barycentric.z * tri.p2;
ivec2 i0 = unpackTexel(index.x, VERTEX_COLUMNS);
ivec2 i1 = unpackTexel(index.y, VERTEX_COLUMNS);
ivec2 i2 = unpackTexel(index.z, VERTEX_COLUMNS);
vec3 n0 = texelFetch(normalBuffer, i0, 0).xyz;
vec3 n1 = texelFetch(normalBuffer, i1, 0).xyz;
vec3 n2 = texelFetch(normalBuffer, i2, 0).xyz;
vec3 normal = normalize(barycentric.x * n0 + barycentric.y * n1 + barycentric.z * n2);
#if defined(NUM_DIFFUSE_MAPS) || defined(NUM_NORMAL_MAPS) || defined(NUM_PBR_MAPS)
vec2 uv0 = texelFetch(uvBuffer, i0, 0).xy;
vec2 uv1 = texelFetch(uvBuffer, i1, 0).xy;
vec2 uv2 = texelFetch(uvBuffer, i2, 0).xy;
vec2 uv = fract(barycentric.x * uv0 + barycentric.y * uv1 + barycentric.z * uv2);
#else
vec2 uv = vec2(0.0);
#endif
si.materialType = int(getMatType(materialIndex));
si.color = getMatColor(materialIndex, uv);
si.roughness = getMatRoughness(materialIndex, uv);
si.metalness = getMatMetalness(materialIndex, uv);
#ifdef NUM_NORMAL_MAPS
vec3 dp1 = tri.p0 - tri.p2;
vec3 dp2 = tri.p1 - tri.p2;
vec2 duv1 = uv0 - uv2;
vec2 duv2 = uv1 - uv2;
si.normal = getMatNormal(materialIndex, uv, normal, dp1, dp2, duv1, duv2);
#else
si.normal = normal;
#endif
}
struct TriangleIntersect {
float t;
vec3 barycentric;
};
// Triangle-ray intersection
// Faster than the classic Möller–Trumbore intersection algorithm
// http://www.pbr-book.org/3ed-2018/Shapes/Triangle_Meshes.html#TriangleIntersection
TriangleIntersect intersectTriangle(Ray r, Triangle tri, int maxDim, vec3 shear) {
TriangleIntersect ti;
vec3 d = r.d;
// translate vertices based on ray origin
vec3 p0t = tri.p0 - r.o;
vec3 p1t = tri.p1 - r.o;
vec3 p2t = tri.p2 - r.o;
// permute components of triangle vertices
if (maxDim == 0) {
p0t = p0t.yzx;
p1t = p1t.yzx;
p2t = p2t.yzx;
} else if (maxDim == 1) {
p0t = p0t.zxy;
p1t = p1t.zxy;
p2t = p2t.zxy;
}
// apply shear transformation to translated vertex positions
p0t.xy += shear.xy * p0t.z;
p1t.xy += shear.xy * p1t.z;
p2t.xy += shear.xy * p2t.z;
// compute edge function coefficients
vec3 e = vec3(
p1t.x * p2t.y - p1t.y * p2t.x,
p2t.x * p0t.y - p2t.y * p0t.x,
p0t.x * p1t.y - p0t.y * p1t.x
);
// check if intersection is inside triangle
if (any(lessThan(e, vec3(0))) && any(greaterThan(e, vec3(0)))) {
return ti;
}
float det = e.x + e.y + e.z;
// not needed?
// if (det == 0.) {
// return ti;
// }
p0t.z *= shear.z;
p1t.z *= shear.z;
p2t.z *= shear.z;
float tScaled = (e.x * p0t.z + e.y * p1t.z + e.z * p2t.z);
// not needed?
// if (sign(det) != sign(tScaled)) {
// return ti;
// }
// check if closer intersection already exists
if (abs(tScaled) > abs(r.tMax * det)) {
return ti;
}
float invDet = 1. / det;
ti.t = tScaled * invDet;
ti.barycentric = e * invDet;
return ti;
}
struct Box {
vec3 min;
vec3 max;
};
// Branchless ray/box intersection
// https://tavianator.com/fast-branchless-raybounding-box-intersections/
float intersectBox(Ray r, Box b) {
vec3 tBot = (b.min - r.o) * r.invD;
vec3 tTop = (b.max - r.o) * r.invD;
vec3 tNear = min(tBot, tTop);
vec3 tFar = max(tBot, tTop);
float t0 = max(tNear.x, max(tNear.y, tNear.z));
float t1 = min(tFar.x, min(tFar.y, tFar.z));
return (t0 > t1 || t0 > r.tMax) ? -1.0 : (t0 > 0.0 ? t0 : t1);
}
int maxDimension(vec3 v) {
return v.x > v.y ? (v.x > v.z ? 0 : 2) : (v.y > v.z ? 1 : 2);
}
// Traverse BVH, find closest triangle intersection, and return surface information
void intersectScene(inout Ray ray, inout SurfaceInteraction si) {
si.hit = false;
int maxDim = maxDimension(abs(ray.d));
// Permute space so that the z dimension is the one where the absolute value of the ray's direction is largest.
// Then create a shear transformation that aligns ray direction with the +z axis
vec3 shear;
if (maxDim == 0) {
shear = vec3(-ray.d.y, -ray.d.z, 1.0) * ray.invD.x;
} else if (maxDim == 1) {
shear = vec3(-ray.d.z, -ray.d.x, 1.0) * ray.invD.y;
} else {
shear = vec3(-ray.d.x, -ray.d.y, 1.0) * ray.invD.z;
}
int nodesToVisit[STACK_SIZE];
int stack = 0;
nodesToVisit[0] = 0;
while(stack >= 0) {
int i = nodesToVisit[stack--];
vec4 r1 = fetchData(bvhBuffer, i, BVH_COLUMNS);
vec4 r2 = fetchData(bvhBuffer, i + 1, BVH_COLUMNS);
int splitAxisOrNumPrimitives = floatBitsToInt(r1.w);
if (splitAxisOrNumPrimitives >= 0) {
// Intersection is a bounding box. Test for box intersection and keep traversing BVH
int splitAxis = splitAxisOrNumPrimitives;
Box bbox = Box(r1.xyz, r2.xyz);
if (intersectBox(ray, bbox) > 0.0) {
// traverse near node to ray first, and far node to ray last
if (ray.d[splitAxis] > 0.0) {
nodesToVisit[++stack] = floatBitsToInt(r2.w);
nodesToVisit[++stack] = i + 2;
} else {
nodesToVisit[++stack] = i + 2;
nodesToVisit[++stack] = floatBitsToInt(r2.w);
}
}
} else {
ivec3 index = floatBitsToInt(r1.xyz);
Triangle tri = Triangle(
fetchData(positionBuffer, index.x, VERTEX_COLUMNS).xyz,
fetchData(positionBuffer, index.y, VERTEX_COLUMNS).xyz,
fetchData(positionBuffer, index.z, VERTEX_COLUMNS).xyz
);
TriangleIntersect hit = intersectTriangle(ray, tri, maxDim, shear);
if (hit.t > 0.0) {
ray.tMax = hit.t;
int materialIndex = floatBitsToInt(r2.w);
vec3 faceNormal = r2.xyz;
surfaceInteractionFromBVH(si, tri, hit.barycentric, index, faceNormal, materialIndex);
}
}
}
// Values must be clamped outside of intersection loop. Clamping inside the loop produces incorrect numbers on some devices.
si.roughness = clamp(si.roughness, ROUGHNESS_MIN, 1.0);
si.metalness = clamp(si.metalness, 0.0, 1.0);
}
bool intersectSceneShadow(inout Ray ray) {
int maxDim = maxDimension(abs(ray.d));
// Permute space so that the z dimension is the one where the absolute value of the ray's direction is largest.
// Then create a shear transformation that aligns ray direction with the +z axis
vec3 shear;
if (maxDim == 0) {
shear = vec3(-ray.d.y, -ray.d.z, 1.0) * ray.invD.x;
} else if (maxDim == 1) {
shear = vec3(-ray.d.z, -ray.d.x, 1.0) * ray.invD.y;
} else {
shear = vec3(-ray.d.x, -ray.d.y, 1.0) * ray.invD.z;
}
int nodesToVisit[STACK_SIZE];
int stack = 0;
nodesToVisit[0] = 0;
while(stack >= 0) {
int i = nodesToVisit[stack--];
vec4 r1 = fetchData(bvhBuffer, i, BVH_COLUMNS);
vec4 r2 = fetchData(bvhBuffer, i + 1, BVH_COLUMNS);
int splitAxisOrNumPrimitives = floatBitsToInt(r1.w);
if (splitAxisOrNumPrimitives >= 0) {
int splitAxis = splitAxisOrNumPrimitives;
Box bbox = Box(r1.xyz, r2.xyz);
if (intersectBox(ray, bbox) > 0.0) {
if (ray.d[splitAxis] > 0.0) {
nodesToVisit[++stack] = floatBitsToInt(r2.w);
nodesToVisit[++stack] = i + 2;
} else {
nodesToVisit[++stack] = i + 2;
nodesToVisit[++stack] = floatBitsToInt(r2.w);
}
}
} else {
ivec3 index = floatBitsToInt(r1.xyz);
Triangle tri = Triangle(
fetchData(positionBuffer, index.x, VERTEX_COLUMNS).xyz,
fetchData(positionBuffer, index.y, VERTEX_COLUMNS).xyz,
fetchData(positionBuffer, index.z, VERTEX_COLUMNS).xyz
);
if (intersectTriangle(ray, tri, maxDim, shear).t > 0.0) {
return true;
}
}
}
return false;
}
`;