webgl_animation_cloth.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. position: absolute;
  17. padding: 10px;
  18. width: 100%;
  19. text-align: center;
  20. }
  21. a {
  22. text-decoration: underline;
  23. cursor: pointer;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">Simple Cloth Simulation<br/>
  29. Verlet integration with Constrains relaxation<br/>
  30. Toggle: <a onclick="rotate = !rotate;">Camera</a> |
  31. <a onclick="wind = !wind;">Wind</a> |
  32. <a onclick="sphere.visible = !sphere.visible;">Ball</a> |
  33. <a onclick="togglePins();">Pins</a>
  34. </div>
  35. <script src="../build/three.js"></script>
  36. <script src="js/Detector.js"></script>
  37. <script src="js/libs/stats.min.js"></script>
  38. <script src="js/Cloth.js"></script>
  39. <script type="x-shader/x-fragment" id="fragmentShaderDepth">
  40. uniform sampler2D texture;
  41. varying vec2 vUV;
  42. vec4 pack_depth( const in float depth ) {
  43. const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );
  44. const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );
  45. vec4 res = fract( depth * bit_shift );
  46. res -= res.xxyz * bit_mask;
  47. return res;
  48. }
  49. void main() {
  50. vec4 pixel = texture2D( texture, vUV );
  51. if ( pixel.a < 0.5 ) discard;
  52. gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );
  53. }
  54. </script>
  55. <script type="x-shader/x-vertex" id="vertexShaderDepth">
  56. varying vec2 vUV;
  57. void main() {
  58. vUV = 0.75 * uv;
  59. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  60. gl_Position = projectionMatrix * mvPosition;
  61. }
  62. </script>
  63. <script>
  64. /* testing cloth simulation */
  65. var pinsFormation = [];
  66. var pins = [ 6 ];
  67. pinsFormation.push( pins );
  68. pins = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
  69. pinsFormation.push( pins );
  70. pins = [ 0 ];
  71. pinsFormation.push( pins );
  72. pins = []; // cut the rope ;)
  73. pinsFormation.push( pins );
  74. pins = [ 0, cloth.w ]; // classic 2 pins
  75. pinsFormation.push( pins );
  76. pins = pinsFormation[ 1 ];
  77. function togglePins() {
  78. pins = pinsFormation[ ~~ ( Math.random() * pinsFormation.length ) ];
  79. }
  80. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  81. var container, stats;
  82. var camera, scene, renderer;
  83. var clothGeometry;
  84. var sphere;
  85. var object;
  86. var rotate = true;
  87. init();
  88. animate();
  89. function init() {
  90. container = document.createElement( 'div' );
  91. document.body.appendChild( container );
  92. // scene
  93. scene = new THREE.Scene();
  94. scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );
  95. // camera
  96. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  97. camera.position.y = 50;
  98. camera.position.z = 1500;
  99. scene.add( camera );
  100. // lights
  101. var light, materials;
  102. scene.add( new THREE.AmbientLight( 0x666666 ) );
  103. light = new THREE.DirectionalLight( 0xdfebff, 1.75 );
  104. light.position.set( 50, 200, 100 );
  105. light.position.multiplyScalar( 1.3 );
  106. light.castShadow = true;
  107. // light.shadowCameraVisible = true;
  108. light.shadow.mapSize.width = 1024;
  109. light.shadow.mapSize.height = 1024;
  110. var d = 300;
  111. light.shadow.camera.left = - d;
  112. light.shadow.camera.right = d;
  113. light.shadow.camera.top = d;
  114. light.shadow.camera.bottom = - d;
  115. light.shadow.camera.far = 1000;
  116. scene.add( light );
  117. // cloth material
  118. var loader = new THREE.TextureLoader();
  119. var clothTexture = loader.load( 'textures/patterns/circuit_pattern.png' );
  120. clothTexture.wrapS = clothTexture.wrapT = THREE.RepeatWrapping;
  121. clothTexture.anisotropy = 16;
  122. var clothMaterial = new THREE.MeshPhongMaterial( {
  123. specular: 0x030303,
  124. map: clothTexture,
  125. side: THREE.DoubleSide,
  126. alphaTest: 0.5
  127. } );
  128. // cloth geometry
  129. clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h );
  130. clothGeometry.dynamic = true;
  131. var uniforms = { texture: { type: "t", value: clothTexture } };
  132. var vertexShader = document.getElementById( 'vertexShaderDepth' ).textContent;
  133. var fragmentShader = document.getElementById( 'fragmentShaderDepth' ).textContent;
  134. // cloth mesh
  135. object = new THREE.Mesh( clothGeometry, clothMaterial );
  136. object.position.set( 0, 0, 0 );
  137. object.castShadow = true;
  138. scene.add( object );
  139. object.customDepthMaterial = new THREE.ShaderMaterial( {
  140. uniforms: uniforms,
  141. vertexShader: vertexShader,
  142. fragmentShader: fragmentShader,
  143. side: THREE.DoubleSide
  144. } );
  145. // sphere
  146. var ballGeo = new THREE.SphereGeometry( ballSize, 20, 20 );
  147. var ballMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  148. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  149. sphere.castShadow = true;
  150. sphere.receiveShadow = true;
  151. scene.add( sphere );
  152. // ground
  153. var groundTexture = loader.load( "textures/terrain/grasslight-big.jpg" );
  154. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  155. groundTexture.repeat.set( 25, 25 );
  156. groundTexture.anisotropy = 16;
  157. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );
  158. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
  159. mesh.position.y = - 250;
  160. mesh.rotation.x = - Math.PI / 2;
  161. mesh.receiveShadow = true;
  162. scene.add( mesh );
  163. // poles
  164. var poleGeo = new THREE.BoxGeometry( 5, 375, 5 );
  165. var poleMat = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 100 } );
  166. var mesh = new THREE.Mesh( poleGeo, poleMat );
  167. mesh.position.x = - 125;
  168. mesh.position.y = - 62;
  169. mesh.receiveShadow = true;
  170. mesh.castShadow = true;
  171. scene.add( mesh );
  172. var mesh = new THREE.Mesh( poleGeo, poleMat );
  173. mesh.position.x = 125;
  174. mesh.position.y = - 62;
  175. mesh.receiveShadow = true;
  176. mesh.castShadow = true;
  177. scene.add( mesh );
  178. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 255, 5, 5 ), poleMat );
  179. mesh.position.y = - 250 + ( 750 / 2 );
  180. mesh.position.x = 0;
  181. mesh.receiveShadow = true;
  182. mesh.castShadow = true;
  183. scene.add( mesh );
  184. var gg = new THREE.BoxGeometry( 10, 10, 10 );
  185. var mesh = new THREE.Mesh( gg, poleMat );
  186. mesh.position.y = - 250;
  187. mesh.position.x = 125;
  188. mesh.receiveShadow = true;
  189. mesh.castShadow = true;
  190. scene.add( mesh );
  191. var mesh = new THREE.Mesh( gg, poleMat );
  192. mesh.position.y = - 250;
  193. mesh.position.x = - 125;
  194. mesh.receiveShadow = true;
  195. mesh.castShadow = true;
  196. scene.add( mesh );
  197. //
  198. renderer = new THREE.WebGLRenderer( { antialias: true } );
  199. renderer.setPixelRatio( window.devicePixelRatio );
  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.shadowMap.enabled = true;
  206. //
  207. stats = new Stats();
  208. stats.domElement.style.position = 'absolute';
  209. stats.domElement.style.top = '0px';
  210. container.appendChild( stats.domElement );
  211. //
  212. window.addEventListener( 'resize', onWindowResize, false );
  213. sphere.visible = ! true
  214. }
  215. //
  216. function onWindowResize() {
  217. camera.aspect = window.innerWidth / window.innerHeight;
  218. camera.updateProjectionMatrix();
  219. renderer.setSize( window.innerWidth, window.innerHeight );
  220. }
  221. //
  222. function animate() {
  223. requestAnimationFrame( animate );
  224. var time = Date.now();
  225. windStrength = Math.cos( time / 7000 ) * 20 + 40;
  226. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) ).normalize().multiplyScalar( windStrength );
  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>