-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathrandom.glsl
More file actions
71 lines (56 loc) · 1.72 KB
/
Copy pathrandom.glsl
File metadata and controls
71 lines (56 loc) · 1.72 KB
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
#ifndef RANDOM_GLSL
#define RANDOM_GLSL
#include "common.glsl"
struct Random {
uint state;
};
Random srand(uint seed) {
Random r;
r.state = uint(uint(seed) * uint(26699)) | uint(1);
return r;
}
Random srand(uvec3 v3) {
Random r;
r.state = uint(v3.x * uint(1973) + v3.y * uint(9277) + v3.z * uint(26699)) | uint(1);
return r;
}
Random srand(uvec2 fragCoord, uint seed) {
Random r;
r.state = uint(uint(fragCoord.x) * uint(1973) + uint(fragCoord.y) * uint(9277) + uint(seed) * uint(26699)) | uint(1);
return r;
}
float randf(inout Random r) {
// return float(wangHash(r.state)) / 4294967296.0;
// return float(wangHash(r.state) >> 8) * (1.0 / 16777216.0); // 2^-24
uint x = wangHash(r.state);
return uintBitsToFloat(0x3F800000u | (x >> 9)) - 1.0;
}
vec3 randVec3(inout Random rng) {
float z = randf(rng) * 2.0 - 1.0;
float a = randf(rng) * 2.0 * M_PI;
float r = sqrt(1.0f - z * z);
float x = r * cos(a);
float y = r * sin(a);
return vec3(x, y, z);
}
vec3 randCosWeightedHemisphereDirection(const vec3 n, inout Random rng) {
vec2 rv2 = vec2(randf(rng), randf(rng));
vec3 uu = normalize( cross( n, vec3(0.0,1.0,1.0) ) );
vec3 vv = normalize( cross( uu, n ) );
float ra = sqrt(rv2.y);
float rx = ra*cos(6.2831*rv2.x);
float ry = ra*sin(6.2831*rv2.x);
float rz = sqrt( 1.0-rv2.y );
vec3 rr = vec3( rx*uu + ry*vv + rz*n );
return normalize(rr);
}
vec3 randCosWeightedHemisphereDirection(const mat3 tbn, inout Random rng) {
vec2 rv2 = vec2(randf(rng), randf(rng));
float ra = sqrt(rv2.y);
float rx = ra*cos(6.2831*rv2.x);
float ry = ra*sin(6.2831*rv2.x);
float rz = sqrt( 1.0-rv2.y );
vec3 rr = vec3( rx*tbn[0] + ry*tbn[1] + rz*tbn[2] );
return normalize(rr);
}
#endif