任何人都可以請建議加載多個模型(從攪拌機導出爲JSON格式)的最佳方式是什麼?使用JSONLoader加載多個模型
在我下面的例子中,我使用了填充了一些預定義對象數據的static_objects
數組,這也是存儲有關模型URL的所有信息的地方。 然後將這些URL傳遞給for循環中的JSONLoader。但由於某種原因,當我訪問JSONLoader.load
方法中的static_objects
數組時,我得到不正確的引用。
請看下面的例子。
var renderer, camera, scene, controls;
/////// JSOn DATA ////
var static_objects = [
{
name:"ground",
pos:{
x:-45.0, y:-1, z:14.0
},
size:20,
model_url: "obj.moon_ground.json",
},
{
name:"cylinder",
pos:{
x:-20.0, y:0.0, z:0.0
},
size:10,
model_url:"obj.cylinder.json",
}
];
var ObjectsToLoad = static_objects.length || 0;
///////////////////////////
function initRenderer(width, height){
console.log(" - renderer");
if(Detector.webgl){
renderer = new THREE.WebGLRenderer({antialias:true});
}else{
renderer = THREE.CanvasRenderer();
}
//// container ////
container = document.createElement('div');
document.body.appendChild(container);
/////////////////////
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(width, height);
renderer.shadowMap.enabled = true;
renderer.shadowMapSoft = true;
renderer.setClearColor(0x142639, 1);
///////////////////////
container.appendChild(renderer.domElement);
}
function initCamera(width, height){
console.log(" - camera");
camera = new THREE.PerspectiveCamera(55, width/height, 1, 100);
camera.position.set(17.05217, 8.07079, 0.0);
camera.lookAt(static_objects[1].pos);
controls = new THREE.OrbitControls(camera);
}
function InitLights(){
console.log(" - lights");
var spot_light = new THREE.SpotLight(0xC1C1C1, 0.4);
spot_light.castShadow = true;
spot_light.shadowDarkness = 0.1;
spot_light.shadowCameraNear = 0.1;
spot_light.shadowCameraFar = 100;
spot_light.shadowCameraFov = 45;
spot_light.shadowMapWidth = 512;
spot_light.shadowMapHeight = 512;
spot_light.position.set(-1.8, 100, 2.5);
spot_light.target.position.set( static_objects[1].pos.x, static_objects[1].pos.y, static_objects[1].pos.z);
scene.add(spot_light);
var c_helper = new THREE.CameraHelper(spot_light.shadow.camera);
scene.add(c_helper);
var ambient_light = new THREE.AmbientLight(0xD0D0D0, 0.25);
scene.add(ambient_light);
}
function initScene(){
console.log(" - scene");
scene = new THREE.Scene();
}
function initObjects(){
console.log(" - StaticObjects");
for(var o = 0; o < static_objects.length; o++){
var loader = new THREE.JSONLoader();
var o_data = static_objects[o];
loader.load(o_data.model_url, function(){
console.log("loading object "+ o_data.name) <----- it always refers to same object ///
ObjectsToLoad--;
//object.scale.set(self.properties.size, self.properties.size, self.properties.size) ;
})
}
}
function initAll(){
console.log(" initializing:");
initRenderer(window.innerWidth/2, window.innerHeight/2);
initScene();
initCamera(window.innerWidth/2, window.innerHeight/2);
InitLights();
initObjects();
animate();
}
function animate(){
window.requestAnimationFrame(animate);
if(ObjectsToLoad === 0){
render_all();
}
}
function render_all(){
//var timer = Date.now() * 0.001;
controls.update();
renderer.render(scene, camera);
}
initAll();
感謝您的解釋。 – Alexus