Interactive VR Experience Using A-Frame and Three.JS Raycasting

I am currently working with two different objects in my A-Frame scene, a cylinder and a polygon, both representing Three.js 3D Objects. I would like to make it so that the polygon changes when hovered over, similar to how the cylinder does.

If you'd like to see a demo of what I have so far, please check out: Demo

When hovering over the cylinder, the text "HOVERED" appears on the screen and the color of the cylinder changes, thanks to the Raycaster component. How can I achieve the same effect for the polygon?

The polygon was created using the following code:

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);
    }
});

Any suggestions or help would be greatly appreciated. Thank you!

Answer №1

The Raycaster is responsible for casting rays against the entity's mesh. This can be configured using the setObject3D method. Here's an example of how to do it:

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

You can view the corrected code in this link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensuring that touch events are activated on Firefox's desktop browser

Testing on a Windows 7 desktop with touch capabilities, I performed a simple test that looked something like this: temp_div.addEventListener('touchstart', function(e){ /*confirm */ }, false) temp_div.addEventListener('pointerdown', fun ...

What is the best way to ensure that the HTML page is only shown once all data from three JSON files has been fully

I have successfully parsed data from three different JSON files using the code below. //factory app.factory('myapp', ['$http', function($http) { function getLists() { var tab = [&a ...

Troubleshooting: jQuery's clone( true ) function fails to function properly with dynamic

Consider this scenario: $.fn.foo = function() { var $input = $('<input type="text"/>'); var $button_one = $('<button>One</button>'); var $button_two = $('<button>Two</button>'); ...

Creating PDFs with barcodes using Node.js

Currently, I am utilizing https://github.com/devongovett/pdfkit to create PDF files easily. This can be achieved with a simple code snippet like below: app.get('/get-pdf', (req, res) => { const doc = new PDFDocument(); const filename = &ap ...

Unexpected error encountered with node.js when attempting to run: npm run dev. A TypeError was thrown stating that require(...) is not

Working on my initial project, I have set up a database and am in the process of creating a login page. However, when trying to run it using 'npm run dev', an error is encountered (see below). I am unsure of what is causing this issue and how to ...

What is the best way to incorporate data from JSON objects in nested arrays within an array into HTML using AngularJS?

I have been struggling to display the JSON data in list.html in the desired format. Despite trying different approaches from similar posts, I have not been successful as my JSON structure is unique. How can I ensure that fields such as givenName, familyNam ...

Alter the background color of the entire document to (EDIT) in the top dialog box

<script> $(function() { $( "#dialog" ).dialog(); }); </script> </head> <body> <div id="dialog" title="Basic dialog"> <p>This default dialog box can display information. It can be moved, re-sized, and closed ...

Utilize JavaScript to trigger a div pop-up directly beneath the input field

Here is the input box code: <input type='text' size='2' name='action_qty' onmouseup='showHideChangePopUp()'> Along with the pop-up div code: <div id='div_change_qty' name='div_change_qty&ap ...

Guide on triggering a bootstrap popup modal using a TypeScript file

I am currently working on an Angular project where I need to launch a popup modal when my function is called. I came across an example on w3schools, but it only contains the HTML logic to open the popup. What I want to achieve is to open the popup from th ...

Error in routing of submit button in Express.js

While attempting to create a basic "to-do list" using HTML requests, I encountered an issue with the PATCH request. Instead of redirecting to "/", it redirected to "/posts/2" and displayed the message "Cannot POST /posts/2", without updating the array elem ...

Organize the dataset into groups based on every possible key

Hi there, I'm facing a challenge while developing an application using NestJS/Prisma. The task at hand is to extract unique results from a table in order to display them in a filter on the front-end. Let me give you an overview of my table structure ...

Breeze js requests metadata without needing to make a second call to the server

Currently, I am developing an Angular application that utilizes Breeze JS and ASP.NET OData controller. While testing, I encountered an issue where Breeze JS successfully makes the initial call to retrieve metadata from the server but fails to make the sec ...

The comparison between using multiple Vue.js instances and components, and implementing Ajax tabs

I am looking to incorporate ajax tabs using vue js. Each tab will need an ajax request to fetch both the template and data from an api. Here is an example of the tabs: <div id="tabs"> <ul class="nav nav-tabs"> <li class="active">& ...

Establishing a session variable using express

For my JavaScript project, I want to create a session variable that can be used across all pages. I attempted to use Express for this purpose, but I am facing an issue when trying to assign a value to the session variable. Below is the relevant code snippe ...

When using the setTimeout function, jQuery's .submit() method may fail to pass data to PHP

I'm attempting to enhance the login form with an effect that triggers a 1-second delay before proceeding to PHP. I tried implementing a setTimeout() function within the .submit event handler along with e.preventDefault() to create the delay, but enco ...

switching back and forth between the registration and login forms

<script> $(document).ready(function(){ $("#register_link").click(function() { $("#login_or_register").empty(); $("#login_or_register").load("register_form.php"); }); $("#login_link").click(function() { $("# ...

Authorization in Confluence REST API

Currently, a user is logged in to Confluence; There is an external web application that saves attachments to a specific page. In order to make REST calls from the external app, I need the user's credentials for Confluence (which I do not have because ...

Refreshing the page causes the HTML input to be cleared

My dilemma involves a javascript function that stores the value entered in an <input> field to a cookie, followed by a page refresh. The purpose of the refresh is to allow the server side to re-render the page using the cookie values. My concern is ...

How can parameters be passed to a JavaScript or jQuery function?

I am currently utilizing a JS/JQ function to convert values into currency by adding commas. Although the function is running smoothly, I am encountering an issue when attempting to pass parameters to it. Kindly provide assistance on how to successfully pas ...

Retrieving information from deeply nested JSON structures within React components

I am developing a web application that focuses on searching and displaying movie information. One of my challenges is accessing nested objects like "principals" from the same endpoint that contains the main object "title". Upon fetching the JSON response: ...