three实现三维点光球碰撞光影动画效果代码

代码语言:html

所属分类:三维

代码描述:three实现三维点光球碰撞光影动画效果代码,单击鼠标左键发射物体射击小球。

代码标签: three 三维 点光 碰撞 光影 动画

下面为部分代码预览,完整代码请点击下载或在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;
}
</style>


  
  
</head>

<body >
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/es-module-shims.1.6.2.js"></script>
<script type="importmap">
  {
    "imports": {      
      "three": "//repo.bfw.wiki/bfwrepo/js/module/three/build/151/three.module.js",
      "three/addons/": "//repo.bfw.wiki/bfwrepo/js/module/three/examples/151/jsm/"
    }
  }
</script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/ammo.wasm.min.js"></script>
      <script  type="module">
async function e() {
  function e(e) {
    let t = null;
    m.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) {
      m.setValue(n[e], n[e + 1], n[e + 2]);
      const i = e >= o - 3;
      t.addPoint(m, i);
    }
    return t && t.setMargin(0), t;
  }
  function n(e, t, n, i = null) {
    u = e.position,
    w = e.quaternion,
    d.setIdentity(),
    m.setValue(u.x, u.y, u.z),
    d.setOrigin(m),
    d.setRotation(new o.btQuaternion(w.x, w.y, w.z, w.w));
    const a = e.scale;
    m.setValue(a.x, a.y, a.z), n.setLocalScaling(m), m.setValue(0, 0, 0);
    const s = new o.btDefaultMotionState(d),
    r = m;
    t > 0 && n.calculateLocalInertia(t, r);
    const c = new o.btRigidBodyConstructionInfo(t, s, n, r),
    E = new o.btRigidBody(c);
    "pointLight" == e.name ? (
    E.setFriction(10), E.setRestitution(0.5)) :
    "floor" == e.name ? (
    E.setFriction(1), E.setRestitution(1), E.setDamping(0, 0)) : (
    E.setFriction(0.5), E.setRestitution(0.5), E.setDamping(0, 0)),
    "shootingBall" == e.name &&
    i && (
    m.setValue(i.x, i.y, i.z), E.setLinearVelocity(m)),
    E.name = e.name,
    l.addRigidBody(E),
    t > 0 && (h.push(e), p.set(e, E));
  }
  if ("Ammo" in window == !1) return;
  const o = await Ammo(),
  i = new o.btDefaultCollisionConfiguration(),
  a = new o.btCollisionDispatcher(i),
  s = new o.btDbvtBroadphase(),
  r = new o.btSequentialImpulseConstraintSolver(),
  l = new o.btDiscreteDynamicsWorld(a, s, r, i);
  l.setGravity(new o.btVector3(0, -10, 0));
  let d = new o.btTransform();
  const c = new o.btTransform();
  let u,
  m = new o.btVector3(0, 0, 0),
  w = new o.btQuaternion(0, 0, 0, 0),
  h = [],
  p = new WeakMap(),
  E = null,
  g = "",
  f = 0;
  return (
    setInterval(function () {
      const e = performance.now();
      if (f > 0) {
        const t = (e - f) / 1e3;
        l.stepSimulation(t, 10);
      }
      f = e;
      for (let e = 0, n = h.length; e < n; e++) {
        if (!h[e]) continue;
        let n = h[e];
        if (n.isInstancedMesh) {
          let e = n.instanceMatrix.array;
          if (!p.has(n)) continue;
          let o = p.get(n);
          for (let n = 0; n < o.length; n++)
          o[n] && (
          o[n].getMotionState().getWorldTransform(c),
          u = c.getOrigin(),
          w = c.getRotation(),
          t(u, w, e, 16 * n));
          p.set(n, o), n.instanceMatrix.needsUpdate = !0;
        } else if (n.isMesh) {
          if (!p.has(n)) continue;
          p.get(n).getMotionState().getWorldTransform(c),
          u = c.getOrigin(),
          w = c.getRotation(),
          n.position.set(u.x(), u.y(), u.z()),
          n.quaternion.set(w.x(), w.y(), w.z(), w.w());
        }
      }
    }, 1e3 / 60),
    {
      addMesh: function (t, o = 0) {
        const i = e(t.geometry);
        if (!i) return !1;
        n(t, o, i);
      },
      addCompoundMesh: function (t, i = 0, a = []) {
        let s = new o.btCompoundShape();
        for (let t = 0; t < a.length; t++) {
          let n = e(a[t]);
          if (!n) return !1;
          m.setValue(0, 0, 0),
          d.setIdentity(),
          d.setOrigin(m),
          s.addChildShape(d, n);
        }
        n(t, i, s);
      },
      addParticleMesh: function (t, i = 0) {
        if (
        (!t.name || t.name && t.name != g) && (
        E = e(t.geometry), g = t.name),
        !E)

        return !1;
        t.isInstancedMesh ?
        function (e, t, n) {
          let i,
          a = e.instanceMatrix.array,
          s = [];
          m.setValue(0, 0, 0);
          for (let r = 0; r < e.count; r++) {
            let c = 16 * r;
            d.setFromOpenGLMatrix(a.slice(c, c + 16));
            let u = new o.btDefaultMotionState(d),
            w = m;
            n.calculateLocalInertia(t, w);
            let h = new o.btRigidBodyConstructionInfo(t, u, n, w);
            (i = new o.btRigidBody(h)).setFriction(0.5),
            i.se.........完整代码请登录后点击上方下载按钮下载查看

网友评论0