-
Notifications
You must be signed in to change notification settings - Fork 17
/
mesh.html
155 lines (122 loc) · 4.41 KB
/
mesh.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
<!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="vendor/BinaryLoader.js"></script>
<script src="vendor/OrbitControls.js"></script>
<script src="ShaderLoader.js"></script>
<script src="fbo.js"></script>
<script>
var scene, camera, renderer, controls;
window.onload = function() {
var sl = new ShaderLoader();
sl.loadShaders({
simulation_vs : "",
simulation_fs : "",
render_vs : "",
render_fs : ""
}, "./glsl/mesh/", loadMesh );
};
function loadMesh(){
var w = window.innerWidth;
var h = window.innerHeight;
//regular scene creation
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(60,w/h, 1,600 );
camera.position.z = 450;
controls = new THREE.OrbitControls(camera);
controls.minDistance = controls.maxDistance = camera.position.z;
renderer = new THREE.WebGLRenderer({
//optional
logarithmicDepthBuffer: true
});
renderer.setSize( w,h );
document.body.appendChild(renderer.domElement);
var bl = new THREE.BinaryLoader();
bl.load( "model/bust.js", init );
}
//distribution
function parseMesh(g){
var vertices = g.vertices;
console.log( vertices.length)
var total = vertices.length;
var size = parseInt( Math.sqrt( total * 3 ) + .5 );
var data = new Float32Array( size*size*3 );
for( var i = 0; i < total; i++ ) {
data[i * 3] = vertices[i].x;
data[i * 3 + 1] = vertices[i].y;
data[i * 3 + 2] = vertices[i].z;
}
return data;
}
function init( g ){
//populate a Float32Array of the geometry's vertices
var data = parseMesh(g);
var size = Math.sqrt( data.length / 3 );
var positions = new THREE.DataTexture( data, size, size, 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: 10 },
nearFar: { type: "v2", value:new THREE.Vector2( 150, 500 ) }
},
vertexShader: ShaderLoader.get( "render_vs" ),
fragmentShader: ShaderLoader.get( "render_fs" ),
transparent: true
} );
//init the FBO
FBO.init( size,size, renderer, simulationShader, renderShader );
scene.add( FBO.particles );
FBO.particles.rotation.y += Math.PI / 4 ;
//GO !
window.addEventListener( "resize", onResize );
onResize();
update();
}
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);
controls.update();
//update the simulation
FBO.update();
//render the particles at the new location
renderer.render( scene, camera );
}
</script>
</body>
</html>