babylonjs - 如何将导入的网格移动或缩放到其范围之外?

babylonjs - how do i move or scale an imported mesh out of its scope?

failed attempts

这里有一些强迫尝试,在 BABYLON.SceneLoader.ImportMesh...{ newMeshes[0].position.x=10; } 它使用本地项目 newMeshes[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:在图像中 post 编码通常不是一个好主意。