-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
69 lines (69 loc) · 2.84 KB
/
App.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
///<reference path="libs/threejs/three.d.ts"/>
///<reference path="xShader/XShader.ts"/>
var App = (function () {
function App() {
//setup renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
var w = window.innerWidth;
var h = window.innerHeight;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, w / h, 0.1, 1000000);
camera.position.z = 2000;
document.getElementById("contener").appendChild(renderer.domElement);
//setup shader----------
var nbQuad = 10000;
var shader = new XShader(null);
shader.materialOptions.side = THREE.DoubleSide;
shader.materialOptions.depthTest = false;
shader.setVertexBuffer(XBuffer.createQuadXYZ_Buffer("vertices", nbQuad));
var centers = shader.setVertexBuffer(XBuffer.createQuadXYZ_Buffer("center", nbQuad));
shader.setVertexBuffer(XBuffer.createQuadRGBA_Buffer("quadColor", nbQuad, true)).isVarying = true;
shader.setIndexBuffer(XIndexBuffer.createQuadIndexBuffer(nbQuad));
var dim = shader.setVertexUniform("quadDimension", new THREE.Vector2(10, 10));
shader.vertex.define("\n vec4 pos = vec4(vertices,1.0);\n pos.xy *= quadDimension;\n pos.xyz += center;\n gl_Position = projectionMatrix * modelViewMatrix * pos;\n ");
shader.fragment.define("\n gl_FragColor = quadColor;\n ");
//----------------------
//create mesh
var mesh = shader.createMesh();
scene.add(mesh);
//update the buffer "center" in the shader
var v = centers.array;
var i, len = v.length / 12;
var px, py, pz, j;
for (i = 0; i < nbQuad; i++) {
px = -w / 2 + Math.random() * w;
py = -h / 2 + Math.random() * h;
pz = -h / 2 + Math.random() * h;
j = i * 12;
v[j + 0] += px;
v[j + 1] += py;
v[j + 2] += pz;
v[j + 3] += px;
v[j + 4] += py;
v[j + 5] += pz;
v[j + 6] += px;
v[j + 7] += py;
v[j + 8] += pz;
v[j + 9] += px;
v[j + 10] += py;
v[j + 11] += pz;
}
centers.updateGeometry();
//render !
var a = 0;
function animate() {
a += 0.05;
dim.value.x = 200 + Math.sin(a) * 100;
dim.value.y = 200 + Math.cos(a * 5) * 100;
mesh.rotation.x += 0.03;
mesh.rotation.y += 0.02;
mesh.rotation.z += 0.01;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
}
return App;
}());
new App();