What is the best way to combine multiple three.js meshes into a single Mesh object?

Can you combine multiple meshes with different materials in Three.js?

After some research, I've discovered that existing solutions either merge the geometry only or simply place the Meshes together in a single Object3D or Group.

Answer №1

Absolutely: Sort of (refer to the comments linked to both the question and this response):

var blueMat = new THREE.MeshPhongMaterial( {color: 0x0000FF } );
var redMat = new THREE.MeshPhongMaterial({ color:0xFF0000 });
var meshFaceMat = new THREE.MeshFaceMaterial( [ blueMat, redMat ] );

var cubeGeo = new THREE.BoxGeometry( 10, 10, 10 );

for ( var f in cubeGeo.faces ) {
    cubeGeo.faces[ f ].materialIndex = 0;
}

var sphereGeo = new THREE.SphereGeometry( 5, 16, 16 );
sphereGeo.applyMatrix( new THREE.Matrix4().makeTranslation(0, 5, 0) );

var combinedGeo = new THREE.Geometry();
combinedGeo.merge( cubeGeo, cubeGeo.matrix );
combinedGeo.merge( sphereGeo, sphereGeo.matrix, 1 );

var meshObj = new THREE.Mesh( combinedGeo, meshFaceMat );
scene.add( meshObj );

I opted for a cube and sphere as an example because a box requires distinct material identifiers for its faces.

http://jsfiddle.net/v49ntxfo/

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

Develop a JavaScript function to generate a multiple selection form

Hey there! I have a question... Can I use JavaScript to create a multiple select form? Here's an example: <select name="item[]"> <option value="0">Option 1</option> <option value="1">Option 2</option> </select> &l ...

Unleash the power of drag-and-drop functionality with cdkDrop

I am currently tackling a project that requires the implementation of a drop zone functionality where elements can be dragged from a list and dropped in a zone for free movement. I intend to utilize a cdkDropList for the zone due to its comprehensive list ...

Encountering an issue in my Next.js application with the app router where I am unable to read properties of undefined, specifically in relation to '

As I develop a basic Rest API in Next.js, my goal is to display "Hello world" in the console for a post api. export const POST = (req: Request) => { console.log('hello world'); }; The error message that appears in my terminal is as follows: ...

What are the circumstances under which JavaScript GCP libraries return null values?

My current project involves working with GCP and Firebase using typescript. I have been utilizing the provided libraries, specifically version 8 of Firebase, and have encountered some unexpected behavior. For instance (firebase, ver. 8.10.1) import 'f ...

Looping through a nested for loop within an HTML table

I am currently working on generating a dynamic table using Lists of varying sizes. My first list is a List of Cars List<Car>. I have successfully placed the names of different car companies in the header. Next, I have a List<List<CarSales>& ...

Guide on configuring remix using aws cdk

Currently, I am delving into the world of remix and attempting to configure a remix project that utilizes AWS CDK for the server. I stumbled upon this GitHub example: https://github.com/ajhaining/remix-cloudfront-cdk-example However, it lacks clarity on ...

What is the significance of utilizing create() when inheriting prototypes?

Discover more about the concept of inheritance in this informative resource: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance#setting_teachers_prototype_and_constructor_reference... When it comes to inheriting prototypes, the ...

Exporting to .xls is successful in the development environment, however, it encounters a network error and fails in the production

Despite working flawlessly in my development environment, my code is failing in production. It's not a browser issue as it fails on Firefox, IE, and Chrome. The file size isn't the problem either, despite other similar questions suggesting otherw ...

Customizing the renderInput of the Material UI DatePicker

Recently I integrated material-ui into my React project with TypeScript. I implemented the following code based on the example provided on the official website. import AdapterDateFns from '@mui/lab/AdapterDateFns'; import DatePicker from '@m ...

Using RTK Query to store data in a Firebase Realtime Database

Currently, I am facing an issue while trying to retrieve data from a Firebase Realtime Database using RTK Query. The code in this section is throwing an error because the return value is incorrect. If anyone has experience with this particular issue, I wou ...

I'm looking for a way to check and validate three input fields for date, month, and year individually using either jQuery or JavaScript. Can anyone

My form requires input fields for date, month, and year separately. The validation rules are as follows: Date should be between 1-31 and month should be between 1-12. The desired date format is dd mm yyyy. Could someone kindly help me with the HTML code ...

When using @pinia/nuxt, an HTTP request is triggered for each iteration of setInterval that occurs outside of the setInterval

I am currently working on creating a timer using @pinia/nuxt within nuxt 3. I also have a requirement to send an http request to sync my database when the timer starts. However, I'm encountering an issue where the http request is being triggered for ...

Preserving the most recent choice made in a dropdown menu

Just started with angular and facing an issue saving the select option tag - the language is saved successfully, but the select option always displays English by default even if I select Arabic. The page refreshes and goes back to English. Any assistance o ...

Choose the content of a specific element while excluding specific descendants

Trying to target the parent element .search-result-item, excluding the h3 tag and btn-grey class from the selection. The current code is as follows: $('div.search-result-item:not(h3 .btn-grey)').click(function(e){ console.log('1'); ...

The expected rendering of column headers was not achieved following the refactoring of <Column />

After making changes, the header is not rendering properly and I cannot see the "Product ID" header for the column: // DataTable.tsx // React Imports import React, { useState } from 'react'; // PrimeReact Imports import { DataTable as DT } from ...

Can you explain the contrast between 'depict' and 'examine' when using Jest?

I am currently diving into the world of unit testing in Vue.js with Jest. One question that keeps popping up is when to use describe and when to use test ? TodoApp.vue Component : <template> <div> <h1>TodoApp Componenent</h1> ...

Using React to fetch images from a directory

I am struggling to add images to my Shopify module developed with react. I have created a folder and added an image, but I am unable to use it on my page. Could it be that I forgot to import something or made a mistake somewhere? import { EmptyState, Layo ...

Obtain data from an ajax request to be included in the form submission

Seeking assistance with my code to retrieve the selected value from ajax_details.php for submission in the form action process_details.php. Here is the code snippet: injury_details.php <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jqu ...

I am encountering an issue with Three.js version r69

I encountered an issue with line geometry.vertices.push(new THREE.Vertex(vector)); In the earlier version of Three.js, r36, this line was functioning. var vector = new THREE.Vector3(-300 + x/4, 240 - y, 0); geometry.vertices.push(new THREE.Vertex(ve ...

Handling Exceptions in Node.js and Express

Having recently delved into the world of javascript and node, I've been working on creating an application using node.js and express. While I've implemented appropriate error callbacks in my code, there are instances where the node.js server abr ...