forked from patriciogonzalezvivo/lygia_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lighting_ssr.frag
132 lines (103 loc) · 3.5 KB
/
lighting_ssr.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
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
// Copyright Patricio Gonzalez Vivo, 2022 - http://patriciogonzalezvivo.com/
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_scene;
uniform sampler2D u_sceneNormal;
uniform sampler2D u_scenePosition;
uniform sampler2D u_sceneBuffer0; // specular
uniform mat4 u_viewMatrix;
uniform mat4 u_projectionMatrix;
uniform mat4 u_inverseProjectionMatrix;
uniform vec3 u_camera;
uniform float u_cameraFarClip;
uniform float u_cameraNearClip;
uniform vec3 u_light;
uniform vec3 u_lightColor;
uniform float u_lightFalloff;
uniform float u_lightIntensity;
uniform float u_iblLuminance;
uniform samplerCube u_cubeMap;
uniform vec3 u_SH[9];
#ifdef LIGHT_SHADOWMAP
uniform sampler2D u_lightShadowMap;
uniform mat4 u_lightMatrix;
varying vec4 v_lightCoord;
#endif
uniform vec2 u_resolution;
uniform float u_time;
varying vec4 v_position;
#ifdef MODEL_VERTEX_COLOR
varying vec4 v_color;
#endif
#ifdef MODEL_VERTEX_NORMAL
varying vec3 v_normal;
#endif
#ifdef MODEL_VERTEX_TEXCOORD
varying vec2 v_texcoord;
#endif
#ifdef MODEL_VERTEX_TANGENT
varying vec4 v_tangent;
varying mat3 v_tangentToWorld;
#endif
#define SURFACE_POSITION v_position
#define CAMERA_POSITION u_camera
#define IBL_LUMINANCE u_iblLuminance
#define LIGHT_POSITION u_light
#define LIGHT_COLOR u_lightColor
#define LIGHT_FALLOFF u_lightFalloff
#define LIGHT_INTENSITY u_lightIntensity
#define LIGHT_COORD v_lightCoord
#include "lygia/math/saturate.glsl"
#include "lygia/math/powFast.glsl"
#include "lygia/color/space/linear2gamma.glsl"
#include "lygia/lighting/pbr.glsl"
#include "lygia/lighting/material/new.glsl"
#include "lygia/lighting/toShininess.glsl"
#define SSR_FRESNEL
#include "lygia/lighting/ssr.glsl"
float checkBoard(vec2 uv, vec2 _scale) {
uv = floor(fract(uv * _scale) * 2.0);
return min(1.0, uv.x + uv.y) - (uv.x * uv.y);
}
void main(void) {
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
vec2 pixel = 1.0/u_resolution;
vec2 st = gl_FragCoord.xy * pixel;
#if defined(POSTPROCESSING)
color = texture2D(u_scene, st);
vec3 n = texture2D(u_sceneNormal, st).xyz;
float mask = texture2D(u_sceneBuffer0, st).r;
float opacity = 1.0;
float dist = 1.0;
vec2 uv = ssr(u_scenePosition, u_sceneNormal, st, pixel, opacity, dist);
color.rgb = mix(color.rgb, texture2D(u_scene, uv).rgb, mask * saturate(1.0-dist));
#else
vec2 uv = st;
#if defined(MODEL_VERTEX_TEXCOORD)
uv = v_texcoord;
#endif
Material material = materialNew();
// material.metallic = 0.0;
// material.roughness = 0.05;
#if defined(FLOOR) && defined(MODEL_VERTEX_TEXCOORD)
material.albedo.rgb = vec3(0.5) + checkBoard(v_texcoord, vec2(8.0)) * 0.5;
material.metallic = 0.8;
material.roughness = 0.5 + checkBoard(v_texcoord, vec2(8.0)) * 0.5;
#endif
#if defined(SCENE_BUFFER_0)
// SPECULAR BUFFER
vec3 L = normalize(LIGHT_POSITION - (SURFACE_POSITION).xyz);
vec3 N = normalize(material.normal);
vec3 V = normalize(CAMERA_POSITION - (SURFACE_POSITION).xyz);
color += saturate(specular(L, N, V, material.roughness));
float spec = saturate(.95 - material.roughness * 0.5);
color.rgb *= 1.0-saturate( (powFast(spec, 4.) * 0.8 + 1.6 * (1.0-material.metallic)) );
#else
// REGULAR PASS
color = pbr(material);
color = linear2gamma(color);
#endif
#endif
gl_FragColor = color;
}