webgl_animation_cloth.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 relaxed constraints<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. #include <packing>
  41. uniform sampler2D texture;
  42. varying vec2 vUV;
  43. void main() {
  44. vec4 pixel = texture2D( texture, vUV );
  45. if ( pixel.a < 0.5 ) discard;
  46. gl_FragData[ 0 ] = packDepthToRGBA( gl_FragCoord.z );
  47. }
  48. </script>
  49. <script type="x-shader/x-vertex" id="vertexShaderDepth">
  50. varying vec2 vUV;
  51. void main() {
  52. vUV = 0.75 * uv;
  53. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  54. gl_Position = projectionMatrix * mvPosition;
  55. }
  56. </script>
  57. <script>
  58. /* testing cloth simulation */
  59. var pinsFormation = [];
  60. var pins = [ 6 ];
  61. pinsFormation.push( pins );
  62. pins = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
  63. pinsFormation.push( pins );
  64. pins = [ 0 ];
  65. pinsFormation.push( pins );
  66. pins = []; // cut the rope ;)
  67. pinsFormation.push( pins );
  68. pins = [ 0, cloth.w ]; // classic 2 pins
  69. pinsFormation.push( pins );
  70. pins = pinsFormation[ 1 ];
  71. function togglePins() {
  72. pins = pinsFormation[ ~~ ( Math.random() * pinsFormation.length ) ];
  73. }
  74. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  75. var container, stats;
  76. var camera, scene, renderer;
  77. var clothGeometry;
  78. var sphere;
  79. var object;
  80. init();
  81. animate();
  82. function init() {
  83. container = document.createElement( 'div' );
  84. document.body.appendChild( container );
  85. // scene
  86. scene = new THREE.Scene();
  87. scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );
  88. // camera
  89. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  90. camera.position.x = 1000;
  91. camera.position.y = 50;
  92. camera.position.z = 1500;
  93. scene.add( camera );
  94. // lights
  95. var light, materials;
  96. scene.add( new THREE.AmbientLight( 0x666666 ) );
  97. light = new THREE.DirectionalLight( 0xdfebff, 1.75 );
  98. light.position.set( 50, 200, 100 );
  99. light.position.multiplyScalar( 1.3 );
  100. light.castShadow = true;
  101. light.shadow.mapSize.width = 1024;
  102. light.shadow.mapSize.height = 1024;
  103. var d = 300;
  104. light.shadow.camera.left = - d;
  105. light.shadow.camera.right = d;
  106. light.shadow.camera.top = d;
  107. light.shadow.camera.bottom = - d;
  108. light.shadow.camera.far = 1000;
  109. scene.add( light );
  110. // cloth material
  111. var loader = new THREE.TextureLoader();
  112. var clothTexture = loader.load( 'textures/patterns/circuit_pattern.png' );
  113. clothTexture.wrapS = clothTexture.wrapT = THREE.RepeatWrapping;
  114. clothTexture.anisotropy = 16;
  115. var clothMaterial = new THREE.MeshPhongMaterial( {
  116. specular: 0x030303,
  117. map: clothTexture,
  118. side: THREE.DoubleSide,
  119. alphaTest: 0.5
  120. } );
  121. // cloth geometry
  122. clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h );
  123. clothGeometry.dynamic = true;
  124. var uniforms = { texture: { value: clothTexture } };
  125. var vertexShader = document.getElementById( 'vertexShaderDepth' ).textContent;
  126. var fragmentShader = document.getElementById( 'fragmentShaderDepth' ).textContent;
  127. // cloth mesh
  128. object = new THREE.Mesh( clothGeometry, clothMaterial );
  129. object.position.set( 0, 0, 0 );
  130. object.castShadow = true;
  131. scene.add( object );
  132. object.customDepthMaterial = new THREE.ShaderMaterial( {
  133. uniforms: uniforms,
  134. vertexShader: vertexShader,
  135. fragmentShader: fragmentShader,
  136. side: THREE.DoubleSide
  137. } );
  138. // sphere
  139. var ballGeo = new THREE.SphereGeometry( ballSize, 20, 20 );
  140. var ballMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  141. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  142. sphere.castShadow = true;
  143. sphere.receiveShadow = true;
  144. scene.add( sphere );
  145. // ground
  146. var groundTexture = loader.load( 'textures/terrain/grasslight-big.jpg' );
  147. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  148. groundTexture.repeat.set( 25, 25 );
  149. groundTexture.anisotropy = 16;
  150. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );
  151. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
  152. mesh.position.y = - 250;
  153. mesh.rotation.x = - Math.PI / 2;
  154. mesh.receiveShadow = true;
  155. scene.add( mesh );
  156. // poles
  157. var poleGeo = new THREE.BoxGeometry( 5, 375, 5 );
  158. var poleMat = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 100 } );
  159. var mesh = new THREE.Mesh( poleGeo, poleMat );
  160. mesh.position.x = - 125;
  161. mesh.position.y = - 62;
  162. mesh.receiveShadow = true;
  163. mesh.castShadow = true;
  164. scene.add( mesh );
  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( new THREE.BoxGeometry( 255, 5, 5 ), poleMat );
  172. mesh.position.y = - 250 + ( 750 / 2 );
  173. mesh.position.x = 0;
  174. mesh.receiveShadow = true;
  175. mesh.castShadow = true;
  176. scene.add( mesh );
  177. var gg = new THREE.BoxGeometry( 10, 10, 10 );
  178. var mesh = new THREE.Mesh( gg, poleMat );
  179. mesh.position.y = - 250;
  180. mesh.position.x = 125;
  181. mesh.receiveShadow = true;
  182. mesh.castShadow = true;
  183. scene.add( mesh );
  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. // renderer
  191. renderer = new THREE.WebGLRenderer( { antialias: true } );
  192. renderer.setPixelRatio( window.devicePixelRatio );
  193. renderer.setSize( window.innerWidth, window.innerHeight );
  194. renderer.setClearColor( scene.fog.color );
  195. container.appendChild( renderer.domElement );
  196. renderer.gammaInput = true;
  197. renderer.gammaOutput = true;
  198. renderer.shadowMap.enabled = true;
  199. // controls
  200. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  201. controls.maxPolarAngle = Math.PI * 0.5;
  202. controls.minDistance = 1000;
  203. controls.maxDistance = 7500;
  204. // performance monitor
  205. stats = new Stats();
  206. container.appendChild( stats.dom );
  207. //
  208. window.addEventListener( 'resize', onWindowResize, false );
  209. sphere.visible = ! true
  210. }
  211. //
  212. function onWindowResize() {
  213. camera.aspect = window.innerWidth / window.innerHeight;
  214. camera.updateProjectionMatrix();
  215. renderer.setSize( window.innerWidth, window.innerHeight );
  216. }
  217. //
  218. function animate() {
  219. requestAnimationFrame( animate );
  220. var time = Date.now();
  221. windStrength = Math.cos( time / 7000 ) * 20 + 40;
  222. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) ).normalize().multiplyScalar( windStrength );
  223. simulate( time );
  224. render();
  225. stats.update();
  226. }
  227. function render() {
  228. var p = cloth.particles;
  229. for ( var i = 0, il = p.length; i < il; i ++ ) {
  230. clothGeometry.vertices[ i ].copy( p[ i ].position );
  231. }
  232. clothGeometry.computeFaceNormals();
  233. clothGeometry.computeVertexNormals();
  234. clothGeometry.normalsNeedUpdate = true;
  235. clothGeometry.verticesNeedUpdate = true;
  236. sphere.position.copy( ballPosition );
  237. camera.lookAt( scene.position );
  238. renderer.render( scene, camera );
  239. }
  240. </script>
  241. </body>
  242. </html>