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 a = e >= o - 3; t.addPoint(m, a); } return t && t.setMargin(0), t; } function n(e, t, n, a = 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 i = e.scale; m.setValue(i.x, i.y, i.z), n.setLocalScaling(m), m.setValue(0, 0, 0); const r = new o.btDefaultMotionState(d), s = m; t > 0 && n.calculateLocalInertia(t, s); const c = new o.btRigidBodyConstructionInfo(t, r, n, s), 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 && a && ( m.setValue(a.x, a.y, a.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(), a = new o.btDefaultCollisionConfiguration(), i = new o.btCollisionDispatcher(a), r = new o.btDbvtBroadphase(), s = new o.btSequentialImpulseConstraintSolver(), l = new o.btDiscreteDynamicsWorld(i, r, s, a); 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 a = e(t.geometry); if (!a) return !1; n(t, o, a); }, addCompoundMesh: function (t, a = 0, i = []) { let r = new o.btCompoundShape(); for (let t = 0; t < i.length; t++) { let n = e(i[t]); if (!n) return !1; m.setValue(0, 0, 0), d.setIdentity(), d.setOrigin(m), r.addChildShape(d, n); } n(t, a, r); }, addParticleMesh: function (t, a = 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 a, i = e.instanceMatrix.array, r = []; m.setValue(0, 0, 0); for (let s = 0; s < e.count; s++) { let c = 16 * s; d.setFromOpenGLMatrix(i.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); (a = new o.btRigidBody(h)).setFriction(0.5), a.setRestitution(0.3), a.setDamping(0, 0), a.name = e.name, l.addRigidBody(a), r.push(a); } t > 0 && (h.push(e), p.set(e, r)); }(t, a, E) : t.isMesh && n(t, a, E); }, addShootingMesh:.........完整代码请登录后点击上方下载按钮下载查看
网友评论0