When incorporating a new group in a three.js Scene, the older object groups are automatically eliminated

Currently, I am developing a visual components designer that includes a functionality to group objects together under a specific name. When any of the grouped objects is selected, a red box should be displayed around them.

However, I am facing an issue where adding the red box as a LineSegments object to the scene causes the original objects to be removed. I have checked my code thoroughly and I am certain that I am not removing them anywhere else in my code.

Although my code is quite lengthy, I will share the relevant snippets with you.

Snippet for creating the wireframe:

const createGroupBoundingBox = (group, groupName) => {
  const newGroup = new Group();
  const box3 = new Box3();

  box3.setFromObject(group);
  const dimensions = new Vector3().subVectors(box3.max, box3.min);

  const planeGeometry = new PlaneGeometry(
    dimensions.x + BoundingBoxMargin,
    dimensions.y + BoundingBoxMargin
  );

  const matrix = new Matrix4().setPosition(
    dimensions.addVectors(box3.min, box3.max).multiplyScalar(0.5)
  );
  planeGeometry.applyMatrix4(matrix);

  const mesh = new Mesh(planeGeometry, new MeshBasicMaterial());
  
  const geo = new EdgesGeometry(mesh.geometry);
  const wireframe = new LineSegments(
    geo,
    new LineBasicMaterial({ color: 0xff0000, linewidth: 5.0, side: DoubleSide })
  );
  wireframe.position.setZ(ZIndexForBoundingBox);
  
  const txt = getText(
    groupName.toUpperCase(),
    planeGeometry.attributes.position.array[0],
    planeGeometry.attributes.position.array[1]
  );

  newGroup.add(wireframe);
  newGroup.add(txt);
  newGroup.userData = {
    objectType: ObjectType.boundingBox,
    info: {
      uuid: group.uuid,
      linePoints: dimensions,
    }
  };

  return newGroup;
};

Snippet for adding the wireframe to the scene:

const bbox = createGroupBoundingBox(group, newGroup.name);
scene.add(bbox);

https://i.sstatic.net/rcTye.png

https://i.sstatic.net/aFzc1.png

Answer №1

In the task at hand, the challenge arose when attempting to amalgamate objects from various groups into a single new group to form a bounding box. These objects were initially attached to the scene under their respective original groups. Upon adding them to a new group that did not exist in the scene, the objects became detached and vanished. The workaround involved cloning the objects without altering the originals and then computing the bounding box based on the cloned versions. Much appreciated for the guidance!

const objsClone = objects.map((obj) => obj.clone());

As a side note, THREE.js offers built-in functionality to clone objects without requiring custom code implementation.

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 purpose behind webpack compiling global.js?

As I dive into the world of webpack, my initial build unearthed an unexpected default file in the output (index 1): build.js 222 kB 0 [emitted] main [1] (webpack)/buildin/global.js 509 bytes {0} [built] [2] ./source/scripts/main.js 105 byte ...

The function createVNode does not exist in the context of Vue integrated with Laravel

I've been attempting to replicate a component rendering based on an online example. While it works smoothly in a sample project, it crashes when applied to the official codebase. Here is the content of the blade file being rendered: <html lang=&q ...

Pattern for either one or two digits with an optional decimal point in regular expressions

Currently, I'm utilizing for input masking. I am specifically focusing on the "patterns" option and encountering difficulties while trying to create a regex expression for capturing 1 or 2 digits with an optional decimal place. Acceptable inputs: ...

Utilizing TypeScript to spread properties onto a component and destructure them from within components

I'm trying to optimize my use of props spreading and destructuring when working with components. Currently, I spread my props in this manner: <RepositoryItem {...node} /> Then, within the component, I destructure the props like so: interface ...

My Node setup is not displaying my scene with the THREE.js Software Renderer

Struggling to incorporate 3d graphics into Node.js environment, I stumbled upon the Software Renderer after exhaustive research. The renderer is up and running, but I am facing difficulties in rendering my scene. The issue lies in the fact that my 3d obje ...

The Facebook share button on my website is malfunctioning

Could someone please help me figure out why my custom Facebook share button is not functioning properly? When I click the button, the share window fails to appear. I have tested it as an html file on a live web server and have thoroughly reviewed the Faceb ...

Conceal list items by clicking elsewhere on the page

Currently, I am facing an issue with my search user functionality. Whenever a user clicks anywhere else on the page, the list of results does not disappear unless the user manually deletes all the words they have typed. This behavior is not ideal and despi ...

Adjust the image dimensions within the DIV container

I am currently working on creating my personal portfolio website. I have encountered an issue where the image inside a div element enlarges the size of the entire div as well. My goal is to keep the image slightly larger while maintaining the same size for ...

Problem integrating 'fs' with Angular and Electron

Currently, I am working with Angular 6.0, Electron 2.0, TypeScript 2.9, and Node.js 9.11 to develop a desktop application using the Electron framework. My main challenge lies in accessing the Node.js native API from my TypeScript code. Despite setting "com ...

What is the best way to access a component's data within its method using Vue and Typescript?

Starting a Vue.js project with TypeScript and using single file components instead of class-styled ones has been my goal. However, I have encountered a recurring issue where I get error TS2339 when trying to reference my components' data using the "th ...

How to troubleshoot missing API data in a Bootstrap 5 modal

I am currently working on a project involving a Pokemon API where I have successfully built the front end using .fetch(). My goal is to create an additional modal that displays some stats. However, I am struggling to get the data from the API to show up in ...

How is it possible that my form is able to save data into the database even without any

I am considering adding a captcha process to my form and I am brainstorming some logic for it. I downloaded a login from Google, but I am confused why my form is still storing data into the database using action=' ' instead of action="register.ph ...

When HTML elements are dynamically inserted through JavaScript using quilljs, they may cause conflicts with the layout properties

I am currently working on creating a simple webpage layout similar to that of Stack Overflow, with a sidebar and a main content area that can scroll. In my case, the content area is intended to host a QuillJS text editor. To integrate the QuillJS editor i ...

An issue has been discovered with the Search function as JavaScript's Array.filter() and .map() methods are not functioning properly, resulting in

Currently, I'm working on integrating a search feature into my Flask application that will display the cities entered by users and are present in the JSON API results of a weather API. I am following a tutorial and have used a code similar to this: h ...

Reverse changes made to a massive object and then redo them

My current project requires the implementation of undo-redo functionality for a product. At the moment, I am managing a substantial Object retrieved from a MongoDB collection The structure is as follows: { cart:{ products:[ { name: " ...

Navigating a controller variable to access a property of a service

I've been struggling to access a property of a service through a bound controller variable in a template. Controller: app.controller('VictoryController', function($scope, AlertStatisticsService) { $scope.AlertStats = AlertStatisticsSer ...

Trouble with AJAX BitMovin: unable to process GET request

I've been struggling to find a solution to my problem as I cannot pinpoint what exactly to search for. My current issue involves using AJAX for a PHP Get request, causing it to submit and navigate away from the page. The BitMovin library appears to b ...

Based on the action taken, send specific data through AJAX - whether a form submission or a div click

There is a function called search, which can be triggered either by clicking on a div or submitting a form. When the div is clicked, the id of the div is sent as data in an AJAX call. However, if the form is submitted, I want to send the inputted data thr ...

In Reactjs, you can prevent users from selecting future dates and times by modifying the KeyboardDateTimePicker component

I am currently using the Material UI KeyboardDateTimePicker component and have successfully disabled future dates with the disabledFuture parameter. However, I am now looking for a way to disable future times as well. Any suggestions or solutions would b ...

Tracking progress within an HTML table

I am facing an issue with a table that is designed to display progress bars for each method or task stored in the database. These progress bars are determined by two dates: the startDate and endDate. However, I have noticed that the progress bar only funct ...