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