gpt4 book ai didi

javascript - 设置在数组中存储的 Three.js 对象的位置

转载 作者:行者123 更新时间:2023-12-03 02:35:09 27 4
gpt4 key购买 nike

每次调用我的动画函数时,我都试图在 x 轴 + 1 上移动我的对象,但我一直遇到无法读取未定义的属性 x

的错误

所以每次我向场景添加网格时,我都会将其添加到 sceneObjects 数组

像这样

addToGlScene(mesh, 'player')

使用此功能

    function addToGlScene(what, name, glow, storage){
glScene.add(what)
what.name = name
sceneObjects.push(what)
what.userData.glow = {glow: glow}
what.userData.storage = {storage: storage}
sceneObjects[name] = what
}

所以我尝试在我的 animate() 函数中使用此方法再次更改对象位置

    sceneObjects['player'].position

但我仍然遇到同样的错误

即使我console.log(sceneObjects['player'])它显示了这一切

    THREE.MorphAnimMesh {uuid: "42A8C528-F4E4-4317-9516-63B7B717BEF8", name: "player", type: "Mesh", parent: vd, children: Array(0), …}
castShadow:
false
children:[]
currentKeyframe:40
direction:1
directionBackwards:false
drawMode:0
duration:1000
endKeyframe:163
frustumCulled:true
geometry:L {uuid: "5C675298-D5DD-4910-B98A-84D71A036636", name: "", type: "Geometry", vertices: Array(300), colors: Array(0), …}
lastKeyframe:39
layers:Wd {mask: 1}
length:164
material:Aa {uuid: "7E4FB381-61C2-4C33-8E08-C1997F94F0A4", name: "", type: "MeshBasicMaterial", fog: true, lights: false, …}
matrix:O {elements: Array(16)}
matrixAutoUpdate:true
matrixWorld:O {elements: Array(16)}
matrixWorldNeedsUpdate:false
mirroredLoop:false
morphTargetDictionary:{stand000î´: 0, stand001î´: 1, stand002î´: 2, stand003î´: 3, stand004î´: 4, …}
morphTargetInfluences:(164) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.29446199999998723, 0.7055380000000128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …]
name:"player"
parent:vd {uuid: "C328C91A-D445-4E48-9C74-A15A4FA318AB", name: "", type: "Scene", parent: null, children: Array(13), …}
position:p {x: 0, y: 0, z: 0}
quaternion:ha {_x: 0, _y: 0, _z: 0, _w: 1, onChangeCallback: ƒ}
receiveShadow:false
renderOrder:0
rotation:gb {_x: 0, _y: 0, _z: 0, _order: "XYZ", onChangeCallback: ƒ}
scale:p {x: 1, y: 1, z: 1}
startKeyframe:0
time:248.20450000000008
type:"Mesh"
up:p {x: 0, y: 1, z: 0}
userData:{glow: {…}, storage: {…}}
uuid:"42A8C528-F4E4-4317-9516-63B7B717BEF8"
visible:true
eulerOrder:(...)
id:28
modelViewMatrix:O {elements: Array(16)}
normalMatrix:ma {elements: Array(9)}
useQuaternion:(...)
__proto__:ra

这是我的 animate() 函数

        function animate() {
if(typeof mesh != "undefined") mesh.updateAnimation(clock.getDelta() * 100);
requestAnimationFrame( animate );
glRenderer.render(glScene, camera);
cssRenderer.render(cssScene, camera);
if ( controlsEnabled === true ) {
var time = performance.now();
var delta = ( time - prevTime ) / 1000;
velocity.x -= velocity.x * 10.0 * delta;
velocity.z -= velocity.z * 10.0 * delta;
velocity.y -= 9.8 * 40.0 * delta; // 100.0 = mass
direction.y = Number( flyDown) - Number(flyUp);
direction.z = Number( moveForward ) - Number( moveBackward );
direction.x = Number( moveLeft ) - Number( moveRight );
direction.normalize(); // this ensures consistent movements in all directions
if (flyUp || flyDown) velocity.y -= direction.y * player.speed * delta;
if ( moveForward || moveBackward ) velocity.z -= direction.z * player.speed * delta;
if ( moveLeft || moveRight ) velocity.x -= direction.x * player.speed * delta;
controls.getObject().translateX( velocity.x * delta );
controls.getObject().translateY( velocity.y * delta );
controls.getObject().translateZ( velocity.z * delta );
if ( controls.getObject().position.y < player.heigth) {
velocity.y = 0;
controls.getObject().position.y = player.heigth;
canJump = true;
}
prevTime = time;
}
glRenderer.render( glScene, camera );
}

我做错了什么?

最佳答案

这应该有效:

sceneObjects['player'].position.x = newXValue;

position 属性是 Vector3 ,您可以从中访问 x 属性。

如果 sceneObjects['player'].position 在 animate 函数中未定义,则说明有其他问题。

关于javascript - 设置在数组中存储的 Three.js 对象的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48566349/

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