Assign a specific material to a specific portion of a mesh in Three.js

In the world of three.js, imagine having a 3DObject that looks something like this:

const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshStandardMaterial({ color: 0xff0000 });
const mesh = new THREE.Mesh(geometry, material);

This mesh is special because it's dynamic - the user can adjust its size, which changes the scale of the mesh.

widthInput.subscribe(value => this.mesh.scale.x = +value); // just to get the idea

Now, I've heard that you can assign different materials to different sides of the geometry. I've also heard whispers about being able to set materials on specific sections of the geometry's sides (if there were more sections).

Here's the catch: the user can adjust the width between 200 and 260, but I need a distinct material on the far right of the mesh, with a fixed width of 10. How would I achieve this without creating a whole new geometry? Is there a method to apply a material to the fixed part of the mesh? Or could I somehow designate one segment to always have the fixed size? Help would be greatly appreciated.

Visual representation of the dilemma (white area needs a fixed width of 10 while the red area adjusts):

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

Answer №1

Is there a method to assign a specific material to a fixed section of the mesh?

While it is possible to assign different materials to various parts of the geometry, the challenge lies in defining what exactly is considered 'fixed':

Alternatively, is there a way to ensure that one segment always maintains a fixed size?

Indeed, this can be achieved by manually modifying the geometry. By accessing properties like g.attributes.position.array and adjusting the vertices that form the segment, you can control its size. This method operates at a lower level compared to the scene graph.

It's important to consider the rationale behind wanting to consolidate everything within the same geometry. Utilizing vertex colors for segment differentiation and implementing GLSL-based animation for extrusion could offer a more efficient solution than using scale.set(). However, if the goal is to apply distinct materials without GLSL programming, multiple draw calls will still be necessary.

In terms of memory optimization, duplicating a basic cube and referencing it twice may prove more efficient than storing additional vertices and faces. The approach you are pursuing may potentially consume more memory and result in similar render overhead. Therefore, employing the scene graph with two meshes and a single geometry (avoiding the need to replicate the box and utilizing two nodes) should offer comparable performance while streamlining the workflow.

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

Swaying while navigating through volumetric fixed step raymarching

Encountering a bug with my shaders while working on the vertex: varying vec3 worldPosition; varying vec3 viewDirection; void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); worldPosition = vec3(modelMatrix * vec4 ...

The dependency path in the package.json file contains all the necessary files

I recently developed a JavaScript package and here is the configuration in my package.json: { "name": "packageName", "version": "1.0.0", "description": "Description of the package", " ...

The second asynchronous function relies on the completion of the first

Despite dedicating two days to exploring async/await and promises, I am still unable to make it work correctly. The challenge lies in the fact that the second await for b relies on the result of the first a. It seems no matter what approach I take, b keep ...

The .load() function seems to have a problem with my slider widget

I am facing an issue with a slider on my page that displays posts from a specific category. When the user clicks on "next category", the content slides to the left and new content is loaded along with its respective slider. The problem arises when the loa ...

Tips for accessing elements other than the root element using this.$el

Within my template, the structure is as follows: <div v-show="showContent" class="content-block-body"> <div class="slider-pro"> <div class="sp-slides"> <slide v-for="block in subItems" ...

Creating separate files for establishing DB connection and writing Node.js queries is a recommended practice for organization

Having an issue with connecting dbconnection.js and demo_api_select.js. When trying to declare a variable in demo_api_select.js, I am encountering errors like this: Error Notification Please assist me in resolving this problem. dbconnection.js: var ...

The issue of banding caused by Bloom and Antialiasing in Three.js rendering

I'm attempting to incorporate a glowing effect into my scene. To achieve this, I understand that using a bloom filter with the EffectComposer is the ideal approach. However, I've encountered an issue where utilizing the EffectComposer compromises ...

You can activate Lightgallery just one time in VueJs

I am facing an issue where lightgallery can only be opened once. Subsequent clicks on the button are unresponsive. The lightgallery is being used as a component. Within my parent component, I have two buttons for opening image or video gallery ParentComp ...

transforming JSON information within an angularJS framework into an array and displaying it visually with a C3 chart

Looking to create an x-y plot using AngularJS with a JSON data feed. The JSON data displays EQ magnitude VS time. How do I convert this data into an array format and plot it in a c3 chart? (similar to the one in this link ) Appreciate any assistance you c ...

Issues with response functionality in Node.js (Openshift) using express

I am currently working with OpenShift and Node.js to calculate the average rating for each result. However, I am facing an issue where the response is not being displayed even though the console logs show the correct data. The console displays 3.9454323, ...

Steps for removing all inline styles within an element:1. Access the element's

I have a group of span elements containing texts generated by tinymce. <span class="ArticleSummary"> This content is produced using a text editor and may include various inline styles. </span> Typically, this text includes numerous inline s ...

Using Vue to cycle through an array of objects, and emphasize the chosen item by clicking on it

I have to display an array of objects in the DOM by using map method. Each item has a @click event listener where I want to highlight it with a CSS class 'hover'. The desired functionality is to mimic a menu system where only the clicked item sho ...

Is it permissible to make alterations to npm modules for node.js and then share them publicly?

I have made modifications to a module called scribe.js that I use in my own module, which is published on npm. Instead of using the original module as a dependency for my module, I would like to include my modified version. I am unsure about the legal impl ...

Navigating by Typing in the URL Bar in React

Whenever I paste a valid URL and press enter, the useEffect function in the parent component does not get triggered. However, if I reload the page, it works fine. Here is the code snippet: Routing path <Route path="/search" element={<Searc ...

What assistance is available for building a JavaScript package that integrates and utilizes all necessary dependencies?

I am looking for a solution to include a third-party library in a JavaScript file that will be downloaded to our project only when we visit a specific page. This library is installed with npm and I want it to be part of the js package without includi ...

What is the method for looping a texture along the z-axis in three.js?

I'm trying to create a road in three. js. Here is the code: var width = 5; var length = 4; var height = 1; var geometry = new THREE.BoxGeometry( width, length, height ); var textureloader = new THREE.TextureLoader(); var texture = textureloader.load( ...

Transferring data between various stages of the user interface

As a newcomer to angularJs, I find myself facing an issue with two forms existing in different UI states (URLs) labeled as Step 1 and Step 2. The process requires filling up Step 1 and moving to the next step by clicking the NEXT button, which then leads t ...

Position an element in the middle of the range between the tallest and shortest characters

Is there a way to vertically center an element between the tallest and shortest characters of another element (preferably using just CSS, but JavaScript is also acceptable)? I want it to be aligned with the actual text content rather than the line height, ...

ChartJS has compatibility issues on Windows 10, regardless of the browser being used

Recently, I performed a fresh installation of Windows 10 on my laptop. However, after this process, I encountered an unusual issue with ChartJS on multiple pages of my site. Despite trying various browsers like IE11, Edge, Chrome, and Firefox, the charts s ...

how can you activate a modal without relying on bootstrap?

Could anyone suggest a more efficient way to create a pop-up modal triggered by a button click without relying on bootstrap? I've attempted different methods but haven't achieved the desired result. Here's a sample plunker for reference. Any ...