Skip to content

Commit 6a149de

Browse files
authored
Improve ray marching in volume rendering examples (#32235)
1 parent 8607598 commit 6a149de

File tree

6 files changed

+17
-24
lines changed

6 files changed

+17
-24
lines changed
420 Bytes
Loading
-6.76 KB
Loading
231 Bytes
Loading

examples/webgl_volume_cloud.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,7 @@
197197
198198
bounds.x = max( bounds.x, 0.0 );
199199
200-
vec3 p = vOrigin + bounds.x * rayDir;
201-
vec3 inc = 1.0 / abs( rayDir );
202-
float delta = min( inc.x, min( inc.y, inc.z ) );
203-
delta /= steps;
200+
float stepSize = ( bounds.y - bounds.x ) / steps;
204201
205202
// Jitter
206203
@@ -209,13 +206,16 @@
209206
uint seed = uint( gl_FragCoord.x ) * uint( 1973 ) + uint( gl_FragCoord.y ) * uint( 9277 ) + uint( frame ) * uint( 26699 );
210207
vec3 size = vec3( textureSize( map, 0 ) );
211208
float randNum = randomFloat( seed ) * 2.0 - 1.0;
209+
vec3 p = vOrigin + bounds.x * rayDir;
212210
p += rayDir * randNum * ( 1.0 / size );
213211
214212
//
215213
216214
vec4 ac = vec4( base, 0.0 );
217215
218-
for ( float t = bounds.x; t < bounds.y; t += delta ) {
216+
for ( float i = 0.0; i < steps; i += 1.0 ) {
217+
218+
float t = bounds.x + i * stepSize;
219219
220220
float d = sample1( p + 0.5 );
221221
@@ -229,7 +229,7 @@
229229
230230
if ( ac.a >= 0.95 ) break;
231231
232-
p += rayDir * delta;
232+
p += rayDir * stepSize;
233233
234234
}
235235

examples/webgl_volume_instancing.html

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@
8888
8989
uniform sampler3D map;
9090
91-
uniform float threshold;
92-
uniform float steps;
93-
9491
vec2 hitBox( vec3 orig, vec3 dir ) {
9592
const vec3 box_min = vec3( - 0.5 );
9693
const vec3 box_max = vec3( 0.5 );
@@ -155,26 +152,25 @@
155152
156153
bounds.x = max( bounds.x, 0.0 );
157154
158-
vec3 p = instRayOrigin + bounds.x * instRayDirection;
159-
vec3 inc = 1.0 / abs( instRayDirection );
160-
float delta = min( inc.x, min( inc.y, inc.z ) );
161-
delta /= 50.0;
155+
float stepSize = ( bounds.y - bounds.x ) / 100.0;
156+
157+
vec3 p;
162158
163159
// march through the volume
164-
for ( float t = bounds.x; t < bounds.y; t += delta ) {
160+
for ( float i = 0.0; i < 100.0; i += 1.0 ) {
165161
162+
float t = bounds.x + i * stepSize;
163+
p = instRayOrigin + t * instRayDirection;
166164
float d = sample1( p + 0.5 );
167165
168166
if ( d > 0.5 ) {
169167
170-
color.rgb = p * 2.0; // normal( p + 0.5 ); // * 0.5 + ( p * 1.5 + 0.25 );
168+
color.rgb = p * 2.0;
171169
color.a = 1.;
172170
break;
173171
174172
}
175173
176-
p += instRayDirection * delta;
177-
178174
}
179175
180176
if ( color.a == 0.0 ) discard;

examples/webgl_volume_perlin.html

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,12 @@
166166
167167
bounds.x = max( bounds.x, 0.0 );
168168
169-
vec3 p = vOrigin + bounds.x * rayDir;
170-
vec3 inc = 1.0 / abs( rayDir );
171-
float delta = min( inc.x, min( inc.y, inc.z ) );
172-
delta /= steps;
169+
float stepSize = ( bounds.y - bounds.x ) / steps;
173170
174-
for ( float t = bounds.x; t < bounds.y; t += delta ) {
171+
for ( float i = 0.0; i < steps; i += 1.0 ) {
175172
173+
float t = bounds.x + i * stepSize;
174+
vec3 p = vOrigin + t * rayDir;
176175
float d = sample1( p + 0.5 );
177176
178177
if ( d > threshold ) {
@@ -183,8 +182,6 @@
183182
184183
}
185184
186-
p += rayDir * delta;
187-
188185
}
189186
190187
if ( color.a == 0.0 ) discard;

0 commit comments

Comments
 (0)