在three.js场景中添加多个球体

时间:2015-11-27 09:29:16

标签: javascript three.js

这是我第一个涉及javascript的第一周和我带有three.js的3d日,所以请耐心等待,这可能是一个愚蠢的问题。 我的问题是这样的:我创建了一个场景,我想在其中添加小球体 - 表示来自kinect feed的骨架关节,作为json对象。 我试图创建场景,添加一个事件监听器并创建相关的几何,但是..没有去。 以下是相关代码:

scene.addEventListener("skeletonEvent", function(e) {
if (e.detail.SkeletonSourceData.Skeletons[0].Joints[10].Position.X > e.detail.SkeletonSourceData.Skeletons[0].Joints[3].Position.X)
{
    scene.getObjectByIdB("skeleton").options[0].selected = true;
}
else
{
    scene.getObjectById("skeleton").options[1].selected = true;
}
jsonObject = e.detail.SkeletonSourceData;


var InitialCenter = new THREE.Vector3( 0, 0, 0 );

//for every skeleton    
for (var i = 0; i < jsonObject.Skeletons.length; i++) {
    //get the coordinates of the joints
    for (var j = 0; j < jsonObject.Skeletons[ i ].Joints.length; j++) {
        InitialCenter = InitialCenter.add(new THREE.Vector3(jsonObject.Skeletons[i].Joints[j].Position.X, jsonObject.Skeletons[i].Joints[j].Position.Y, jsonObject.Skeletons[i].Joints[j].Position.Z ));

        var joint1 = jsonObject.Skeletons[ i ].Joints[ j ];

        //add joint to geometry
        var sphereGeometry = new THREE.SphereGeometry(1,10,10);
        var sphereMaterial = new THREE.MeshLambertMaterial({color: 'rgb(255, 0, 0)'});

        //add joint to geometry
        var jointSpheres = [];

    jointSpheres[j] = new THREE.Mesh(sphereGeometry, sphereMaterial);

    jointSpheres[j].position.x = joint1.position.x[j];
    jointSpheres[j].position.y = joint1.position.y[j];
    jointSpheres[j].position.z = joint1.position.z[j];

    scene.add(jointSpheres[j]);
 jointSpheres[j].updateMatrix();
 jointSpheres[j].matrixAutoUpdate=false;
}
}

有什么建议吗? 更新:最令人困惑的是听众无法工作。总而言之。我已经检查了我能想到的一切,在调试时,它永远不会进入监听器..:S

1 个答案:

答案 0 :(得分:0)

尝试更新以下代码块:

jointSpheres[j].position.x = joint1.position.x[j];
jointSpheres[j].position.y = joint1.position.y[j];
jointSpheres[j].position.z = joint1.position.z[j];

要:

jointSpheres[j].position.x = joint1.position.x;
jointSpheres[j].position.y = joint1.position.y;
jointSpheres[j].position.z = joint1.position.z;

如果你有超过1个骨架,你的关节球也会破裂(它们会在下一次传球中被覆盖)。也许你应该做

jointspheres[i][j] = new THREE.Mesh(sphereGeometry, sphereMaterial);

等等......