首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Three.js中基于局部向量的子网格定位

Three.js中基于局部向量的子网格定位
EN

Stack Overflow用户
提问于 2017-07-06 13:32:59
回答 1查看 842关注 0票数 4

我需要帮助定位网格到特定的点,根据当地的坐标。我制作了高度为0.1的粉红色圆柱体,以展示我想相互触摸的立方体的局部点。

我还想让cube2成为cube1的孩子,但这暂时不是一个交易的破坏者。

预期结果:立方体与其特定的角接触,并在适当的旋转-粉红色圆柱体应该是像素完美的相互覆盖。

我试着用:

代码语言:javascript
复制
cube2.position.copy(cube_position.clone().add(cube2_position))
cube2.rotation.setFromVector3(cube_rotation.clone().add(cube2_rotation))

但这不像预期的那样。我应该使用某种矩阵变换吗?

代码语言:javascript
复制
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(25, 25, 12);
// setup cubes

var geometry = new THREE.CubeGeometry(6, 6, 6);
var material = new THREE.MeshLambertMaterial({
  color: 0x00fff0
});
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);

cube.position.set(0.3, 1, -2);
cube.rotation.set(Math.PI / 3, Math.PI / 2, 1)

var geometry2 = new THREE.CubeGeometry(4, 4, 4);
var material2 = new THREE.MeshLambertMaterial({
  color: 0x0fff00
});
var cube2 = new THREE.Mesh(geometry2, material2);
cube.add(cube2);

cube_position = new THREE.Vector3(3, 3, 3)
cube_rotation = new THREE.Vector3(Math.PI / 3, Math.PI / 4, 0)
cube2_position = new THREE.Vector3(2, -2, 2)
cube2_rotation = new THREE.Vector3(Math.PI / 2, Math.PI / 2, 0)

// visualize points
vmat = new THREE.MeshLambertMaterial({
  color: 0xFF00FF,
  opacity: 0.5,
  transparent: true
});
v1 = new THREE.Mesh(new THREE.CylinderGeometry(1, 1, 0.1, 10), vmat);
cube.add(v1)
v1.position.copy(cube_position)
v1.rotation.setFromVector3(cube_rotation)

v2 = new THREE.Mesh(new THREE.CylinderGeometry(1, 1, 0.1, 10), vmat);
cube2.add(v2)
v2.position.copy(cube2_position)
v2.rotation.setFromVector3(cube2_rotation)

// BUG: connect cube with cube2 on specified points

cube2.position.copy(cube_position.clone().add(cube2_position))
cube2.rotation.setFromVector3(cube_rotation.clone().add(cube2_rotation))

// setup rest
var pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.x = 10;
pointLight.position.y = 50;
pointLight.position.z = 130;
scene.add(pointLight)

const renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x20252f);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);

const controls = new THREE.OrbitControls(camera, renderer.domElement);

animate();

function animate() {
  requestAnimationFrame(animate);
  render();
}

function render() {
  renderer.render(scene, camera);
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r82/three.js"></script>
<script src="https://yume.human-interactive.org/examples/buffer-geometry/OrbitControls.js"></script>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-07 10:40:09

因此,矩阵对于解决旋转问题是非常有效的。我用两个步骤来正确地设置cube2旋转和位置。

  • 旋转 cube1cube2v1v2之间的关系是:

我们想要的是v2世界旋转等于v1相反的世界旋转。当两个物体具有相同的旋转时,它们具有相同的旋转矩阵(世界矩阵)。我们有以下公式:

V1相反。w= V2.w V1相反。l* C1.w = V2.l * C2.l * C1.l V1相反。l= V2.l * C2.l C2.l =V1相反。

其中V1相反世界矩阵,V2.w是v2世界矩阵,C是cube,V2.l(-1)是v2局部矩阵的逆矩阵。现在,我们可以计算cube2旋转矩阵。

  • 位置 我们需要的是使v1v2在世界上的地位保持一致。下面是一个显示坐标空间的图表:

因为v2cube2的孩子,所以我们需要移动cube2。在世界空间中,我们应该将cube2v2v1的一段距离转换,现在我们得到cube2的世界位置,cube2cube1的一个子项,只需应用cube1的逆矩阵就可以得到cube2的局部位置。

这里是一个jsfiddle 示例

希望能帮上忙。

更新

我编写了一个函数,您可以不用v1v2使用它。

代码语言:javascript
复制
//connect obj1 and obj2 on obj1's point1 and obj2's point2.
function connect(obj1,obj2,point1,point2)
{
    var point1World = new THREE.Vector3().copy(point1).applyMatrix4(obj1.matrixWorld);
    var point2World = new THREE.Vector3().copy(point2).applyMatrix4(obj2.matrixWorld);
    var translation = new THREE.Vector3().subVectors(point1World,point2World);
    var obj2World = new THREE.Vector3().copy(obj2.getWorldPosition());
    obj2World.add(translation);
    if(obj2.parent !== null)
    {
        var inverseMatrix = new THREE.Matrix4().getInverse(obj2.parent.matrixWorld);
        obj2World.applyMatrix4(inverseMatrix);
    }
    obj2.position.copy(obj2World);
}

我还添加了一个包含cube1cube2的祖父多维数据集。小提琴

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44950463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档