-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfrag-pass1.js
214 lines (161 loc) · 4.36 KB
/
frag-pass1.js
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
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D geotex;
uniform sampler2D dentex;
uniform vec4 globalinfo;
float Rmindist;
varying vec3 v_ray;
float minr, maxr;
varying vec2 v_texCoord;
uniform vec4 eye;
vec3 color;
uniform vec4 scale;
uniform vec4 texsize;
uniform vec4 invtexsize;
const vec3 lshw = vec3( 0. );
const int maxsteps = 256;
const float maxdist = 10.;
vec3 dircomps;
vec4 lastvox;
vec3 ptr;
vec3 dir;
vec4 sofarcolor;
float totaltravel;
vec3 lastnorm;
vec4 AtCell( vec3 pos )
{
pos /= scale.xyz;
vec4 v = texture2D( geotex, vec2( invtexsize.x * invtexsize.y * pos.x + invtexsize.y * pos.y, invtexsize.z * pos.z ) );
lastnorm = normalize((v.xyz-0.5)*2.0);
return texture2D( dentex, vec2( v.a*255.5/256.0, 0.0 ) );
}
bool already_hit;
float scal = 1.0;
float nscal = 1.0;
void UpdateSoFar()
{
float intensity = pow( lastvox.a, 2.73-Rmindist );
float qty = (( 1.0 - sofarcolor.a )) * intensity;
sofarcolor.rgb += ( dot( lastnorm.xyz, vec3(1.,1.,1.) ) / 2.0 + 1.0 ) * qty* lastvox.rgb;// * qty * dot( lastnorm, -dir );
sofarcolor.a += qty;
}
//Large-scale traversal function
void TraverseIn( )
{
vec3 dists;
//Load the firsxt voxel in.
lastvox = AtCell( ( floor(ptr) ) );
dircomps = -sign( dir );
for( int rstep = 0; rstep < maxsteps; rstep++ )
{
//Find the distance to the edges of our local cube. These
//are always positive values from 0 to 1.
vec3 nextsteps = fract( ptr * dircomps ) ;
//Find out how many units the intersection point between us and
//the next intersection is in ray space.
//dists = nextsteps / abs( dir );
dists = nextsteps / abs( dir );
//Find the closest axis. We do this so we don't overshoot a hit.
float mindist = 0.;
if( dists.x < dists.y && dists.x < dists.z )
{
mindist = dists.x;
nscal = 1.0;
}
else if( dists.y < dists.z )
{
mindist = dists.y;
nscal = .9;
}
else
{
mindist = dists.z;
nscal = .8;
}
mindist+=.001;
//Go there, plus a /tiny/ amount to prevent ourselves from hitting
//an infinite loop.
vec3 motion = (mindist+0.001) * dir;
//TotalDistanceTraversed += length( motion );
ptr += motion;
totaltravel += mindist;
Rmindist = mindist;
if( already_hit )
{
UpdateSoFar();
}
scal = nscal;
if( length( ptr - eye.xyz ) > maxr ) break;
if( ptr.x < 0.0 ) continue;
if( ptr.y < 0.0 ) continue;
if( ptr.z < 0.0 ) continue;
if( ptr.x >= texsize.x ) continue;
if( ptr.y >= texsize.y ) continue;
if( ptr.z >= texsize.z ) continue;
//Load the new voxel
//If it's a hit, we're good! Return
lastvox = AtCell( (floor(ptr) ) );
already_hit = true;
}
}
void Intersect( int axis, float dalong, vec2 minhit, vec2 maxhit )
{
//Use ptr / dir.
float dist;
vec2 hitplane;
if( axis == 0 )
{
dist = (dalong-ptr.x)/dir.x;
hitplane = ptr.yz + dir.yz * dist;
}
else if( axis == 1 )
{
dist = (dalong-ptr.y)/dir.y;
hitplane = ptr.xz + dir.xz * dist;
}
else
{
dist = (dalong-ptr.z)/dir.z;
hitplane = ptr.xy + dir.xy * dist;
}
float hit = 1.0;
if( hitplane.x < minhit.x || hitplane.y < minhit.y || hitplane.x > maxhit.x || hitplane.y > maxhit.y )
hit = 0.0;
if( hit > 0.5 )
{
if( dist < minr ) minr = dist;
if( dist > maxr ) maxr = dist;
}
}
void main()
{
already_hit = false;
sofarcolor = vec4( 0.0 );
ptr = vec3( eye.xyz );
dir = normalize(v_ray);
//Tricky: Intersect the bounds of our world.
minr = 10000.0;
maxr = -10000.0;
Intersect( 0, 0.0, vec2( 0.0, 0.0 ), vec2( texsize.y, texsize.z ) );
Intersect( 1, 0.0, vec2( 0.0, 0.0 ), vec2( texsize.x, texsize.z ) );
Intersect( 2, 0.0, vec2( 0.0, 0.0 ), vec2( texsize.x, texsize.y ) );
Intersect( 0, texsize.x, vec2( 0.0, 0.0 ), vec2( texsize.y, texsize.z ) );
Intersect( 1, texsize.y, vec2( 0.0, 0.0 ), vec2( texsize.x, texsize.z ) );
Intersect( 2, texsize.z, vec2( 0.0, 0.0 ), vec2( texsize.x, texsize.y ) );
minr -= .005;
maxr += .005;
//If we're inside the cube, we want it to end.
if( minr < 0.0 ) minr = 0.0;
if( minr < maxr )
{
ptr += dir * minr;
totaltravel = minr;
TraverseIn();
if( already_hit )
{
UpdateSoFar();
}
}
gl_FragColor = vec4( mix( vec3( .1, .1, .1 ), sofarcolor.rgb, sofarcolor.a ), 1. );
}