gpt4 book ai didi

javascript - Three.js - 多次调用JSON模型

转载 作者:行者123 更新时间:2023-12-03 08:26:03 29 4
gpt4 key购买 nike

如何加载 JSON 模型一次并将其多次添加到场景中?

目前,我调用该模型两次。也许有人可以给我一个我想要完成的工作的例子。

var loader = new THREE.JSONLoader();

loader.load( "models/model1.js", meshloader1("models/model1.js"));
loader.load( "models/model1.js", meshloader2("models/model1.js"));

function meshloader1(fileName){
return function(geometry){
mesh1 = new THREE.Mesh(geometry, material);
mesh1 .position.set( 0, 0, 0 );
mesh1 .scale.set( 1, 1, 1 );
scene.add(mesh1 );
}
}

function meshloader2(fileName){
return function(geometry){
mesh2 = new THREE.Mesh(geometry, material);
mesh2 .position.set( 0, 0, 0 );
mesh2 .scale.set( 1, 1, 1 );
scene.add(mesh2 );
}
}

最佳答案

您可以在加载器外部定义一个geometry对象,如下所示:

var loader = new THREE.JSONLoader();
var genericGeometry;

loader.load( "models/model1.js", meshloader("models/model1.js"));

function meshloader(fileName){
return function(geometry){
genericGeometry = geometry;
createMeshs();
}
}

// Since `loader.load()` method is async,
// you need to wrap this inside a function and call it when the geometry is actually loaded
function createMeshs(){
var mesh1 = new THREE.Mesh(genericGeometry, material);
mesh1.position.set( 0, 0, 0 );
mesh1.scale.set( 1, 1, 1 );
scene.add(mesh1);

var mesh2 = new THREE.Mesh(genericGeometry, material);
mesh2.position.set( 1, 1, 0 );
mesh2.scale.set( 1, 1, 1 );
scene.add(mesh2);
}

关于javascript - Three.js - 多次调用JSON模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33542820/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com