-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
165 lines (149 loc) · 5.94 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
<!DOCTYPE html>
<html>
<head>
<style>
#container {
position: absolute;
display: block;
left: 0vw;
top: 0vh;
z-index: 2;
}
</style>
</head>
<body>
<canvas id="container"></canvas>
<script src="js/three.min.js"></script>
<script src="js/jquery-3.2.1.min.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
void main() {
gl_Position = vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
uniform sampler2D u_backbuffer;
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
vec3 color = texture2D(u_backbuffer, st).rgb;
color.g *= 0.0;
color.b *= 0.0;
gl_FragColor=vec4(color,1.0);
}
</script>
<script>
var container;
var camera, scene, renderer;
var uniforms;
var bytebuffer, lastbytebuffer, bufferheight, bufferwidth, datatex;
var mousepos = { x: 0, y: 0 };
var mousedown = false;
init();
animate();
function init() {
container = document.getElementById('container');
camera = new THREE.Camera();
camera.position.z = 2;
scene = new THREE.Scene();
bufferwidth = 500;
bufferheight = 500;
bytebuffer = new Uint8Array(bufferwidth * bufferheight);
lastbytebuffer = new Uint8Array(bufferwidth * bufferheight);
for (var i = 0 ; i < bufferwidth*bufferheight; i += (bufferwidth + 1)) {
bytebuffer[i] = 255;
lastbytebuffer[i] = 255;
}
for (var i = bufferwidth -2 ; i< bufferwidth*bufferheight; i += (bufferwidth - 1)) {
bytebuffer[i] = 255;
lastbytebuffer[i] = 255;
}
var geometry = new THREE.PlaneBufferGeometry(2, 2);
datatex = new THREE.DataTexture(bytebuffer,
bufferwidth,
bufferheight,
THREE.LuminanceFormat,
THREE.UnsignedByteType,
THREE.UVMapping);
uniforms = {
u_time: { type: "f", value: 1.0 },
u_resolution: { type: "v2", value: new THREE.Vector2() },
u_mouse: { type: "v2", value: new THREE.Vector2() },
u_backbuffer: { type: "sampler2D", value: datatex }
};
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);
renderer = new THREE.WebGLRenderer({ canvas: container });
renderer.setPixelRatio(window.devicePixelRatio);
onWindowResize();
window.addEventListener('resize', onWindowResize, false);
document.onmousemove = function (e) {
mousepos = { x: e.pageX, y: e.pageY };
}
document.onmousedown = function(e) {
mousedown = true;
}
document.onmouseup = function(e) {
mousedown = false;
}
}
function onWindowResize(event) {
renderer.setSize(window.innerWidth, window.innerHeight);
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
}
function animate() {
setTimeout( function() {
requestAnimationFrame(animate);
}, 1000/60);
if (mousedown) {
var mousecol = Math.floor((mousepos.x / $(document).width()) * bufferwidth);
var mouserow = bufferheight - Math.floor((mousepos.y / $(document).height()) * bufferheight);
var index = mousecol + mouserow*bufferheight;
var max = 2;
lastbytebuffer[index] = 255;
for (var i = 1 ; i < max; i++) {
lastbytebuffer[index + bufferwidth*i] =255;
lastbytebuffer[index - bufferwidth*i] =255;
lastbytebuffer[index + i] = 255;
lastbytebuffer[index - i] =255;
}
}
datatex.needsUpdate = true;
bytebuffer.forEach(function (x, index, array) {
var count = 0;
var row = Math.floor(index / bufferwidth);
var col = index % bufferwidth;
if (lastbytebuffer[index - bufferwidth] > 0) count++; // top
if (lastbytebuffer[index + bufferwidth] > 0) count++; // bottom
if (lastbytebuffer[index - 1] > 0) count++; // left
if (lastbytebuffer[index + 1] > 0) count++; //right
if (lastbytebuffer[index - bufferwidth + 1] > 0) count++; // top right
if (lastbytebuffer[index + bufferwidth + 1] > 0) count++; // bottom right
if (lastbytebuffer[index - bufferwidth - 1] > 0) count++; // top left
if (lastbytebuffer[index + bufferwidth - 1] > 0) count++; // bottom left
if (count <= 1 || count > 3) {
array[index] = 0;
} else if (count == 3) {
array[index] = 255;
}
});
bytebuffer.forEach(function (x, index) {
lastbytebuffer[index] = x;
});
datatex.needsUpdate = true;
render();
}
function render() {
uniforms.u_time.value += 0.05;
renderer.render(scene, camera);
}
</script>
</body>
</html>