gpt4 book ai didi

javascript - 移动对象 Three.js

转载 作者:行者123 更新时间:2023-11-30 06:10:06 27 4
gpt4 key购买 nike

onKeyboard: (event) => {
let vars = Scene.vars;
var vitesse = 0.5;
var keyCode = event.which;
if(keyCode ==37){
vars.lambo.position.x += vitesse;
vars.lambo.rotation.y += vitesse;
}else if(keyCode == 39){
vars.lambo.position.x -= vitesse;
vars.lambo.rotation.y -= vitesse;
}else if(keyCode == 40){
vars.lambo.position.z -= vitesse;
}else if(keyCode == 38){
vars.lambo.position.z += vitesse;
}

}
Scene.loadFBX("Logo_Feelity.FBX", 10, [45, 22, 0], [0, 0, 0], 0xFFFFFF, 'logo', () => {
Scene.loadFBX("Statuette.FBX", 10, [0, 0, 0], [0, 0, 0], 0xFFD700, 'statuette', () => {
Scene.loadFBX("Socle_Partie1.FBX", 10, [0, 0, 0], [0, 0, 0], 0x1A1A1A, 'socle1', () => {
Scene.loadFBX("Socle_Partie2.FBX", 10, [0, 0, 0], [0, 0, 0], 0x1A1A1A, 'socle2', () => {
Scene.loadFBX("Plaquette.FBX", 10, [0, 4, 45], [0, 0, 0], 0xFFFFFF, 'plaquette', () => {
Scene.loadText(Scene.vars.text, 10, [0, 23, 52], [0, 0, 0], 0x1A1A1A, "texte", () => {
Scene.loadFBX("/Lamborginhi Aventador FBX/Lamborghini_Aventador.FBX", 10, [0, 0, 0], [0, 0, 0], 0xFFD700, 'voiture', () => {

let vars = Scene.vars;

let lambo = new THREE.Group();

lambo.add(vars.voiture);
lambo.position.set(0,0,200);
lambo.scale.set(0.01,0.01,0.01);
vars.scene.add(lambo);
console.log(vars.scene.add(lambo))

let gold = new THREE.Group();
gold.add(vars.socle1);
gold.add(vars.socle2);
gold.add(vars.statuette);
gold.add(vars.logo);
gold.add(vars.texte);
gold.add(vars.plaquette);

let logo2 = vars.logo.clone();
logo2.rotation.z = Math.PI;
logo2.position.x = -45;
vars.logo2 = logo2;
gold.add(logo2);
gold.position.z = -50;
gold.position.y = 10;
vars.scene.add(gold);
vars.goldGroup = gold;

let silver = gold.clone();
silver.position.set(-200, 10, 0);
silver.rotation.y = Math.PI / 4;
silver.children[2].traverse(node => {
if (node.isMesh) {
node.material = new THREE.MeshStandardMaterial({
color: new THREE.Color(0xC0C0C0),
metalness: .6,
roughness: .3
})
}
});
vars.scene.add(silver);
vars.silverGroup = silver;

let bronze = gold.clone();
bronze.position.set(200, 10, 0);
bronze.rotation.y = -Math.PI / 4;
bronze.children[2].traverse(node => {
if (node.isMesh) {
node.material = new THREE.MeshStandardMaterial({
color: new THREE.Color(0xCD7F32),
metalness: .6,
roughness: .3
})
}
});
vars.scene.add(bronze);
vars.bronzeGroup = bronze;

let elem = document.querySelector('#loading');
elem.parentNode.removeChild(elem);
});
});
});
});
});
});
});

// ajout des controles
vars.controls = new OrbitControls(vars.camera, vars.renderer.domElement);
vars.controls.minDistance = 300;
vars.controls.maxDistance = 600;
vars.controls.minPolarAngle = Math.PI / 4;
vars.controls.maxPolarAngle = Math.PI / 2;
vars.controls.minAzimuthAngle = - Math.PI / 4;
vars.controls.maxAzimuthAngle = Math.PI / 4;
vars.controls.target.set(0, 100, 0);
vars.controls.update();

window.addEventListener('keydown', Scene.onKeyboard, false);
window.addEventListener('resize', Scene.onWindowResize, false);
window.addEventListener('mousemove', Scene.onMouseMove, false);

vars.stats = new Stats();
vars.container.appendChild(vars.stats.dom);

Scene.animate();
}

大家好,我正在用 three.js 制作一个类(class)项目。我想移动我添加到场景中的名为“lambo”的对象。但是使用我的功能“onKeyboard,只有场景的相机移动,我希望对象”lambo“移动。我有一个错误:“无法读取未定义的属性‘position’”对于“vars.lambo.position.x += vitesse”和其他位置。

我想知道如何解决这个问题,谢谢。

最佳答案

您的代码中似乎缺少以下行:

vars.lambo = lambo;

添加此行应确保 vars.lambo 不再是 undefined,以便访问其 position 属性。

关于javascript - 移动对象 Three.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59435560/

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