three+ammo实现雪地中三维雪人下雪动画效果代码
代码语言:html
所属分类:三维
代码描述:three+ammo实现雪地中三维雪人下雪动画效果代码
代码标签: three ammo 雪地 三维 雪人 下雪 动画
下面为部分代码预览,完整代码请点击下载或在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;
}
canvas {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
margin: 0;
padding: 0;
}
</style>
</head>
<body >
<!-- using three.js -->
<!-- using ammo.js -->
<canvas id="colorMap"></canvas>
<canvas id="alphaMap"></canvas>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.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/OrbitControls.145.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/BufferGeometryUtils.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/ConvexHull.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/ConvexGeometry.145.js"></script>
<script type="module">
let amount = 8;
async function AmmoPhysics() {
if ("Ammo" in window == !1) return void console.error("AmmoPhysics: Couldn't find Ammo.js");
let e;
const t = await Ammo(),o = new t.btDefaultCollisionConfiguration(),n = new t.btCollisionDispatcher(o),a = new t.btDbvtBroadphase(),r = new t.btSequentialImpulseConstraintSolver(),s = new t.btDiscreteDynamicsWorld(n, a, r, o);
s.setGravity(new t.btVector3(0, -6, 0));
let i = new t.btTransform();
const l = new t.btTransform();
let d,c = new t.btVector3(0, 0, 0),h = new t.btQuaternion(0, 0, 0, 0);
function E(e) {
let t = null;
c.setValue(0, 0, 0);
let o = e.attributes.position.array;
t = new Ammo.btConvexHullShape();
for (let e = 0, n = o.length; e < n; e += 3) {
c.setValue(o[e], o[e + 1], o[e + 2]);
const a = e >= n - 3;
t.addPoint(c, a);
}
return t && t.setMargin(0), t || console.error("AmmoPhysics: Shape error."), t;
}
let w = [],u = new WeakMap();
let p = null,m = "";
function f(e, o, n) {
d = e.position, h = e.quaternion, i.setIdentity(), c.setValue(d.x, d.y, d.z), i.setOrigin(c),
i.setRotation(new t.btQuaternion(h.x, h.y, h.z, h.w));
const a = e.scale;
c.setValue(a.x, a.y, a.z), n.setLocalScaling(c), c.setValue(0, 0, 0);
const r = new t.btDefaultMotionState(i),l = c;
o > 0 && n.calculateLocalInertia(o, l);
const E = new t.btRigidBodyConstructionInfo(o, r, n, l),p = new t.btRigidBody(E);
p.setFriction(.2), p.setRestitution(.5), p.setDamping(0, .8), s.addRigidBody(p),
o > 0 && (w.push(e), u.set(e, p));
}
let R = 0;
const T = -.5;
return setInterval(function () {
const t = performance.now();
if (R > 0) {
const e = (t - R) / 1e3;
s.stepSimulation(e, 10);
}
R = t;
for (let t = 0, o = w.length; t < o; t++) {
if (!w[t]) continue;
let o = w[t];
if (o.isInstancedMesh) {
let t = o.instanceMatrix.array;
if (!u.has(o)) continue;
let n = u.get(o);
for (let o = 0; o < n.length; o++) if (n[o]) {
if (n[o].getMotionState().getWorldTransform(l), (d = l.getOrigin()).y() < T) {
let t = THREE.MathUtils.randFloat(-e / 2, e / 2),a = 10,r = THREE.MathUtils.randFloat(-e / 2, e / 2);
c.setValue(t, a, r), l.setIdentity(), l.setOrigin(c), n[o].setWorldTransform(l),
c.setValue(0, 0, 0), n[o].setLinearVelocity(c), n[o].setAngularVelocity(c), n[o].clearForces(),
d.x(t), d.y(a), d.z(r), h.setValue(0, 0, 0, 0);
} else h = l.getRotation();
compose(d, h, t, 16 * o);
}
u.set(o, n), o.instanceMatrix.needsUpdate = !0;
} else if (o.isMesh) {
if (!u.has(o)) continue;
const t = u.get(o);
if (t.getMotionState().getWorldTransform(l), (d = l.getOrigin()).y() < T) {
let n = THREE.MathUtils.randFloat(-e / 2, e / 2),a = 10,r = THREE.MathUtils.randFloat(-e / 2, e / 2);
c.setValue(n, a, r), l.setIdentity(), l.setOrigin(c), t.setWorldTransform(l), c.setValue(0, 0, 0),
t.setLinearVelocity(c), t.setAngularVelocity(c), t.clearForces(), h.setValue(0, 0, 0, 0),
o.position.set(n, a, r);
} else h = l.getRotation(), o.position.set(d.x(), d.y(), d.z());
o.quaternion.set(h.x(), h.y(), h.z(), h.w());
}
}
}, 1e3 / 60), {
addMesh: function (e, t = 0) {
const o = E(e.geometry);
if (!o) return console.error("AmmoPhysics: Shape is NULL."), !1;
f(e, t, o);
},
addCompoundMesh: function (e, o = 0, n = []) {
let a = new t.btCompoundShape();
for (let e = 0; e < n.length; e++) {
let t = E(n[e]);
if (!t) return console.error("AmmoPhysics: Shape is NULL."), !1;
c.setValue(0, 0, 0), i.setIdentity(), i.setOrigin(c), a.addChildShape(i, t);
}
f(e, o, a);
},
addParticleMesh: function (e, o = 0) {
if ((!e.name || e.name && e.name != m) && (p = E(e.geometry), m = e.name), !p) return console.error("AmmoPhysics: Shape is NULL."),
!1;
e.isInstancedMesh ? function (e, o, n) {
let a,r = e.instanceMatrix.array,l = [];
c.setValue(0, 0, 0);
for (let d = 0; d < e.count; d++) {
let e = 16 * d;
i.setFromOpenGLMatrix(r.slice(.........完整代码请登录后点击上方下载按钮下载查看
网友评论0