Exploring the world of interconnected meshes in three.js

Currently, I am in the process of working on 3D models for a project using three.js.

The model I am creating is a combination of basic geometries that come together to form a defense tower.

Specifically, I have 2 Boxes and 3 Cylinders that make up the structure.

Boxes:

var base = new THREE.Mesh(new THREE.BoxGeometry(7, 0.2, 7), new THREE.MeshPhongMaterial({color: 0xff000f}));
var head = new THREE.Mesh(new THREE.BoxGeometry(3, 1, 2.5), new THREE.MeshPhongMaterial({color: 0xaaffff}));

Cylinders:

var body = new THREE.Mesh(new THREE.CylinderGeometry(0.5, 0.5, 5, 20), new THREE.MeshPhongMaterial({color: 0xffffff}));
var leftCannon = new THREE.Mesh(new THREE.CylinderGeometry(0.35, 0.35, 4, 20), new THREE.MeshPhongMaterial({color: 0xffffff}));
var rightCannon = new THREE.Mesh(new THREE.CylinderGeometry(0.35, 0.35, 4, 20), new THREE.MeshPhongMaterial({color: 0xffffff}));

Please note that the colors of the materials are temporary.

Originally, I planned to add each mesh separately to the scene:

scene.add(base);
scene.add(body);
scene.add(head);
scene.add(leftCannon);
scene.add(rightCannon);

However, I believe there must be a more efficient way to do this, one that allows for more meshes without compromising performance.

I am considering using a container to hold all the geometries and adding a single object to the scene instead of multiple.

My goal is to have a unique material for each mesh (such as a metallic finish for the cannons and a dark color for the base) while still being able to manipulate each mesh independently (currently, the tower's head moves with mouse dragging and using WASD keys).

Answer №1

Construct a new THREE.Object3D or THREE.Group and place the Meshes within it. Insert this object into the Scene. By doing so, you can easily manipulate and update the material for each piece independently.

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

Trying my hand at using JQuery to dynamically update the image source of a draggable element upon dropping

Essentially, I have an object being dragged with the class .dragme, a dropzone with the class .dropzone1 for it to be dropped at, and an image that I want the .dragme object to become once it has been dropped. My current code looks like this: $(".dropzon ...

Is there a way to change an object into a string in JavaScript without using JSON.stringify?

Usually I wouldn't approach it this way, but for the sake of a practice exercise, I am attempting to convert an object without relying on JSON.stringify(). Take a look at the object in question: obj = { num: 0, string: "string", func: function ...

Unable to receive the expected response within the Ajax success callback

I'm new to PHP and WordPress and I'm using an Ajax call to submit form information. I want to display a "Thank you" message within a <div> element as a response. Although I can execute the function in function.php, I'm not receiving a ...

Convenient Method for Making POST Requests with the Node Request Module and Callback

Does the .post() convenience method in Javascript/Node's request module accept a callback? I'm confused why it would be throwing an error like this: var request = require('request'); request.post({url: 'https://identity.api.foo/v ...

How can I show hidden column names in the Sencha Touch 2 date picker component with CSS?

I am currently utilizing a date and time picker that has the ability to display ['month', 'day', 'year','hour','minute','ampm']. You can find the source code here. Although everything is function ...

Can an open inventor scenegraph be transformed into a threejs scene?

Is there a way to display open inventor scengraph in a webgl format? I'm looking for some suggestions on how to tackle this project. Any ideas are welcome! ...

Encountering a problem while verifying pattern using regular expressions

I'm facing an issue when manually checking if my inputs match the specified patterns. Below is the function I am using for this check: if (!$element.attr("pattern")) return true; let pattern = $element.attr("pattern"); le ...

I am attempting to send an array from the controller to JavaScript using AJAX, but instead of returning as an array, it is being returned as a string

Here is the code snippet from my controller: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class AjaxController extends Controller { public function s ...

The application monitored by nodemon has halted - awaiting modifications in files before restarting the process

1. My ProductController Implementation: const Product = require('../models/product') //Creating a new product => /ap1/v1/product/new exports.newProduct = async(req, res, next) => { const product = await Product.create(req.body); re ...

Show me a list of either only development or production dependencies in npm

When attempting to list only the production dependencies from package.json according to the npm docs, I tried: npm list -depth 0 -prod or npm list -depth 0 -only prod However, npm continues to list both dependencies and devDependencies. Can anyone sugg ...

The transition of a controlled input to an uncontrolled state within a component, along with a partial update to the state

In my project, I have a main component that needs to collect a list of contacts including their name and email: import { useState } from 'react' import AddContactFn from './components/AddContactFn' function App() { const [contacts, ...

Enhance jQuery dialog form by populating it with specific record information

When I click the "Edit" button on a user records table, a jQuery dialog form appears to allow editing of the record. I've successfully stored the record values in jQuery variables and used them to populate the form. For text input elements, everythin ...

Please verify the utilization of jQuery.ajaxSettings.xhr

var _orgAjax = jQuery.ajaxSettings.xhr; jQuery.ajaxSettings.xhr = function () { var xhr = _orgAjax(); DoSomeLogging(); return xhr; }; I came across this code snippet and I'm trying to make sense of it. Does this mean that any $.ajax(...) ...

I am encountering a JQuery syntax error while using Bootstrap 3 button-dropdown links

I'm trying to replicate the example found here in order to create a similar markup: <div class="btn-group"> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> ...

Explore within another map and analyze the node to spot the differences

I have some questions about iterating through a JavaScript Object and using array functions in JavaScript. Let's assume I have the following variables: var json1 = "[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]"; var json2 = "[{"id": 1, "name":"x"}, ...

Navigating with AngularJS through link redirection using anchor tags

How can I achieve smooth scrolling to a specific section using angularJS when clicking on a link? When clicking on a link like this: The page instantly redirects to the specified footer section. I want to implement an angular controller to handle this fun ...

Error in hover feature not performing to expectations

Check out my jsfiddle link: http://jsfiddle.net/ganganp/x62wR/5/ $('#rotator0 div').hover( function () { ssrc = $(this).find('img').attr("src"); valt = $(this).find('img').attr("alt"); ...

How come I lose a day when attempting to convert a date to an ISO string in JavaScript?

I've been attempting to convert a date object to the ISOString() format, but it's consistently returning a result that is 1 day off (i.e., it subtracts 1 day). var fromDate = { day:4, month:5, year:2012 } var fromDateString = new Date ...

Instant disconnection from OBS WebSocket detected

I'm currently working on developing an application to manage OBS, but I encountered an issue while trying to establish a connection with Node. Despite having the correct port and password set up, my connection gets terminated immediately after running ...

I can only use innerHTML once in my React application

I am attempting to clear my container div when the user clicks the search button in order to remove all existing search results. However, I am encountering an issue where using innerHTML to clear the container div only works the first time. If a second sea ...