forked from patriciogonzalezvivo/lygia_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_equirect.frag
79 lines (58 loc) · 2.23 KB
/
sample_equirect.frag
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
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_doubleBuffer0;
uniform sampler2D u_tex0;
uniform samplerCube u_cubeMap;
uniform vec3 u_SH[9];
uniform vec3 u_light;
uniform vec3 u_camera;
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
varying vec4 v_position;
varying vec4 v_color;
varying vec3 v_normal;
varying vec2 v_texcoord;
// #define SAMPLEEQUIRET_ITERATIONS 24
// #define SCENE_CUBEMAP u_tex0
#define SAMPLE_CUBE_FNC(CUBEMAP, NORM, LOD) sampleEquirect(CUBEMAP, NORM, LOD)
#include "lygia/math/const.glsl"
#include "lygia/math/mirror.glsl"
#include "lygia/math/saturate.glsl"
#include "lygia/generative/srandom.glsl"
#include "lygia/sample/equirect.glsl"
#include "lygia/lighting/raymarch/camera.glsl"
#include "lygia/lighting/envMap.glsl"
#include "lygia/lighting/reflection.glsl"
#include "lygia/lighting/fresnelReflection.glsl"
#include "lygia/lighting/ior/2f0.glsl"
#include "lygia/color/tonemap.glsl"
void main (void) {
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
vec2 pixel = 1.0/u_resolution.xy;
vec2 st = gl_FragCoord.xy * pixel;
vec2 uv = v_texcoord;
mat3 ca = raymarchCamera(u_camera);
vec3 dir = ca * normalize(vec3(st*2.0-1.0, 1.0));
#if defined(DOUBLE_BUFFER_0)
color = texture2D(u_doubleBuffer0, st) * 0.9;
vec3 offset = vec3(0.0);
offset = srandom3( vec3(st, u_time * 0.1)) * 0.01;
color += sampleEquirect(u_tex0, dir + offset, 3.0);
#elif defined(BACKGROUND)
vec4 backgroundBuffer = texture2D(u_doubleBuffer0, st);
color.rgb = backgroundBuffer.rgb / backgroundBuffer.a;
#else
float roughness = 0.1;
float metallic = 0.9;
vec3 ior = vec3(0.15, 0.14, 0.13); // IOR (Silver)
vec3 N = v_normal; // Normal
vec3 V = normalize(u_camera - v_position.xyz); // View
float NoV = dot(N, V); // Normal . View
vec3 R = reflection(V, N, roughness);
color.rgb = tonemap( envMap(R, roughness, metallic) );
color.rgb += tonemap( fresnelReflection(R, ior2f0(ior), NoV) ) * metallic;
#endif
gl_FragColor = color;
}