-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbasic.html
More file actions
141 lines (108 loc) · 3.83 KB
/
Copy pathbasic.html
File metadata and controls
141 lines (108 loc) · 3.83 KB
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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>FBO</title>
<style>
head, body{
width:100%;
height:100%;
overflow: hidden;
top:0;
left:0;
margin:0;
padding:0;
}
</style>
</head>
<body>
<script src="vendor/three.min.js"></script>
<script src="ShaderLoader.js"></script>
<script src="fbo.js"></script>
<script>
var scene, camera, renderer;
window.onload = function() {
var sl = new ShaderLoader();
sl.loadShaders({
simulation_vs : "",
simulation_fs : "",
render_vs : "",
render_fs : ""
}, "./glsl/basic/", init );
};
function init()
{
var w = window.innerWidth;
var h = window.innerHeight;
//regular scene creation
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(60,w/h, 1,10000 );
camera.position.z = 500;
renderer = new THREE.WebGLRenderer();
renderer.setSize( w,h );
document.body.appendChild(renderer.domElement);
//width / height of the FBO
var width = 256;
var height = 256;
//populate a Float32Array of random positions
var data = getRandomData( width, height, 256 );
var positions = new THREE.DataTexture( data, width, height, THREE.RGBFormat, THREE.FloatType );
positions.needsUpdate = true;
//this will be used to update the particles' positions
var simulationShader = new THREE.ShaderMaterial({
uniforms: {
positions: { type: "t", value: positions }
},
vertexShader: ShaderLoader.get( "simulation_vs" ),
fragmentShader: ShaderLoader.get( "simulation_fs" )
});
//this will be used to represent the particles on screen
//note that 'positions' is a texture that will be set and updated during the FBO.update() call
var renderShader = new THREE.ShaderMaterial( {
uniforms: {
positions: { type: "t", value: null },
pointSize: { type: "f", value: 2 }
},
vertexShader: ShaderLoader.get( "render_vs" ),
fragmentShader: ShaderLoader.get( "render_fs" ),
transparent: true,
blending:THREE.AdditiveBlending
} );
//init the FBO
FBO.init( width,height, renderer, simulationShader, renderShader );
scene.add( FBO.particles );
//GO !
window.addEventListener( "resize", onResize );
onResize();
update();
}
//returns an array of random 3D coordinates
function getRandomData( width, height, size ){
var len = width * height * 3;
var data = new Float32Array( len );
while( len-- )data[len] = ( Math.random() -.5 ) * size ;
return data;
}
function onResize()
{
var w = window.innerWidth;
var h = window.innerHeight;
renderer.setSize(w,h);
camera.aspect = w/h;
camera.updateProjectionMatrix();
}
//update loop
function update()
{
requestAnimationFrame(update);
//update the simulation
FBO.update();
//update mesh
FBO.particles.rotation.x += Math.PI / 180 * .5;
FBO.particles.rotation.y -= Math.PI / 180 * .5;
//render the particles at the new location
renderer.render( scene, camera );
}
</script>
</body>
</html>