Tips for incorporating various color schemes into all sides of a shape generated through the extrude geometry feature in three.js

I have recently designed a wall-like structure using extrude geometry and now I want to assign different colors to each side based on the settings below:

var extrusionSettings = {
        curveSegments:5,
        steps: 10,
        amount: 200,
        bevelEnabled: true,
        bevelThickness: default_height,
        bevelSize: 0,
        bevelSegments: 8,
        material: 0,
        extrudeMaterial: 5
    };

var geometry1 = new THREE.ExtrudeGeometry( shape1, extrusionSettings );

var materialLeft = new THREE.MeshLambertMaterial({
    color: 0xd6d6d6,// cement   
    //transparent:true,
    side: THREE.DoubleSide,
    ambient: 0xd6d6d6
});
var materialRight = new THREE.MeshLambertMaterial({
    color: 0xd6d6d6,//violet    
    //transparent:true,
    side: THREE.DoubleSide,
    ambient: 0xcc49c3
});
var materialTop = new THREE.MeshLambertMaterial({
    color: 0x8649cc,//blue  
    //transparent:true,
    side: THREE.DoubleSide,
    ambient: 0x8649cc
});
var materialBottom = new THREE.MeshLambertMaterial({
    color: 0x4ac4b6,// green
    //transparent:true,
    side: THREE.DoubleSide,
    ambient: 0x4ac4b6
});
var materialFront = new THREE.MeshLambertMaterial({
    color: 0x567E14,// dark green
    //transparent:true,
    side: THREE.DoubleSide,
    ambient: 0x567E14
});
var materialSide = new THREE.MeshLambertMaterial({
    color: 0xa8a843,// yellow
    //transparent:true,
    side: THREE.DoubleSide,
    ambient: 0xa8a843
});

//var materials = [ materialFront, materialSide ];
var materials = [
                      materialLeft, // Left side 
                      materialRight, // Right side 
                      materialTop,   // Top side    
                      materialBottom,// Bottom side 
                      materialFront, // Front side 
                      materialSide, // Back side                                 
                    ];

var material = new THREE.MeshFaceMaterial( materials );

var mesh1 = new THREE.Mesh( geometry1, material );

When using material: 0,extrudeMaterial: 5, it refers to the materials at positions 0 and 5 in the array. However, I am looking for guidance on how to properly distribute all colors from the array to correspond with the sides of the shape.

If anyone could provide assistance, that would be greatly appreciated.

Answer №1

let count = 0;
let limit = obstacles.length;

for (let j = 0; j < limit; j++) {
    obstacles[j].colorIndex = j % limit;
}

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

Navigating sub-domains swiftly

Having trouble setting up sub-domains and routing in Express Node. I need to direct users based on their device and browser type. If the user is on a desktop, they should be routed to web.. If they are on a mobile device, it should be mobile.. And if the ...

Deleting an ID from an array within a document using Node.js Mongoose

In my model document, there is an array that I am working with. I want to be able to remove a specific ID from this array. Is it possible to do so? https://i.sstatic.net/agaD3.png Below is what I attempted. module.exports.RemoveFavourite = async (req, re ...

Having trouble hiding the hamburger menu depending on the screen width

I'm trying to figure out how to hide the hamburger menu when the screen width exceeds 400px. You can check out the site at damf.co Currently, the hamburger menu appears when the screen width is less than 400px. If you click on it and then expand the ...

Guide to sending both JSON data and form data in a single request using Laravel 9

I am working on a form where I need to input multiple images that will be converted into JSON format. The HTML for my form: create.blade.php <form method="post" action="{{ route('m_announcement.store') }}" enctype="mu ...

The JavaScript function's if and else statements are being executed simultaneously

Upon loading the page, I am checking the value of a dropdown. Strangely, when I debug, it appears that the controller is executing both the if and else statements, which should not be happening. /* Here is my code: */ $(window).load(function() { ...

Is there a way to overlay a div on a particular line within a p element?

Within this paragraph lies a collection of text encapsulated by a < p > tag. When this content is displayed on the page, it spans across 5 lines. My objective is to creatively style and position a < div > tag in order to highlight a specific li ...

Is it possible to devise a universal click handler in TypeScript that will consistently execute after all other click handlers?

In my ReactJS based application written in TypeScript, we have implemented various click handlers. Different teams contribute to the application and can add their own handlers as well. The challenge we face is ensuring that a specific global click handler ...

Enhance Your Website with GatsbyJS Custom Scrollbars

I'm struggling to customize the default scrollbar on my Gatsby website with an overlay scrollbar. I want to avoid the page 'shifting' slightly to the left when switching between pages that have scrollbars and those that do not. My attempt i ...

Insert a variable into the URL path of a JavaScript file

Struggling to insert a variable in the code snippet below: eleventyConfig.addPassthroughCopy({ "random-folder/img": "subfolder/img" }); This is what I've attempted: var directory = "random-folder"; eleventyConfig.addPassthroughCopy({ directory + "/ ...

Display notification following successful data saving in CodeIgniter

I attempted to set up a notification to appear when saving data and displaying it in the view, but it didn't work as expected using notify.js. Can someone offer some assistance? This is my save function: function save() { $data = array( ...

Essential Guide to Binding Events with JQuery

Why is the handler for an event on an element triggering the wrong response? I was expecting the click event of Div1 to display a dialog stating 'div1', but it's showing 'div2' instead. I am new to this and trying to figure out wh ...

Postgres.js Date Range query failing to fetch any results

Recently, I have been utilizing the Postgres.js npm module to interact with a PostgreSQL database Below is the code snippet for executing the query: let startDate = '2020-01-28 08:39:00'; let endDate = '2020-01-28 08:39:59'; let table ...

An uncomplicated broadcasting and receiving method utilizing an event emitter

This code is from chapter 3, example 11 of the book Node.JS in Action, found on page 52. var events = require('events'); var net = require('net'); var channel = new events.EventEmitter(); channel.clients = {}; channel.subscriptions = ...

Troubleshooting: Why Won't My Basic JQuery POST Request Work?

Can someone help me figure out how to use JQuery to send a POST request and display the data in a PHP file? Here is the HTML file: <html> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> ...

Exploring the concept of jQuery handler functions

Within my code, I have the following: <script type="text/javascript"> $(document).ready(function (e) { $('#EventCreate').click(function (e) { location.href = '@Url.Action("Create", "AEvents")'; }); ...

The project is throwing an error: Unable to locate module './lib/md5-hex' in Ember JS

I attempted the following steps: uninstalling md5-hex using npm with the command: npm uninstall md5-hex --save reinstalling md5-hex using npm with the command: npm install md5-hex --save After performing these actions, I ran ember s but unfortunately, i ...

Using jQuery's append function will retrieve external source files within script tags, however, it will not officially render them in the DOM

After including a script with an external source and attempting to parse it using jQuery, the script is downloaded but not loaded into the DOM. This issue persists regardless of which jQuery DOM insertion method I use, such as .append(). Take a look at th ...

Using Javascript's Speech Recognition to activate a button

I am new to using JavaScript Speech Recognition and decided to work with the Annyang library. My goal is to automatically trigger the "show date" button when the user says 'hello', without actually clicking the button. However, I've been fac ...

JSP form continues to submit despite JavaScript validation returning false

On my JSP page, I have a form named "deleteCont" and a JavaScript function called "validateDelete". When the user clicks the "Delete contact" button, the "validateDelete" function is triggered successfully, showing an alert message. Unfortunately, even whe ...

The Next JS build process exclusively creates server-side pages

I have noticed that some of my pages like /contact, /about, and /rules are server-side generated even though I am not using getServerSideProps(). Since these pages only contain static images and text without any API requests, I want them to be treated as s ...