webgl_animation_cloth.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #cce0ff;
  11. color: #000;
  12. }
  13. a {
  14. color: #080;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">Simple Cloth Simulation<br/>
  20. Verlet integration with relaxed constraints<br/>
  21. <a onclick="wind = !wind;">Wind</a> |
  22. <a onclick="sphere.visible = !sphere.visible;">Ball</a> |
  23. <a onclick="togglePins();">Pins</a>
  24. </div>
  25. <script src="../build/three.js"></script>
  26. <script src="js/WebGL.js"></script>
  27. <script src="js/controls/OrbitControls.js"></script>
  28. <script src="js/libs/stats.min.js"></script>
  29. <script src="js/Cloth.js"></script>
  30. <script>
  31. /* testing cloth simulation */
  32. var pinsFormation = [];
  33. var pins = [ 6 ];
  34. pinsFormation.push( pins );
  35. pins = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
  36. pinsFormation.push( pins );
  37. pins = [ 0 ];
  38. pinsFormation.push( pins );
  39. pins = []; // cut the rope ;)
  40. pinsFormation.push( pins );
  41. pins = [ 0, cloth.w ]; // classic 2 pins
  42. pinsFormation.push( pins );
  43. pins = pinsFormation[ 1 ];
  44. function togglePins() {
  45. pins = pinsFormation[ ~~ ( Math.random() * pinsFormation.length ) ];
  46. }
  47. if ( WEBGL.isWebGLAvailable() === false ) {
  48. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  49. }
  50. var container, stats;
  51. var camera, scene, renderer;
  52. var clothGeometry;
  53. var sphere;
  54. var object;
  55. init();
  56. animate();
  57. function init() {
  58. container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. // scene
  61. scene = new THREE.Scene();
  62. scene.background = new THREE.Color( 0xcce0ff );
  63. scene.fog = new THREE.Fog( 0xcce0ff, 500, 10000 );
  64. // camera
  65. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  66. camera.position.set( 1000, 50, 1500 );
  67. // lights
  68. scene.add( new THREE.AmbientLight( 0x666666 ) );
  69. var light = new THREE.DirectionalLight( 0xdfebff, 1 );
  70. light.position.set( 50, 200, 100 );
  71. light.position.multiplyScalar( 1.3 );
  72. light.castShadow = true;
  73. light.shadow.mapSize.width = 1024;
  74. light.shadow.mapSize.height = 1024;
  75. var d = 300;
  76. light.shadow.camera.left = - d;
  77. light.shadow.camera.right = d;
  78. light.shadow.camera.top = d;
  79. light.shadow.camera.bottom = - d;
  80. light.shadow.camera.far = 1000;
  81. scene.add( light );
  82. // cloth material
  83. var loader = new THREE.TextureLoader();
  84. var clothTexture = loader.load( 'textures/patterns/circuit_pattern.png' );
  85. clothTexture.anisotropy = 16;
  86. var clothMaterial = new THREE.MeshLambertMaterial( {
  87. map: clothTexture,
  88. side: THREE.DoubleSide,
  89. alphaTest: 0.5
  90. } );
  91. // cloth geometry
  92. clothGeometry = new THREE.ParametricBufferGeometry( clothFunction, cloth.w, cloth.h );
  93. // cloth mesh
  94. object = new THREE.Mesh( clothGeometry, clothMaterial );
  95. object.position.set( 0, 0, 0 );
  96. object.castShadow = true;
  97. scene.add( object );
  98. object.customDepthMaterial = new THREE.MeshDepthMaterial( {
  99. depthPacking: THREE.RGBADepthPacking,
  100. map: clothTexture,
  101. alphaTest: 0.5
  102. } );
  103. // sphere
  104. var ballGeo = new THREE.SphereBufferGeometry( ballSize, 32, 16 );
  105. var ballMaterial = new THREE.MeshLambertMaterial();
  106. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  107. sphere.castShadow = true;
  108. sphere.receiveShadow = true;
  109. scene.add( sphere );
  110. // ground
  111. var groundTexture = loader.load( 'textures/terrain/grasslight-big.jpg' );
  112. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  113. groundTexture.repeat.set( 25, 25 );
  114. groundTexture.anisotropy = 16;
  115. var groundMaterial = new THREE.MeshLambertMaterial( { map: groundTexture } );
  116. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
  117. mesh.position.y = - 250;
  118. mesh.rotation.x = - Math.PI / 2;
  119. mesh.receiveShadow = true;
  120. scene.add( mesh );
  121. // poles
  122. var poleGeo = new THREE.BoxBufferGeometry( 5, 375, 5 );
  123. var poleMat = new THREE.MeshLambertMaterial();
  124. var mesh = new THREE.Mesh( poleGeo, poleMat );
  125. mesh.position.x = - 125;
  126. mesh.position.y = - 62;
  127. mesh.receiveShadow = true;
  128. mesh.castShadow = true;
  129. scene.add( mesh );
  130. var mesh = new THREE.Mesh( poleGeo, poleMat );
  131. mesh.position.x = 125;
  132. mesh.position.y = - 62;
  133. mesh.receiveShadow = true;
  134. mesh.castShadow = true;
  135. scene.add( mesh );
  136. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 255, 5, 5 ), poleMat );
  137. mesh.position.y = - 250 + ( 750 / 2 );
  138. mesh.position.x = 0;
  139. mesh.receiveShadow = true;
  140. mesh.castShadow = true;
  141. scene.add( mesh );
  142. var gg = new THREE.BoxBufferGeometry( 10, 10, 10 );
  143. var mesh = new THREE.Mesh( gg, poleMat );
  144. mesh.position.y = - 250;
  145. mesh.position.x = 125;
  146. mesh.receiveShadow = true;
  147. mesh.castShadow = true;
  148. scene.add( mesh );
  149. var mesh = new THREE.Mesh( gg, poleMat );
  150. mesh.position.y = - 250;
  151. mesh.position.x = - 125;
  152. mesh.receiveShadow = true;
  153. mesh.castShadow = true;
  154. scene.add( mesh );
  155. // renderer
  156. renderer = new THREE.WebGLRenderer( { antialias: true } );
  157. renderer.setPixelRatio( window.devicePixelRatio );
  158. renderer.setSize( window.innerWidth, window.innerHeight );
  159. container.appendChild( renderer.domElement );
  160. renderer.gammaInput = true;
  161. renderer.gammaOutput = true;
  162. renderer.shadowMap.enabled = true;
  163. // controls
  164. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  165. controls.maxPolarAngle = Math.PI * 0.5;
  166. controls.minDistance = 1000;
  167. controls.maxDistance = 5000;
  168. // performance monitor
  169. stats = new Stats();
  170. container.appendChild( stats.dom );
  171. //
  172. window.addEventListener( 'resize', onWindowResize, false );
  173. sphere.visible = ! true;
  174. }
  175. //
  176. function onWindowResize() {
  177. camera.aspect = window.innerWidth / window.innerHeight;
  178. camera.updateProjectionMatrix();
  179. renderer.setSize( window.innerWidth, window.innerHeight );
  180. }
  181. //
  182. function animate() {
  183. requestAnimationFrame( animate );
  184. var time = Date.now();
  185. var windStrength = Math.cos( time / 7000 ) * 20 + 40;
  186. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) )
  187. windForce.normalize()
  188. windForce.multiplyScalar( windStrength );
  189. simulate( time );
  190. render();
  191. stats.update();
  192. }
  193. function render() {
  194. var p = cloth.particles;
  195. for ( var i = 0, il = p.length; i < il; i ++ ) {
  196. var v = p[ i ].position;
  197. clothGeometry.attributes.position.setXYZ( i, v.x, v.y, v.z );
  198. }
  199. clothGeometry.attributes.position.needsUpdate = true;
  200. clothGeometry.computeVertexNormals();
  201. sphere.position.copy( ballPosition );
  202. renderer.render( scene, camera );
  203. }
  204. </script>
  205. </body>
  206. </html>