Exploring the Universe: Modify the hue of a celestial body and showcase information upon hoveringokableCall

I have been working on developing a simulation that shows the positions of 4673 of the nearest galaxies.

In this simulation, each galaxy is represented as a point.

My goal is to change the color of a point when the mouse hovers over it and display the name of the galaxy.

Despite spending a lot of time on this project, I have faced difficulties in individually changing the color or raycasting each point separately. Currently, all points are raycasted and colored as a group, as can be seen in the current version.

I am seeking advice on how to correct this issue. Thank you for taking the time to help a beginner like me.

You can find the complete code here.

Below is the relevant code snippet:

window.addEventListener( "mousemove", onDocumentMouseMove, false );

var selectedObject = null;

function onDocumentMouseMove( event ) {

    event.preventDefault();
    if ( selectedObject ) {

        selectedObject.material.color.set( '#fff' );
        selectedObject = null;

    }

    var intersects = getIntersects( event.layerX, event.layerY );
    if ( intersects.length > 0 ) {

        var res = intersects.filter( function ( res ) {

            return res && res.object;

        } )[ 0 ];

        if ( res && res.object ) {

            selectedObject = res.object;
            selectedObject.material.color.set( '#69f' );

        }

    }

}

var raycaster = new THREE.Raycaster();
var mouseVector = new THREE.Vector3();

function getIntersects( x, y ) {

    x = ( x / window.innerWidth ) * 2 - 1;
    y = - ( y / window.innerHeight ) * 2 + 1;

    mouseVector.set( x, y, 0.5 );
    raycaster.setFromCamera( mouseVector, camera );

    return raycaster.intersectObject( dots, true );

}

Answer №1

To start, make sure to assign the value of

raycaster.params.Points.threshold
to be equal to the size of your points. This will ensure that when a user hovers over any point, the colors of all points will change:

(I have increased the size of your points for easier hovering):

<html>
<head>
  <meta charset="UTF-8">
  <style>
    body { margin: 0; }
  </style>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script>
  <script src="https://rawcdn.githack.com/mrdoob/three.js/f32e6f14046b5affabe35a0f42f0cad7b5f2470e/examples/js/controls/TrackballControls.js"></script>
</head>

<body>
<script>

// Code snippets for creating the scene, camera, and renderer

</script>
</body>
</html>

Following that, make the necessary adjustments so that only the hovered point changes color:

<html>
<head>
  <meta charset="UTF-8>
  <style>
    body { margin: 0; }
  </style>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script>
  <script src="https://rawcdn.githack.com/mrdoob/three.js/f32e6f14046b5affabe35a0f42f0cad7b5f2470e/examples/js/controls/TrackballControls.js"></script></script>
</head>

<body>
<script>

// Code snippets for creating the scene, camera, and renderer

</script>
</body>
</html>

When a user hovers over a point, you will notice the points turning white. Feel free to explore how to revert the points back to their original color after the mouse exits a point!

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

Guide on making a submit and close button within an overlay

Seeking a button to both submit and close an overlay window. The button code is as follows: <a href="javascript:additem('Info to add here', 10.00,1);" role="button"><button class="purchase btn btn-warning">Select</button></ ...

Sending a function along with event and additional arguments to a child component as a prop

I've managed to set up a navigation bar, but now I need to add more complexity to it. Specifically, I have certain links that should only be accessible to users with specific permissions. If a user without the necessary permissions tries to access the ...

Tips for saving data after reading lines in Node.js

I am working on a project where I need to read data from an external text file into my function. How can I efficiently store each line of the file as a separate variable? const fs = require("fs"); const readline = require("readline"); const firstVariable ...

Clickable link that directs to a particular tab on a webpage (cbpFWTabs)

I have been utilizing tabs from the codrops Tab Styles Inspiration and I am looking for a way to open specific tabs using URLs. For instance, if I wanted to open tab3, I could link it as www.google.com/index#tab3. Below is the code I am currently using: ...

Upon successful completion of the Ajax call, refresh the page without causing any blinking

Hey there, I'm facing an issue with my website, I've implemented ajax to fetch data from my API, and upon successful retrieval, I need to reload the page to display the information, However, sometimes the page blinks before reloading, while oth ...

How to convert typescript path aliases into relative paths for NPM deployment?

I am currently working on a typescript project that utilizes paths for imports. For instance: "paths": { "@example/*": ["./src/*"], } This allows the project to import files directly using statements like: import { foo } from "@example/boo/foo"; Whe ...

How can I trigger a method after the user has finished selecting items in a v-autocomplete with multiple selection using Vuetify?

After multiple selections are made in a v-autocomplete, I need to trigger a method. However, the @input and @change events fire after each selection, not after the full batch of selections. Is there an event that triggers when the dropdown menu closes? Al ...

What is the reason behind capitalizing Angular CLI class file imports?

After creating a basic class in Angular using the CLI starter, I encountered an issue when trying to use the imported class. Instead of functioning as expected, it returned an empty object. Through troubleshooting, I discovered that simply changing the fil ...

Access the extended controller and call the core controller function without directly interacting with the core controller

i have a core controller that contains an array called vm.validationTypes with only 2 objects. I need to add 3 or 4 more objects to this array. to achieve this, i created another controller by extending the core controller: // CustomValidation angular.m ...

Limiting drag and drop in Three.js with specified distance thresholds

Is there a way to restrict the distance an object can be dragged and dropped in Three.js using DragControl? I am looking to set a maximum distance from the object's original position to prevent users from dragging it too far. ...

Transforming a high chart into an image and transmitting it to the server through an ajax request

I am looking for a way to save multiple charts as PDF files on the server using an AJAX call. Each chart is rendered in a distinct container on the same page, and I need to convert them into images before sending them to the server for export. Any assist ...

Cookie-powered JavaScript timer ceases after 60 seconds

I'm having an issue with my countdown timer where it stops counting after just one minute. It seems to pause at 54:00 when I set it for 55 minutes, and at 1:00 when I set it for 2 minutes. Any suggestions on how I can resolve this and make it continue ...

Exploring the strange camera motion around a 3D model with ThreeJS and the

Since moving from R95 to R99, I've been facing some challenges with the new updates to the Perspective camera and OrbitControls. Previously, my camera was a child of a Group that also held a mesh as the main reference point. However, after updating p ...

attempting to retrieve numerical variable beyond the function in jQuery

Trying to implement a star rating system and need to access the rating number for further actions. How can I make the number variable accessible outside of the click function? $(document).ready(function () { $(".li").mouseover(functio ...

Calculate the time difference between time A and time B using DayJs

This is a common issue, but the unique aspect of this situation is as follows: I am attempting to calculate the hours between 12:00 (timeA) and 20:00 (timeB). A basic dayjs(timeA).diff(timeB, 'hours') yields 8 hours. The challenge arises when c ...

In the VSCode editor, the color of the text is

Can someone assist me in resolving this issue? I am currently using the one time pad theme, but for some reason, all the code in JavaScript or TypeScript has white text, while other code appears normal. I have attempted to switch to different themes, but ...

Disable the button until all input fields contain text in ASP

Curious if anyone knows how to disable a button until all text boxes have input in ASP.NET and C#. Here is an image showing the scenario I'm referring to - wanting to gray out the commit button. Thanks, Chris! ...

A declaration of "import '***.css';" was located within an ECMAScript module file in the monaco-editor npm library

It's perplexing to me why the developers of monaco-editor included these statements, as they are actually causing errors in my browser such as: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME typ ...

Error message: The variable "data" is not defined in next.js

Being new to React, I recently used getStaticprops to retrieve data from an API for a filter gallery project. However, I encountered an issue when trying to access the {props: data} outside of the blog function - it resulted in a Reference Error stating th ...

What is the best way to utilize Google Maps autocomplete while filtering addresses by county and city?

Currently, I am working on a project that requires integration of the Google Maps autocomplete address API. In the registration form, I need to collect the user's address in three separate input fields: Country: ______________ City: _______________ ...