webgl_physics_volume.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <html lang="en">
  2. <head>
  3. <title>Ammo.js softbody volume 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">
  15. Ammo.js physics soft body volume demo<br/>
  16. Click to throw a ball
  17. </div>
  18. <div id="container"></div>
  19. <script src="js/libs/ammo.js"></script>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. import Stats from './jsm/libs/stats.module.js';
  23. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  24. import { BufferGeometryUtils } from './jsm/utils/BufferGeometryUtils.js';
  25. // Graphics variables
  26. var container, stats;
  27. var camera, controls, scene, renderer;
  28. var textureLoader;
  29. var clock = new THREE.Clock();
  30. var clickRequest = false;
  31. var mouseCoords = new THREE.Vector2();
  32. var raycaster = new THREE.Raycaster();
  33. var ballMaterial = new THREE.MeshPhongMaterial( { color: 0x202020 } );
  34. var pos = new THREE.Vector3();
  35. var quat = new THREE.Quaternion();
  36. // Physics variables
  37. var gravityConstant = - 9.8;
  38. var physicsWorld;
  39. var rigidBodies = [];
  40. var softBodies = [];
  41. var margin = 0.05;
  42. var transformAux1;
  43. var softBodyHelpers;
  44. Ammo().then( function ( AmmoLib ) {
  45. Ammo = AmmoLib;
  46. init();
  47. animate();
  48. } );
  49. function init() {
  50. initGraphics();
  51. initPhysics();
  52. createObjects();
  53. initInput();
  54. }
  55. function initGraphics() {
  56. container = document.getElementById( 'container' );
  57. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.2, 2000 );
  58. scene = new THREE.Scene();
  59. scene.background = new THREE.Color( 0xbfd1e5 );
  60. camera.position.set( - 7, 5, 8 );
  61. renderer = new THREE.WebGLRenderer();
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.shadowMap.enabled = true;
  65. container.appendChild( renderer.domElement );
  66. controls = new OrbitControls( camera, renderer.domElement );
  67. controls.target.set( 0, 2, 0 );
  68. controls.update();
  69. textureLoader = new THREE.TextureLoader();
  70. var ambientLight = new THREE.AmbientLight( 0x404040 );
  71. scene.add( ambientLight );
  72. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  73. light.position.set( - 10, 10, 5 );
  74. light.castShadow = true;
  75. var d = 20;
  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.near = 2;
  81. light.shadow.camera.far = 50;
  82. light.shadow.mapSize.x = 1024;
  83. light.shadow.mapSize.y = 1024;
  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. }
  91. function initPhysics() {
  92. // Physics configuration
  93. var collisionConfiguration = new Ammo.btSoftBodyRigidBodyCollisionConfiguration();
  94. var dispatcher = new Ammo.btCollisionDispatcher( collisionConfiguration );
  95. var broadphase = new Ammo.btDbvtBroadphase();
  96. var solver = new Ammo.btSequentialImpulseConstraintSolver();
  97. var softBodySolver = new Ammo.btDefaultSoftBodySolver();
  98. physicsWorld = new Ammo.btSoftRigidDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration, softBodySolver );
  99. physicsWorld.setGravity( new Ammo.btVector3( 0, gravityConstant, 0 ) );
  100. physicsWorld.getWorldInfo().set_m_gravity( new Ammo.btVector3( 0, gravityConstant, 0 ) );
  101. transformAux1 = new Ammo.btTransform();
  102. softBodyHelpers = new Ammo.btSoftBodyHelpers();
  103. }
  104. function createObjects() {
  105. // Ground
  106. pos.set( 0, - 0.5, 0 );
  107. quat.set( 0, 0, 0, 1 );
  108. var ground = createParalellepiped( 40, 1, 40, 0, pos, quat, new THREE.MeshPhongMaterial( { color: 0xFFFFFF } ) );
  109. ground.castShadow = true;
  110. ground.receiveShadow = true;
  111. textureLoader.load( "textures/grid.png", function ( texture ) {
  112. texture.wrapS = THREE.RepeatWrapping;
  113. texture.wrapT = THREE.RepeatWrapping;
  114. texture.repeat.set( 40, 40 );
  115. ground.material.map = texture;
  116. ground.material.needsUpdate = true;
  117. } );
  118. // Create soft volumes
  119. var volumeMass = 15;
  120. var sphereGeometry = new THREE.SphereBufferGeometry( 1.5, 40, 25 );
  121. sphereGeometry.translate( 5, 5, 0 );
  122. createSoftVolume( sphereGeometry, volumeMass, 250 );
  123. var boxGeometry = new THREE.BoxBufferGeometry( 1, 1, 5, 4, 4, 20 );
  124. boxGeometry.translate( - 2, 5, 0 );
  125. createSoftVolume( boxGeometry, volumeMass, 120 );
  126. // Ramp
  127. pos.set( 3, 1, 0 );
  128. quat.setFromAxisAngle( new THREE.Vector3( 0, 0, 1 ), 30 * Math.PI / 180 );
  129. var obstacle = createParalellepiped( 10, 1, 4, 0, pos, quat, new THREE.MeshPhongMaterial( { color: 0x606060 } ) );
  130. obstacle.castShadow = true;
  131. obstacle.receiveShadow = true;
  132. }
  133. function processGeometry( bufGeometry ) {
  134. // Ony consider the position values when merging the vertices
  135. var posOnlyBufGeometry = new THREE.BufferGeometry();
  136. posOnlyBufGeometry.setAttribute( 'position', bufGeometry.getAttribute( 'position' ) );
  137. posOnlyBufGeometry.setIndex( bufGeometry.getIndex() );
  138. // Merge the vertices so the triangle soup is converted to indexed triangles
  139. var indexedBufferGeom = BufferGeometryUtils.mergeVertices( posOnlyBufGeometry );
  140. // Create index arrays mapping the indexed vertices to bufGeometry vertices
  141. mapIndices( bufGeometry, indexedBufferGeom );
  142. }
  143. function isEqual( x1, y1, z1, x2, y2, z2 ) {
  144. var delta = 0.000001;
  145. return Math.abs( x2 - x1 ) < delta &&
  146. Math.abs( y2 - y1 ) < delta &&
  147. Math.abs( z2 - z1 ) < delta;
  148. }
  149. function mapIndices( bufGeometry, indexedBufferGeom ) {
  150. // Creates ammoVertices, ammoIndices and ammoIndexAssociation in bufGeometry
  151. var vertices = bufGeometry.attributes.position.array;
  152. var idxVertices = indexedBufferGeom.attributes.position.array;
  153. var indices = indexedBufferGeom.index.array;
  154. var numIdxVertices = idxVertices.length / 3;
  155. var numVertices = vertices.length / 3;
  156. bufGeometry.ammoVertices = idxVertices;
  157. bufGeometry.ammoIndices = indices;
  158. bufGeometry.ammoIndexAssociation = [];
  159. for ( var i = 0; i < numIdxVertices; i ++ ) {
  160. var association = [];
  161. bufGeometry.ammoIndexAssociation.push( association );
  162. var i3 = i * 3;
  163. for ( var j = 0; j < numVertices; j ++ ) {
  164. var j3 = j * 3;
  165. if ( isEqual( idxVertices[ i3 ], idxVertices[ i3 + 1 ], idxVertices[ i3 + 2 ],
  166. vertices[ j3 ], vertices[ j3 + 1 ], vertices[ j3 + 2 ] ) ) {
  167. association.push( j3 );
  168. }
  169. }
  170. }
  171. }
  172. function createSoftVolume( bufferGeom, mass, pressure ) {
  173. processGeometry( bufferGeom );
  174. var volume = new THREE.Mesh( bufferGeom, new THREE.MeshPhongMaterial( { color: 0xFFFFFF } ) );
  175. volume.castShadow = true;
  176. volume.receiveShadow = true;
  177. volume.frustumCulled = false;
  178. scene.add( volume );
  179. textureLoader.load( "textures/colors.png", function ( texture ) {
  180. volume.material.map = texture;
  181. volume.material.needsUpdate = true;
  182. } );
  183. // Volume physic object
  184. var volumeSoftBody = softBodyHelpers.CreateFromTriMesh(
  185. physicsWorld.getWorldInfo(),
  186. bufferGeom.ammoVertices,
  187. bufferGeom.ammoIndices,
  188. bufferGeom.ammoIndices.length / 3,
  189. true );
  190. var sbConfig = volumeSoftBody.get_m_cfg();
  191. sbConfig.set_viterations( 40 );
  192. sbConfig.set_piterations( 40 );
  193. // Soft-soft and soft-rigid collisions
  194. sbConfig.set_collisions( 0x11 );
  195. // Friction
  196. sbConfig.set_kDF( 0.1 );
  197. // Damping
  198. sbConfig.set_kDP( 0.01 );
  199. // Pressure
  200. sbConfig.set_kPR( pressure );
  201. // Stiffness
  202. volumeSoftBody.get_m_materials().at( 0 ).set_m_kLST( 0.9 );
  203. volumeSoftBody.get_m_materials().at( 0 ).set_m_kAST( 0.9 );
  204. volumeSoftBody.setTotalMass( mass, false );
  205. Ammo.castObject( volumeSoftBody, Ammo.btCollisionObject ).getCollisionShape().setMargin( margin );
  206. physicsWorld.addSoftBody( volumeSoftBody, 1, - 1 );
  207. volume.userData.physicsBody = volumeSoftBody;
  208. // Disable deactivation
  209. volumeSoftBody.setActivationState( 4 );
  210. softBodies.push( volume );
  211. }
  212. function createParalellepiped( sx, sy, sz, mass, pos, quat, material ) {
  213. var threeObject = new THREE.Mesh( new THREE.BoxBufferGeometry( sx, sy, sz, 1, 1, 1 ), material );
  214. var shape = new Ammo.btBoxShape( new Ammo.btVector3( sx * 0.5, sy * 0.5, sz * 0.5 ) );
  215. shape.setMargin( margin );
  216. createRigidBody( threeObject, shape, mass, pos, quat );
  217. return threeObject;
  218. }
  219. function createRigidBody( threeObject, physicsShape, mass, pos, quat ) {
  220. threeObject.position.copy( pos );
  221. threeObject.quaternion.copy( quat );
  222. var transform = new Ammo.btTransform();
  223. transform.setIdentity();
  224. transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
  225. transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) );
  226. var motionState = new Ammo.btDefaultMotionState( transform );
  227. var localInertia = new Ammo.btVector3( 0, 0, 0 );
  228. physicsShape.calculateLocalInertia( mass, localInertia );
  229. var rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, physicsShape, localInertia );
  230. var body = new Ammo.btRigidBody( rbInfo );
  231. threeObject.userData.physicsBody = body;
  232. scene.add( threeObject );
  233. if ( mass > 0 ) {
  234. rigidBodies.push( threeObject );
  235. // Disable deactivation
  236. body.setActivationState( 4 );
  237. }
  238. physicsWorld.addRigidBody( body );
  239. return body;
  240. }
  241. function initInput() {
  242. window.addEventListener( 'mousedown', function ( event ) {
  243. if ( ! clickRequest ) {
  244. mouseCoords.set(
  245. ( event.clientX / window.innerWidth ) * 2 - 1,
  246. - ( event.clientY / window.innerHeight ) * 2 + 1
  247. );
  248. clickRequest = true;
  249. }
  250. }, false );
  251. }
  252. function processClick() {
  253. if ( clickRequest ) {
  254. raycaster.setFromCamera( mouseCoords, camera );
  255. // Creates a ball
  256. var ballMass = 3;
  257. var ballRadius = 0.4;
  258. var ball = new THREE.Mesh( new THREE.SphereBufferGeometry( ballRadius, 18, 16 ), ballMaterial );
  259. ball.castShadow = true;
  260. ball.receiveShadow = true;
  261. var ballShape = new Ammo.btSphereShape( ballRadius );
  262. ballShape.setMargin( margin );
  263. pos.copy( raycaster.ray.direction );
  264. pos.add( raycaster.ray.origin );
  265. quat.set( 0, 0, 0, 1 );
  266. var ballBody = createRigidBody( ball, ballShape, ballMass, pos, quat );
  267. ballBody.setFriction( 0.5 );
  268. pos.copy( raycaster.ray.direction );
  269. pos.multiplyScalar( 14 );
  270. ballBody.setLinearVelocity( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
  271. clickRequest = false;
  272. }
  273. }
  274. function onWindowResize() {
  275. camera.aspect = window.innerWidth / window.innerHeight;
  276. camera.updateProjectionMatrix();
  277. renderer.setSize( window.innerWidth, window.innerHeight );
  278. }
  279. function animate() {
  280. requestAnimationFrame( animate );
  281. render();
  282. stats.update();
  283. }
  284. function render() {
  285. var deltaTime = clock.getDelta();
  286. updatePhysics( deltaTime );
  287. processClick();
  288. renderer.render( scene, camera );
  289. }
  290. function updatePhysics( deltaTime ) {
  291. // Step world
  292. physicsWorld.stepSimulation( deltaTime, 10 );
  293. // Update soft volumes
  294. for ( var i = 0, il = softBodies.length; i < il; i ++ ) {
  295. var volume = softBodies[ i ];
  296. var geometry = volume.geometry;
  297. var softBody = volume.userData.physicsBody;
  298. var volumePositions = geometry.attributes.position.array;
  299. var volumeNormals = geometry.attributes.normal.array;
  300. var association = geometry.ammoIndexAssociation;
  301. var numVerts = association.length;
  302. var nodes = softBody.get_m_nodes();
  303. for ( var j = 0; j < numVerts; j ++ ) {
  304. var node = nodes.at( j );
  305. var nodePos = node.get_m_x();
  306. var x = nodePos.x();
  307. var y = nodePos.y();
  308. var z = nodePos.z();
  309. var nodeNormal = node.get_m_n();
  310. var nx = nodeNormal.x();
  311. var ny = nodeNormal.y();
  312. var nz = nodeNormal.z();
  313. var assocVertex = association[ j ];
  314. for ( var k = 0, kl = assocVertex.length; k < kl; k ++ ) {
  315. var indexVertex = assocVertex[ k ];
  316. volumePositions[ indexVertex ] = x;
  317. volumeNormals[ indexVertex ] = nx;
  318. indexVertex ++;
  319. volumePositions[ indexVertex ] = y;
  320. volumeNormals[ indexVertex ] = ny;
  321. indexVertex ++;
  322. volumePositions[ indexVertex ] = z;
  323. volumeNormals[ indexVertex ] = nz;
  324. }
  325. }
  326. geometry.attributes.position.needsUpdate = true;
  327. geometry.attributes.normal.needsUpdate = true;
  328. }
  329. // Update rigid bodies
  330. for ( var i = 0, il = rigidBodies.length; i < il; i ++ ) {
  331. var objThree = rigidBodies[ i ];
  332. var objPhys = objThree.userData.physicsBody;
  333. var ms = objPhys.getMotionState();
  334. if ( ms ) {
  335. ms.getWorldTransform( transformAux1 );
  336. var p = transformAux1.getOrigin();
  337. var q = transformAux1.getRotation();
  338. objThree.position.set( p.x(), p.y(), p.z() );
  339. objThree.quaternion.set( q.x(), q.y(), q.z(), q.w() );
  340. }
  341. }
  342. }
  343. </script>
  344. </body>
  345. </html>