布料 模拟 html5,Three.js 简单布料/衣服动画模拟

本文介绍了一种使用JavaScript和Three.js库创建简单布料模拟的方法。通过应用约束解决器、阻尼、质量和重力等参数,实现了布料在风力和球体碰撞下的动态效果。代码中涉及Verlet积分、风力和球体碰撞的力计算,以及粒子系统来模拟布料的粒子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

/*

* Cloth Simulation using a relaxed constrains solver

*/

var DAMPING = 0.03;

var DRAG = 1 - DAMPING;

var MASS = 0.1;

var restDistance = 25;

var xSegs = 10;

var ySegs = 10;

var clothFunction = plane(restDistance * xSegs, restDistance * ySegs);

var cloth = new Cloth(xSegs, ySegs);

var GRAVITY = 981 * 1.4;

var gravity = new THREE.Vector3(0, -GRAVITY, 0).multiplyScalar(MASS);

var TIMESTEP = 18 / 1000;

var TIMESTEP_SQ = TIMESTEP * TIMESTEP;

var pins = [];

var wind = true;

var windStrength = 2;

var windForce = new THREE.Vector3(0, 0, 0);

var ballPosition = new THREE.Vector3(0, -45, 0);

var ballSize = 60; //40

var tmpForce = new THREE.Vector3();

var lastTime;

function plane(width, height) {

return function(u, v) {

var x = (u - 0.5) * width;

var y = (v + 0.5) * height;

var z = 0;

return new THREE.Vector3(x, y, z);

};

}

function Particle(x, y, z, mass) {

this.position = clothFunction(x, y); // position

this.previous = clothFunction(x, y); // previous

this.original = clothFunction(x, y);

this.a = new THREE.Vector3(0, 0, 0); // acceleration

this.mass = mass;

this.invMass = 1 / mass;

this.tmp = new THREE.Vector3();

this.tmp2 = new THREE.Vector3();

}

// Force -> Acceleration

Particle.prototype.addForce = function(force) {

this.a.add(

this.tmp2.copy(force).multiplyScalar(this.invMass)

);

};

// Performs verlet integration

Particle.prototype.integrate = function(timesq) {

var newPos = this.tmp.subVectors(this.position, this.previous);

newPos.multiplyScalar(DRAG).add(this.position);

newPos.add(this.a.multiplyScalar(timesq));

this.tmp = this.previous;

this.previous = this.position;

this.position = newPos;

this.a.set(0, 0, 0);

};

var diff = new THREE.Vector3();

function satisifyConstrains(p1, p2, distance) {

diff.subVectors(p2.position, p1.position);

var currentDist = diff.length();

if (currentDist === 0) return; // prevents division by 0

var correction = diff.multiplyScalar(1 - distance / currentDist);

var correctionHalf = correction.multiplyScalar(0.5);

p1.position.add(correctionHalf);

p2.position.sub(correctionHalf);

}

function Cloth(w, h) {

w = w || 10;

h = h || 10;

this.w = w;

this.h = h;

var particles = [];

var constrains

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值