-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShaderLab2.html
255 lines (188 loc) · 9.08 KB
/
ShaderLab2.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
<!DOCTYPE html>
<html>
<head>
<title>Shaders Lab</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1">
<style type="text/css">
body {
color: #ffffff;
font-family: Monospace;
font-size: 13px;
text-align: center;
background-color: #ffffff;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
padding: 5px;
}
a {color: #0080ff;}
</style>
</head>
<body>
<div id="WebGLOutput"></div>
<div id="info">Experiments with Shaders and WebGL. Made with <a href="http://threejs.org" target="_blank">three.js</a></div>
<script type="text/javascript" src="../../../libs/threejs/three.min.js"></script>
<script type="text/javascript" src="../../../libs/threejs/Dector.js"></script>
<script type="text/javascript" src="../../../libs/threejs/TrackballControls.js"></script>
<script type="text/javascript" src="../../../libs/threejs/stats.min.js"></script>
<script type="text/javascript" src="../../../libs/threejs/dat.gui.min.js"></script>
<script type="text/javascript" src="../../../libs/threejs/util.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
varying vec2 vUv;
void main() {
vUv = uv;
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * mvPosition;
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform vec2 resolution;
uniform float mouseX;
uniform float mouseY;
uniform float time;
varying vec2 vUv;
//uniform sample2D texture1;
// Example From https://www.shadertoy.com/view/MsSSRV
vec2 cmul(vec2 a, vec2 b) { return a * mat2(b.x, -b.y, b.y, b.x); }
vec2 cinv(vec2 z) { return vec2(z.x, -z.y) / dot(z, z); }
vec2 cdiv(vec2 a, vec2 b) { return cmul(a, cinv(b)); }
vec2 invmobius(vec2 z, vec2 a, vec2 b, vec2 c, vec2 d) { return cdiv(cmul(d, z) - b, a - cmul(c, z)); }
vec2 map(vec2 p) {
float localTime = time * 0.1;
vec2 t = vec2(sin(localTime), 0.0);
vec2 a = vec2(cos(localTime * 5.0), sin(localTime * 5.0));
vec2 b = vec2(0.0, 0.0);
vec2 c = t * cos(time * 0.25) * 10.0;
vec2 d = vec2(1.0, 1.0);
return invmobius(p, a, b, c, d);
}
void main(void) {
vec2 aspect = vec2(resolution.x / resolution.y, 1.0);
vec2 a = ((gl_FragCoord.xy + vec2(0.0, 0.0)) / resolution.xy * 2.0 - 1.0) * aspect;
vec2 b = ((gl_FragCoord.xy + vec2(1.0, 0.0)) / resolution.xy * 2.0 - 1.0) * aspect;
vec2 c = ((gl_FragCoord.xy + vec2(1.0, 1.0)) / resolution.xy * 2.0 - 1.0) * aspect;
vec2 d = ((gl_FragCoord.xy + vec2(0.0, 1.0)) / resolution.xy * 2.0 - 1.0) * aspect;
vec2 ma = map(a);
vec2 mb = map(b);
vec2 mc = map(c);
vec2 md = map(d);
float da = length(mb - ma);
float db = length(mc - mb);
float dc = length(md - mc);
float dd = length(ma - md);
/* try to anti-alias by flattening the colours when the transformation causes too much stretch */
float stretch = max(max(max(da, db), dc), dd);
float aa = 1.0 / (1.0 + stretch * stretch * 1000.0);
vec2 block = floor(ma * 10.0 + 0.5);
vec2 square = abs(ma - block / 10.0);
square *= aa;
float an = time * 0.1 ;
vec2 rot = cmul(square, vec2(cos(an), sin(an)));
vec3 r;
r.x = (0.5 + sin(rot.x * 100.0 + time * 0.5) * 0.5);
r.y = (0.5 + sin(rot.y * 100.0) * 0.5);
r.z = (0.5 + sin(square.y * 100.0 + time) * 0.5);
gl_FragColor = vec4(r, 1.0);
}
</script>
<script type="text/javascript">
var renderer,
scene,
camera,
cameraControl;
var uniforms,
stats,
mouseX,
mouseY,
clock;
// timer,
// frequency;
function initThreeJS() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("WebGLOutput").appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000);
scene.add(camera);
}
function createInteractionGUI() {
cameraControl = new THREE.TrackballControls(camera);
cameraControl.rotateSpeed = 1.0;
cameraControl.zoomSpeed = 1.2;
cameraControl.panSpeed = 0.8;
cameraControl.noZoom = false;
cameraControl.noPan = false;
cameraControl.staticMoving = false;
cameraControl.dynamicDampingFactor = 0.3;
stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
stats.domElement.style.zIndex = 100;
document.getElementById("WebGLOutput").appendChild(stats.domElement);
window.addEventListener("resize", onWindowResize, false);
}
function createContent() {
clock = new THREE.Clock();
timer = 0;
frequency = 0.05; // seconds
var geometry = new THREE.BoxGeometry(0.75, 0.75, 0.75);
uniforms = {time: {type: "f", value: 1.0},
mouseX: {type: "f", value: 0.0},
mouseY: {type: "f", value: 0.0},
resolution: {type: "v2", value: new THREE.Vector2()}
//, texture1: {type: "t", value: THREE.ImageUtils.loadTexture("textures/lava/cloud.png")}
};
//uniforms.texture1.value.wrapS = uniforms.texture1.value.wrapT = THREE.RepeatWrapping;
var material = new THREE.ShaderMaterial({uniforms: uniforms,
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent});
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
var target = new THREE.Vector3(0, 0, 0);
camera.position.set(0, 0, 2);
camera.lookAt(new THREE.Vector3(0, 0, 0));
cameraControl.target = new THREE.Vector3(0, 0, 0);
}
function onWindowResize() {
uniforms.resolution.value.x = window.innerWidth;
uniforms.resolution.value.y = window.innerHeight;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function onDocumentMouseMove(event) {
mouseX = event.clientX;
mouseY = event.clientY;
}
function onDocumentKeyDown(event) {
var keyCode = event.which;
if (keyCode == KEY_CODE.S) {
}
}
function animateScene() {
update();
draw();
requestAnimationFrame(animateScene);
}
function update() {
stats.update();
cameraControl.update();
uniforms.time.value += clock.getDelta();//0.05;
uniforms.mouseX.value = mouseX;
uniforms.mouseY.value = mouseY;
}
function draw() { renderer.render(scene, camera); }
initThreeJS();
createInteractionGUI();
createContent();
animateScene();
</script>
</body>
</html>