简体   繁体   English

Three.js - 使用深度和方向向量的 ExtrudeGeometry

[英]Three.js - ExtrudeGeometry using depth and a direction vector

I want to extrude a shape and create an ExtrudeGeometry , but the shape has to be extruded into a certain direction.我想挤出一个形状并创建一个ExtrudeGeometry ,但形状必须被挤出到某个方向。 I have a direction in a Vector3我在Vector3有一个方向

The shape is drawn in in the x, y plane and normally the z is the extrude direction (extrusion depth).形状是在 x、y 平面中绘制的,通常 z 是挤出方向(挤出深度)。 So a direction vector (0,0,1) would result in the default extrusion.因此,方向向量(0,0,1)将导致默认挤出。 But for example a (0,0,-1) would extrude the shape in the other direction.但例如 a (0,0,-1)会在另一个方向挤压形状。

I first tried to use an extrude path to achieve this, but when using a path the shape is allowed to "spin" freely and the initial orientation is arbitrary.我首先尝试使用挤出路径来实现这一点,但是当使用路径时,允许形状自由“旋转”并且初始方向是任意的。 This is not what I need, the shape must stay oriented as is.这不是我需要的,形状必须保持原样。 You can read details on this here in my previous question .您可以在我之前的问题中阅读有关此内容的详细信息。

I already came up with the idea of applying a matrix to the second half of the vertices of the resulting ExtrudedGeometry, but I cannot seem to get the geometry I want.我已经想出了将矩阵应用于生成的 ExtrudedGeometry 顶点的后半部分的想法,但我似乎无法获得我想要的几何图形。 Maybe it is my clumsy use of matrices, but I think that the face normals are pointing inside out after this trick.也许这是我对矩阵的笨拙使用,但我认为在这个技巧之后面部法线是由内向外指向的。

Note The direction vector will never be orthogonal to the z axis since this would give invalid shapes注意方向向量永远不会与 z 轴正交,因为这会给出无效的形状


So the question:所以问题是:

How do I get a reliable solution to extrude my shape into the given direction.我如何获得可靠的解决方案将我的形状挤压到给定的方向。 Here an example.这里有一个例子。 The shape is a square in the x,y plane (width and length 2000) the extrusion depth is also 2000 and three different vectors with a drawing of the expected result seen in 2D (front view) and 3D.形状是 x,y 平面(宽度和长度 2000)中的正方形,挤压深度也是 2000 和三个不同的向量,以及在 2D(前视图)和 3D 中看到的预期结果的绘图。

二维前视图

3D视图

Extrude your geometry in the usual way by specifying an extrusion depth, and then apply a shear matrix to your geometry.通过指定挤出深度以通常的方式挤出几何体,然后将剪切矩阵应用于几何体。

Here is how to specify a shear matrix that will tilt a geometry.以下是如何指定将倾斜几何体的剪切矩阵。

var matrix = new THREE.Matrix4();

var dir = new THREE.Vector3( 0.25, 1, 0.25 ); // you set this. a unit-length vector is not required.

var Syx = dir.x / dir.y,
    Syz = dir.z / dir.y;

matrix.set(   1,   Syx,   0,   0,
              0,     1,   0,   0,
              0,   Syz,   1,   0,
              0,     0,   0,   1  );

geometry.applyMatrix4( matrix );

(The three.js coordinate system has the y-axis up -- unlike in your illustration. You will have to accommodate.) (three.js 坐标系的 y 轴向上 - 与您的插图不同。您必须适应。)

three.js r.113三.js r.113

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM