【问题标题】:How to get the exact x,y,z location of all the corners of a cube in three.js如何在three.js中获取立方体所有角的确切x,y,z位置
【发布时间】:2020-09-29 00:35:26
【问题描述】:

有another answer to this question,但在实现之后,我发现这些位置并不是顶点绝对 x、y、z 位置的真实表示。我检查了网格,并查看了 geometry:BoxGeometry 属性中的 _corners 数组。在那里我也可以找到值。如果立方体在两个不同的轴上旋转,并且在 x 和 z 轴上移动,角位置的 y 值不会改变,因此这些不是角的 x、y 和 z 位置的准确值。我可以明显看到角位置在 x、y 和 z 轴上移动。

我还能如何得出这些角的准确、绝对位置?到目前为止,我找不到确定这一点的属性。我是否需要使用具有这些角位置的旋转值来应用某种数学?

【问题讨论】:

    标签: three.js rotation position vertices


    【解决方案1】:

    您只需要获取立方体的WorldMatrix(包含所有变换)并将其应用于具有原始顶点位置的 Vector3:

    var w = 10;
    var h = 15;
    var d = 7;
    
    const geom = new THREE.BoxBufferGeometry(w, h, d);
    const mat = new THREE.MeshBasicMaterial();
    const box = new THREE.Mesh(geom, mat);
    
    // Get world matrix transformation of the box
    var boxMatrix = box.matrixWorld;
    
    // Set the initial position of the desired vertex into a Vector3
    var vertex = new THREE.Vector3(w / 2, h / 2, d / 2);
    
    // Apply the matrix transformation to the vector
    vertex.applyMatrix4(boxMatrix);
    
    // Read the result
    console.log(vertex);
    

    【讨论】:

    • 这会给我盒子所有 8 个角的 x、y、z 位置吗?
    • 不,这会给你一个角落的位置。您需要使用(+x, +y, +z)、(+x, -y, +z)、(-x, +y, +z)、(-x, -y, +z)...等重复相同的过程 8 次,以获得所有八个顶点。
    猜你喜欢
    • 2020-02-04
    • 2012-09-14
    • 1970-01-01
    • 2012-10-14
    • 2018-12-13
    • 2014-02-07
    • 2019-10-09
    • 2014-12-02
    • 2019-11-28
    相关资源
    最近更新 更多