-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
268 lines (227 loc) · 7.71 KB
/
index.html
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!D0CTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Reflection Hex</title>
<script src="script.js" type="text/javascript"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script>
<script src="gui.js" type="text/javascript"></script>
<script id="vs" type="vertex-shader">#version 300 es
in vec3 vPos;
out vec2 fragCoord;
out vec2 texCoord;
void main() {
fragCoord = (vPos.xy + vec2(1., 1.))/2.;
texCoord = vec2(fragCoord.x, 1. - fragCoord.y);
gl_Position = vec4(vPos, 1.);
}
</script>
<script id="buildingfs" type="fragment-shader">#version 300 es
precision mediump float;
in vec2 fragCoord;
layout (location = 0) out vec4 fragColor;
uniform vec2 resolution;
uniform vec2 omouse;
uniform vec2 mouse;
uniform float mousePress;
uniform float wheel;
uniform float time;
uniform float _Reflectance;
uniform float _Hex_height;
uniform float _Fog;
float smax( float a, float b, float k )
{
float res = exp( k*a ) + exp( k*b );
return log( res )/k;
}
#define SQRT3 1.7320508
#define SQRT0_75 0.8660254
float sdRoundHex(vec3 p, vec2 h, float r){
vec3 q;
q.xy = abs(p.xy);
q.z = p.z;
vec2 hex = vec2(SQRT0_75, .5);
float d1 = dot(q.xz, hex);
d1 = smax(d1, q.z, r);
float d2 = dot(q.xz, vec2(hex.x, -hex.y));
d2 = smax(d2, -q.z, r);
float d = smax(d1, d2, r);
return smax(q.y-h.y,d-h.x, r);
}
float sdPlane( vec3 p, vec3 n, float h )
{
// n must be normalized
return dot(p,n) + h;
}
float hash(float p) { p = fract(p * 0.011); p *= p + 7.5; p *= p + p; return fract(p); }
float noise(vec2 x) {
const vec2 step = vec2(110, 241);
vec2 i = floor(x);
vec2 f = fract(x);
// For performance, compute the base input to a 1D hash from the integer part of the argument and the
// incremental change to the 1D based on the 3D -> 1D wrapping
float n = dot(i, step);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(mix( hash(n + dot(step, vec2(0, 0))), hash(n + dot(step, vec2(1, 0))), u.x),
mix( hash(n + dot(step, vec2(0, 1))), hash(n + dot(step, vec2(1, 1))), u.x), u.y);
}
float dist_func(vec3 p){
p.y = mix(p.y+0.5, -p.y +1.5, step(.5, p.y));
vec3 q1 = p,
q2 = p;
vec2 hex = vec2(SQRT3, 1.);
vec2 halfhex = hex*0.5;
q1.xz = mod(p.xz, hex)-halfhex;
q2.xz = mod(p.xz-halfhex, hex)-halfhex;
float d = step(length(q1.xz), length(q2.xz));
vec3 q = mix(q2, q1, d);
vec2 id = p.xz-q.xz;
float h = _Hex_height*noise(id);
q.y -= h;
return min(sdRoundHex(q, vec2(0.35, h), 88.),
sdPlane( p, vec3(0,1,0), 0. ));
}
vec3 get_n(vec3 p){
float ep = 0.0001;
vec3 epx = vec3(ep, 0., 0.);
vec3 epy = vec3(0., ep, 0.);
vec3 epz = vec3(0., 0., ep);
return normalize(vec3(
dist_func(p+epx)-dist_func(p-epx),
dist_func(p+epy)-dist_func(p-epy),
dist_func(p+epz)-dist_func(p-epz)
));
}
float fog(float d, float density) {
float dd = d * density;
return exp(-dd * dd);
}
void main() {
float t = time*0.2;
float t2 = t - 0.2;
vec2 uv = (fragCoord * 2. - 1.)*resolution/min(resolution.x,resolution.y);
uv /= 1. + length(uv)*.1;
// camera
vec3 camera = vec3(0, 0.5, 0);
vec3 ro = vec3(0, 0 ,t2) + camera;
vec3 lookat = vec3(0, 0, t) + camera;
float zoom = wheel*0.1 + 1.;
vec3 f = normalize(lookat-ro);
vec3 vertical = normalize(cross(vec3(0,1,0), f));
vec3 horizontal = cross(f, vertical);
vec3 i = ro + f*zoom + uv.x * vertical + uv.y * horizontal;
// ray direction
vec3 rdir = normalize(i - ro);
vec3 ray = ro;
vec3 col = vec3(1.);
float d,
alpha = 1.;
float firsthitmarch = 0.;
vec3 hitpos = vec3(0);
for(int i = 0; i < 64; i++){
float d = dist_func(ray);
if(d < 0.001){
float fi = float(i);
float hitted = step(1., firsthitmarch); // ray has hit -> 1., not hit -> 0.
firsthitmarch = mix(fi, firsthitmarch, hitted);
hitpos += ray*hitted;
vec3 n = get_n(ray);
rdir = reflect(rdir,n);
ray += n*0.1;
alpha *= _Reflectance;
col = vec3(alpha);
}
ray += rdir * d;
}
col += fog(length(hitpos-ro), _Fog);
fragColor.xyz = col;
fragColor.w = 1.;
}
</script>
<script id="fsp" type="fragment-shader">#version 300 es
precision mediump float;
in vec2 fragCoord;
layout (location = 0) out vec4 fragColor;
uniform vec2 mouse;
uniform vec2 resolution;
uniform sampler2D previous;
// FXAA Reference source
// https://www.geeks3d.com/20110405/fxaa-fast-approximate-anti-aliasing-demo-glsl-opengl-test-radeon-geforce/3/
const float FXAA_SPAN_MAX = 64.0;
const float FXAA_REDUCE_MUL = 1.0 / 64.0;
const float FXAA_SUBPIX_SHIFT = 1.0 / 32.0;
vec3 FxaaPixelShader(
sampler2D tex, // Input texture.
vec4 posPos, // Output of FxaaVertexShader interpolated accross screen.
vec2 rcpFrame) // Constant { 1.0 / frameWidth, 1.0 / frameHeight }
{
#define FXAA_REDUCE_MIN (1.0 / 128.0)
vec3 rgbNW = texture(tex, posPos.zw).xyz;
vec3 rgbNE = texture(tex, posPos.zw + vec2(rcpFrame.x, 0)).xyz;
vec3 rgbSW = texture(tex, posPos.zw + vec2(0, rcpFrame.y)).xyz;
vec3 rgbSE = texture(tex, posPos.zw + vec2(rcpFrame.x, rcpFrame.y)).xyz;
vec3 rgbM = texture(tex, posPos.xy).xyz;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot(rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max(
(lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL),
FXAA_REDUCE_MIN);
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * rcpFrame.xy;
vec3 rgbA = (1.0 / 2.0) * (
texture(tex, posPos.xy + dir * (1.0 / 3.0 - 0.5)).xyz +
texture(tex, posPos.xy + dir * (2.0 / 3.0 - 0.5)).xyz);
vec3 rgbB = rgbA * (1.0 / 2.0) + (1.0 / 4.0) * (
texture(tex, posPos.xy + dir * (0.0 / 3.0 - 0.5)).xyz +
texture(tex, posPos.xy + dir * (3.0 / 3.0 - 0.5)).xyz);
float lumaB = dot(rgbB, luma);
if ((lumaB < lumaMin) || (lumaB > lumaMax))
{
return rgbA;
}
else
{
return rgbB;
}
}
void main() {
vec4 c = vec4(0.0);
vec2 rcpFrame = vec2(1.0 / resolution.x, 1.0 / resolution.y);
vec4 posPos = vec4(fragCoord, fragCoord - rcpFrame * (0.5 + FXAA_SUBPIX_SHIFT));
//vec3 original = texture(previous, fragCoord).xyz;
//vec3 col = vec3(0);
//if(abs(mouse.x-fragCoord.x) < 0.001)col += 1.;
//fragColor.xyz = mix(original, FxaaPixelShader(previous, posPos, rcpFrame), step(mouse.x, fragCoord.x))+col;
fragColor.xyz = FxaaPixelShader(previous, posPos, rcpFrame);
fragColor.w = 1.;
}
</script>
<style type="text/css">
body{
margin : 0px ;
padding : 0px ;
}
body #wrapper{
width: 100%;
height: 100%;
position: fixed;
}
</style>
</head>
<body>
<div id="wrapper">
<canvas id="canvas"></canvas>
</div>
</body>
</html>