webgl_materials_sheen.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <html lang="en">
  2. <head>
  3. <title>Ammo.js softbody cloth demo</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. <style>
  8. body {
  9. color: #333;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div id="info">Sheen demo by <a href="https://github.com/DanielSturk">DanielSturk</a><br>Demo copied from <a href="./?q=cloth#webgl_physics_cloth">webgl_physics_cloth.html</a></div>
  15. <div id="container"></div>
  16. <script src="../build/three.js"></script>
  17. <script src="js/libs/ammo.js"></script>
  18. <script src="js/controls/OrbitControls.js"></script>
  19. <script src="js/WebGL.js"></script>
  20. <script src="js/libs/stats.min.js"></script>
  21. <script src="js/libs/dat.gui.min.js"></script>
  22. <script>
  23. // Detects webgl
  24. if ( WEBGL.isWebGLAvailable() === false ) {
  25. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  26. }
  27. // Graphics variables
  28. var container, stats;
  29. var camera, controls, scene, renderer;
  30. var textureLoader;
  31. var clock = new THREE.Clock();
  32. // Physics variables
  33. var gravityConstant = - 9.8;
  34. var physicsWorld;
  35. var rigidBodies = [];
  36. var margin = 0.05;
  37. var hinge;
  38. var cloth;
  39. var transformAux1;
  40. var clothMaterial;
  41. var params = {
  42. sheen: .5
  43. };
  44. Ammo().then( function( AmmoLib ) {
  45. Ammo = AmmoLib;
  46. init();
  47. animate();
  48. } );
  49. function init() {
  50. initGraphics();
  51. initPhysics();
  52. createObjects();
  53. }
  54. function initGraphics() {
  55. container = document.getElementById( 'container' );
  56. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.2, 2000 );
  57. scene = new THREE.Scene();
  58. scene.background = new THREE.Color( 0xbfd1e5 );
  59. camera.position.set( - 12, 7, 4 );
  60. renderer = new THREE.WebGLRenderer();
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.shadowMap.enabled = true;
  64. container.appendChild( renderer.domElement );
  65. controls = new THREE.OrbitControls( camera, renderer.domElement );
  66. controls.target.set( 0, 2, 0 );
  67. controls.update();
  68. textureLoader = new THREE.TextureLoader();
  69. var ambientLight = new THREE.AmbientLight( 0x404040 );
  70. scene.add( ambientLight );
  71. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  72. light.position.set( - 7, 10, 15 );
  73. light.castShadow = true;
  74. var d = 10;
  75. light.shadow.camera.left = - d;
  76. light.shadow.camera.right = d;
  77. light.shadow.camera.top = d;
  78. light.shadow.camera.bottom = - d;
  79. light.shadow.camera.near = 2;
  80. light.shadow.camera.far = 50;
  81. light.shadow.mapSize.x = 1024;
  82. light.shadow.mapSize.y = 1024;
  83. light.shadow.bias = - 0.003;
  84. scene.add( light );
  85. stats = new Stats();
  86. stats.domElement.style.position = 'absolute';
  87. stats.domElement.style.top = '0px';
  88. container.appendChild( stats.domElement );
  89. window.addEventListener( 'resize', onWindowResize, false );
  90. var gui = new dat.GUI();
  91. gui.add( params, 'sheen', 0, 1 );
  92. gui.open();
  93. }
  94. function initPhysics() {
  95. // Physics configuration
  96. var collisionConfiguration = new Ammo.btSoftBodyRigidBodyCollisionConfiguration();
  97. var dispatcher = new Ammo.btCollisionDispatcher( collisionConfiguration );
  98. var broadphase = new Ammo.btDbvtBroadphase();
  99. var solver = new Ammo.btSequentialImpulseConstraintSolver();
  100. var softBodySolver = new Ammo.btDefaultSoftBodySolver();
  101. physicsWorld = new Ammo.btSoftRigidDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration, softBodySolver );
  102. physicsWorld.setGravity( new Ammo.btVector3( 0, gravityConstant, 0 ) );
  103. physicsWorld.getWorldInfo().set_m_gravity( new Ammo.btVector3( 0, gravityConstant, 0 ) );
  104. transformAux1 = new Ammo.btTransform();
  105. }
  106. function createObjects() {
  107. var pos = new THREE.Vector3();
  108. var quat = new THREE.Quaternion();
  109. // The cloth
  110. // Cloth graphic object
  111. var clothWidth = 4;
  112. var clothHeight = 3;
  113. var clothNumSegmentsZ = clothWidth * 15;
  114. var clothNumSegmentsY = clothHeight * 15;
  115. var clothPos = new THREE.Vector3( - 3, 3, 2 );
  116. //var clothGeometry = new THREE.BufferGeometry();
  117. var clothGeometry = new THREE.PlaneBufferGeometry( clothWidth, clothHeight, clothNumSegmentsZ, clothNumSegmentsY );
  118. clothGeometry.rotateY( Math.PI * 0.5 );
  119. clothGeometry.translate( clothPos.x, clothPos.y + clothHeight * 0.5, clothPos.z - clothWidth * 0.5 );
  120. clothMaterial = new THREE.MeshPhysicalMaterial( {
  121. color: 0xFFFFFF,
  122. side: THREE.DoubleSide,
  123. roughness: 1,
  124. sheen: .9
  125. } );
  126. cloth = new THREE.Mesh( clothGeometry, clothMaterial );
  127. cloth.castShadow = true;
  128. //cloth.receiveShadow = true;
  129. scene.add( cloth );
  130. textureLoader.load( "textures/grid.png", function ( texture ) {
  131. texture.wrapS = THREE.RepeatWrapping;
  132. texture.wrapT = THREE.RepeatWrapping;
  133. texture.repeat.set( clothNumSegmentsZ, clothNumSegmentsY );
  134. cloth.material.map = texture;
  135. cloth.material.needsUpdate = true;
  136. } );
  137. var sphere = new THREE.Mesh(new THREE.SphereBufferGeometry(.5, 32, 32), clothMaterial);
  138. sphere.position.set(0, 2, 0);
  139. scene.add(sphere);
  140. // Cloth physic object
  141. var softBodyHelpers = new Ammo.btSoftBodyHelpers();
  142. var clothCorner00 = new Ammo.btVector3( clothPos.x, clothPos.y + clothHeight, clothPos.z );
  143. var clothCorner01 = new Ammo.btVector3( clothPos.x, clothPos.y + clothHeight, clothPos.z - clothWidth );
  144. var clothCorner10 = new Ammo.btVector3( clothPos.x, clothPos.y, clothPos.z );
  145. var clothCorner11 = new Ammo.btVector3( clothPos.x, clothPos.y, clothPos.z - clothWidth );
  146. var clothSoftBody = softBodyHelpers.CreatePatch( physicsWorld.getWorldInfo(), clothCorner00, clothCorner01, clothCorner10, clothCorner11, clothNumSegmentsZ + 1, clothNumSegmentsY + 1, 0, true );
  147. var sbConfig = clothSoftBody.get_m_cfg();
  148. sbConfig.set_viterations( 10 );
  149. sbConfig.set_piterations( 10 );
  150. clothSoftBody.setTotalMass( 0.9, false );
  151. Ammo.castObject( clothSoftBody, Ammo.btCollisionObject ).getCollisionShape().setMargin( margin * 3 );
  152. physicsWorld.addSoftBody( clothSoftBody, 1, - 1 );
  153. cloth.userData.physicsBody = clothSoftBody;
  154. // Disable deactivation
  155. clothSoftBody.setActivationState( 4 );
  156. // The base
  157. var armMass = 2;
  158. var armLength = 3 + clothWidth;
  159. var pylonHeight = clothPos.y + clothHeight;
  160. var baseMaterial = new THREE.MeshPhongMaterial( { color: 0x606060 } );
  161. pos.set( clothPos.x, 0.1, clothPos.z - armLength );
  162. quat.set( 0, 0, 0, 1 );
  163. var base = createParalellepiped( 1, 0.2, 1, 0, pos, quat, baseMaterial );
  164. base.castShadow = true;
  165. base.receiveShadow = true;
  166. pos.set( clothPos.x, 0.5 * pylonHeight, clothPos.z - armLength );
  167. var pylon = createParalellepiped( 0.4, pylonHeight, 0.4, 0, pos, quat, baseMaterial );
  168. pylon.castShadow = true;
  169. pylon.receiveShadow = true;
  170. pos.set( clothPos.x, pylonHeight + 0.2, clothPos.z - 0.5 * armLength );
  171. var arm = createParalellepiped( 0.4, 0.4, armLength + 0.4, armMass, pos, quat, baseMaterial );
  172. arm.castShadow = true;
  173. arm.receiveShadow = true;
  174. // Glue the cloth to the arm
  175. var influence = 0.5;
  176. clothSoftBody.appendAnchor( 0, arm.userData.physicsBody, false, influence );
  177. clothSoftBody.appendAnchor( clothNumSegmentsZ, arm.userData.physicsBody, false, influence );
  178. // Hinge constraint to move the arm
  179. var pivotA = new Ammo.btVector3( 0, pylonHeight * 0.5, 0 );
  180. var pivotB = new Ammo.btVector3( 0, - 0.2, - armLength * 0.5 );
  181. var axis = new Ammo.btVector3( 0, 1, 0 );
  182. hinge = new Ammo.btHingeConstraint( pylon.userData.physicsBody, arm.userData.physicsBody, pivotA, pivotB, axis, axis, true );
  183. physicsWorld.addConstraint( hinge, true );
  184. }
  185. function createParalellepiped( sx, sy, sz, mass, pos, quat, material ) {
  186. var threeObject = new THREE.Mesh( new THREE.BoxBufferGeometry( sx, sy, sz, 1, 1, 1 ), material );
  187. var shape = new Ammo.btBoxShape( new Ammo.btVector3( sx * 0.5, sy * 0.5, sz * 0.5 ) );
  188. shape.setMargin( margin );
  189. createRigidBody( threeObject, shape, mass, pos, quat );
  190. return threeObject;
  191. }
  192. function createRigidBody( threeObject, physicsShape, mass, pos, quat ) {
  193. threeObject.position.copy( pos );
  194. threeObject.quaternion.copy( quat );
  195. var transform = new Ammo.btTransform();
  196. transform.setIdentity();
  197. transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
  198. transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) );
  199. var motionState = new Ammo.btDefaultMotionState( transform );
  200. var localInertia = new Ammo.btVector3( 0, 0, 0 );
  201. physicsShape.calculateLocalInertia( mass, localInertia );
  202. var rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, physicsShape, localInertia );
  203. var body = new Ammo.btRigidBody( rbInfo );
  204. threeObject.userData.physicsBody = body;
  205. scene.add( threeObject );
  206. if ( mass > 0 ) {
  207. rigidBodies.push( threeObject );
  208. // Disable deactivation
  209. body.setActivationState( 4 );
  210. }
  211. physicsWorld.addRigidBody( body );
  212. }
  213. function createRandomColor() {
  214. return Math.floor( Math.random() * ( 1 << 24 ) );
  215. }
  216. function createMaterial() {
  217. return new THREE.MeshPhongMaterial( { color: createRandomColor() } );
  218. }
  219. function onWindowResize() {
  220. camera.aspect = window.innerWidth / window.innerHeight;
  221. camera.updateProjectionMatrix();
  222. renderer.setSize( window.innerWidth, window.innerHeight );
  223. }
  224. function animate() {
  225. requestAnimationFrame( animate );
  226. render();
  227. stats.update();
  228. }
  229. function render() {
  230. var deltaTime = clock.getDelta();
  231. updatePhysics( deltaTime );
  232. clothMaterial.sheen = params.sheen;
  233. renderer.render( scene, camera );
  234. }
  235. function updatePhysics( deltaTime ) {
  236. // Hinge control
  237. hinge.enableAngularMotor( true, (Math.random() - .5) * 1., 50 );
  238. // Step world
  239. physicsWorld.stepSimulation( deltaTime, 10 );
  240. // Update cloth
  241. var softBody = cloth.userData.physicsBody;
  242. var clothPositions = cloth.geometry.attributes.position.array;
  243. var numVerts = clothPositions.length / 3;
  244. var nodes = softBody.get_m_nodes();
  245. var indexFloat = 0;
  246. for ( var i = 0; i < numVerts; i ++ ) {
  247. var node = nodes.at( i );
  248. var nodePos = node.get_m_x();
  249. clothPositions[ indexFloat ++ ] = nodePos.x();
  250. clothPositions[ indexFloat ++ ] = nodePos.y();
  251. clothPositions[ indexFloat ++ ] = nodePos.z();
  252. }
  253. cloth.geometry.computeVertexNormals();
  254. cloth.geometry.attributes.position.needsUpdate = true;
  255. cloth.geometry.attributes.normal.needsUpdate = true;
  256. // Update rigid bodies
  257. for ( var i = 0, il = rigidBodies.length; i < il; i ++ ) {
  258. var objThree = rigidBodies[ i ];
  259. var objPhys = objThree.userData.physicsBody;
  260. var ms = objPhys.getMotionState();
  261. if ( ms ) {
  262. ms.getWorldTransform( transformAux1 );
  263. var p = transformAux1.getOrigin();
  264. var q = transformAux1.getRotation();
  265. objThree.position.set( p.x(), p.y(), p.z() );
  266. objThree.quaternion.set( q.x(), q.y(), q.z(), q.w() );
  267. }
  268. }
  269. }
  270. </script>
  271. </body>
  272. </html>
粤ICP备19079148号