Adding a geometry to three.js and dynamically rotating it in real-time

Check out https://jsfiddle.net/pmankar/svt0nhuv/ for more info.

The primary large red icosahedron geometry continues rotating along the y-axis. I successfully incorporated a small red sphere geometry and merged it with the primary geometry. Everything was functioning well up until this point. To achieve this, I utilized

THREE.GeometryUtils.merge(point_sphere_iso_geom, sphere);

However, whenever I attempt to dynamically add spheres with a mouse click, although they are successfully added (appearing as yellow spheres), they do not rotate alongside the primary large red icosahedron geometry.

Could someone clarify why this functioned initially but not when added dynamically, along with suggestions on how to enable this functionality dynamically as well.

Answer №1

It seems like the process involves creating a new geometry for each mouse click, starting from the previous one by merging it with a new sphere geometry. This new geometry is then applied to a fresh mesh, replacing the old one in the process.

There are also some changes in variables:

var geometry, material, point_sphere_iso_geom, mesh;

The initial merged mesh creation involves:

point_sphere_iso_geom = new THREE.IcosahedronGeometry(100, 4);
cygeo = new THREE.SphereGeometry(5, 10, 10);
cygeo.translate(0,0,120);
point_sphere_iso_geom.merge( cygeo );
mesh = new THREE.Mesh(point_sphere_iso_geom, material);

And here's the function for adding a yellow point:

function addYellowPoint(valX, valY) {
    var sgeometry = new THREE.SphereGeometry(2.5, 10, 10);

    var range = 150;
    var x = Math.random() * range - range / 2;
    var y = Math.random() * range - range / 2;
    var z = Math.random() * range - range / 2;
    sgeometry.translate(x,y,z);
    point_sphere_iso_geom = mesh.geometry.clone();
    point_sphere_iso_geom.applyMatrix(mesh.matrix);
    point_sphere_iso_geom.merge(sgeometry);

    scene.remove(mesh);
    mesh.geometry.dispose();
    mesh.material.dispose();
    mesh = new THREE.Mesh(point_sphere_iso_geom, material);
    scene.add(mesh);
}

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

What is the impact of a floated element on vertical spacing?

I have come across an article discussing the usage of CSS, but I am puzzled as to why image number four is not positioned below image number one. Instead, it appears below image number three. Can someone please explain this to me? Here is the HTML code sni ...

The dropdown feature does not activate when the checkbox is selected, leading to the dropdown remaining disabled

I'm attempting to make a dropdown menu active when a checkbox is selected. However, when I try to do so... it doesn't seem to be working. Can someone provide assistance with this issue? HTML : <label class="checkbox-inline"> ...

Adjust the border color of Material UI's DatePicker

https://i.sstatic.net/ZvNOA.png Hello everyone, I am currently working with a DatePicker component from Material UI. My main goal is to change the border color of this component. I have attempted various methods such as modifying classes, adjusting the th ...

What is the process for retrieving the user's information following successful social media login?

I have implemented Firebase for authentication in plain JavaScript, without using any frameworks like AngularJS or Angular2. When a user signs up successfully with Google or Facebook, I want to display their name, email, and profile picture. firebase.auth ...

Determine future dates based on selected date and time

When a user selects a date in the datetimepicker, I want to automatically set three additional dates. The first date will be the selected date + 28 days, the second date will be selected date + 56 days, and the third date will be selected date + 84 days. I ...

Struggling with react three fiber and encountering the error message: THREE.WebGLRenderer: Context Lost

I'm facing an issue with my dice where I'm getting the error message THREE.WebGLRenderer: Context Lost. I suspect the problem arose from switching from use-cannon to @react-three/cannon. When trying to render D4, I encounter the error. Surpris ...

Can someone guide me on integrating Apache Superset as an application within a Django project?

What is the best way to integrate Apache Superset into my Django project as an app? I attempted to install Apache Superset using Docker, but now I am looking to fully incorporate the Superset tool into my Django project as one of the applications. ...

Steps for resolving the "endless redirection loop" issue in Sharepoint

I am currently learning Javascript and working on setting up a multi-language Sharepoint site. I am trying to implement a code into each page that checks the user's email and the language in the URL (Portuguese or Spanish) and then redirects according ...

Toggle the state of a Material UI checkbox in ReactJS based on the data from hooks that return a true or checked value

I need assistance with checking/unchecking a checkbox value in an edit modal based on the return of addAdvisory(hooks) which is 'Y', indicating true/checked. Below is my basic code snippet: const [addAdvisory, setaddAdvisory] = useState({ SY ...

center text above images in flexbox when page is resized

When resizing the page, I'm facing an issue where the links overlap with the images in a flexbox layout. It seems like the padding and margin between the images and links are not working due to them not being "related" or connected. I believe I need t ...

In JavaScript, can you combine values within an array of objects that share the same key value pair?

Here is the JSON data that needs to be merged based on the toolName: [ { "data": { "toolName": "Login", "data": [ { "scrapValue": " Find The ...

Having trouble getting my Win and Lose Divs to display in my IF statement within the Card Game

Currently, I am developing a card game where players are presented with a card generated by the computer. The player has to decide whether the next card will be higher, lower, or equal to the current one by clicking corresponding buttons. if ((playerChoic ...

Displaying elements above my React sidebar

I am working on developing a Login application with a landing page using React Router and Redux. In order to have a Sidebar that is always present in all the components within the application, I have setup the Sidebar component as a route that is constantl ...

The form submission feature is malfunctioning due to JavaScript issues

I have forms that allow file uploads (images), and I am trying to restrict the size of those images to 500KB. However, for some reason, the forms are not submitting and I am unsure why. Below is the jQuery code: $('form').on('submit', ...

Issue: the module '@raruto/leaflet-elevation' does not include the expected export 'control' as imported under the alias 'L' . This results in an error message indicating the absence of exports within the module

Looking for guidance on adding a custom Leaflet package to my Angular application called "leaflet-elevation". The package can be found at: https://github.com/Raruto/leaflet-elevation I have attempted to integrate it by running the command: npm i @raruto/ ...

Troubleshooting CORS Problems with jQuery and Spring Boot

Recently, I have encountered an issue with my front-end pages that are developed using jQuery 3.2.1 and running by npm. Here is a snippet of how it is set up: "start": "http-server -a localhost -p 8000 -P http://localhost:8080 -c-1 --cors ./app" When mak ...

Challenges with Hangman in JavaScript

As a beginner in JavaScript, I recently developed a simple hangman-like game. However, I encountered some challenges that I need help with. One issue is related to my lettersGuessed array. Currently, the array displays every time a key is pressed and repea ...

I'm attempting to access a PHP file by clicking a button using AJAX and jQuery

I am just starting to learn the basics of jQuery and AJAX. I have already attempted to solve this problem by checking previous answers on Stackoverflow, but the code provided did not work for me. I have created two pages, index.php and testing.php, with a ...

Looking for JavaScript code that can dynamically create an HTML table from JSON data

I am in need of a javascript solution that can dynamically generate either an HTML table or a bootstrap grid layout based on a specific data structure. [ {"x":0,"y":0,"width":2,"height":1,"title":"Lorem ipsum dolor sit amet"}, {"x":2,"y":0,"width ...

Guide to incorporating JSON data into HTML through JavaScript

As I attempt to load data from a JSON file to .js and then to an HTML file, I am facing a challenge. While I can successfully load the JSON values into .js, I am unable to transfer the same values to HTML. Below is the code snippet - could anyone provide a ...