访问由Raycast Intersection生成的对象上的UserData

时间:2015-10-28 23:54:11

标签: three.js raycasting

我正在尝试在由three.js驱动的WebGL场景中显示悬停对象的名称。我使用Raycaster类进行光线投射,效果很好,我的对象会改变颜色,我知道它的位置,但是当我尝试访问对象的userData时,它会显示undefined

intersectObject()函数返回的对象是否不是原始对象的副本?我想知道如果某个地方失去了它的属性。

    function onMouseMove(e) {

        var intersectedOne = false;

        for (var i = 0, len = group.children.length; i < len; i++) {
            setMaterial(group, material);
        }


        mouseVector.x = 2 * (e.clientX / window.innerWidth) - 1;
        mouseVector.y = 1 - 2 * (e.clientY / window.innerHeight);

        raycaster.setFromCamera(mouseVector, camera);

        var intersects = raycaster.intersectObjects(group.children, true);

        for (var i = 0; i < intersects.length; i++) {
            var intersection = intersects[i];
            var obj = intersection.object;
            if (intersection != 0 && intersectedOne == false) {
                intersectedOne = true;
                obj.material = selectedMaterial;
                text2.innerHTML = obj.userData.CADEIRA;
            }
        }
    }
每次我将鼠标悬停在某个对象上时,

text2.innerHTML会显示undefined,同时它应显示该对象的名称。

1 个答案:

答案 0 :(得分:2)

而不是做

var newObject = object.clone();

你应该做

var newObject = child.clone();