webgl_animation_cloth.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. <a onclick="wind = !wind;">Wind</a> |
  31. <a onclick="sphere.visible = !sphere.visible;">Ball</a> |
  32. <a onclick="togglePins();">Pins</a>
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/controls/OrbitControls.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. init();
  87. animate();
  88. function init() {
  89. container = document.createElement( 'div' );
  90. document.body.appendChild( container );
  91. // scene
  92. scene = new THREE.Scene();
  93. scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );
  94. // camera
  95. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  96. camera.position.x = 1000;
  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.shadow.mapSize.width = 1024;
  108. light.shadow.mapSize.height = 1024;
  109. var d = 300;
  110. light.shadow.camera.left = - d;
  111. light.shadow.camera.right = d;
  112. light.shadow.camera.top = d;
  113. light.shadow.camera.bottom = - d;
  114. light.shadow.camera.far = 1000;
  115. scene.add( light );
  116. // cloth material
  117. var loader = new THREE.TextureLoader();
  118. var clothTexture = loader.load( 'textures/patterns/circuit_pattern.png' );
  119. clothTexture.wrapS = clothTexture.wrapT = THREE.RepeatWrapping;
  120. clothTexture.anisotropy = 16;
  121. var clothMaterial = new THREE.MeshPhongMaterial( {
  122. specular: 0x030303,
  123. map: clothTexture,
  124. side: THREE.DoubleSide,
  125. alphaTest: 0.5
  126. } );
  127. // cloth geometry
  128. clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h );
  129. clothGeometry.dynamic = true;
  130. var uniforms = { texture: { type: "t", value: clothTexture } };
  131. var vertexShader = document.getElementById( 'vertexShaderDepth' ).textContent;
  132. var fragmentShader = document.getElementById( 'fragmentShaderDepth' ).textContent;
  133. // cloth mesh
  134. object = new THREE.Mesh( clothGeometry, clothMaterial );
  135. object.position.set( 0, 0, 0 );
  136. object.castShadow = true;
  137. scene.add( object );
  138. object.customDepthMaterial = new THREE.ShaderMaterial( {
  139. uniforms: uniforms,
  140. vertexShader: vertexShader,
  141. fragmentShader: fragmentShader,
  142. side: THREE.DoubleSide
  143. } );
  144. // sphere
  145. var ballGeo = new THREE.SphereGeometry( ballSize, 20, 20 );
  146. var ballMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  147. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  148. sphere.castShadow = true;
  149. sphere.receiveShadow = true;
  150. scene.add( sphere );
  151. // ground
  152. var groundTexture = loader.load( 'textures/terrain/grasslight-big.jpg' );
  153. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  154. groundTexture.repeat.set( 25, 25 );
  155. groundTexture.anisotropy = 16;
  156. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );
  157. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
  158. mesh.position.y = - 250;
  159. mesh.rotation.x = - Math.PI / 2;
  160. mesh.receiveShadow = true;
  161. scene.add( mesh );
  162. // poles
  163. var poleGeo = new THREE.BoxGeometry( 5, 375, 5 );
  164. var poleMat = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 100 } );
  165. var mesh = new THREE.Mesh( poleGeo, poleMat );
  166. mesh.position.x = - 125;
  167. mesh.position.y = - 62;
  168. mesh.receiveShadow = true;
  169. mesh.castShadow = true;
  170. scene.add( mesh );
  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( new THREE.BoxGeometry( 255, 5, 5 ), poleMat );
  178. mesh.position.y = - 250 + ( 750 / 2 );
  179. mesh.position.x = 0;
  180. mesh.receiveShadow = true;
  181. mesh.castShadow = true;
  182. scene.add( mesh );
  183. var gg = new THREE.BoxGeometry( 10, 10, 10 );
  184. var mesh = new THREE.Mesh( gg, poleMat );
  185. mesh.position.y = - 250;
  186. mesh.position.x = 125;
  187. mesh.receiveShadow = true;
  188. mesh.castShadow = true;
  189. scene.add( mesh );
  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. // renderer
  197. renderer = new THREE.WebGLRenderer( { antialias: true } );
  198. renderer.setPixelRatio( window.devicePixelRatio );
  199. renderer.setSize( window.innerWidth, window.innerHeight );
  200. renderer.setClearColor( scene.fog.color );
  201. container.appendChild( renderer.domElement );
  202. renderer.gammaInput = true;
  203. renderer.gammaOutput = true;
  204. renderer.shadowMap.enabled = true;
  205. // controls
  206. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  207. controls.maxPolarAngle = Math.PI * 0.5;
  208. controls.minDistance = 1000;
  209. controls.maxDistance = 7500;
  210. // performance monitor
  211. stats = new Stats();
  212. stats.domElement.style.position = 'absolute';
  213. stats.domElement.style.top = '0px';
  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. simulate( time );
  232. render();
  233. stats.update();
  234. }
  235. function render() {
  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. camera.lookAt( scene.position );
  246. renderer.render( scene, camera );
  247. }
  248. </script>
  249. </body>
  250. </html>