-
Notifications
You must be signed in to change notification settings - Fork 17
/
image.html
178 lines (139 loc) · 5.01 KB
/
image.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!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/image/", loadImage );
};
function loadImage()
{
var img = new Image();
img.onload = function(e)
{
init(e.target);
};
img.crossOrigin = "anonymous";
img.src = "img/noise.jpg";
}
function init(img)
{
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 = 250;
renderer = new THREE.WebGLRenderer();
renderer.setSize( w,h );
document.body.appendChild(renderer.domElement);
//width / height of the FBO
var width = img.width;
var height = img.height;
var elevation = 64;
var data = getImage( img, width, height, elevation );
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: 1 }
},
vertexShader: ShaderLoader.get( "render_vs" ),
fragmentShader: ShaderLoader.get( "render_fs" )
} );
//init the FBO
FBO.init( width,height, renderer, simulationShader, renderShader );
scene.add( FBO.particles );
//GO !
window.addEventListener( "resize", onResize );
onResize();
update();
}
//returns a Float32Array buffer of 3D points after an image
function getImage( img, width, height, elevation ){
var ctx = getContext( null, width, height );
ctx.drawImage(img, 0, 0);
var imgData = ctx.getImageData(0,0,width,height);
var iData = imgData.data;
var l = (width * height );
var data = new Float32Array( l * 3 );
for ( var i = 0; i < l; i++ ) {
var i3 = i * 3;
var i4 = i * 4;
data[ i3 ] = ( ( i % width ) - width * .5 );
data[ i3 + 1 ] = ( iData[i4] / 0xFF * 0.299 +iData[i4+1]/0xFF * 0.587 + iData[i4+2] / 0xFF * 0.114 ) * elevation;
data[ i3 + 2 ] = ( parseInt( i / width ) - height * .5 );
}
return data;
}
function getCanvas( w,h ){
var canvas = document.createElement( "canvas");
canvas.width = w || 512;
canvas.height = h || 512;
return canvas;
}
function getContext( canvas, w,h ){
canvas = canvas || getCanvas(w,h);
canvas.width = w || canvas.width;
canvas.height = h || canvas.height;
return canvas.getContext("2d");
}
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.position.y = -128;
FBO.particles.rotation.y -= Math.PI / 180 * .5;
camera.lookAt( FBO.particles.position );
//render the particles at the new location
renderer.render( scene, camera );
}
</script>
</body>
</html>