-
Notifications
You must be signed in to change notification settings - Fork 17
/
morph.html
191 lines (153 loc) · 5.53 KB
/
morph.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
179
180
181
182
183
184
185
186
187
188
189
190
191
<!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;
}
#time
{
position: absolute;
top: 1em;
width:100%;
height: 32px;
}
</style>
</head>
<body>
<script src="vendor/three.min.js"></script>
<script src="ShaderLoader.js"></script>
<script src="fbo.js"></script>
<!--<input id="time"type="range" min="0" max="1" step="0.001" >-->
<script>
var scene, camera, renderer;
var simulationnShader, slider, automatic;
window.onload = function() {
var sl = new ShaderLoader();
sl.loadShaders({
simulation_vs : "",
simulation_fs : "",
render_vs : "",
render_fs : ""
}, "./glsl/morph/", init );
};
function init()
{
slider = createSlider();
var w = window.innerWidth;
var h = window.innerHeight;
renderer = new THREE.WebGLRenderer();
renderer.setSize( w,h );
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(60,w/h, 1,10000 );
camera.position.z = 500;
var width = 256;
var height = 256;
//first model
var dataA = getRandomData( width, height, 256 );
var textureA = new THREE.DataTexture( dataA, width, height, THREE.RGBFormat, THREE.FloatType, THREE.DEFAULT_MAPPING, THREE.RepeatWrapping, THREE.RepeatWrapping );
textureA.needsUpdate = true;
//second model
var dataB = getSphere( width * height, 128 );
var textureB = new THREE.DataTexture( dataB, width, height, THREE.RGBFormat, THREE.FloatType, THREE.DEFAULT_MAPPING, THREE.RepeatWrapping, THREE.RepeatWrapping );
textureB.needsUpdate = true;
simulationShader = new THREE.ShaderMaterial({
uniforms: {
textureA: { type: "t", value: textureA },
textureB: { type: "t", value: textureB },
timer: { type: "f", value: 0}
},
vertexShader: ShaderLoader.get( "simulation_vs"),
fragmentShader: ShaderLoader.get( "simulation_fs")
});
var renderShader = new THREE.ShaderMaterial( {
uniforms: {
positions: { type: "t", value: null },
pointSize: { type: "f", value: 1 },
alpha: { type: "f", value:.5 }
},
vertexShader: ShaderLoader.get( "render_vs"),
fragmentShader: ShaderLoader.get( "render_fs"),
transparent: true,
blending:THREE.AdditiveBlending
} );
FBO.init( width,height, renderer, simulationShader, renderShader );
scene.add( FBO.particles );
window.addEventListener( "resize", onResize );
onResize();
update();
}
function createSlider()
{
var slider = document.createElement("input");
slider.setAttribute( 'type', "range" );
slider.setAttribute( 'min',0 );
slider.setAttribute( 'max',1 );
slider.setAttribute( 'step',0.01 );
slider.style.position = "absolute";
slider.style.top = "0";
slider.style.width = "100%";
slider.style.zIndex = "1";
document.body.appendChild(slider);
return slider;
}
//returns a Float32Array buffer 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;
}
//returns a Float32Array buffer of spherical 3D points
function getPoint(v,size)
{
v.x = Math.random() * 2 - 1 ;
v.y = Math.random() * 2 - 1 ;
v.z = Math.random() * 2 - 1 ;
if(v.length()>1)return getPoint(v,size);
return v.normalize().multiplyScalar(size);
}
function getSphere( count, size ){
var len = count * 3;
var data = new Float32Array( len );
var p = new THREE.Vector3();
for( var i = 0; i < len; i+=3 )
{
getPoint( p, size );
data[ i ] = p.x;
data[ i + 1 ] = p.y;
data[ i + 2 ] = p.z;
}
return data;
}
function onResize()
{
var w = window.innerWidth;
var h = window.innerHeight;
renderer.setSize(w,h);
camera.aspect = w/h;
camera.updateProjectionMatrix();
}
function update()
{
requestAnimationFrame(update);
//update params
simulationShader.uniforms.timer.value = parseFloat( slider.value );
FBO.particles.rotation.y -= Math.PI / 180 * .1;
//update simulation
FBO.update();
//render the particles at the new location
renderer.render( scene, camera );
}
</script>
</body>
</html>