-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
356 lines (320 loc) · 10.4 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!DOCTYPE html>
<!--
Raymarched shapes demo
Raymarched with hard shadows
Phong shading model, rendered with webgl
--TODO--
Ambient Occulusion
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- OG Meta Tags for Social Media Queries -->
<meta property="og:title" content="CERXA Engine"/>
<!--
Thumbnail here <meta property="og:image" content="https://cerxa.polarity.sh"/>
-->
<meta property="og:description" content="WebGL-Powered Volumetric Raytracing Engine" />
<meta property="og:author" content="polarity" />
<meta property="og:url" content= "https://cerxa.polarity.sh"/>
<meta property="og:type" content="website"/>
<script type="text/javascript" src="dat.gui.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/stats.js"></script>
<title>CERXA Engine</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
font-family: "Trebuchet MS", sans-serif;
overflow-x: hidden;
}
.canvas-wrapper {
position: relative;
width: 100vw;
height: 100vh;
background-color: rgb(0, 0, 127.5);
display: flex;
justify-content: center;
align-items: center;
}
.canvas-overlay {
position: absolute;
z-index: 1;
bottom: 0;
right: 0;
padding: .5rem 1rem;
color: #ffffff;
}
#canvas {
height: 100vmin;
width: 100vmin;
}
.ui{
text-align: center;
background-color: #ffffff;
}
ul {
display: table;
margin: 0 auto;
text-align: left;
list-style-position: inside
}
.title-wrapper{
margin-top: 30px;
}
.footer {
width: 100vw;
display: flex;
justify-content: space-between;
padding: 1rem;
}
.dg.ac {
z-index: 2;
}
.dg.a {
margin-right: 0;
}
</style>
</head>
<body>
<div class="canvas-wrapper">
<canvas id = "canvas">
Your browser does not currently support HTML5. <br> Please try Chrome, Firefox, or Edge.
</canvas>
<div class="canvas-overlay">
CERXA ▼
</div>
</div>
<div class = "ui">
<div class = "title-wrapper">
<h1>CERXA Engine</h1>
<p>WebGL Rasterization & Volumetric Raytracing Engine</p>
<p>From-scratch shaders and light-tracing algorithms written in GLSL with my WebGL boilerplate library <a href = "https://www.github.com/polarr/Impact">Impact</a>.</p>
<h3>Features:</h3>
<ul>
<li>Diffuse, Ambient, Specular Lighting</li>
<li>Dynamic Camera & Scale</li>
<li>Hard Shadows</li>
<li>Distance-Estimated Fractal Support</li>
</ul>
<div>
</div>
<div class = "footer">
<span>
© 2020-present
</span>
<span>
~<a href = "https://polarity.sh">polarity</a>
</span>
</div>
</body>
<script id = "vts" type = "vertex-shader">
attribute vec4 position;
precision highp float;
void main(){
gl_Position = position;
}
</script>
<script id = "fts" type = "fragment-shader">
precision highp float;
uniform vec2 res;
uniform float time;
uniform int fractal;
uniform float camZ;
uniform float THICC;
uniform float specularExp;
uniform float lightAngle;
float PI = 3.14;
float EPSILON = 0.001;
// rotation matrix around origin
vec3 rotate(vec3 p, vec3 r){
// d --> r
r.x *= PI/180.0;
r.y *= PI/180.0;
r.z *= PI/180.0;
mat3 xRot = mat3 (1,0,0,
0,cos(r.x),-sin(r.x),
0,sin(r.x), cos(r.x));
mat3 yRot = mat3 (cos(r.y),0,sin(r.y),
0,1,0,
-sin(r.y),0,cos(r.y));
mat3 zRot = mat3 (cos(r.z),-sin(r.z),0,
sin(r.z),cos(r.z),0,
0,0,1);
return xRot * yRot * zRot * p;
}
//http://blog.hvidtfeldts.net/index.php/2011/08/distance-estimated-3d-fractals-iii-folding-space/
float pyramidDE(vec3 p){
float scale = 1.75;
float offset = 2.5;
for (int n = 0; n < 16; ++n) {
if(p.x + p.y < 0.0){
p.xy = - p.yx;
}
if(p.x + p.z < 0.0){
p.xz = - p.zx;
}
if(p.y + p.z < 0.0){
p.zy = - p.yz;
}
p = p * scale - offset*(scale - 1.0);
}
return length(p) * pow(scale, - 16.0);
}
float foldingLimit = 1.50;
float fixedRadius2 = 2.0;
float minRadius2 = 0.0;
void sphereFold(inout vec3 p, inout float dp) {
float r2 = dot(p, p);
float temp = r2 < minRadius2 ? fixedRadius2/minRadius2 : (r2 < fixedRadius2 ? fixedRadius2/r2 : 1.0);
p *= temp;
dp *= temp;
}
void boxFold(inout vec3 p, inout float dp) {
p = clamp(p, -foldingLimit, foldingLimit) * 2.0 - p;
}
float mandelboxDE(vec3 p){
float scale = 2.48;
vec3 offset = p;
float dr = 1.0;
for (int n = 0; n < 7; ++n) {
boxFold(p, dr);
sphereFold(p, dr);
p = scale * p + offset;
dr = dr * abs(scale) + 1.0;
}
return length(p)/abs(dr);
}
float mandelbulbDE(vec3 p) {
float power = 8.0;
vec3 z = p;
float dr = 1.0;
float r;
for (int n = 0; n < 20; ++n) {
r = length(z);
if (r > 2.0){
break;
}
float theta = acos(z.z / r);
float phi = atan(z.y, z.x);
dr = pow(r, power - 1.0) * power * dr + 1.0;
float zr = pow(r, power);
theta *= power;
phi *= power;
z = p + vec3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) * zr;
}
return 0.5 * log(r) * r / dr;
}
//distance to box
float sdBox(vec3 p, vec3 b){
vec3 d = abs(p) - b;
return length(max(d, 0.0)) + min(max(d.x, max(d.y, d.z)), 0.0);
}
float mengerSpongeDE(vec3 p){
vec3 n1 = normalize(vec3(1, 0, -1));
vec3 n2 = normalize(vec3(0, 1, -1));
float s = 1.;
for (int n = 0; n < 5; ++n) {
//folding
p = abs(p);
p -= 2. * min(0., dot(p,n1)) * n1;
p -= 2. * min(0., dot(p,n2)) * n2;
//scale
p *= 3.;
s /= 3.;
//offset
p.z -= 1.;
p.z = -abs(p.z);
p.z += 1.;
p.x -= 2.;
p.y -= 2.;
}
float dis = sdBox(p, vec3(1));
dis *= s;
return dis;
}
float DE(vec3 p){
if (fractal == 0){
return pyramidDE(p);
}
if (fractal == 1){
return mandelboxDE(p);
}
if (fractal == 2){
return mandelbulbDE(p);
}
if (fractal == 3){
return mengerSpongeDE(p);
}
}
float NORM_EPSILON = 0.001;
vec3 approxNormal(vec3 p)
{
return normalize(vec3(
DE(p + vec3(NORM_EPSILON, 0, 0)) - DE(p - vec3(NORM_EPSILON, 0, 0)),
DE(p + vec3(0, NORM_EPSILON, 0)) - DE(p - vec3(0, NORM_EPSILON, 0)),
DE(p + vec3(0, 0, NORM_EPSILON)) - DE(p - vec3(0, 0, NORM_EPSILON))));
}
float hardshadow (vec3 origin, vec3 direction, float smoothness){
float distance = EPSILON;
float light = 1.0;
float eps = 0.1;
for (float iters = 0.0; iters < 200.0; ++iters){
vec3 pos = origin + distance * direction;
float nextDistance = DE(pos);
distance += nextDistance;
light = min(light, 1.0 - (eps - distance) / eps);
if (abs(nextDistance) < EPSILON/2.0){
return distance * smoothness;
}
}
//return max(light, 0.0);
return 1.0;
}
vec3 raymarch(vec3 origin, vec3 direction, vec3 light){
float distance = 0.0;
for (float iters = 0.0; iters < 200.0; ++iters){
vec3 pos = origin + distance * direction;
float nextDistance = DE(pos);
distance += nextDistance;
if (abs(nextDistance) < THICC){
vec3 normal = approxNormal(pos);
float diffuse = max(0.0, dot(normal, light));
// https://learnopengl.com/Lighting/Basic-Lighting
vec3 reflected = reflect(direction, normal);
float specular = pow(max(dot(reflected, light), 0.0), specularExp);
float shade = diffuse * 0.7 + specular * 0.3;
float occlusion = 1.0;
vec3 ambient = vec3(1.0, 1.0, 1.0);
return shade * ambient * occlusion * hardshadow(pos, light, 0.1);
}
if (distance > 100.0){
break;
}
}
// Background blue
return vec3(0.0, 0.0, 0.5);
}
void main(){
// window relative -> clip space
vec2 uv = gl_FragCoord.xy * res - 1.0;
// directional light
vec3 rot_axis = vec3(0.707107, 0, -0.707107);
vec3 rot = vec3(1.0, 0.8, 1.0);
vec3 light = normalize(rot * cos(lightAngle) + cross(rot_axis, rot) * sin(lightAngle) + rot_axis * dot(rot_axis, rot) * (1.0 - cos(lightAngle)));
//camera
vec3 rotation = vec3(0.0, mod(time, 360.0) * 30.0 + 45.0, 55.0);
vec3 cameraPosition = vec3(0.0, 0.0, camZ);
gl_FragColor = vec4(raymarch(rotate(cameraPosition, rotation), rotate(normalize(vec3(uv, 1.0)), rotation), light), 1.0);
}
</script>
<script src = "https://cdn.jsdelivr.net/gh/1e9end/Impact/main.js"></script><!-- My WebGL library -->
<script src = "main.js"></script>
<script></script>
</html>