-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.glsl
423 lines (327 loc) · 11.6 KB
/
demo.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#define INFINITY 3.402823e+38
#define STACK_SIZE 32
#define FRAME_LOC 6u
#define BOUND_LOC (FRAME_LOC+2u)
#define NODE_LOC (BOUND_LOC+6u)
struct Traverse {
uint node;
float tmin,tmax;
} stk[STACK_SIZE];
uint read1u(uint i) {
#ifdef FROM_IMAGE
uvec2 s=uvec2(textureSize(iChannel0,0));
uvec2 uv=uvec2(i%s.x,i/s.x);
vec4 tex=texelFetch(iChannel0,ivec2(uv),0);
uvec4 bytes=uvec4(tex*255.0).abgr;
return (bytes.r<<24)|(bytes.g<<16)|(bytes.b<<8)|bytes.a;
#else
uint w=uint(textureSize(iChannel0,0).x);
uint x=i%w;
uint y=i/w;
return texelFetch(iChannel0,ivec2(x,y),0).r;
#endif
}
uvec2 read2u(uint i) {
return uvec2(read1u(i),read1u(i+1u));
}
uvec3 read3u(uint i) {
return uvec3(read2u(i),read1u(i+2u));
}
uvec4 read4u(uint i) {
return uvec4(read3u(i),read1u(i+3u));
}
vec4 uint_to_vec4(uint v) {
return vec4(v&0xffu,(v>>8u)&0xffu,(v>>16u)&0xffu,(v>>24u)&0xffu)/255.0;
}
uvec2 uint_to_uvec2(uint v) {
return uvec2(v&0xffffu,(v>>16u)&0xffffu);
}
vec2 fromBarycentric(float b1,float b2,vec2 a0,vec2 a1,vec2 a2) {
return (1.0-b1-b2 )*a0+b1*a1+b2*a2;
}
vec3 fromBarycentric(float b1,float b2,vec3 a0,vec3 a1,vec3 a2) {
return (1.0-b1-b2 )*a0+b1*a1+b2*a2;
}
vec4 fromBarycentric(float b1,float b2,vec4 a0,vec4 a1,vec4 a2) {
return (1.0-b1-b2 )*a0+b1*a1+b2*a2;
}
bool intersectTriangle(vec3 ro,vec3 rd,vec3 p0,vec3 p1,vec3 p2,out vec2 bcOut,out float tOut) {
//Compute s1
vec3 e1 = p1 - p0;
vec3 e2 = p2 - p0;
vec3 s1 = cross(rd, e2);
float divisor = dot(s1, e1);
if (divisor == 0.0) {
return false;
}
float invDivisor = 1.0 / divisor;
//Compute first barycentric coordinate
vec3 d = ro - p0;
float b1 = dot(d, s1) * invDivisor;
if(b1 < 0.0 || b1 > 1.0) {
return false;
}
//Compute second barycentric coordinate
vec3 s2 = cross(d, e1);
float b2 = dot(rd, s2) * invDivisor;
if (b2 < 0.0 || b1 + b2 > 1.0) {
return false;
}
//Compute t to intersection point
float t = dot(e2, s2) * invDivisor;
//
tOut = t;
bcOut=vec2(b1,b2);
return true;
}
bool searchTree(vec3 P,vec3 V,vec3 invV,float rayMax,
inout uint stkNum, out float tminOut,out float tmaxOut,
out uint primsStartOut,out uint primsNumOut) {
while(stkNum>0u) {
stkNum--;
uint node=stk[stkNum].node;
float tmin=stk[stkNum].tmin;
float tmax=stk[stkNum].tmax;
uint a=read1u(node);
uint type=a&3u;
if(rayMax < tmin) {
return false;
}
if(type==3u) { //leaf
uint primsNum=a>>2u;
if(primsNum!=0u) {
primsStartOut=read1u(node+1u);
primsNumOut=primsNum;
tminOut=tmin;
tmaxOut=tmax;
return true;
}
} else { //branch
uint axis=type;
uint aboveChild=a>>2u;
uint belowChild=node+2u;
float split=uintBitsToFloat(read1u(node+1u));
float tplane=(split-P[axis])*invV[axis];
bool belowFirst=(P[axis]<split) || (P[axis] == split && V[axis] >= 0.0);
uint firstNode=belowFirst?belowChild:aboveChild;
uint secondNode=belowFirst?aboveChild:belowChild;
if(tplane > tmax || tplane <= 0.0) {
stk[stkNum].node=firstNode;
stk[stkNum].tmin=tmin;
stk[stkNum].tmax=tmax;
stkNum++;
} else if (tplane < tmin) {
stk[stkNum].node=secondNode;
stk[stkNum].tmin=tmin;
stk[stkNum].tmax=tmax;
stkNum++;
} else {
stk[stkNum].node=secondNode;
stk[stkNum].tmin=tplane;
stk[stkNum].tmax=tmax;
stkNum++;
stk[stkNum].node=firstNode;
stk[stkNum].tmin=tmin;
stk[stkNum].tmax=tplane;
stkNum++;
}
}
}
return false;
}
bool intersectAabb(vec3 P,vec3 invV,vec3 bMin,vec3 bMax, out float enterOut,out float leaveOut) {
vec3 tmin = (bMin - P) * invV;
vec3 tmax = (bMax - P) * invV;
vec3 tnear = min(tmin, tmax);
vec3 tfar = max(tmin, tmax);
float enter = max(tnear.x, max(tnear.y, tnear.z)); //max(tnear.x, 0.0)
float exit = min(tfar.x, min(tfar.y, tfar.z));
enterOut=enter;
leaveOut=exit;
return exit > max(enter, 0.0); //exit>0.0 && enter<exit
}
bool intersectTree(vec3 P,vec3 V,vec3 invV,vec3 bmin,vec3 bmax,
out uvec4 out_tri, out vec2 out_bc, inout float last_t) {
float tmin,tmax;
if(!intersectAabb(P,invV,bmin,bmax,tmin,tmax)) {
return false;
}
stk[0].node=NODE_LOC;
stk[0].tmin=tmin;
stk[0].tmax=tmax;
uint stkNum=1u;
uint primsStart,primsNum;
while(searchTree(P,V,invV,INFINITY,stkNum,tmin,tmax,primsStart,primsNum)) {
for(uint i=0u;i<primsNum;i++) {
vec3 ps[3];
uint prim=(primsNum==1u)?primsStart:read1u(primsStart+i);
uvec4 tri=uvec4(read3u(prim),prim);//triangle inds
for(uint j=0u;j<3u;j++) {
ps[j]=uintBitsToFloat(read3u(tri[j]));
}
vec3 faceNor=normalize(cross(ps[1]-ps[0],ps[2]-ps[0]));
float t;
vec2 bc;
if(dot(faceNor,V)<0.0&&intersectTriangle(P,V,ps[0],ps[1],ps[2],bc,t)&&t<last_t) {
last_t=t;
out_bc=bc;
out_tri=tri;
}
}
if(last_t >= tmin && last_t <= tmax) {
break;
}
}
return (last_t<INFINITY);
}
bool intersectTreeP(vec3 P,vec3 V,vec3 invV,vec3 bmin,vec3 bmax,float rayMax) {
float tmin,tmax;
if(!intersectAabb(P,invV,bmin,bmax,tmin,tmax)) {
return false;
}
stk[0].node=NODE_LOC;
stk[0].tmin=tmin;
stk[0].tmax=tmax;
uint stkNum=1u;
uint primsStart,primsNum;
while(searchTree(P,V,invV,rayMax,stkNum,tmin,tmax,primsStart,primsNum)) {
for(uint i=0u;i<primsNum;i++) {
vec2 bc;
float t;
vec3 ps[3];
uint prim=(primsNum==1u)?primsStart:read1u(primsStart+i);
uvec3 tri=read3u(prim);//triangle inds
for(uint j=0u;j<3u;j++) {
ps[j]=uintBitsToFloat(read3u(tri[j]));
}
if(intersectTriangle(P,V,ps[0],ps[1],ps[2],bc,t)) {
if(t <= rayMax) {
return true;
}
}
}
}
return false;
}
vec3 calcPtLightCol(vec3 P,vec3 N,vec3 lPos,vec3 lAtten,vec3 mCol,vec3 lCol,float shininess,float strength) {
vec3 L=lPos.xyz-P;
float lDist=length(L);
L=L/lDist;
float atten = 1.0/dot(lAtten,vec3(1.0,lDist,lDist*lDist));
vec3 R=reflect(-L,N);
float NdotL = max(0.0,dot(N,L));
float NdotR = max(0.0, dot(N,R));
float spec = (NdotL > 0.0)?pow(NdotR,shininess*128.0)*strength:0.0;
float diffuse=NdotL;
return lCol*(mCol*diffuse+spec)*atten;
}
float calcFlare(vec3 ro,vec3 rd,vec3 lightPos2,float size) {
vec3 viewLightDir=normalize(lightPos2-ro);
float viewLightDist=length(lightPos2-ro);
float q = dot(rd,viewLightDir)*0.5+0.5;
float o = (1.0/viewLightDist)*size;
return clamp(pow(q,900.0/o)*1.0,0.0,2.0);
}
vec4 sampleNearest(vec2 tc,uint texStart,uvec2 texSize) {
uvec2 tc2=uvec2(mod(tc,1.0)*vec2(texSize));
uint ind=tc2.y*texSize.x+tc2.x;
return uint_to_vec4(read1u(texStart+ind));
}
vec4 sampleLinear(vec2 TexCoord,uint texStart,uvec2 texSize) {
vec2 texSizef=vec2(texSize);
vec2 invTexSizef=1.0/texSizef;
//vec2 uv=TexCoord*texSizef;
//uvec2 uvi=uvec2(uv);
vec2 uvf=fract(TexCoord*texSizef);// uv-vec2(uvi);
vec4 n0 = sampleNearest(TexCoord,texStart,texSize);
vec4 n1 = sampleNearest(TexCoord+vec2(invTexSizef.x,0.0),texStart,texSize);
vec4 n2 = sampleNearest(TexCoord+vec2(0.0,invTexSizef.y),texStart,texSize) ;
vec4 n3 = sampleNearest(TexCoord+invTexSizef,texStart,texSize);
return mix ( mix(n0,n1,uvf.x), mix(n2,n3,uvf.x), uvf.y );
}
vec3 render(vec3 ro,vec3 rd) {
vec3 invRd=1.0/rd;
vec3 bmin=uintBitsToFloat(read3u(BOUND_LOC+0u));
vec3 bmax=uintBitsToFloat(read3u(BOUND_LOC+3u));
uvec4 tri;
vec2 bc;
float t=INFINITY;
if(!intersectTree(ro,rd,invRd,bmin,bmax,tri,bc,t)) {
return vec3(0.0);
}
//
vec3 ns[3],cs[3];
vec2 tcs[3];
vec4 tangs[3];
uint mtrl=read1u(tri.w+3u);
vec4 mtrlCol=uint_to_vec4(read1u(mtrl+0u));
//if(mtrl>) {return vec3(1.0,0.0,0.0);}
for(uint j=0u;j<3u;j++) {
uint ind=tri[j];
ns[j]=(uint_to_vec4(read1u(ind+3u)).rgb*2.0-1.0);
//cs[j]=uint_to_vec4(read1u(ind+10u)).rgb;
tcs[j]=unpackHalf2x16(read1u(ind+4u));
tangs[j]=uint_to_vec4(read1u(ind+5u))*2.0-1.0;
// tangs[j]=uintBitsToFloat(read4u(ind+6u));
}
vec3 nor=normalize(fromBarycentric(bc.x,bc.y,ns[0],ns[1],ns[2]));
vec3 mCol=vec3(1.0);//=fromBarycentric(bc.x,bc.y,cs[0],cs[1],cs[2]);
vec2 tc=fromBarycentric(bc.x,bc.y,tcs[0],tcs[1],tcs[2]);
uint texLoc0=read1u(mtrl+1u+0u);
if(texLoc0!=0u) {
uvec2 texSize0=uint_to_uvec2(read1u(texLoc0));
if(useLinearFiltering) {
mCol*=sampleLinear(tc,texLoc0+1u,texSize0).rgb;
} else {
mCol*=sampleNearest(tc,texLoc0+1u,texSize0).rgb;
}
}
mCol*=mtrlCol.rgb;
vec3 pt=ro+rd*t;
vec3 eyeDir=normalize(ro-pt);
vec3 lightPos2=lightPos;
if(lightAnimate) {
lightPos2+=vec3(cos(iTime*0.1)*sin(iTime*0.1)*1.0,-1.0,-5.0+sin(iTime*0.5)*12.0);
}
//vec3 lightPos2=lightPos2+vec3(cos(iTime*0.25),0.0,sin(iTime*0.25))*2.0;
vec3 lightDir=normalize(lightPos2-pt);
vec3 invLightDir=1.0/lightDir;
float lightDist=length(lightPos2-pt);
//return dirLight(nor,eyeDir,rd,mCol,vec3(1.0),0.1, 0.1);
vec3 c= vec3(0.0);
if(!intersectTreeP(lightPos2,-lightDir,-invLightDir,bmin,bmax,lightDist-1e-4)) {
c=calcPtLightCol(pt,nor,lightPos2,vec3(1.0,0.01,0.001),mCol,vec3(1.0),0.2, 0.2);
} else {
c=mCol*0.1;
}
if(t>=length(lightPos2-ro)) {
c=mix(c,vec3(3.0),calcFlare(ro,rd,lightPos2,0.05));
}
return min(c,1.0);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
float fovy=0.7854;
float aspect=iResolution.x/iResolution.y;
//vec2 ms=(iMouse.xy==vec2(0.0))?vec2(0.0):(iMouse.xy/iResolution.xy)*2.0-1.0;
#ifdef SHADRON
//vec3 viewPos=vec3(0.0);
vec3 ro=shadron_CameraView[3].xyz*5.0;
//mat3 viewRot=lookRot(viewYaw,viewPitch);//mat3(1.0);
mat3 viewRot=transpose(mat3(shadron_CameraView));
//mat3 viewRot=mat3(shadron_CameraView[0].xyz,shadron_CameraView[1].xyz,shadron_CameraView[2].xyz);
#else
//mat3 viewRot=lookRot(ms.x*-4.0+3.14,ms.y*1.7);
//vec3 ro=vec3(2.0,2.0,-3.0);
//vec3 ro=vec3(1.0,3.0,1.0);
vec3 ro=viewPos;
//mat3 viewRot=orbitRot(ms.x*2.0,ms.y*2.0);
//vec3 ro=viewRot*vec3(0.0,0.0,10.0);
#endif
vec2 uv=fragCoord/iResolution.xy;
vec2 scr=uv*2.0-1.0;
vec3 primary=normalize(vec3(scr.x*aspect,scr.y,-1.0/tan(fovy/2.0)));
vec3 rd=normalize(viewRot*primary);
vec3 col=render(ro,rd);
//col=mix(col,vec3(1.0),step(abs(floor(length(fragCoord-iMouse.xy))-2.0),0.0));
fragColor=vec4(col,1.0);
}