m33_ammo_turn_cloth.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  6. <meta name="author" content="火星科技 http://mars3d.cn " />
  7. <meta name="apple-touch-fullscreen" content="yes" />
  8. <meta name="apple-mobile-web-app-capable" content="yes" />
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  10. <meta name="format-detection" content="telephone=no" />
  11. <meta name="x5-fullscreen" content="true" />
  12. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  13. <!-- 标题及搜索关键字 -->
  14. <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  15. <meta
  16. name="description"
  17. content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型 gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包 地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
  18. />
  19. <link rel="shortcut icon" type="image/x-icon" href="" />
  20. <title>ammo物理引擎扩展支持 </title>
  21. <!--第三方lib-->
  22. <script
  23. type="text/javascript"
  24. src="../lib/include-lib.js"
  25. libpath="../lib/"
  26. include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars3d,cesium-meshVisualizer"
  27. ></script>
  28. <link href="css/style.css" rel="stylesheet" />
  29. </head>
  30. <body class="dark">
  31. <div id="mars3dContainer" class="mars3d-container"></div>
  32. <!-- 面板 -->
  33. <div class="infoview">操作提示: Q 左摆动、 A 右摆动</div>
  34. <script src="./js/common.js"></script>
  35. <script type="text/javascript">
  36. "use script"; //开发环境建议开启严格模式
  37. var map;
  38. function initMap(options) {
  39. //合并属性参数,可覆盖config.json中的对应配置
  40. var mapOptions = mars3d.Util.merge(options, {
  41. scene: {
  42. center: { lat: 31.834535, lng: 117.21992, alt: 80, heading: 38, pitch: -34 },
  43. fxaa: true,
  44. },
  45. });
  46. //创建三维地球场景
  47. map = new mars3d.Map("mars3dContainer", mapOptions);
  48. var MeshVisualizer = Cesium.MeshVisualizer;
  49. var Mesh = Cesium.Mesh;
  50. var MeshMaterial = Cesium.MeshMaterial;
  51. var FramebufferTexture = Cesium.FramebufferTexture;
  52. var GeometryUtils = Cesium.GeometryUtils;
  53. var LOD = Cesium.LOD;
  54. var PlaneBufferGeometry = Cesium.PlaneBufferGeometry;
  55. var BasicMeshMaterial = Cesium.BasicMeshMaterial;
  56. var MeshPhongMaterial = Cesium.MeshPhongMaterial;
  57. var center = Cesium.Cartesian3.fromDegrees(117.220206, 31.834866, 50);
  58. var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(center);
  59. var meshVisualizer = new MeshVisualizer({
  60. modelMatrix: modelMatrix,
  61. up: { y: 1 },
  62. referenceAxisParameter: {
  63. length: 100,
  64. width: 0.05,
  65. headLength: 2,
  66. headWidth: 0.1,
  67. },
  68. });
  69. map.scene.primitives.add(meshVisualizer);
  70. meshVisualizer.showReference = true; //显示坐标轴
  71. Ammo().then(function () {
  72. // Physics variables
  73. var gravityConstant = -9.8;
  74. var collisionConfiguration;
  75. var dispatcher;
  76. var broadphase;
  77. var solver;
  78. var physicsWorld;
  79. var rigidBodies = [];
  80. var margin = 0.05;
  81. var hinge;
  82. var cloth;
  83. var transformAux1 = new Ammo.btTransform();
  84. var armMovement = 0;
  85. var softBodySolver;
  86. function initPhysics() {
  87. // Physics configuration
  88. collisionConfiguration = new Ammo.btSoftBodyRigidBodyCollisionConfiguration();
  89. dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration);
  90. broadphase = new Ammo.btDbvtBroadphase();
  91. solver = new Ammo.btSequentialImpulseConstraintSolver();
  92. softBodySolver = new Ammo.btDefaultSoftBodySolver();
  93. physicsWorld = new Ammo.btSoftRigidDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration, softBodySolver);
  94. physicsWorld.setGravity(new Ammo.btVector3(0, gravityConstant, 0));
  95. physicsWorld.getWorldInfo().set_m_gravity(new Ammo.btVector3(0, gravityConstant, 0));
  96. }
  97. function createRandomColor() {
  98. return Cesium.Color.fromRandom({ alpha: 1 }); //fromRgba(Math.floor(Math.random() * (1 << 24)));
  99. }
  100. function createMaterial() {
  101. return new MeshPhongMaterial({
  102. defaultColor: createRandomColor(),
  103. side: MeshMaterial.Sides.DOUBLE,
  104. translucent: false,
  105. });
  106. }
  107. function createObjects() {
  108. var pos = new Cesium.Cartesian3();
  109. var quat = new Cesium.Quaternion();
  110. // Ground
  111. (pos.x = 0), (pos.y = -0.5), (pos.z = 0);
  112. (quat.x = 0), (quat.y = 0), (quat.z = 0), (quat.w = 1);
  113. var ground = createParalellepiped(
  114. 40,
  115. 1,
  116. 40,
  117. 0,
  118. pos,
  119. quat,
  120. new MeshPhongMaterial({
  121. defaultColor: "rgb(125,125,125)",
  122. side: MeshMaterial.Sides.DOUBLE,
  123. translucent: false,
  124. })
  125. );
  126. // Wall
  127. var brickMass = 0.5;
  128. var brickLength = 1.2;
  129. var brickDepth = 0.6;
  130. var brickHeight = brickLength * 0.5;
  131. var numBricksLength = 6;
  132. var numBricksHeight = 8;
  133. var z0 = -numBricksLength * brickLength * 0.5;
  134. (pos.x = 0), (pos.y = brickHeight * 0.5), (pos.z = z0);
  135. (quat.x = 0), (quat.y = 0), (quat.z = 0), (quat.w = 1);
  136. for (var j = 0; j < numBricksHeight; j++) {
  137. var oddRow = j % 2 == 1;
  138. pos.z = z0;
  139. if (oddRow) {
  140. pos.z -= 0.25 * brickLength;
  141. }
  142. var nRow = oddRow ? numBricksLength + 1 : numBricksLength;
  143. for (let i = 0; i < nRow; i++) {
  144. var brickLengthCurrent = brickLength;
  145. var brickMassCurrent = brickMass;
  146. if (oddRow && (i == 0 || i == nRow - 1)) {
  147. brickLengthCurrent *= 0.5;
  148. brickMassCurrent *= 0.5;
  149. }
  150. var brick = createParalellepiped(brickDepth, brickHeight, brickLengthCurrent, brickMassCurrent, pos, quat, createMaterial());
  151. if (oddRow && (i == 0 || i == nRow - 2)) {
  152. pos.z += 0.75 * brickLength;
  153. } else {
  154. pos.z += brickLength;
  155. }
  156. }
  157. pos.y += brickHeight;
  158. }
  159. // The cloth
  160. // Cloth graphic object
  161. var clothWidth = 4;
  162. var clothHeight = 3;
  163. var clothNumSegmentsZ = clothWidth * 5;
  164. var clothNumSegmentsY = clothHeight * 5;
  165. var clothSegmentLengthZ = clothWidth / clothNumSegmentsZ;
  166. var clothSegmentLengthY = clothHeight / clothNumSegmentsY;
  167. var clothPos = new Cesium.Cartesian3(-3, 3, 2);
  168. var clothGeometry = new PlaneBufferGeometry(clothWidth, clothHeight, clothNumSegmentsZ, clothNumSegmentsY);
  169. var clothMaterial = new MeshPhongMaterial({
  170. defaultColor: "rgb(255,255,255)",
  171. side: MeshMaterial.Sides.DOUBLE,
  172. translucent: false,
  173. });
  174. cloth = new Mesh(clothGeometry, clothMaterial);
  175. GeometryUtils.rotateY(cloth.geometry, Math.PI * 0.5);
  176. GeometryUtils.translate(cloth.geometry, clothPos.x, clothPos.y + clothHeight * 0.5, clothPos.z - clothWidth * 0.5);
  177. GeometryUtils.computeVertexNormals(cloth.geometry);
  178. meshVisualizer.add(cloth);
  179. // Cloth physic object
  180. var softBodyHelpers = new Ammo.btSoftBodyHelpers();
  181. var clothCorner00 = new Ammo.btVector3(clothPos.x, clothPos.y + clothHeight, clothPos.z);
  182. var clothCorner01 = new Ammo.btVector3(clothPos.x, clothPos.y + clothHeight, clothPos.z - clothWidth);
  183. var clothCorner10 = new Ammo.btVector3(clothPos.x, clothPos.y, clothPos.z);
  184. var clothCorner11 = new Ammo.btVector3(clothPos.x, clothPos.y, clothPos.z - clothWidth);
  185. var clothSoftBody = softBodyHelpers.CreatePatch(
  186. physicsWorld.getWorldInfo(),
  187. clothCorner00,
  188. clothCorner01,
  189. clothCorner10,
  190. clothCorner11,
  191. clothNumSegmentsZ + 1,
  192. clothNumSegmentsY + 1,
  193. 0,
  194. true
  195. );
  196. var sbConfig = clothSoftBody.get_m_cfg();
  197. sbConfig.set_viterations(10);
  198. sbConfig.set_piterations(10);
  199. clothSoftBody.setTotalMass(0.9, false);
  200. Ammo.castObject(clothSoftBody, Ammo.btCollisionObject)
  201. .getCollisionShape()
  202. .setMargin(margin * 3);
  203. physicsWorld.addSoftBody(clothSoftBody, 1, -1);
  204. cloth.physicsBody = clothSoftBody;
  205. // Disable deactivation
  206. clothSoftBody.setActivationState(4);
  207. // The base
  208. var armMass = 2;
  209. var armLength = 3 + clothWidth;
  210. var pylonHeight = clothPos.y + clothHeight;
  211. var baseMaterial = new MeshPhongMaterial({
  212. defaultColor: "rgb(255,255,0)",
  213. side: MeshMaterial.Sides.DOUBLE,
  214. translucent: false,
  215. });
  216. (pos.x = clothPos.x), (pos.y = 0.1), (pos.z = clothPos.z - armLength);
  217. (quat.x = 0), (quat.y = 0), (quat.z = 0), (quat.w = 1);
  218. var base = createParalellepiped(1, 0.2, 1, 0, pos, quat, baseMaterial);
  219. (pos.x = clothPos.x), (pos.y = 0.5 * pylonHeight), (pos.z = clothPos.z - armLength);
  220. var pylon = createParalellepiped(0.4, pylonHeight, 0.4, 0, pos, quat, baseMaterial);
  221. (pos.x = clothPos.x), (pos.y = pylonHeight + 0.2), (pos.z = clothPos.z - 0.5 * armLength);
  222. var arm = createParalellepiped(0.4, 0.4, armLength + 0.4, armMass, pos, quat, baseMaterial);
  223. // Glue the cloth to the arm
  224. var influence = 0.5;
  225. clothSoftBody.appendAnchor(0, arm.physicsBody, false, influence);
  226. clothSoftBody.appendAnchor(clothNumSegmentsZ, arm.physicsBody, false, influence);
  227. // Hinge constraint to move the arm
  228. var pivotA = new Ammo.btVector3(0, pylonHeight * 0.5, 0);
  229. var pivotB = new Ammo.btVector3(0, -0.2, -armLength * 0.5);
  230. var axis = new Ammo.btVector3(0, 1, 0);
  231. hinge = new Ammo.btHingeConstraint(pylon.physicsBody, arm.physicsBody, pivotA, pivotB, axis, axis, true);
  232. physicsWorld.addConstraint(hinge, true);
  233. }
  234. function createParalellepiped(sx, sy, sz, mass, pos, quat, material) {
  235. var threeObject = new Mesh(
  236. Cesium.BoxGeometry.fromDimensions({
  237. dimensions: new Cesium.Cartesian3(sx, sy, sz),
  238. vertexFormat: new Cesium.VertexFormat({
  239. position: true,
  240. st: true,
  241. normal: true,
  242. }),
  243. }),
  244. material
  245. );
  246. threeObject.quaternion = new Cesium.Quaternion();
  247. threeObject.geometry.attributes.uv = threeObject.geometry.attributes.st;
  248. threeObject.geometry.attributes.st = undefined;
  249. var shape = new Ammo.btBoxShape(new Ammo.btVector3(sx * 0.5, sy * 0.5, sz * 0.5));
  250. shape.setMargin(margin);
  251. createRigidBody(threeObject, shape, mass, pos, quat);
  252. return threeObject;
  253. }
  254. function createRigidBody(threeObject, physicsShape, mass, pos, quat) {
  255. Cesium.Cartesian3.clone(pos, threeObject.position);
  256. Cesium.Quaternion.clone(quat, threeObject.quaternion);
  257. var transform = new Ammo.btTransform();
  258. transform.setIdentity();
  259. transform.setOrigin(new Ammo.btVector3(pos.x, pos.y, pos.z));
  260. transform.setRotation(new Ammo.btQuaternion(quat.x, quat.y, quat.z, quat.w));
  261. var motionState = new Ammo.btDefaultMotionState(transform);
  262. var localInertia = new Ammo.btVector3(0, 0, 0);
  263. physicsShape.calculateLocalInertia(mass, localInertia);
  264. var rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, motionState, physicsShape, localInertia);
  265. var body = new Ammo.btRigidBody(rbInfo);
  266. threeObject.physicsBody = body;
  267. meshVisualizer.add(threeObject);
  268. if (mass > 0) {
  269. rigidBodies.push(threeObject);
  270. // Disable deactivation
  271. body.setActivationState(4);
  272. }
  273. physicsWorld.addRigidBody(body);
  274. }
  275. //deltaTime单位是:秒/s
  276. function updatePhysics(deltaTime) {
  277. // Hinge control
  278. hinge.enableAngularMotor(true, 0.8 * armMovement, 50);
  279. // Step world
  280. physicsWorld.stepSimulation(deltaTime, 10);
  281. // Update cloth
  282. var softBody = cloth.physicsBody;
  283. var clothPositions = cloth.geometry.attributes.position.values;
  284. var numVerts = clothPositions.length / 3;
  285. var nodes = softBody.get_m_nodes();
  286. var indexFloat = 0;
  287. for (let i = 0; i < numVerts; i++) {
  288. var node = nodes.at(i);
  289. var nodePos = node.get_m_x();
  290. clothPositions[indexFloat++] = nodePos.x();
  291. clothPositions[indexFloat++] = nodePos.y();
  292. clothPositions[indexFloat++] = nodePos.z();
  293. }
  294. cloth.geometry.attributes.position.needsUpdate = true;
  295. if (cloth.geometry.primitiveType == Cesium.PrimitiveType.TRIANGLES) {
  296. // Cesium.GeometryPipeline.computeNormal(cloth.geometry);
  297. GeometryUtils.computeVertexNormals(cloth.geometry);
  298. }
  299. // Update rigid bodies
  300. for (let i = 0, il = rigidBodies.length; i < il; i++) {
  301. var objGraph = rigidBodies[i];
  302. var objPhys = objGraph.physicsBody;
  303. var ms = objPhys.getMotionState();
  304. if (ms) {
  305. ms.getWorldTransform(transformAux1);
  306. var p = transformAux1.getOrigin();
  307. var q = transformAux1.getRotation();
  308. objGraph.position.x = p.x();
  309. objGraph.position.y = p.y();
  310. objGraph.position.z = p.z();
  311. objGraph.quaternion.x = q.x();
  312. objGraph.quaternion.y = q.y();
  313. objGraph.quaternion.z = q.z();
  314. objGraph.quaternion.w = q.w();
  315. //objGraph.needsUpdate = true;
  316. objGraph.modelMatrixNeedsUpdate = true;
  317. }
  318. }
  319. }
  320. var start = false;
  321. function initInput() {
  322. window.addEventListener(
  323. "keydown",
  324. function (event) {
  325. if (!start) {
  326. return;
  327. }
  328. switch (event.keyCode) {
  329. // Q
  330. case 81:
  331. armMovement = 1;
  332. break;
  333. // A
  334. case 65:
  335. armMovement = -1;
  336. break;
  337. }
  338. },
  339. false
  340. );
  341. window.addEventListener(
  342. "keyup",
  343. function (event) {
  344. armMovement = 0;
  345. },
  346. false
  347. );
  348. }
  349. var startTime = new Date();
  350. function update(frameState) {
  351. var deltaTime = (new Date() - startTime) / 1000.0;
  352. updatePhysics(deltaTime);
  353. startTime = new Date();
  354. }
  355. var init = false;
  356. setTimeout(function () {
  357. if (!init) {
  358. initPhysics();
  359. createObjects();
  360. initInput();
  361. init = true;
  362. }
  363. if (!start) {
  364. meshVisualizer.beforeUpdate.addEventListener(update);
  365. start = true;
  366. } else {
  367. meshVisualizer.beforeUpdate.removeEventListener(update);
  368. start = false;
  369. }
  370. }, 1000 * 3);
  371. });
  372. //});
  373. }
  374. </script>
  375. </body>
  376. </html>