-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
180 lines (149 loc) · 4.45 KB
/
main.js
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
define(function(require, exports, module) {
'use strict';
// todo: show fps meter and shader selector if #debug param is true only
var gl = require('three');
var Stats = require('stats');
var routing = require('./routing');
var shaders = require('./shaders');
var stats = new Stats();
document.body.appendChild(stats.domElement);
var size = new gl.Vector2(innerWidth, innerHeight)
.divideScalar(routing.hd ? 1 : 2);
var config = new gl.Vector3(1000, 0, 0);
var isRunning = false;
var time = 0;
var mouse = new gl.Vector2(0, 0);
var canvas = document.querySelector('canvas');
var renderer = new gl.WebGLRenderer({ canvas: canvas });
renderer.setSize(size.x, size.y);
var camera = new gl.PerspectiveCamera(45, size.x/size.y, 0.1, 100);
camera.position.set(0, 0, 1);
camera.lookAt(new gl.Vector3(0, 0, 0));
var scene = new gl.Scene();
var shader = new gl.ShaderMaterial({
vertexShader: shaders.vertex.projection,
fragmentShader: shaders.fragment[routing.shader || 'upstream'],
uniforms: {
time: { type: 'f', value: 0 },
resolution: { type: 'v2', value: size },
mouse: { type: 'v2', value: mouse },
config: { type: 'v3' }
}
});
var box = new gl.Mesh(new gl.BoxGeometry(1, size.y/size.x, 1), shader);
scene.add(box);
console.time('render');
renderer.render(scene, camera);
console.timeEnd('render');
var selectShader = document.getElementById('select-shader');
selectShader.value = routing.shader || 'upstream';
selectShader.addEventListener('change', onShaderChanged);
if (routing.start) {
start();
}
console.log('done');
///////////////////////////////////
// todo
module.exports = {
size, config, isRunning,
start, stop
};
///////////////////////////////////
function start() {
console.log('start');
config.x = 100;
isRunning = true;
time = Date.now();
loop();
}
function stop() {
console.log('stop');
config.x = 1000;
isRunning = false;
render();
}
function loop() {
stats.begin();
if (isRunning) {
requestAnimationFrame(loop);
step();
render();
}
stats.end();
}
function step() {
var now = Date.now();
shader.uniforms.time.value += (now - time) / 1000;
time = now;
}
function render() {
renderer.render(scene, camera);
}
///////////////////////////////////
function onKeyDown(e) {
switch (e.keyCode) {
case 87: // w
break;
case 83: // s
break;
case 65: // a
break;
case 68: // d
break;
case 38: // up
break;
case 40: // down
break;
}
}
function onKeyUp(e) {
switch (e.keyCode) {
case 32: // space
if (isRunning) {
stop();
} else {
start();
}
break;
}
console.log('keyup', e.keyCode);
}
function onMouseMove(e) {
mouse.x = e.clientX/window.innerWidth;
mouse.y = e.clientY/window.innerHeight;
mouse.multiplyScalar(2);
mouse.addScalar(-1);
if (!isRunning) {
render();
}
console.log('mousemove', mouse.x, mouse.y);
}
function onCLick() {
console.log('click');
if (isRunning) {
stop();
} else {
start();
}
}
function onShaderChanged() {
console.log('onShaderChanged', selectShader.value);
shader.fragmentShader = shaders.fragment[selectShader.value];
shader.needsUpdate = true;
setHash();
render();
}
///////////////////////////////////
function setHash() {
var hash = [];
hash.push('shader:' + selectShader.value);
if (routing.start) hash.push('start');
if (routing.hd) hash.push('hd');
location.hash = hash.join('/');
}
///////////////////////////////////
window.addEventListener('keydown', onKeyDown);
window.addEventListener('keyup', onKeyUp);
window.addEventListener('mousemove', onMouseMove);
canvas.addEventListener('click', onCLick);
});