webgl_animation_cloth.html 7.1 KB

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