webgl_animation_cloth.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. var light, materials;
  76. scene.add( new THREE.AmbientLight( 0x666666 ) );
  77. light = new THREE.DirectionalLight( 0xdfebff, 1 );
  78. light.position.set( -50, 200, -100 );
  79. light.position.multiplyScalar( 1.3 );
  80. light.castShadow = true;
  81. light.shadow.mapSize.width = 1024;
  82. light.shadow.mapSize.height = 1024;
  83. var d = 300;
  84. light.shadow.camera.left = - d;
  85. light.shadow.camera.right = d;
  86. light.shadow.camera.top = d;
  87. light.shadow.camera.bottom = - d;
  88. light.shadow.camera.far = 1000;
  89. scene.add( light );
  90. // cloth material
  91. var loader = new THREE.TextureLoader();
  92. var clothTexture = loader.load( 'textures/patterns/circuit_pattern.png' );
  93. clothTexture.wrapS = clothTexture.wrapT = THREE.RepeatWrapping;
  94. clothTexture.offset.set( 0.1, 0.1 );
  95. clothTexture.repeat.set( 0.5, 0.5 );
  96. clothTexture.anisotropy = 16;
  97. var clothMaterial = new THREE.MeshPhongMaterial( {
  98. specular: 0x030303,
  99. map: clothTexture,
  100. side: THREE.DoubleSide,
  101. alphaTest: 0.5
  102. } );
  103. // cloth geometry
  104. clothGeometry = new THREE.ParametricGeometry( clothFunction, cloth.w, cloth.h );
  105. // cloth mesh
  106. object = new THREE.Mesh( clothGeometry, clothMaterial );
  107. object.position.set( 0, 0, 0 );
  108. object.castShadow = true;
  109. scene.add( object );
  110. object.customDepthMaterial = new THREE.MeshDepthMaterial( {
  111. depthPacking: THREE.RGBADepthPacking,
  112. map: clothTexture,
  113. alphaTest: 0.5
  114. } );
  115. // sphere
  116. var ballGeo = new THREE.SphereBufferGeometry( ballSize, 32, 16 );
  117. var ballMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  118. sphere = new THREE.Mesh( ballGeo, ballMaterial );
  119. sphere.castShadow = true;
  120. sphere.receiveShadow = true;
  121. scene.add( sphere );
  122. // ground
  123. var groundTexture = loader.load( 'textures/terrain/grasslight-big.jpg' );
  124. groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
  125. groundTexture.repeat.set( 25, 25 );
  126. groundTexture.anisotropy = 16;
  127. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, map: groundTexture } );
  128. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
  129. mesh.position.y = - 250;
  130. mesh.rotation.x = - Math.PI / 2;
  131. mesh.receiveShadow = true;
  132. scene.add( mesh );
  133. // poles
  134. var poleGeo = new THREE.BoxBufferGeometry( 5, 375, 5 );
  135. var poleMat = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 100 } );
  136. var mesh = new THREE.Mesh( poleGeo, poleMat );
  137. mesh.position.x = - 125;
  138. mesh.position.y = - 62;
  139. mesh.receiveShadow = true;
  140. mesh.castShadow = true;
  141. scene.add( mesh );
  142. var mesh = new THREE.Mesh( poleGeo, poleMat );
  143. mesh.position.x = 125;
  144. mesh.position.y = - 62;
  145. mesh.receiveShadow = true;
  146. mesh.castShadow = true;
  147. scene.add( mesh );
  148. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 255, 5, 5 ), poleMat );
  149. mesh.position.y = - 250 + ( 750 / 2 );
  150. mesh.position.x = 0;
  151. mesh.receiveShadow = true;
  152. mesh.castShadow = true;
  153. scene.add( mesh );
  154. var gg = new THREE.BoxBufferGeometry( 10, 10, 10 );
  155. var mesh = new THREE.Mesh( gg, poleMat );
  156. mesh.position.y = - 250;
  157. mesh.position.x = 125;
  158. mesh.receiveShadow = true;
  159. mesh.castShadow = true;
  160. scene.add( mesh );
  161. var mesh = new THREE.Mesh( gg, poleMat );
  162. mesh.position.y = - 250;
  163. mesh.position.x = - 125;
  164. mesh.receiveShadow = true;
  165. mesh.castShadow = true;
  166. scene.add( mesh );
  167. // renderer
  168. renderer = new THREE.WebGLRenderer( { antialias: true } );
  169. renderer.setPixelRatio( window.devicePixelRatio );
  170. renderer.setSize( window.innerWidth, window.innerHeight );
  171. renderer.shadowMap.renderSingleSided = false;
  172. container.appendChild( renderer.domElement );
  173. renderer.gammaInput = true;
  174. renderer.gammaOutput = true;
  175. renderer.shadowMap.enabled = true;
  176. // controls
  177. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  178. controls.maxPolarAngle = Math.PI * 0.5;
  179. controls.minDistance = 1000;
  180. controls.maxDistance = 5000;
  181. // performance monitor
  182. stats = new Stats();
  183. container.appendChild( stats.dom );
  184. //
  185. window.addEventListener( 'resize', onWindowResize, false );
  186. sphere.visible = ! true;
  187. }
  188. //
  189. function onWindowResize() {
  190. camera.aspect = window.innerWidth / window.innerHeight;
  191. camera.updateProjectionMatrix();
  192. renderer.setSize( window.innerWidth, window.innerHeight );
  193. }
  194. //
  195. function animate() {
  196. requestAnimationFrame( animate );
  197. var time = Date.now();
  198. var windStrength = Math.cos( time / 7000 ) * 20 + 40;
  199. windForce.set( Math.sin( time / 2000 ), Math.cos( time / 3000 ), Math.sin( time / 1000 ) )
  200. windForce.normalize()
  201. windForce.multiplyScalar( windStrength );
  202. simulate( time );
  203. render();
  204. stats.update();
  205. }
  206. function render() {
  207. var p = cloth.particles;
  208. for ( var i = 0, il = p.length; i < il; i ++ ) {
  209. clothGeometry.vertices[ i ].copy( p[ i ].position );
  210. }
  211. clothGeometry.verticesNeedUpdate = true;
  212. clothGeometry.computeFaceNormals();
  213. clothGeometry.computeVertexNormals();
  214. sphere.position.copy( ballPosition );
  215. renderer.render( scene, camera );
  216. }
  217. </script>
  218. </body>
  219. </html>