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