threejs 形状几何体_Threejs-如何按距离偏移2D几何图形上的所有点

Using Three.js, (although I believe this is more math related) I have a set of 2D points that can create a 2D geometry. such as square, rectangle, pentagon, or custom 2D shape. Based of the original 2D shape, I would like to create a method to offset the points inward or outward uniformly in such a way like the attached image.

I don't know if there is a simple way to offset/grow/shrink all the points (vector3) uniformly on the 2D shape inward or outward. And if so, it'll be cool if I can offset the points by X distance? Kinda of like saying offset the points on the 2D shape outward or inward by X distance.

And no, I'm not referring to scaling from a center point. While scaling may work for symmetrical shapes, it won't work when it comes to non-symmetrical shapes.

see image for example

Thanks in advance.

解决方案

You can read that forum thread.

I've made some changes with ProfiledContourGeometry and got OffsetContour, so I leave it here, just in case, what if it helps :)

function OffsetContour(offset, contour) {

let result = [];

offset = new THREE.BufferAttribute(new Float32Array([offset, 0, 0]), 3);

console.log("offset", offset);

for (let i = 0; i < contour.length; i++) {

let v1 = new THREE.Vector2().subVectors(contour[i - 1 < 0 ? contour.length - 1 : i - 1], contour[i]);

let v2 = new THREE.Vector2().subVectors(contour[i + 1 == contour.length ? 0 : i + 1], contour[i]);

let angle = v2.angle() - v1.angle();

let halfAngle = angle * 0.5;

let hA = halfAngle;

let tA = v2.angle() + Math.PI * 0.5;

let shift = Math.tan(hA - Math.PI * 0.5);

let shiftMatrix = new THREE.Matrix4().set(

1, 0, 0, 0,

-shift, 1, 0, 0,

0, 0, 1, 0,

0, 0, 0, 1

);

let tempAngle = tA;

let rotationMatrix = new THREE.Matrix4().set(

Math.cos(tempAngle), -Math.sin(tempAngle), 0, 0,

Math.sin(tempAngle), Math.cos(tempAngle), 0, 0,

0, 0, 1, 0,

0, 0, 0, 1

);

let translationMatrix = new THREE.Matrix4().set(

1, 0, 0, contour[i].x,

0, 1, 0, contour[i].y,

0, 0, 1, 0,

0, 0, 0, 1,

);

let cloneOffset = offset.clone();

console.log("cloneOffset", cloneOffset);

shiftMatrix.applyToBufferAttribute(cloneOffset);

rotationMatrix.applyToBufferAttribute(cloneOffset);

translationMatrix.applyToBufferAttribute(cloneOffset);

result.push(new THREE.Vector2(cloneOffset.getX(0), cloneOffset.getY(0)));

}

return result;

}

Feel free to modify it :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值