userData未定义

时间:2018-06-18 12:24:30

标签: javascript three.js

大家好,

我正在尝试为3DObject添加名称,以便稍后打印其名称。 因此,点击对象效果很好,大纲也可以工作......但是userData字段仍然是空的。

这是我的代码:

function checkIfClicked(event) {
        mouse.x = (event.offsetX / renderer.domElement.clientWidth) * 2 - 1;
        mouse.y = - (event.offsetY / renderer.domElement.clientHeight) * 2 + 1;
        raycaster.setFromCamera(mouse, camera);
        var intersects = raycaster.intersectObjects([model], true);
        if (intersects.length > 0) {
            click_level += 1;

            selectedObject = intersects[0].object;
            console.log(selectedObject.userData.name); //Here is the problem
            outlinePass.selectedObjects = [selectedObject];
            if (click_level > 1) {
                if (confirm("You are going to be redirected to Zalando.be")) {
                    document.location.href = "http://www.zalando.be";
                }
            }
        }
        else {
            click_level = 0;
            outlinePass.selectedObjects = [];
        }
}

在我的主代码中,一旦加载了3DObject:

object.userData = { name: "homme" };
model = object;
console.log(model.userData); //Works here

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试将其设置为不像对象:

var object = new THREE.Object3D();
object.userData.name = "name1";
document.body.innerHTML = object.userData.name;
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/93/three.min.js"></script>