The direction of view decides the reversal of Three.js Materials

Working on my latest project using Three.js to create a virtual model of a house. The main purpose is to showcase the design to an architect for further discussion. The interactive aspect allows users to walk through the home virtually on this website. To navigate, use arrow keys for horizontal movement relative to the grass, W/S keys for up/down, and A/D keys to change the viewing angle.

Check it out here

One issue I encountered was the inconsistency in texture appearance on walls depending on the viewing angle. This discrepancy became apparent when comparing the external view to the internal view as shown in the image below. It seems like there's a specific problem with how I set up the panels, causing this visual distortion. My goal now is to find a solution to ensure the textures display consistently across different viewpoints.

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

I believe the key lies in modifying the material settings for the panels. Below is a snippet of the code where I define materials and apply them to create the panels within the scene:

 
var materialArray = new Object();

function makeMaterial2(filename, repeatX, repeatY)
{
  var m = new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( filename ), transparent: true, opacity: 0.9, color: 0xffffff }); 
  return m;
}

materialArray["rDoor"] = makeMaterial2("doorRight.png",false,false);
materialArray["crDoor"] = makeMaterial2("doorRightClapboard.png",false,false);

function drawPanel2(x,y,z,x2,y2,z2,str)
{
  var cubegeometry = new THREE.BoxGeometry(Math.abs(x-x2),Math.abs(y-y2),Math.abs(z-z2));
  var cube = new THREE.Mesh(cubegeometry, materialArray[str]);
  cube.position.set((x+x2)/2,(y+y2)/2,(z+z2)/2);
  return cube;
}

scene.add(drawPanel2(-10,level,front,0,level+height,front,"rDoor"));
scene.add(drawPanel2(-10,level,front+margin,0,level+height,front+margin,"crDoor"));

Answer №1

When using a basic BoxGeometry with standard UV texture coordinates, any texture applied will look the same on all sides (see box example). However, if you want the open space of a door to appear in a specific position, there are several ways to achieve this:

  1. Assign different materials and textures to each side of the box. The 6 sides of the BoxGeometry are already set up for multiple material usage.

    a) Flip a texture using an image editing software and save it as a separate file. Then load these flipped textures individually.

    b) Clone the texture and use

    texture.wrapS = THREE.RepeatWrapping; texture.repeat.x = - 1;
    to flip it horizontally (Learn how to flip a Three.js texture horizontally).

    Create an array of 6 materials, each with its corresponding texture, and apply them to the Mesh.

  2. Adjust the UV texture coordinates of the BoxGeometry so that one side displays the texture flipped.

  3. If your boxes are flat and only have visible sides, consider creating a PlaneGeometry instead of a BoxGeometry. Set material.side: THREE.DoubleSide to make the plane visible from both sides. With this method, you'll need to modify your drawPanel2 function to properly orient the panels on the plane rather than flattening one side of a geometry.

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

Leveraging the wheelDelta property in the onmousewheel event handler with Script#

When working with Script#, I often utilize mouse events using the ElementEvent argument. However, one thing seems to be missing - the WheelDelta property for handling the onmousewheel event. Can anyone provide guidance on how to access this property in t ...

Changing the background color of radio buttons using Chrome and the power of WebGL<script>canvas</script>

When using the Three.js WebGL renderer, I noticed that radio buttons have white backgrounds, but this issue only seems to occur in Chrome. If you want to see the problem for yourself, check out this fiddle: http://jsfiddle.net/5pL8v/328/ Does anyone know ...

Sending data from Ajax to PHP

I've gone through all the previous examples without success, and now I am facing a simple php question. You can find the example here. My goal is to click on a table and have the options displayed as follows: When I explicitly declare the table name ...

How can I format the input type number with a thousand separator as 123.456.789?

When entering the number 123456, I want to see 123.456 displayed in the input field. I tried using my code, but it displays 123,456 instead. Is there a way to ensure that the thousand separator is a dot and not a comma? You can view my code at this link ...

Information is only displayed within the Node Request function and is not accessible to

I am currently developing a small web scraping tool using Node (Express) to search URLs from a list. However, I'm encountering an issue with accessing the results of the search outside of the request callback function in a forEach loop. Can anyone hel ...

Utilizing AJAX in Wordpress to Dynamically Update HREF Links

My website now has AJAX functionality, and you can see a live example at www.mathewhood.com. I am interested in changing the URL format when clicked from To something like , without the /sitefiles/ for security reasons. Below is my code. If anyone is ex ...

Personalizing the dimensions of audio.js

I recently integrated the audio.js plugin into my website. I've added the necessary code to the header.php file located in the includes folder. <script src="audio/audiojs/audio.min.js"></script> While the player appears correctly on othe ...

Having trouble with JSON/jQuery syntax?

I am attempting to populate a set of DIVs with image backgrounds by reading images from a specific folder. The jQuery code I have written is shown below: $.getJSON('../functions.php', function(images) { images.each( function() { $('#m ...

The code seems to be malfunctioning in a separate JS file, but oddly enough, it functions properly when placed within a <script> tag

I am trying to create a loader, but I have encountered an issue where the script works when placed directly in the HTML file, but not when it is in a separate JavaScript file. Here is the script: var loader = document.getElementById("ld"); w ...

Tips for generating a dynamic Array name to be sorted with React JS

My lack of experience is causing some issues for me. I am currently working on a form in react where the user has to select two values first. Based on these two values, a third value will be available for selection. However, the options for this third val ...

Designating a class for the main block

Having an issue with a slider in uikit. When the slide changes, the visible elements also change their class to uk-active. I'm trying to select the central element from the active ones but css nth-child doesn't work. Any suggestions on how to do ...

Postpone reloading automatically if a division is in view

I endeavored to develop a small project aimed at monitoring flights passing over my vicinity. Utilizing the flight-radar24 API python package, I managed to extract the necessary data. Subsequently, I designed a systemd service to execute my python script ...

Tips for implementing a confirmation dialogue box prior to submitting a form:

As a new developer, I am facing an issue that I believe you can help me with. I want to display a confirmation box before deleting. How can I achieve this in my Ajax code? HTML: <input type='button' class="btn btn-danger delete_button" id="de ...

Refreshing CKFinder Form Field with jQuery

Looking to update the value of an input field .ckfinder-input using CKFinder's pop-up image selector. Everything runs smoothly until attempting to assign the selected image URL to the input field with input.val() = fileUrl, resulting in the error mess ...

Transition between different segments

Currently working on a sapui5 fiori app, I find myself utilizing multiple fragments based on the mode (read, create, edit). However, I am stuck on how to smoothly navigate from one fragment to another, and more importantly, how to seamlessly return to th ...

How to avoid breaks and edges when incorporating texture into a sprite sheet

I've been working on rendering a grid of tiles using textured quads from a sprite sheet. However, I've encountered a problem where there are small gaps between the tiles once they've been rendered: After trying to fix this issue by changing ...

How can information be exchanged between PHP and JavaScript?

I am working on a concept to display a graph of someone's scores within a div element. The process involves a button that triggers the graph drawing function upon clicking. Additionally, there is a data retrieval function that retrieves information fr ...

Error in overwritePermissions caused by a bug

message.guild.channels.create(`ticket-${message.author.username}`).then (channel => channel.overwritePermissions(Support, { SEND_MESSAGES: true, READ_MESSAGES: true ...

Exploring ReactJS: Utilizing the useEffect Hook for Retrieving Various Data Sources

I have recently started working with react and currently have a function that fetches data in my useEffect hook. I am using a loading state to handle component rendering, and then populating my state component with the fetched data successfully. However, ...

Customize the background color of MaterialUI KeyboardDateTimePicker without altering the padding and layout of the component

Is it possible to alter the background color of KeyboardDateTimePicker components without affecting the padding and layout? I attempted to set the background property to style={{background: "rgb(232, 241, 250)"}}, but when combined with inputVari ...