简体   繁体   English

A-Frame - Raycast Three.JS 3D Object

[英]A-Frame - Raycast Three.JS 3D Object

I have two objects, one cylinder and one polygon, representing a Three.js 3D Object, in an A-Frame scene.我有两个对象,一个圆柱体和一个多边形,代表 A 帧场景中的 Three.js 3D Object。 All I want is that the polygon changes when hovering over it, like the cylinder.我想要的是当将鼠标悬停在它上面时多边形会发生变化,就像圆柱体一样。

I have a working demo here: Demo我在这里有一个工作演示:演示

When the cursor hovers on the cylinder, the text HOVERED appears on the screen, and the color of the cylinder changes, using the Raycaster component.当 cursor 悬停在圆柱体上时,文本 HOVERED 出现在屏幕上,圆柱体的颜色发生变化,使用 Raycaster 组件。 How can I do the same to the polygon?我怎样才能对多边形做同样的事情?

The polygon was created with this:多边形是用这个创建的:

AFRAME.registerComponent('foo', {
    init: function() {
        let points = [];
        points.push(new THREE.Vector2(0, 0));
        points.push(new THREE.Vector2(1.5, 0));
        points.push(new THREE.Vector2(2.5, 1));
        points.push(new THREE.Vector2(2.5, 2.5));
        points.push(new THREE.Vector2(1, 3.5));

        for (let i = 0; i < points.length; i++) {
            points[i].multiplyScalar(0.25);
        }
        let shape = new THREE.Shape(points);


        let extrudedGeometry = new THREE.ExtrudeGeometry(shape, {
            amount: 1,
            bevelEnabled: false
        });

        // Geometry doesn't do much on its own, we need to create a Mesh from it
        let extrudedMesh = new THREE.Mesh(extrudedGeometry, new THREE.MeshPhongMaterial({
            color: 0xff0000
        }));
        this.el.object3D.add(extrudedMesh);

        let geometry = new THREE.ShapeGeometry(shape);
        let material = new THREE.MeshBasicMaterial({
            color: 0x00ff00
        });
        let mesh = new THREE.Mesh(geometry, material);
        this.el.object3D.add(mesh);
    }
});

Thank you.谢谢你。

Raycaster casts rays against the entity's mesh. Raycaster 向实体的网格投射光线。 You set it with setObject3D .你用setObject3D设置它。 Do:做:

let mesh = new THREE.Mesh(geometry, material);
this.el.setObject3D('mesh', mesh);

Corrected code更正的代码

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

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