/
hyperpixel.js
57 lines (53 loc) 路 1.53 KB
/
hyperpixel.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
var HyperPixel = function(canvas) {
this.canvas = canvas;
this.width = canvas.width;
this.height = canvas.height;
this.camera = new THREE.OrthographicCamera(
0,
this.width,
0,
this.height,
1,
2000
);
this.camera.position.z = 2000;
this.scene = new THREE.Scene();
this.scene.fog = new THREE.Fog(0x050505, 2000, 3500);
var particles = this.width * this.height;
var geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(new Array(particles * 3), 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(new Array(particles * 3), 3)
);
var positions = geometry.attributes.position.array;
this.colors = geometry.attributes.color.array;
var i = 0;
var w = this.width;
var h = this.height;
for (var x = 0; x < w; x++) {
for (var y = 0; y < h; y++) {
var i = (y * w + x) * 3;
positions[i] = x + 0.5;
positions[i + 1] = y + 0.5;
positions[i + 2] = 10;
}
}
var material = new THREE.PointsMaterial({
size: 1,
vertexColors: THREE.VertexColors
});
this.particleSystem = points = new THREE.Points(geometry, material);
this.scene.add(this.particleSystem);
this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas });
};
HyperPixel.prototype.render = function(data) {
if (data) {
this.particleSystem.geometry.attributes.color.array = data;
}
this.particleSystem.geometry.attributes.color.needsUpdate = true;
this.renderer.render(this.scene, this.camera);
};