webgl_animation_cloth.html 8.3 KB
Newer Older
M
r74  
Mr.doob 已提交
1
<!DOCTYPE html>
M
Mr.doob 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<html lang="en">
	<head>
		<title>three.js webgl - cloth simulation</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				color: #000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
M
r72  
Mr.doob 已提交
17
				position: absolute;
M
Mr.doob 已提交
18 19
				padding: 10px;
				width: 100%;
M
r72  
Mr.doob 已提交
20
				text-align: center;
M
Mr.doob 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
			}

			a {
				text-decoration: underline;
				cursor: pointer;
			}

		</style>
	</head>

	<body>
		<div id="info">Simple Cloth Simulation<br/>
			Verlet integration with Constrains relaxation<br/>
			<a onclick="wind = !wind;">Wind</a> |
			<a onclick="sphere.visible = !sphere.visible;">Ball</a> |
			<a onclick="togglePins();">Pins</a>
		</div>

M
r76  
Mr.doob 已提交
39
		<script src="../build/three.js"></script>
M
Mr.doob 已提交
40 41

		<script src="js/Detector.js"></script>
M
r76  
Mr.doob 已提交
42
		<script src="js/controls/OrbitControls.js"></script>
M
Mr.doob 已提交
43 44 45 46 47 48
		<script src="js/libs/stats.min.js"></script>

		<script src="js/Cloth.js"></script>

		<script type="x-shader/x-fragment" id="fragmentShaderDepth">

M
r76  
Mr.doob 已提交
49 50
			#include <packing>

M
Mr.doob 已提交
51 52 53 54 55 56 57 58 59
			uniform sampler2D texture;
			varying vec2 vUV;

			void main() {

				vec4 pixel = texture2D( texture, vUV );

				if ( pixel.a < 0.5 ) discard;

M
r76  
Mr.doob 已提交
60
				gl_FragData[ 0 ] = packDepthToRGBA( gl_FragCoord.z );
M
Mr.doob 已提交
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

			}
		</script>

		<script type="x-shader/x-vertex" id="vertexShaderDepth">

			varying vec2 vUV;

			void main() {

				vUV = 0.75 * uv;

				vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );

				gl_Position = projectionMatrix * mvPosition;

			}

		</script>

		<script>

			/* testing cloth simulation */

			var pinsFormation = [];
M
r75  
Mr.doob 已提交
86
			var pins = [ 6 ];
M
Mr.doob 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

			pinsFormation.push( pins );

			pins = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
			pinsFormation.push( pins );

			pins = [ 0 ];
			pinsFormation.push( pins );

			pins = []; // cut the rope ;)
			pinsFormation.push( pins );

			pins = [ 0, cloth.w ]; // classic 2 pins
			pinsFormation.push( pins );

			pins = pinsFormation[ 1 ];


			function togglePins() {

M
r75  
Mr.doob 已提交
107
				pins = pinsFormation[ ~~ ( Math.random() * pinsFormation.length ) ];
M
Mr.doob 已提交
108 109 110 111 112 113 114 115 116 117

			}

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;
			var camera, scene, renderer;

			var clothGeometry;
			var sphere;
M
r72  
Mr.doob 已提交
118
			var object;
M
Mr.doob 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				// scene

				scene = new THREE.Scene();
				scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );

				// camera

				camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
M
r76  
Mr.doob 已提交
136
				camera.position.x = 1000;
M
Mr.doob 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
				camera.position.y = 50;
				camera.position.z = 1500;
				scene.add( camera );

				// lights

				var light, materials;

				scene.add( new THREE.AmbientLight( 0x666666 ) );

				light = new THREE.DirectionalLight( 0xdfebff, 1.75 );
				light.position.set( 50, 200, 100 );
				light.position.multiplyScalar( 1.3 );

				light.castShadow = true;

M
r75  
Mr.doob 已提交
153 154
				light.shadow.mapSize.width = 1024;
				light.shadow.mapSize.height = 1024;
M
Mr.doob 已提交
155 156 157

				var d = 300;

M
r75  
Mr.doob 已提交
158 159 160 161
				light.shadow.camera.left = - d;
				light.shadow.camera.right = d;
				light.shadow.camera.top = d;
				light.shadow.camera.bottom = - d;
M
Mr.doob 已提交
162

M
r75  
Mr.doob 已提交
163
				light.shadow.camera.far = 1000;
M
Mr.doob 已提交
164 165 166 167 168

				scene.add( light );

				// cloth material

M
r74  
Mr.doob 已提交
169 170
				var loader = new THREE.TextureLoader();
				var clothTexture = loader.load( 'textures/patterns/circuit_pattern.png' );
M
Mr.doob 已提交
171 172 173
				clothTexture.wrapS = clothTexture.wrapT = THREE.RepeatWrapping;
				clothTexture.anisotropy = 16;

M
r72  
Mr.doob 已提交
174 175 176 177 178 179
				var clothMaterial = new THREE.MeshPhongMaterial( {
					specular: 0x030303,
					map: clothTexture,
					side: THREE.DoubleSide,
					alphaTest: 0.5
				} );
M
Mr.doob 已提交
180 181

				// cloth geometry
M
r60  
Mr.doob 已提交
182
				clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h );
M
Mr.doob 已提交
183 184
				clothGeometry.dynamic = true;

M
r78  
Mr.doob 已提交
185
				var uniforms = { texture:  { value: clothTexture } };
M
Mr.doob 已提交
186 187 188 189 190 191 192 193 194 195
				var vertexShader = document.getElementById( 'vertexShaderDepth' ).textContent;
				var fragmentShader = document.getElementById( 'fragmentShaderDepth' ).textContent;

				// cloth mesh

				object = new THREE.Mesh( clothGeometry, clothMaterial );
				object.position.set( 0, 0, 0 );
				object.castShadow = true;
				scene.add( object );

M
r72  
Mr.doob 已提交
196 197 198 199 200 201
				object.customDepthMaterial = new THREE.ShaderMaterial( {
					uniforms: uniforms,
					vertexShader: vertexShader,
					fragmentShader: fragmentShader,
					side: THREE.DoubleSide
				} );
M
Mr.doob 已提交
202 203 204 205

				// sphere

				var ballGeo = new THREE.SphereGeometry( ballSize, 20, 20 );
M
r72  
Mr.doob 已提交
206
				var ballMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
M
Mr.doob 已提交
207 208 209 210 211 212 213 214

				sphere = new THREE.Mesh( ballGeo, ballMaterial );
				sphere.castShadow = true;
				sphere.receiveShadow = true;
				scene.add( sphere );

				// ground

M
r76  
Mr.doob 已提交
215
				var groundTexture = loader.load( 'textures/terrain/grasslight-big.jpg' );
M
Mr.doob 已提交
216 217 218
				groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
				groundTexture.repeat.set( 25, 25 );
				groundTexture.anisotropy = 16;
M
r72  
Mr.doob 已提交
219

M
r68  
Mr.doob 已提交
220
				var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );
M
Mr.doob 已提交
221

M
r69  
Mr.doob 已提交
222
				var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
M
r75  
Mr.doob 已提交
223
				mesh.position.y = - 250;
M
Mr.doob 已提交
224 225 226 227 228 229
				mesh.rotation.x = - Math.PI / 2;
				mesh.receiveShadow = true;
				scene.add( mesh );

				// poles

M
r66  
Mr.doob 已提交
230
				var poleGeo = new THREE.BoxGeometry( 5, 375, 5 );
M
r72  
Mr.doob 已提交
231
				var poleMat = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 100 } );
M
Mr.doob 已提交
232 233

				var mesh = new THREE.Mesh( poleGeo, poleMat );
M
r75  
Mr.doob 已提交
234 235
				mesh.position.x = - 125;
				mesh.position.y = - 62;
M
Mr.doob 已提交
236 237 238 239 240 241
				mesh.receiveShadow = true;
				mesh.castShadow = true;
				scene.add( mesh );

				var mesh = new THREE.Mesh( poleGeo, poleMat );
				mesh.position.x = 125;
M
r75  
Mr.doob 已提交
242
				mesh.position.y = - 62;
M
Mr.doob 已提交
243 244 245 246
				mesh.receiveShadow = true;
				mesh.castShadow = true;
				scene.add( mesh );

M
r66  
Mr.doob 已提交
247
				var mesh = new THREE.Mesh( new THREE.BoxGeometry( 255, 5, 5 ), poleMat );
M
r75  
Mr.doob 已提交
248
				mesh.position.y = - 250 + ( 750 / 2 );
M
Mr.doob 已提交
249 250 251 252 253
				mesh.position.x = 0;
				mesh.receiveShadow = true;
				mesh.castShadow = true;
				scene.add( mesh );

M
r66  
Mr.doob 已提交
254
				var gg = new THREE.BoxGeometry( 10, 10, 10 );
M
Mr.doob 已提交
255
				var mesh = new THREE.Mesh( gg, poleMat );
M
r75  
Mr.doob 已提交
256
				mesh.position.y = - 250;
M
Mr.doob 已提交
257 258 259 260 261 262
				mesh.position.x = 125;
				mesh.receiveShadow = true;
				mesh.castShadow = true;
				scene.add( mesh );

				var mesh = new THREE.Mesh( gg, poleMat );
M
r75  
Mr.doob 已提交
263 264
				mesh.position.y = - 250;
				mesh.position.x = - 125;
M
Mr.doob 已提交
265 266 267 268
				mesh.receiveShadow = true;
				mesh.castShadow = true;
				scene.add( mesh );

M
r76  
Mr.doob 已提交
269
				// renderer
M
Mr.doob 已提交
270 271

				renderer = new THREE.WebGLRenderer( { antialias: true } );
M
r70  
Mr.doob 已提交
272
				renderer.setPixelRatio( window.devicePixelRatio );
M
Mr.doob 已提交
273 274 275 276 277 278 279 280
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setClearColor( scene.fog.color );

				container.appendChild( renderer.domElement );

				renderer.gammaInput = true;
				renderer.gammaOutput = true;

M
r72  
Mr.doob 已提交
281
				renderer.shadowMap.enabled = true;
M
Mr.doob 已提交
282

M
r76  
Mr.doob 已提交
283 284 285 286 287 288 289
				// controls
				var controls = new THREE.OrbitControls( camera, renderer.domElement );
				controls.maxPolarAngle = Math.PI * 0.5;
				controls.minDistance = 1000;
				controls.maxDistance = 7500;

				// performance monitor
M
Mr.doob 已提交
290 291

				stats = new Stats();
M
r76  
Mr.doob 已提交
292
				container.appendChild( stats.dom );
M
Mr.doob 已提交
293 294 295 296 297

				//

				window.addEventListener( 'resize', onWindowResize, false );

M
r75  
Mr.doob 已提交
298
				sphere.visible = ! true
M
Mr.doob 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

			}

			//

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				var time = Date.now();

				windStrength = Math.cos( time / 7000 ) * 20 + 40;
				windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) ).normalize().multiplyScalar( windStrength );

M
r75  
Mr.doob 已提交
324
				simulate( time );
M
Mr.doob 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
				render();
				stats.update();

			}

			function render() {

				var p = cloth.particles;

				for ( var i = 0, il = p.length; i < il; i ++ ) {

					clothGeometry.vertices[ i ].copy( p[ i ].position );

				}

				clothGeometry.computeFaceNormals();
				clothGeometry.computeVertexNormals();

				clothGeometry.normalsNeedUpdate = true;
				clothGeometry.verticesNeedUpdate = true;

				sphere.position.copy( ballPosition );

				camera.lookAt( scene.position );

				renderer.render( scene, camera );

			}

		</script>
	</body>
</html>