webgl_animation_cloth.html 9.7 KB

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