forked from patriciogonzalezvivo/lygia_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lighting_raymarching_glass_refraction.frag
142 lines (105 loc) · 3.58 KB
/
lighting_raymarching_glass_refraction.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
133
134
135
136
137
138
139
140
141
142
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform vec3 u_camera;
uniform samplerCube u_cubeMap;
uniform vec3 u_SH[9];
uniform vec3 u_light;
uniform vec3 u_lightColor;
uniform float u_time;
varying vec2 v_texcoord;
// #define SCENE_CUBEMAP u_cubeMap
// Defines
#define LIGHT_DIRECTION u_light
#define RESOLUTION u_resolution
#define LIGHT_COLOR vec3(.9294,.9294,.9294)
#define RAYMARCH_SAMPLES 256
#define RAYMARCH_MULTISAMPLE 4
/*
Uncomment Defines below to see more options results
*/
// #define RAYMARCH_GLASS_WAVELENGTH
// #define RAYMARCH_GLASS_EDGE_SHARPNESS 0.02
// #define RAYMARCH_GLASS_CHROMATIC_ABBERATION .052
// #define RAYMARCH_GLASS_ENABLE_REFLECTION
// #define RAYMARCH_GLASS_DENSITY 0.1
// #define RAYMARCH_GLASS_REFLECTION_EFFECT 0.
// #define RAYMARCH_GLASS_COLOR vec3 (1., 0., 0.)
#define RAYMARCH_MATERIAL_FNC raymarchGlassRender
vec3 raymarchGlassRender(vec3 ray,vec3 pos,vec3 nor,vec3 map);
/*
Uncomment Defines & functions below to see custom chromatic abberation
*/
// #define RAYMARCH_GLASS_MAP_FNC myOwnChromaticAbberation
// void myOwnChromaticAbberation(inout vec3 res, in vec3 rdIn, in vec3 rdOut, in vec3 pEnter, in vec3 pExit, in vec3 nEnter, in vec3 nExit, in float ior, in float roughness);
/*
COMMON IMPORT
*/
#include "lygia/space/ratio.glsl"
#include "lygia/sdf.glsl"
#include "lygia/color/space/linear2gamma.glsl"
#include "lygia/color/tonemap/reinhard.glsl"
/*
RAYMARCH IMPORTS
*/
#include "lygia/lighting/raymarch.glsl"
#include "lygia/lighting/ior.glsl"
#include "lygia/lighting/raymarch/glass.glsl"
void myOwnChromaticAbberation(inout vec3 res,in vec3 rdIn,in vec3 rdOut,in vec3 pEnter,in vec3 pExit,in vec3 nEnter,in vec3 nExit,in float ior,in float roughness){
rdOut = refract(rdIn, nExit, ior + RAYMARCH_GLASS_CHROMATIC_ABBERATION);
if(dot(rdOut, rdOut) == 0.)
rdOut = reflect(rdIn, nExit);
res.r = envMap(rdOut, roughness).r;
// Green
rdOut = refract(rdIn, nExit, ior);
if(dot(rdOut, rdOut) == 0.)
rdOut = reflect(rdIn, nExit);
res.g = envMap(rdOut, roughness).g;
// Blue
rdOut = refract(rdIn, nExit, ior - RAYMARCH_GLASS_CHROMATIC_ABBERATION);
if(dot(rdOut, rdOut) == 0.)
rdOut = reflect(rdIn, nExit);
res.b = envMap(rdOut, roughness).b;
}
vec4 raymarchMap(in vec3 pos) {
vec4 res = vec4(vec3(1.), 1.);
float roughness = 1.;
vec3 logoColor = vec3(roughness, 1., 1.);
pos += 1.5;
pos = opRepeat(pos, vec3(-1.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 3.);
pos -= 1.5;
res = opUnion(
res,
vec4(
logoColor,
// icosahedronSDF(pos - vec3(0., 0. ,0.), 2.)
cubeSDF(pos - vec3(0., 0. ,0.), 1.)
)
);
return res;
}
vec3 raymarchGlassRender(vec3 ray, vec3 pos, vec3 nor, vec3 map) {
if ( map.r + map.g + map.b <= 0.0 )
return tonemapReinhard( envMap(ray, 0.).rgb );
float roughness = 0.;
vec3 color = vec3(0.0);
vec3 glass = raymarchGlass(ray, pos, IOR_GLASS, roughness);
color += glass;
return color;
}
void main(void) {
vec4 color = vec4(vec3(0.0), 1.0);
vec2 pixel = 1.0/u_resolution.xy;
vec2 st = gl_FragCoord.xy * pixel;
vec2 uv = v_texcoord;
uv = ratio(uv, u_resolution);
vec4 marchRay = raymarch(u_camera, uv);
color.rgb += marchRay.rgb;
float t = marchRay.RAYMARCH_MAP_DISTANCE;
if(t > RAYMARCH_MAX_DIST)
// color.rgb = vec3(0.); // Background
color = linear2gamma(color);
gl_FragColor = color;
}