three模拟三维0重力碰撞效果代码

代码语言:html

所属分类:三维

代码描述:three模拟三维0重力碰撞效果代码,点击鼠标发射一个物体

代码标签: three 模拟 三维 0 重力 碰撞

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">

  
  
  
  
<style>
* {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  margin: 0;
  padding: 0;
}
body {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
}
canvas {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  margin: 0;
  padding: 0;
}
#msg_start {
  font-family: "Fredoka One", sans-serif;
  position: fixed;
  inset: auto 0 0;
  margin: 0 auto 1svh;
  display: block;
  color: #009b77;
  text-stroke: 1px #fff;
  -webkit-text-stroke: 1px #fff;
  text-align: center;
  font-size: max(28px, 8vh);
  pointer-events: none;
  z-index: 1;
  animation: blink 1s 0.22s infinite alternate;
}
@keyframes blink {
  0% {
    opacity: 1;
  }
  78% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
</style>


</head>

<body >

<link href="https://fonts.googleapis.com/css2?family=Fredoka+One&display=swap" rel="stylesheet">

<div id='msg_start'>Tap to shoot a ball!</div>


<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.145.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/OrbitControls.145.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/ammo.wasm.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/BufferGeometryUtils.js"></script>
      <script  type="module">
async function AmmoPhysics() {
  if ("Ammo" in window == !1) return void console.error("AmmoPhysics: Couldn't find Ammo.js");
  const e = await Ammo(),t = new e.btDefaultCollisionConfiguration(),n = new e.btCollisionDispatcher(t),o = new e.btDbvtBroadphase(),i = new e.btSequentialImpulseConstraintSolver(),a = new e.btDiscreteDynamicsWorld(n, o, i, t);
  a.setGravity(new e.btVector3(0, 0, 0));
  let r = new e.btTransform();
  const s = new e.btTransform();
  let l,c = new e.btVector3(0, 0, 0),d = new e.btQuaternion(0, 0, 0, 0);
  function m(e) {
    let t = null;
    c.setValue(0, 0, 0);
    let n = e.attributes.position.array;
    t = new Ammo.btConvexHullShape();
    for (let e = 0, o = n.length; e < o; e += 3) {
      c.setValue(n[e], n[e + 1], n[e + 2]);
      const i = e >= o - 3;
      t.addPoint(c, i);
    }
    return t && t.setMargin(0), t || console.error("AmmoPhysics: Shape error."), t;
  }
  let u = [],w = new WeakMap();
  let h = null,E = "";
  function p(t, n, o, i = null) {
    l = t.position, d = t.quaternion, r.setIdentity(), c.setValue(l.x, l.y, l.z), r.setOrigin(c),
    r.setRotation(new e.btQuaternion(d.x, d.y, d.z, d.w));
    const s = t.scale;
    c.setValue(s.x, s.y, s.z), o.setLocalScaling(c), c.setValue(0, 0, 0);
    const m = new e.btDefaultMotionState(r),h = c;
    n > 0 && o.calculateLocalInertia(n, h);
    const E = new e.btRigidBodyConstructionInfo(n, m, o, h),p = new e.btRigidBody(E);
    "pointLight" == t.name ? (p.setFriction(10), p.setRestitution(.1), p.setDamping(1, 1)) : (p.setFriction(1),
    p.setRestitution(1), p.setDamping(0, 0)), "shootingBall" == t.name && i && (c.setValue(i.x, i.y, i.z),
    p.setLinearVelocity(c)), p.name = t.name, a.addRigidBody(p), n > 0 && (u.push(t),
    w.set(t, p));
  }
  let g = 0;
  return setInterval(function () {
    const e = performance.now();
    if (g > 0) {
      const t = (e - g) / 1e3;
      a.stepSimulation(t, 10);
    }
    g = e;
    for (let e = 0, t = u.length; e < t; e++) {
      if (!u[e]) continue;
      let t = u[e];
      if (t.isInstancedMesh) {
        let e = t.instanceMatrix.array;
        if (!w.has(t)) continue;
        let n = w.get(t);
        for (let t = 0; t < n.length; t++) n[t] && (n[t].getMotionState().getWorldTransform(s),
        l = s.getOrigin(), d = s.getRotation(), compose(l, d, e, 16 * t));
        w.set(t, n), t.instanceMatrix.needsUpdate = !0;
      } else if (t.isMesh) {
        if (!w.has(t)) continue;
        w.get(t).getMotionState().getWorldTransform(s), l = s.getOrigin(), d = s.getRotation(),
        t.position.set(l.x(), l.y(), l.z()), t.quaternion.set(d.x(), d.y(), d.z(), d.w());
      }
    }
  }, 1e3 / 60), {
    addMesh: function (e, t = 0) {
      const n = m(e.geometry);
      if (!n) return console.error("AmmoPhysics: Shape is NULL."), !1;
      p(e, t, n);
    },
    addCompoundMesh: function (t, n = 0, o = []) {
      let i = new e.btCompoundShape();
      for (let e = 0; e < o.length; e++) {
        let t = m(o[e]);
        if (!t) return console.error("AmmoPhysics: Shape is NULL."), !1;
        c.setValue(0, 0, 0), r.setIdentity(), r.setOrigin(c), i.addChildShape(r, t);
      }
      p(t, n, i);
    },
    addParticleMesh: function (t, n = 0) {
      if ((!t.name || t.name && t.name != E) && (h = m(t.geometry), E = t.name), !h) return console.error("AmmoPhysics: Shape is NULL."),
      !1;
      t.isInstancedMesh ? function (t, n, o) {
        let i,s = t.instanceMatrix.array,l = [];
        c.setValue(0, 0, 0);
        for (let d = 0; d < t.count; d++) {
          let m = 16 * d;
          r.setFromOpenGLMatrix(s.slice(m, m + 16));
          let u = new e.btDefaultMotionState(r),w = c;
          o.calculateLocalInertia(n, w);
          let h = new e.btRigidBodyConstructionInfo(n, u, o, w);
          (i = new e.btRigidBody(h)).setFriction(1), i.setRestitution(1), i.setDamping(0, 0),
          i.name = t.name, a.addRigidBody(i), l.push(i);
        }
        n > 0 && (u.push(t), w.set(t, l));
      }(t, n, h) : t.isMesh && p(t, n, h);
    },
    addShootingMesh: function (e, t = 0, n = null) {
      return n ? ((!e.name || e.name && e.name != E) && (h = m(e.geometry), E = e.name),
      h ? void p(e, t, h, n) : (console.error("AmmoPhysics: Shape is NULL."), !1)) : (console.error("AmmoPhysics: Parameter is NULL."),
      !1);
    } };

}

function compose(e, t, n, o) {
  const i = t.x(),a = t.y(),r = t.z(),s = t.w(),l = i + i,c = a + a,d = r + r,m = i * l,u = i * c,w = i * d,h = a * c,E = a * d,p = r * d,g = s * l,R = s * c,y = s * d;
  n[o + 0] = 1 - (h + p), n[o + 1] = u + y, n[o + 2] = w - R, n[o + 3].........完整代码请登录后点击上方下载按钮下载查看

网友评论0