three模拟三维0重力碰撞效果代码
代码语言:html
所属分类:三维
代码描述: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().........完整代码请登录后点击上方下载按钮下载查看
网友评论0