webgl_animation_cloth.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 = 2048;
  114. light.shadowMapHeight = 2048;
  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. light = new THREE.DirectionalLight( 0x3dff0c, 0.35 );
  124. light.position.set( 0, -1, 0 );
  125. scene.add( light );
  126. // cloth material
  127. var clothTexture = THREE.ImageUtils.loadTexture( 'textures/patterns/circuit_pattern.png' );
  128. clothTexture.wrapS = clothTexture.wrapT = THREE.RepeatWrapping;
  129. clothTexture.anisotropy = 16;
  130. var clothMaterial = new THREE.MeshPhongMaterial( { alphaTest: 0.5, ambient: 0xffffff, color: 0xffffff, specular: 0x030303, emissive: 0x111111, shiness: 10, map: clothTexture, side: THREE.DoubleSide } );
  131. // cloth geometry
  132. clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h );
  133. clothGeometry.dynamic = true;
  134. clothGeometry.computeFaceNormals();
  135. var uniforms = { texture: { type: "t", value: clothTexture } };
  136. var vertexShader = document.getElementById( 'vertexShaderDepth' ).textContent;
  137. var fragmentShader = document.getElementById( 'fragmentShaderDepth' ).textContent;
  138. // cloth mesh
  139. object = new THREE.Mesh( clothGeometry, clothMaterial );
  140. object.position.set( 0, 0, 0 );
  141. object.castShadow = true;
  142. object.receiveShadow = true;
  143. scene.add( object );
  144. object.customDepthMaterial = new THREE.ShaderMaterial( { uniforms: uniforms, vertexShader: vertexShader, fragmentShader: fragmentShader } );
  145. // sphere
  146. var ballGeo = new THREE.SphereGeometry( ballSize, 20, 20 );
  147. var ballMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  148. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  149. sphere.castShadow = true;
  150. sphere.receiveShadow = true;
  151. scene.add( sphere );
  152. // arrow
  153. arrow = new THREE.ArrowHelper( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 0 ), 50, 0xff0000 );
  154. arrow.position.set( -200, 0, -200 );
  155. // scene.add( arrow );
  156. // ground
  157. var initColor = new THREE.Color( 0x497f13 );
  158. var initTexture = THREE.ImageUtils.generateDataTexture( 1, 1, initColor );
  159. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: initTexture } );
  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 } );
  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. container.appendChild( stats.domElement );
  215. //
  216. window.addEventListener( 'resize', onWindowResize, false );
  217. sphere.visible = !true
  218. }
  219. //
  220. function onWindowResize() {
  221. camera.aspect = window.innerWidth / window.innerHeight;
  222. camera.updateProjectionMatrix();
  223. renderer.setSize( window.innerWidth, window.innerHeight );
  224. }
  225. //
  226. function animate() {
  227. requestAnimationFrame( animate );
  228. var time = Date.now();
  229. windStrength = Math.cos( time / 7000 ) * 20 + 40;
  230. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) ).normalize().multiplyScalar( windStrength );
  231. arrow.setLength( windStrength );
  232. arrow.setDirection( windForce );
  233. simulate(time);
  234. render();
  235. stats.update();
  236. }
  237. function render() {
  238. var timer = Date.now() * 0.0002;
  239. var p = cloth.particles;
  240. for ( var i = 0, il = p.length; i < il; i ++ ) {
  241. clothGeometry.vertices[ i ].copy( p[ i ].position );
  242. }
  243. clothGeometry.computeFaceNormals();
  244. clothGeometry.computeVertexNormals();
  245. clothGeometry.normalsNeedUpdate = true;
  246. clothGeometry.verticesNeedUpdate = true;
  247. sphere.position.copy( ballPosition );
  248. if ( rotate ) {
  249. camera.position.x = Math.cos( timer ) * 1500;
  250. camera.position.z = Math.sin( timer ) * 1500;
  251. }
  252. camera.lookAt( scene.position );
  253. renderer.render( scene, camera );
  254. }
  255. </script>
  256. </body>
  257. </html>