-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.ts
92 lines (75 loc) · 2.94 KB
/
App.ts
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
///<reference path="libs/threejs/three.d.ts"/>
///<reference path="xShader/XShader.ts"/>
class App {
constructor() {
//setup renderer
var renderer:THREE.WebGLRenderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,window.innerHeight);
var w:number = window.innerWidth;
var h:number = window.innerHeight;
var scene:THREE.Scene = new THREE.Scene();
var camera:THREE.PerspectiveCamera = new THREE.PerspectiveCamera(60,w/h,0.1,1000000);
camera.position.z = 2000;
document.getElementById("contener").appendChild(renderer.domElement);
//setup shader----------
var nbQuad:number = 10000;
var shader:XShader = new XShader(null);
shader.materialOptions.side = THREE.DoubleSide;
shader.materialOptions.depthTest = false;
shader.setVertexBuffer(XBuffer.createQuadXYZ_Buffer("vertices",nbQuad));
var centers:XBuffer = shader.setVertexBuffer(XBuffer.createQuadXYZ_Buffer("center",nbQuad));
shader.setVertexBuffer(XBuffer.createQuadRGBA_Buffer("quadColor",nbQuad,true)).isVarying = true;
shader.setIndexBuffer(XIndexBuffer.createQuadIndexBuffer(nbQuad));
var dim:XElement = shader.setVertexUniform("quadDimension",new THREE.Vector2(10,10));
shader.vertex.define(`
vec4 pos = vec4(vertices,1.0);
pos.xy *= quadDimension;
pos.xyz += center;
gl_Position = projectionMatrix * modelViewMatrix * pos;
`)
shader.fragment.define(`
gl_FragColor = quadColor;
`)
//----------------------
//create mesh
var mesh:THREE.Mesh = shader.createMesh();
scene.add(mesh);
//update the buffer "center" in the shader
var v:Float32Array = centers.array;
var i:number,len:number = v.length/12;
var px:number,py:number,pz:number,j:number;
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:number = 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();
}
}
new App();