Three.js 中的粒子 ID 和检索多个 ID

发布于 2024-12-21 04:13:12 字数 364 浏览 0 评论 0原文

我正在尝试使用 Three.js 创建粒子,目前作为粒子系统。 该项目的目标是创建具有定义位置和唯一 ID 的粒子,并添加一个选项来选择多个粒子并检索其 ID。

我做了一些研究,但只找到了检测鼠标单击位置的方法。

所以,我正在寻找以下信息:

第一:是否有一个选项可以将 ID 分配给单个粒子(就像数字一样简单)? 第二:有没有办法选择多个粒子并在(弹出)窗口中查看它们的 ID?

感谢您的帮助!

PS:我发现,您可以将 ID 分配给 THREE.ParticleDOMMaterial (我不知道,这个函数的作用是什么!?)和 THREE.ParticleBasicMaterial (将 ID 分配给材质,但不能分配给粒子本身)。

I am trying to create particles with three.js, currently as a particle system.
The goal of the project is to create particles, with defined positions and unique IDs, and add an option to select multiple particles and retrieve their IDs.

I did some research, but found only ways to detect the position of a mouse-click.

So, I'm looking for the following information:

First: Is there an option to assign IDs to single particles (as simple as a number)?
Second: Is there a way to select multiple particles and view their IDs in a (popup) window?

Thanks for your Help!

PS: I found, that you can assign IDs to THREE.ParticleDOMMaterial (I don't know, what this function does!?) and THREE.ParticleBasicMaterial (Assign an ID to the material, but not to the particle itself).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-12-28 04:13:12

默认情况下,所有 Object3D 实例在 Three.js 中都有一个唯一的 id,并且 Particle 继承了该 ID。另外,Object3D 有一个 name 属性(如果有帮助的话)。

下面是一个小片段,说明了粒子的命名:

for ( var i = 0; i < 100; i++ ) {

                    particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
                    particle.name = "particle"+i;
                    particle.position.x = Math.random() * 2000 - 1000;
                    particle.position.y = Math.random() * 2000 - 1000;
                    particle.position.z = Math.random() * 2000 - 1000;
                    particle.scale.x = particle.scale.y = Math.random() * 10 + 5;
                    group.add( particle );
                }

这是一个鼠标按下事件处理程序的示例,它在控制台中打印所单击粒子的名称和 ID:

function onDocumentMouseDown( event ) {

                event.preventDefault();

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

                var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

                var intersects = ray.intersectScene( scene );

                if ( intersects.length > 0 ) {

                    console.log("you clicked particle named '" + intersects[0].object.name + "' with id: " + intersects[0].object.id);

                }

            }

祝你好运!

All Object3D instances have a unique id in three.js by default and Particle inherits that. Also Object3D has a name property, if that helps.

Here's a little snippet which illustrates naming particles:

for ( var i = 0; i < 100; i++ ) {

                    particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
                    particle.name = "particle"+i;
                    particle.position.x = Math.random() * 2000 - 1000;
                    particle.position.y = Math.random() * 2000 - 1000;
                    particle.position.z = Math.random() * 2000 - 1000;
                    particle.scale.x = particle.scale.y = Math.random() * 10 + 5;
                    group.add( particle );
                }

and here's an example of a mouse down event handler which prints the name and id of the clicked particle in the console:

function onDocumentMouseDown( event ) {

                event.preventDefault();

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

                var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

                var intersects = ray.intersectScene( scene );

                if ( intersects.length > 0 ) {

                    console.log("you clicked particle named '" + intersects[0].object.name + "' with id: " + intersects[0].object.id);

                }

            }

Goodluck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文