-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathraymarching_steel-frame.html
179 lines (153 loc) · 5.06 KB
/
raymarching_steel-frame.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
<!DOCTYPE html>
<!--
Copyright (c) 2015 gam0022
Released under the MIT license
http://opensource.org/licenses/mit-license.php
-->
<html>
<head>
<meta charset="utf-8">
<meta name="twitter:card" content="player" />
<meta name="twitter:site" content="@gam0022" />
<meta name="twitter:creator" content="@gam0022" />
<meta name="twitter:title" content="Steel Frame" />
<meta name="twitter:description" content="Steel Frame" />
<meta name="twitter:image" content="https://gam0022.net/images/works/steel_frame.png" />
<meta name="twitter:player" content="https://gam0022.net/webgl/raymarching_steel-frame.html" />
<meta name="twitter:player:width" content="512" />
<meta name="twitter:player:height" content="512" />
<title>Steel Frame</title>
<style type="text/css">
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<script id="fragment_shader" type="x-shader/x-fragment">
precision highp float;
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
const float EPS = 0.001;
// distance functions
vec2 onRep(vec2 p, float interval) {
return mod(p, interval) - interval * 0.5;
}
float barDist(vec2 p, float interval, float width) {
return length(max(abs(onRep(p, interval)) - width, 0.0));
}
float tubeDist(vec2 p, float interval, float width) {
return length(onRep(p, interval)) - width;
}
float sceneDist(vec3 p) {
float bar_x = barDist(p.yz, 1.0, 0.1);
float bar_y = barDist(p.xz, 1.0, 0.1);
float bar_z = barDist(p.xy, 1.0, 0.1);
float tube_x = tubeDist(p.yz, 0.1, 0.025);
float tube_y = tubeDist(p.xz, 0.1, 0.025);
float tube_z = tubeDist(p.xy, 0.1, 0.025);
return max(max(max(min(min(bar_x, bar_y),bar_z), -tube_x), -tube_y), -tube_z);
}
vec3 getNormal(vec3 p) {
return normalize(vec3(
sceneDist(p + vec3( EPS, 0.0, 0.0)) - sceneDist(p + vec3( -EPS, 0.0, 0.0)),
sceneDist(p + vec3(0.0, EPS, 0.0)) - sceneDist(p + vec3(0.0, -EPS, 0.0)),
sceneDist(p + vec3(0.0, 0.0, EPS)) - sceneDist(p + vec3(0.0, 0.0, -EPS))
));
}
void main(void) {
// fragment position
vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y);
// camera and ray
vec3 cPos = vec3(mouse.x - 0.5, mouse.y - 0.5, time);
vec3 cUp = normalize(vec3(0.1, 0.4, 0.0));
vec3 cDir = cross(cUp, vec3(-1.0, 0.0, 0.0));
vec3 cSide = cross(cDir, cUp);
float targetDepth = 1.0;
vec3 ray = normalize(cSide * p.x + cUp * p.y + cDir * targetDepth);
// direction light
vec3 lightDir = normalize(vec3(1, 1, -2));
// marching loop
float dist;
float depth = 0.0;
vec3 dPos = cPos;
for(int i = 0; i < 64; i++){
dist = sceneDist(dPos);
depth += dist;
dPos = cPos + depth * ray;
if (abs(dist) < EPS) break;
}
// hit check
vec3 color;
if (abs(dist) < EPS) {
vec3 normal = getNormal(dPos);
float diffuse = clamp(dot(lightDir, normal), 0.1, 1.0);
color = vec3(1.0, 0.1, 0.1) * diffuse;
} else {
color = vec3(0.0);
}
gl_FragColor = vec4(color + 0.05 * depth, 1.0);
}
</script>
<script id="vertex_shader" type="x-shader/x-vertex">
void main(void) {
gl_Position = vec4(position, 1.0);
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r73/three.min.js" type="text/javascript"></script>
<script type="text/javascript">
var scene, camera, renderer;
var geometry, material, mesh;
var mouse = new THREE.Vector2(0.5, 0.5);
var canvas;
init();
render();
function init() {
scene = new THREE.Scene();
camera = new THREE.Camera();
geometry = new THREE.PlaneBufferGeometry(2.0, 2.0);
material = new THREE.ShaderMaterial({
uniforms: {
time: { type: "f", value: 0.0 },
resolution: { type: "v2", value: new THREE.Vector2(512.0, 512.0) },
mouse: { type: "v2", value: mouse }
},
vertexShader: document.getElementById('vertex_shader').textContent,
fragmentShader: document.getElementById('fragment_shader').textContent
});
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setSize(512.0, 512.0);
canvas = renderer.domElement;
canvas.addEventListener('mousemove', onMouseMove);
document.body.appendChild(canvas);
}
function render(timestamp) {
requestAnimationFrame(render);
material.uniforms.time.value = timestamp * 0.001;
material.uniforms.mouse.value = mouse;
renderer.render(scene, camera);
}
function onMouseMove(e) {
mouse.x = e.offsetX / canvas.width;
mouse.y = e.offsetY / canvas.height;
}
</script>
</body>
</html>