-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphong.rpu
62 lines (50 loc) · 1.99 KB
/
phong.rpu
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
// Based on https://www.shadertoy.com/view/XlXGDj
vec3 ambientColor = vec3(0.05, 0.15, 0.2);
vec3 diffuseColor = vec3(0.2, 0.6, 0.8);
vec3 specularColor = vec3(1.0, 1.0, 1.0);
vec3 lightDir = vec3(0.0, 4.0, 5.0);
vec3 spherePos = vec3(0.0, 0.5, 0.0);
float raytraceSphere(vec3 ro, vec3 rd, float tmin, float tmax, float r) {
vec3 ce = ro - spherePos;
float b = dot(rd, ce);
float c = dot(ce, ce) - r * r;
float t = b * b - c;
if (t > tmin) {
t = -b - sqrt(t);
if (t < tmax)
return t;
}
return -1.0;
}
export vec4 shader(vec2 coord, vec2 resolution) {
// Generate the uv with random jittering for anti-aliasing
vec2 p = (-resolution.xy + 2.0 * (coord.xy + vec2(rand(), rand()))) / resolution.y;
vec3 eye = vec3(0.0, 1.0, 2.0);
vec2 rot = 6.2831 * (vec2(0.1 + 150.0 * 0.25, 0.0) + vec2(1.0, 0.0) * (vec2(0) - resolution.xy * 0.25) / resolution.x);
eye.yz = cos(rot.y) * eye.yz + sin(rot.y) * eye.zy * vec2(-1.0, 1.0);
//eye.xz = cos(rot.x) * eye.xz + sin(rot.x) * eye.zx * vec2(1.0, -1.0);
vec3 ro = eye;
vec3 ta = vec3(0.0, 0.5, 0.0);
vec3 cw = normalize(ta - eye);
vec3 cu = normalize(cross(vec3(0.0, 1.0, 0.0), cw));
vec3 cv = normalize(cross(cw, cu));
mat3 cam = mat3(cu, cv, cw);
vec3 rd = cam * normalize(vec3(p.xy, 1.5));
vec3 color = vec3(0);
float tmin = 0.1;
float tmax = 50.0;
float t = raytraceSphere(ro, rd, tmin, tmax, 1.0);
if (t > tmin && t < tmax) {
vec3 pos = ro + rd * t;
vec3 norm = normalize(pos - spherePos);
float occ = 0.5 + 0.5 * norm.y;
float amb = clamp(0.5 + 0.5 * norm.y, 0.0, 1.0);
float dif = clamp(dot(lightDir, norm), 0.0, 1.0);
vec3 h = normalize(-rd + lightDir);
float spe = pow(clamp(dot(h, norm), 0.0, 1.0), 64.0);
color = amb * ambientColor * occ;
color += dif * diffuseColor * occ;
color += dif * spe * specularColor * occ;
}
return vec4(pow(color, 1.0 / 2.2), 1.0);
}