简体   繁体   English

使用three.js,我可以将信息附加到对象,例如URL吗?

[英]With three.js, can I attach information to an object, such as a URL?

I have a webgl question related to three.js. 我有一个与three.js相关的webgl问题。 I would like to attach information to an object. 我想将信息附加到一个对象。 For instance, when I click on an object, I would like to retrieve some information that's tied to the object, like a URL, and then run a function using that information. 例如,当我点击一个对象时,我想检索一些与该对象绑定的信息,如URL,然后使用该信息运行一个函数。

The code I'm using for finding the object is: 我用来查找对象的代码是:

    var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
    projector.unprojectVector(vector, camera);

    var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());

    var intersects = raycaster.intersectObjects(objects);

    intersects[0].object.material.color.setHex(Math.random() * 0xffffff);

The last line being how I'm currently manipulating the object. 最后一行是我当前如何操纵对象。 The array objects is simply an array of all of the objects. 数组objects只是所有对象的数组。 The issue is I don't know enough about how Raycaster or intersectObjects works, or enough about how to tie any information to an object so it can be recalled later. 问题是我不太了解Raycaster或intersectObjects是如何工作的,或者关于如何将任何信息绑定到对象以便稍后可以调用它。

indeed you can set custom user data to an object in Three.js. 实际上,您可以将自定义用户数据设置为Three.js中的对象。 The key here is the Object3D which is the basis for all scene graph objects. 这里的关键是Object3D,它是所有场景图形对象的基础。 The docs have a property called 文档有一个叫做的属性

Object3D.userData; 

This can be loaded with an object of your choice and when the Raycaster class retrieves your intersect you can simply access it directly at that point. 这可以加载您选择的对象,当Raycaster类检索您的交叉时,您可以直接在该点访问它。

Setter during init or runtime 初始化或运行时的setter

Object3D.userData = { URL: "http://myurl.com" };

Getter on Collision 碰撞中的吸气剂

window.location = intersects[0].object.userData.URL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM