我正在处理一个像我这样加载的对象(我把它保存为全局变量,以便我以后可以更改):
model1 = null;
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load("http://blabla.mtl", function(materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.load("http://blabla.obj", function(object) {
object.scale.x = 0.0004;
object.scale.y = 0.0004;
object.scale.z = 0.0004;
object.rotateX(Math.PI / 2);
object.rotateZ(Math.PI / 2);
object.add(new THREE.AxisHelper(2))
model1 = object; //save in global variables
scene.add(model1);
});
});
当我渲染时,我想根据盒子的位置变化来改变模型的位置:
function render(ms: number) {
if (lastTime) {
update((ms-lastTime)/1000)
}
lastTime = ms
requestAnimationFrame(render)
renderer.render( scene, camera )
}
var pos_current;
var pos_new;
var trans;
function update(dt: number) {
if (pause.on) return
//save position of box before it is transformed
pos_current = box.position.clone();
box.updatePosition()
//save new position of box
pos_new = box.position.clone();
//transform the 3D-model according to the box's transformation
trans = pos_current.sub(pos_new);
model1.position.add(trans);
console.log(model1.position)
我可以在控制台中看到model1的位置发生了变化,但是对象根本就没有移动,我无法弄清楚原因。
非常感谢任何帮助:)
答案 0 :(得分:0)
让它工作 - 问题是我在尝试在obj-loader中实际加载之前更新了model1的位置:D
答案 1 :(得分:0)
我有一个查询。我也做了一个全局变量,并在加载时将其等于对象。但它在控制台日志中表示该变量为null。有什么我想念的吗?
更新:- 我了解在加载人类模型之前要使humanModel等于对象的那部分,然后如何处理呢?
let humanModel = null;
OBJLoader.load(
// resource URL
"/Models/Model_1(Malequins)/1/Cadnav.com_B0426014.obj",
// called when resource is loaded
function(object) {
scene.add(object);
humanModel = object; //adding the humanModel to the global variable
camera.lookAt(object.position);
console.log(object.position);
console.log(camera.position);
},
// called when loading is in progresses
function(xhr) {
console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
},
// called when loading has errors
function(error) {
console.log("An error happened " + error);
}
);
console.log(humanModel.position);
谢谢
答案 2 :(得分:0)
您可以将对象(obj文件)保持在x,y或z方向,因为您需要将其位置设置为:
var loader = new THREE.OBJLoader();
loader.load(
'/models/female.obj',
function(object){
object.position.y=-20;
scene.add(object);
}
)