webgl_animation_cloth.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - cloth simulation</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #000;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. text-align: center;
  17. padding: 10px;
  18. z-index: 10;
  19. width: 100%;
  20. position: absolute;
  21. }
  22. a {
  23. text-decoration: underline;
  24. cursor: pointer;
  25. }
  26. #stats { position: absolute; top:0; left: 0 }
  27. #stats #fps { background: transparent !important }
  28. #stats #fps #fpsText { color: #aaa !important }
  29. #stats #fps #fpsGraph { display: none }
  30. </style>
  31. </head>
  32. <body>
  33. <div id="info">Simple Cloth Simulation<br/>
  34. Verlet integration with Constrains relaxation<br/>
  35. Toggle: <a onclick="rotate = !rotate;">Camera</a> |
  36. <a onclick="wind = !wind;">Wind</a> |
  37. <a onclick="sphere.visible = !sphere.visible;">Ball</a> |
  38. <a onclick="togglePins();">Pins</a>
  39. </div>
  40. <script src="../build/three.min.js"></script>
  41. <script src="js/Detector.js"></script>
  42. <script src="js/libs/stats.min.js"></script>
  43. <script src="js/Cloth.js"></script>
  44. <script type="x-shader/x-fragment" id="fragmentShaderDepth">
  45. uniform sampler2D texture;
  46. varying vec2 vUV;
  47. vec4 pack_depth( const in float depth ) {
  48. const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );
  49. const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );
  50. vec4 res = fract( depth * bit_shift );
  51. res -= res.xxyz * bit_mask;
  52. return res;
  53. }
  54. void main() {
  55. vec4 pixel = texture2D( texture, vUV );
  56. if ( pixel.a < 0.5 ) discard;
  57. gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );
  58. }
  59. </script>
  60. <script type="x-shader/x-vertex" id="vertexShaderDepth">
  61. varying vec2 vUV;
  62. void main() {
  63. vUV = 0.75 * uv;
  64. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  65. gl_Position = projectionMatrix * mvPosition;
  66. }
  67. </script>
  68. <script>
  69. /* testing cloth simulation */
  70. var pinsFormation = [];
  71. var pins = [6];
  72. pinsFormation.push( pins );
  73. pins = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
  74. pinsFormation.push( pins );
  75. pins = [ 0 ];
  76. pinsFormation.push( pins );
  77. pins = []; // cut the rope ;)
  78. pinsFormation.push( pins );
  79. pins = [ 0, cloth.w ]; // classic 2 pins
  80. pinsFormation.push( pins );
  81. pins = pinsFormation[ 1 ];
  82. function togglePins() {
  83. pins = pinsFormation[ ~~( Math.random() * pinsFormation.length ) ];
  84. }
  85. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  86. var container, stats;
  87. var camera, scene, renderer;
  88. var clothGeometry;
  89. var sphere;
  90. var object, arrow;
  91. var rotate = true;
  92. init();
  93. animate();
  94. function init() {
  95. container = document.createElement( 'div' );
  96. document.body.appendChild( container );
  97. // scene
  98. scene = new THREE.Scene();
  99. scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );
  100. // camera
  101. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  102. camera.position.y = 50;
  103. camera.position.z = 1500;
  104. scene.add( camera );
  105. // lights
  106. var light, materials;
  107. scene.add( new THREE.AmbientLight( 0x666666 ) );
  108. light = new THREE.DirectionalLight( 0xdfebff, 1.75 );
  109. light.position.set( 50, 200, 100 );
  110. light.position.multiplyScalar( 1.3 );
  111. light.castShadow = true;
  112. //light.shadowCameraVisible = true;
  113. light.shadowMapWidth = 1024;
  114. light.shadowMapHeight = 1024;
  115. var d = 300;
  116. light.shadowCameraLeft = -d;
  117. light.shadowCameraRight = d;
  118. light.shadowCameraTop = d;
  119. light.shadowCameraBottom = -d;
  120. light.shadowCameraFar = 1000;
  121. light.shadowDarkness = 0.5;
  122. scene.add( light );
  123. // cloth material
  124. var clothTexture = THREE.ImageUtils.loadTexture( 'textures/patterns/circuit_pattern.png' );
  125. clothTexture.wrapS = clothTexture.wrapT = THREE.RepeatWrapping;
  126. clothTexture.anisotropy = 16;
  127. var clothMaterial = new THREE.MeshPhongMaterial( { alphaTest: 0.5, ambient: 0xffffff, color: 0xffffff, specular: 0x030303, emissive: 0x111111, shiness: 10, map: clothTexture, side: THREE.DoubleSide } );
  128. // cloth geometry
  129. clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h );
  130. clothGeometry.dynamic = true;
  131. clothGeometry.computeFaceNormals();
  132. var uniforms = { texture: { type: "t", value: clothTexture } };
  133. var vertexShader = document.getElementById( 'vertexShaderDepth' ).textContent;
  134. var fragmentShader = document.getElementById( 'fragmentShaderDepth' ).textContent;
  135. // cloth mesh
  136. object = new THREE.Mesh( clothGeometry, clothMaterial );
  137. object.position.set( 0, 0, 0 );
  138. object.castShadow = true;
  139. object.receiveShadow = true;
  140. scene.add( object );
  141. object.customDepthMaterial = new THREE.ShaderMaterial( { uniforms: uniforms, vertexShader: vertexShader, fragmentShader: fragmentShader } );
  142. // sphere
  143. var ballGeo = new THREE.SphereGeometry( ballSize, 20, 20 );
  144. var ballMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  145. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  146. sphere.castShadow = true;
  147. sphere.receiveShadow = true;
  148. scene.add( sphere );
  149. // arrow
  150. arrow = new THREE.ArrowHelper( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 0 ), 50, 0xff0000 );
  151. arrow.position.set( -200, 0, -200 );
  152. // scene.add( arrow );
  153. // ground
  154. var groundTexture = THREE.ImageUtils.loadTexture( "textures/terrain/grasslight-big.jpg" );
  155. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  156. groundTexture.repeat.set( 25, 25 );
  157. groundTexture.anisotropy = 16;
  158. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );
  159. var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 20000, 20000 ), groundMaterial );
  160. mesh.position.y = -250;
  161. mesh.rotation.x = - Math.PI / 2;
  162. mesh.receiveShadow = true;
  163. scene.add( mesh );
  164. // poles
  165. var poleGeo = new THREE.BoxGeometry( 5, 375, 5 );
  166. var poleMat = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shiness: 100 } );
  167. var mesh = new THREE.Mesh( poleGeo, poleMat );
  168. mesh.position.x = -125;
  169. mesh.position.y = -62;
  170. mesh.receiveShadow = true;
  171. mesh.castShadow = true;
  172. scene.add( mesh );
  173. var mesh = new THREE.Mesh( poleGeo, poleMat );
  174. mesh.position.x = 125;
  175. mesh.position.y = -62;
  176. mesh.receiveShadow = true;
  177. mesh.castShadow = true;
  178. scene.add( mesh );
  179. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 255, 5, 5 ), poleMat );
  180. mesh.position.y = -250 + 750/2;
  181. mesh.position.x = 0;
  182. mesh.receiveShadow = true;
  183. mesh.castShadow = true;
  184. scene.add( mesh );
  185. var gg = new THREE.BoxGeometry( 10, 10, 10 );
  186. var mesh = new THREE.Mesh( gg, poleMat );
  187. mesh.position.y = -250;
  188. mesh.position.x = 125;
  189. mesh.receiveShadow = true;
  190. mesh.castShadow = true;
  191. scene.add( mesh );
  192. var mesh = new THREE.Mesh( gg, poleMat );
  193. mesh.position.y = -250;
  194. mesh.position.x = -125;
  195. mesh.receiveShadow = true;
  196. mesh.castShadow = true;
  197. scene.add( mesh );
  198. //
  199. renderer = new THREE.WebGLRenderer( { antialias: true } );
  200. renderer.setSize( window.innerWidth, window.innerHeight );
  201. renderer.setClearColor( scene.fog.color );
  202. container.appendChild( renderer.domElement );
  203. renderer.gammaInput = true;
  204. renderer.gammaOutput = true;
  205. renderer.shadowMapEnabled = true;
  206. //
  207. stats = new Stats();
  208. container.appendChild( stats.domElement );
  209. //
  210. window.addEventListener( 'resize', onWindowResize, false );
  211. sphere.visible = !true
  212. }
  213. //
  214. function onWindowResize() {
  215. camera.aspect = window.innerWidth / window.innerHeight;
  216. camera.updateProjectionMatrix();
  217. renderer.setSize( window.innerWidth, window.innerHeight );
  218. }
  219. //
  220. function animate() {
  221. requestAnimationFrame( animate );
  222. var time = Date.now();
  223. windStrength = Math.cos( time / 7000 ) * 20 + 40;
  224. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) ).normalize().multiplyScalar( windStrength );
  225. arrow.setLength( windStrength );
  226. arrow.setDirection( windForce );
  227. simulate(time);
  228. render();
  229. stats.update();
  230. }
  231. function render() {
  232. var timer = Date.now() * 0.0002;
  233. var p = cloth.particles;
  234. for ( var i = 0, il = p.length; i < il; i ++ ) {
  235. clothGeometry.vertices[ i ].copy( p[ i ].position );
  236. }
  237. clothGeometry.computeFaceNormals();
  238. clothGeometry.computeVertexNormals();
  239. clothGeometry.normalsNeedUpdate = true;
  240. clothGeometry.verticesNeedUpdate = true;
  241. sphere.position.copy( ballPosition );
  242. if ( rotate ) {
  243. camera.position.x = Math.cos( timer ) * 1500;
  244. camera.position.z = Math.sin( timer ) * 1500;
  245. }
  246. camera.lookAt( scene.position );
  247. renderer.render( scene, camera );
  248. }
  249. </script>
  250. </body>
  251. </html>