Difficulty with Three.js texture when incorporating a gap into the geometry

After successfully creating a shape using ExtrudeGeometry with different top and bottom textures, I encountered an issue where adding holes to the shape caused the bottom side texture to mix up with the top side. This unexpected behavior has left me puzzled about what might be causing it.

Here is a comparison of the bottom side texture without and with holes:

Bottom side texture without holes Bottom side texture with holes

In my code snippet below, you can see that I used a for loop at the end to set different textures on the front and back sides of the shape. The approach was inspired by a helpful thread on Stack Overflow.

this.group = new THREE.Group();
this.scene.add( this.group );       

for(i=0; i < boardDrill.length; i++) {
    boardShape.holes.push(boardDrill[i]);
}

var extrudeSettings = { amount: 1, bevelEnabled: false, material: 0, extrudeMaterial: 1};

var geometry = new THREE.ExtrudeGeometry( boardShape, extrudeSettings );

var textureLoader1 = new THREE.TextureLoader();
var topTexture = textureLoader1.load(this.boardData.topImage);
topTexture.repeat.set(1/boardWidth, 1/boardHeight);

var textureLoader2 = new THREE.TextureLoader();
var bottomTexture = textureLoader2.load(this.boardData.bottomImage);
bottomTexture.repeat.set(1/boardWidth, 1/boardHeight);

var frontMaterial = new THREE.MeshPhongMaterial({ map : topTexture});
var backMaterial = new THREE.MeshPhongMaterial({ map : bottomTexture });
var sideMaterial = new THREE.MeshPhongMaterial({ color: 0x00cc00 });

var materials = [frontMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial, backMaterial];
var material = new THREE.MeshFaceMaterial( materials );

var mesh = new THREE.Mesh( geometry, material );                        

for ( var face in mesh.geometry.faces ) {
    if (mesh.geometry.faces[ face ].normal.z == -1) {                   
        mesh.geometry.faces[ face ].materialIndex = 5;
    }
}


this.group.add( mesh );

Answer №1

Shoutout to Matey for the helpful suggestion! By simply changing normal.z < -0.9 as mentioned in the comments, the issue is resolved.

Here's the updated working code:

for (var face in mesh.geometry.faces) {
    if (mesh.geometry.faces[face].normal.< -0.9) {                   
        mesh.geometry.faces[face].materialIndex = 5;
    }
}

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

Identify the visitor who accessed the page using PHP

I searched extensively for a solution to this query, but came up empty-handed. My situation involves a PHP-generated page that creates a JSON string. This string is then retrieved by javascript/jQuery/AJAX. I am faced with the challenge of displaying a m ...

Resolving issues with JavaScript caused by Polymer updates

I am a novice when it comes to working with Polymer. From what I have gathered, there seems to be compatibility issues with Mozilla and Safari. After researching on StackOverflow, I found that adding addEventListener('WebComponentsReady', funct ...

Utilizing D3.js to selectively apply the nest.key() function to certain elements within an array

I have a CSV file containing hierarchical tree data as shown below: industry,level1,level2,level3,name Ecommerce,Web,,,Rakuten Ecommerce,Crowdsourcing,,,Lancers Social,Photo sharing,Deco apps,,Snapeee Social,Photo sharing,Deco apps,Collage apps,DecoAlbum ...

Real-time data feeds straight from JSON

Currently, I have a JSON file that is generated dynamically and it contains match information along with a unique id. This JSON data is categorized into live, upcoming, and recent arrays. Being new to Javascript, I am unsure about the best approach to crea ...

Are you looking for a way to prevent the default behavior of an event in angular-ui-router 1.0.x? Check out

Recently, I made a change in my code where I replaced $stateChangeStart with $transitions.onStart $rootScope.$on('$stateChangeStart', function(e, ...){ e.preventDefault(); // other code goes here... }); Now, the updated code looks lik ...

using the information from the child array within a v-if condition

I'm struggling to extract data from a child array and utilize it in my v-if condition. Below are my data and code. Any assistance would be appreciated, even if it's just pointers to the right documentation. <div class='post' v-for= ...

What is the best way to code a JavaScript function that changes the labels on a mat-slider when using a range

As a newcomer, I am currently working with a mat-slider and trying to modify the label based on the range selected. However, I'm unsure how to write JavaScript code for different labels. Can anyone provide me with assistance? Here is the mat-slider th ...

Customize the MUI Select component with unique item children

I am currently working on developing a custom Select component that involves mapping through a list of items with specific types. Each item is supposed to display a certain MUI icon based on its type. To achieve this, I have created separate components for ...

Navigating Information in the Response - Node.js

Currently, I am working on a node.js project that has grown significantly with numerous endpoints. As the project continues to expand, modifying the data within the responses for each endpoint has become quite cumbersome. I find myself constantly navigatin ...

api for enhancing images in Laravel app through preview, enlarge, and zoom functionalities

As I work on my website, I aim to display images in a compact space, such as within a 300x300 <div>. What I envision is the ability for users to preview or enlarge these images upon clicking, allowing for a closer and more detailed view. For exampl ...

Using NodeJS, pull JSON data from a JavaScript file and render it in a route file

I am currently utilizing Nodejs Express to work with a script that generates an array of objects from the Google API. My objective is to integrate this JSON data into my templates. How can I invoke the function within my script file from my route file? Be ...

Display file name in Vue.js event.target.files

I am currently working on implementing an upload feature in my web application using vue 2. Here is a snippet of the code: <label class="btn btn-xs btn-primary"> <input type="file" name="attachment[]" @change="onFileChange" multiple/> ...

Obtain the identification numbers from the row within the table

I have a table displaying an email Inbox (see excerpt screenshot here). When the user clicks on a checkbox, I need to populate two dropdowns with the correct items. function fnHandleSelectCBClick(cb) { try { var tableRow = $(cb).parent().par ...

Using Axios in a Vue component to modify a user's settings

I'm currently working on updating a preference, with the UI example attached below. The default option is set to yes, but I would like to give users the choice to select no as well. I feel like I may be missing something here, so any guidance or assis ...

Transforming an Array into a String and Extracting a Substring

I started with a numeric array in JavaScript and converted it to a string using the join method. However, when attempting to extract a substring using substr, I encountered a technical issue. Can someone please assist me with this problem? var array = [85 ...

Unusual phenomenon of child unordered list interacting with parent list item's animation

http://jsfiddle.net/RedKnight91/Z6Ueu/4/ Hey there! Take a look at the last menu (at the bottom). When you hover over one of the LIs with the "+" symbol that have children UL, which is a submenu, the slideToggle displays the child UL, but after the animat ...

Fade in and out a Div with a distinct class and ID

I'm currently experiencing a minor issue with some jQuery code. Below are some divs: <div class="add" id="1">Follow</div> <div class="added" id="1">Following</div> <div class="add" id="2">Follow</div> <div clas ...

Implementing tether in webpack: A step-by-step guide

My webpack application, which I am using with Laravel Elixir, includes a 'bootstrap.js' file for initializing all libraries. Here is the content of the file: window._ = require('lodash'); /** * We'll load jQuery and the Bootstra ...

Adding additional elements to a div in a horizontal orientation

I am currently working on a project where I need to display bars and restaurants based on specific filter criteria. I have a container div called .resultsContainer where all the results will be displayed. While I can easily append more divs (.barContainer) ...

React-Select for Creating a Dynamic Multi-Category Dropdown Menu

I am looking to incorporate react-select into my project for a multi-category dropdown list. Specifically, I need the ability to select only one option at most from each category. To better illustrate this requirement, consider the following example wher ...