three.js如何将一个对象放在另一个对象旁边(全球世界位置)

时间:2016-04-23 16:14:06

标签: three.js position global relative

我在场景中使用objloader加载一个对象,稍后,我在场景中加载第二个对象,但我想将它的绝对位置更改为第一个对象的另一侧。

两个对象的所有位置都相对于本地(0,0,0)。

我如何改变它的全球地位?

3 个答案:

答案 0 :(得分:6)

davincy发布的答案现已过时。在我的threejs版本中,它回应了这个警告:

  

THREE.Matrix4:.getPosition()已被删除。使用   改为Vector3.setFromMatrixPosition(矩阵)。

那么你需要用以下内容替换它:

objectA.position.setFromMatrixPosition( objectB.matrixWorld );

其中objectB是场景中的对象,以及您希望objectA从中复制其位置的对象。

答案 1 :(得分:1)

我找到了答案:

@Override
protected void onDraw(Canvas canvas) {
    int circleCenter = getHeight() / 2;
    radius = circleCenter;
    loadBitmap();

    if(hideBackground) {
        Path circlePath = new Path();
        circlePath.addCircle(circleCenter, circleCenter, radius, Path.Direction.CCW);

        circlePath.setFillType(Path.FillType.INVERSE_EVEN_ODD);
        paint.setColor(0x00000000);
        paint.setShader(null);
        canvas.drawPath(circlePath, paint);
    } else {
        if (image != null) {
            radius -= 5;
            paint.setShader(computeBitmapShader(image));
            canvas.drawCircle(circleCenter, circleCenter, radius, paint);
        }
    }
}

答案 2 :(得分:0)

您可以放入场景的每个对象都有一个"位置"属性,这是一个三维向量。您只需更改其x / y / z值即可。

例如,假设您的对象被称为"网格":

mesh.position.x += 1; //you need re-render if you change something in the object after adding to the scene
yourScene.add(mesh); 

这会将对象移动一个" unit"在现场。这意味着您可以这样做:

mesh2.position.x = mesh1.position.x + 1;
yourScene.add(mesh1); 
yourScene.add(mesh2); 

在这里,您可以检查可以放在THREE.js场景中的对象的其他属性和方法:

http://threejs.org/docs/#Reference/Core/Object3D