babylonjs-我如何移动或缩放导入的网格超出其范围?

时间:2018-12-13 23:48:40

标签: babylonjs

failed attempts

在BABYLON.SceneLoader.ImportMesh ... {newMeshes [0] .position.x = 10;中,有一些强制尝试这样做。 }它可以使用本地项目newMeshes [0]起作用,但是没有任何作用。

1 个答案:

答案 0 :(得分:0)

这是因为变量newMeshes仅在回调函数中定义。如果要在函数之外获取变量,则需要在全局范围内对其进行定义。为此,只需在调用ImportMesh之前声明一个变量,然后在ImportMesh的回调函数内部将该变量设置为newMeshes[0],如下所示:

var meshisin = BABYLON.AbstractMesh;
// Define the variable in the global scope.
var skullMesh;
meshisin = BABYLON.SceneLoader.ImportMesh("", "scenes/", "skull.babylon", scene, function (newMeshes) {
    skullMesh = newMeshes[0];
});

然后,您可以使用skullMesh.position.x = 10;来更改网格的位置。

但是由于加载网格需要花费1秒钟的时间,因此您会延迟使用网格,直到它被加载setTimeout为止,如下所示:

setTimeout(function() {
    skullMesh.position.x = 10;
}, 1000);

所有代码都将变成:

var meshisin = BABYLON.AbstractMesh;
// Define the variable in the global scope.
var skullMesh;
meshisin = BABYLON.SceneLoader.ImportMesh("", "scenes/", "skull.babylon", scene, function (newMeshes) {
    skullMesh = newMeshes[0];
});

setTimeout(function() {
    skullMesh.position.x = 10;
}, 1000);

PS:在图像中发布代码通常不是一个好主意。

相关问题