Skip to content

Commit 826533b

Browse files
authored
Factorize intersectionFunctions (#16082)
* Fix oneffecterrorobs * Fix NormalMaterial with ThinInstances * Fix Animation loop in CreateAndStartAnimation * Fix lineMesh cloned alpha values * Fix recursive loop in onPointerOutAction * Fix ScreenshotTools * Factorize intersectionFunctions
1 parent 7b14790 commit 826533b

File tree

4 files changed

+97
-92
lines changed

4 files changed

+97
-92
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// From: https://www.shadertoy.com/view/4tsBD7
2+
// keeping for reference the general formula for a disk
3+
// float diskIntersectWithBackFaceCulling(vec3 ro, vec3 rd, vec3 c, vec3 n, float r) {
4+
// float d = dot(rd, n);
5+
// if(d > 0.0) { return 1e6; }
6+
// vec3 o = ro - c;
7+
// float t = -dot(n, o) / d;
8+
// vec3 q = o + rd * t;
9+
// return (dot(q, q) < r * r) ? t : 1e6;
10+
// }
11+
// optimized for a disk on the ground facing up
12+
float diskIntersectWithBackFaceCulling(vec3 ro, vec3 rd, vec3 c, float r) {
13+
float d = rd.y;
14+
if(d > 0.0) { return 1e6; }
15+
vec3 o = ro - c;
16+
float t = -o.y / d;
17+
vec3 q = o + rd * t;
18+
return (dot(q, q) < r * r) ? t : 1e6;
19+
}
20+
21+
// From: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
22+
vec2 sphereIntersect(vec3 ro, vec3 rd, vec3 ce, float ra) {
23+
vec3 oc = ro - ce;
24+
float b = dot(oc, rd);
25+
float c = dot(oc, oc) - ra * ra;
26+
float h = b * b - c;
27+
if(h < 0.0) { return vec2(-1.0, -1.0); }
28+
h = sqrt(h);
29+
return vec2(-b + h, -b - h);
30+
}
31+
32+
// optimized for a sphere centered at the origin
33+
vec2 sphereIntersectFromOrigin(vec3 ro, vec3 rd, float ra) {
34+
float b = dot(ro, rd);
35+
float c = dot(ro, ro) - ra * ra;
36+
float h = b * b - c;
37+
38+
if(h < 0.0) { return vec2(-1.0, -1.0); }
39+
40+
h = sqrt(h);
41+
42+
return vec2(-b + h, -b - h);
43+
}

packages/dev/core/src/Shaders/background.fragment.fx

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -113,49 +113,7 @@ varying vec3 vNormalW;
113113
#endif
114114

115115
#ifdef PROJECTED_GROUND
116-
// From: https://www.shadertoy.com/view/4tsBD7
117-
// keeping for reference the general formula for a disk
118-
// float diskIntersectWithBackFaceCulling(vec3 ro, vec3 rd, vec3 c, vec3 n, float r) {
119-
// float d = dot(rd, n);
120-
// if(d > 0.0) { return 1e6; }
121-
// vec3 o = ro - c;
122-
// float t = -dot(n, o) / d;
123-
// vec3 q = o + rd * t;
124-
// return (dot(q, q) < r * r) ? t : 1e6;
125-
// }
126-
// optimized for a disk on the ground facing up
127-
float diskIntersectWithBackFaceCulling(vec3 ro, vec3 rd, vec3 c, float r) {
128-
float d = rd.y;
129-
if(d > 0.0) { return 1e6; }
130-
vec3 o = ro - c;
131-
float t = -o.y / d;
132-
vec3 q = o + rd * t;
133-
return (dot(q, q) < r * r) ? t : 1e6;
134-
}
135-
136-
// From: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
137-
// keeping for reference the general formula for a sphere
138-
// float sphereIntersect(vec3 ro, vec3 rd, vec3 ce, float ra) {
139-
// vec3 oc = ro - ce;
140-
// float b = dot(oc, rd);
141-
// float c = dot(oc, oc) - ra * ra;
142-
// float h = b * b - c;
143-
// if(h < 0.0) { return -1.0; }
144-
// h = sqrt(h);
145-
// return - b + h;
146-
// }
147-
// optimized for a sphere centered at the origin
148-
float sphereIntersect(vec3 ro, vec3 rd, float ra) {
149-
float b = dot(ro, rd);
150-
float c = dot(ro, ro) - ra * ra;
151-
float h = b * b - c;
152-
153-
if(h < 0.0) { return -1.0; }
154-
155-
h = sqrt(h);
156-
157-
return - b + h;
158-
}
116+
#include<intersectionFunctions>
159117

160118
vec3 project(vec3 viewDirectionW, vec3 eyePosition) {
161119
float radius = projectedGroundInfos.x;
@@ -165,14 +123,14 @@ varying vec3 vNormalW;
165123
// to help with shadows
166124
// vec3 p = normalize(vPositionW);
167125
vec3 camDir = -viewDirectionW;
168-
float skySphereDistance = sphereIntersect(eyePosition, camDir, radius);
126+
float skySphereDistance = sphereIntersectFromOrigin(eyePosition, camDir, radius).x;
169127
vec3 skySpherePositionW = eyePosition + camDir * skySphereDistance;
170128

171129
vec3 p = normalize(skySpherePositionW);
172130
eyePosition.y -= height;
173131

174132
// Let s remove extra conditions in the following block
175-
// float intersection = sphereIntersect(eyePosition, p, radius);
133+
// float intersection = sphereIntersectFromOrigin(eyePosition, p, radius).x;
176134
// if(intersection > 0.0) {
177135
// vec3 h = vec3(0.0, -height, 0.0);
178136
// float intersection2 = diskIntersectWithBackFaceCulling(eyePosition, p, h, radius);
@@ -181,7 +139,7 @@ varying vec3 vNormalW;
181139
// p = vec3(0.0, 1.0, 0.0);
182140
// }
183141

184-
float sIntersection = sphereIntersect(eyePosition, p, radius);
142+
float sIntersection = sphereIntersectFromOrigin(eyePosition, p, radius).x;
185143
vec3 h = vec3(0.0, -height, 0.0);
186144
float dIntersection = diskIntersectWithBackFaceCulling(eyePosition, p, h, radius);
187145
p = (eyePosition + min(sIntersection, dIntersection) * p);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// From: https://www.shadertoy.com/view/4tsBD7
2+
// keeping for reference the general formula for a disk
3+
// fn diskIntersectWithBackFaceCulling(ro: vec3f, rd: vec3f, c: vec3f, n: vec3f, r: f32) -> f32 {
4+
// var d: f32 = dot(rd, n);
5+
// if(d > 0.0) { return 1e6; }
6+
// var o: vec3f = ro - c;
7+
// var t: f32 = -dot(n, o) / d;
8+
// var q: vec3f = o + rd * t;
9+
// return (dot(q, q) < r * r) ? t : 1e6;
10+
// }
11+
// optimized for a disk on the ground facing up
12+
fn diskIntersectWithBackFaceCulling(ro: vec3f, rd: vec3f, c: vec3f, r: f32) -> f32 {
13+
var d: f32 = rd.y;
14+
if(d > 0.0) { return 1e6; }
15+
var o: vec3f = ro - c;
16+
var t: f32 = -o.y / d;
17+
var q: vec3f = o + rd * t;
18+
return select(1e6, t, (dot(q, q) < r * r));
19+
}
20+
21+
// From: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
22+
fn sphereIntersect(ro: vec3f, rd: vec3f, ce: vec3f, ra: f32) -> vec2f {
23+
var oc: vec3f = ro - ce;
24+
var b: f32 = dot(oc, rd);
25+
var c: f32 = dot(oc, oc) - ra * ra;
26+
var h: f32 = b * b - c;
27+
28+
if(h < 0.0) { return vec2f(-1., -1.); }
29+
30+
h = sqrt(h);
31+
32+
return vec2f(-b + h, -b - h);
33+
}
34+
35+
// optimized for a sphere centered at the origin
36+
fn sphereIntersectFromOrigin(ro: vec3f, rd: vec3f, ra: f32) -> vec2f {
37+
var b: f32 = dot(ro, rd);
38+
var c: f32 = dot(ro, ro) - ra * ra;
39+
var h: f32 = b * b - c;
40+
41+
if(h < 0.0) { return vec2f(-1., -1.); }
42+
43+
h = sqrt(h);
44+
45+
return vec2f(-b + h, -b - h);
46+
}

packages/dev/core/src/ShadersWGSL/background.fragment.fx

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -105,49 +105,7 @@ varying vPositionW: vec3f;
105105
#endif
106106

107107
#ifdef PROJECTED_GROUND
108-
// From: https://www.shadertoy.com/view/4tsBD7
109-
// keeping for reference the general formula for a disk
110-
// fn diskIntersectWithBackFaceCulling(ro: vec3f, rd: vec3f, c: vec3f, n: vec3f, r: f32) -> f32 {
111-
// var d: f32 = dot(rd, n);
112-
// if(d > 0.0) { return 1e6; }
113-
// var o: vec3f = ro - c;
114-
// var t: f32 = -dot(n, o) / d;
115-
// var q: vec3f = o + rd * t;
116-
// return (dot(q, q) < r * r) ? t : 1e6;
117-
// }
118-
// optimized for a disk on the ground facing up
119-
fn diskIntersectWithBackFaceCulling(ro: vec3f, rd: vec3f, c: vec3f, r: f32) -> f32 {
120-
var d: f32 = rd.y;
121-
if(d > 0.0) { return 1e6; }
122-
var o: vec3f = ro - c;
123-
var t: f32 = -o.y / d;
124-
var q: vec3f = o + rd * t;
125-
return select(1e6, t, (dot(q, q) < r * r));
126-
}
127-
128-
// From: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
129-
// keeping for reference the general formula for a sphere
130-
// fn sphereIntersect(ro: vec3f, rd: vec3f, ce: vec3f, ra: f32) -> f32 {
131-
// var oc: vec3f = ro - ce;
132-
// var b: f32 = dot(oc, rd);
133-
// var c: f32 = dot(oc, oc) - ra * ra;
134-
// var h: f32 = b * b - c;
135-
// if(h < 0.0) { return -1.0; }
136-
// h = sqrt(h);
137-
// return - b + h;
138-
// }
139-
// optimized for a sphere centered at the origin
140-
fn sphereIntersect(ro: vec3f, rd: vec3f, ra: f32) -> f32 {
141-
var b: f32 = dot(ro, rd);
142-
var c: f32 = dot(ro, ro) - ra * ra;
143-
var h: f32 = b * b - c;
144-
145-
if(h < 0.0) { return -1.0; }
146-
147-
h = sqrt(h);
148-
149-
return - b + h;
150-
}
108+
#include<intersectionFunctions>
151109

152110
fn project(viewDirectionW: vec3f, eyePosition: vec3f) -> vec3f {
153111
var radius: f32 = uniforms.projectedGroundInfos.x;
@@ -157,14 +115,14 @@ varying vPositionW: vec3f;
157115
// to help with shadows
158116
// var p: vec3f = normalize(vPositionW);
159117
var camDir: vec3f = -viewDirectionW;
160-
var skySphereDistance: f32 = sphereIntersect(eyePosition, camDir, radius);
118+
var skySphereDistance: f32 = sphereIntersectFromOrigin(eyePosition, camDir, radius).x;
161119
var skySpherePositionW: vec3f = eyePosition + camDir * skySphereDistance;
162120

163121
var p: vec3f = normalize(skySpherePositionW);
164122
var upEyePosition = vec3f(eyePosition.x, eyePosition.y - height, eyePosition.z);
165123

166124
// Let s remove extra conditions in the following block
167-
// var intersection: f32 = sphereIntersect(eyePosition, p, radius);
125+
// var intersection: f32 = sphereIntersectFromOrigin(eyePosition, p, radius).x;
168126
// if(intersection > 0.0) {
169127
// var h: vec3f = vec3f(0.0, -height, 0.0);
170128
// var intersection2: f32 = diskIntersectWithBackFaceCulling(eyePosition, p, h, radius);
@@ -173,7 +131,7 @@ varying vPositionW: vec3f;
173131
// p = vec3f(0.0, 1.0, 0.0);
174132
// }
175133

176-
var sIntersection: f32 = sphereIntersect(upEyePosition, p, radius);
134+
var sIntersection: f32 = sphereIntersectFromOrigin(upEyePosition, p, radius).x;
177135
var h: vec3f = vec3f(0.0, -height, 0.0);
178136
var dIntersection: f32 = diskIntersectWithBackFaceCulling(upEyePosition, p, h, radius);
179137
p = (upEyePosition + min(sIntersection, dIntersection) * p);

0 commit comments

Comments
 (0)