Polyhedron with a unique mix of textures adorning each face

I'm currently working on a project where I am trying to recreate a truncated icosidodecahedron using three.js. My goal is to apply unique image textures to each of the faces of this complex shape.

To start, I used the code from this Three.js example as a reference point because I am relatively new to Three.js and 3D graphics. However, I have encountered difficulties while trying to implement texture materials into my project.

The snippet below is crucial to my current approach. In the initial setup, I have defined three different solid color materials based on the number of vertices each face has.

var materials           = [
    new THREE.MeshBasicMaterial({ color: 0x00ccdd }),
    new THREE.MeshBasicMaterial({ color: 0xccff00 }),
    new THREE.MeshBasicMaterial({ color: 0x6600ff })
];

var faceMaterial        = new THREE.MeshFaceMaterial(materials);

var faceGeom            = new THREE.Geometry();
    faceGeom.vertices   = vertices;

var faceIndex           = 0;

for (var i = 0; i < data.faces.length; i++) {
    var vertexIndexes   = data.faces[i];
    var vertexCount     = vertexIndexes.length;

    for (var j = 0; j < vertexIndexes.length - 2; j++) {
        var face        = new THREE.Face3(vertexIndexes[0], vertexIndexes[j + 1], vertexIndexes[j + 2]);

        switch (vertexCount) {
            case 4:  face.materialIndex = 0; break;
            case 6:  face.materialIndex = 1; break;
            case 10: face.materialIndex = 2; break;
        }

        faceGeom.faces[faceIndex]   = face;
        faceIndex++;
    }
}

After applying the above code, this correctly generates the following output:

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

However, once I attempt to replace even one of these materials with an image texture like so:

var materials           = [
    new THREE.MeshBasicMaterial({ color: 0x00ccdd }),
    new THREE.MeshBasicMaterial({ color: 0xccff00 }),
    new THREE.MeshBasicMaterial({
        map: THREE.ImageUtils.loadTexture("assets/images/lovely-texture.jpg")
    }),
];

. I encounter various WebGL errors, along with the texture failing to display properly.

[.WebGLRenderingContext-0x7ffc6a6f3ef0]GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 1
WebGL: too many errors, no more errors will be reported to the console for this context.

This issue has led me to wonder if I might be making a simple mistake or if there are limitations when it comes to applying textures to Face3 triangles?

Additionally, I am open to suggestions regarding alternative approaches to building this object while maintaining the ability to showcase distinct textures on different faces.

Answer №1

When working with textured materials, it is essential to have defined UVs for the geometry of your mesh.

For a demonstration on setting the faceVertexUvs[ 0 ] property of a THREE.Geometry, take a look at the code in SphereGeometry.js.

The method you choose to set your UVs is completely up to you. It might be helpful to start with a simpler example and then progress from there as you become more comfortable with the process.

This information pertains to version r.71 of three.js.

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

Maintain synchrony of the state with swiftly unfolding occurrences

I developed a custom hook to keep track of a state variable that increments based on the number of socket events received. However, when I tested by sending 10 simultaneous events, the total value of the state variable ended up being 6, 7, or 8 instead of ...

When the value remains unchanged, Angular's @Input() value does not get updated

Upon running this program, I initially receive the output as false, 5, 500 since they have been initialized in the child component. However, upon clicking the update button, I am unable to revert to the previous values. Instead of getting true, 10, 1000, I ...

Tips on positioning a button "above" the carousel-control-next icon

I have a carousel and I added a button to stop the spinning. However, when I try to click on the button, it seems like the "carousel-control-next" tag is being clicked instead. How can I solve this issue? ** Interestingly, when I remove the "carousel-cont ...

Tsyringe - Utilizing Dependency Injection with Multiple Constructors

Hey there, how's everyone doing today? I'm venturing into something new and different, stepping slightly away from the usual concept but aiming to accomplish my goal in a more refined manner. Currently, I am utilizing a repository pattern and l ...

Issue encountered while generating dynamic bootstrap toggle button

Looking to make my Bootstrap toggle buttons dynamic instead of static by loading them at runtime. Here is the HTML code I've been using: <a> Test-Title <input checked data-onstyle="success" data-toggle="toggle" id= ...

The perfect timing for incorporating a JavaScript MVC framework into your project

As part of my job at a small web agency specializing in web applications for startups, I'm urging my boss to invest more in developing strong client-side frameworks using Javascript MVC standards like BackboneJS and templating with Underscore. I unde ...

Searching for similar but not identical results using Knex.js

I am seeking a solution to retrieve similar posts without including the post itself. Here is my approach: export async function getSimilars(slug: string) { const excludeThis = await getBySlug(slug) const posts = await knex('posts') .whe ...

There seems to be an issue: [ng:areq] - Please visit the following link for more information: http://errors.angularjs.org/1

Hey there! I'm in need of some assistance. Just started learning Angular and tried setting it up like this. This is the structure of my files: AboutController.js function AboutController( $scope ){ $scope.data = { "data" : { "name ...

Persistent Angular Factory Variables Showing Outdated Data - Instances Stuck with Old Values

One of the challenges I faced was setting up a resource factory to build objects for accessing our API. The base part of the URL needed to be determined using an environment variable, which would include 'account/id' path segments when the admin ...

Transform the dynamic JSON format into a key-value pair structure with nested children nodes

Looking to convert a dynamic JSON structure into a tree node: { "video": { "width": 1920, "height": 1080, "video_codec": "H264", "CBR": "4337025", "frame_rate& ...

JavaScript is utilized to update HTML content through an Ajax request, but unfortunately, the styling is

Apologies for the unconventional title, I am currently in the process of creating a website. When the page is initially loaded, I call upon two scripts within the 'head' tag to create an attractive dynamic button effect. <script src="https:// ...

tips for implementing JSON loading in Ext JS

While attempting to load values from a web service, I encountered the following error message: "ChatStore.data.items[i] is undefined." The mapping code for extjs is as follows: ChatStore = new Ext.data.JsonStore({ storeId: 'ChatStore1' ...

"Utilizing an exported constant from a TypeScript file in a JavaScript file: A step-by-step guide

I am facing an issue when trying to import a constant from a TypeScript file into a JavaScript file. I keep encountering the error Unexpected token, expected ,. This is how the constant looks in the ts file: export const articleQuery = (slug: string, cate ...

What could be causing this jQuery selector to not select any elements?

Here's a simple question with some code that needs troubleshooting: $insides = $('<thead><tr><th><!--Blank space --></th></tr></thead><tbody><tr class="green-row"><td>Opportunities to D ...

Can JavaScript values be passed into CSS styles?

I am currently working on dynamically updating the width of a CSS line based on a JavaScript value (a percentage). The line is currently set at 30%, but I would like to replace that hard-coded value with my JavaScript variable (c2_percent) to make it mor ...

Performing AJAX request when a link is clicked within a Wordpress website

I am looking for a way to initiate a PHP function in my Wordpress site when a link is clicked, rather than using a form submission. I have tried using an AJAX request but it doesn't seem to be working. Any suggestions would be greatly appreciated. Be ...

The color of active links in TailwindCSS remains unchanged

In my project, I am using NextJS along with Tailwind CSS to create a top navigation bar. My goal is to change the text color for active links within the navigation bar. Below is the code snippet I have implemented: const Header = () => { return( ...

Mongodb processes timestamp data in a long format

I'm curious about how to work with timestamps in MongoDB using NumberLong in our database. Which JavaScript function should I use in the MongoDB shell for this purpose? For instance, how can I determine the millisecond time of the next day after a ce ...

using an external JavaScript function in the MongoDB shell

In my case, I have JavaScript functions that are stored in a JSON file: functions={} functions.a = function(){ return "returned" } I've come across tutorials suggesting that by importing this JSON file, I should be able to use these ...

Animate a nested element dynamically with jQuery

Whenever I click on the "view" button, I am dynamically adding data to a div. This feature is working perfectly fine. However, I wanted to improve it by adding another nested dynamic element. The problem I'm facing now is that when I try to expand al ...