What's the best way to add contrasting textures to both sides of an airplane?

Issue: I am attempting to create a simple poker card (just for fun) with two different images for the back and front. I have successfully created a Plane geometry with a single texture for both sides, but I am struggling to assign a texture for each side separately.

var textureBack = new THREE.ImageUtils.loadTexture( 'images/cardBack.png' );
var textureFront = new THREE.ImageUtils.loadTexture( 'images/cardFront.png' );      

var material1 = new THREE.MeshBasicMaterial( { map: textureBack } );
var material2 = new THREE.MeshBasicMaterial( { map: textureFront } );

var geometry = new THREE.PlaneGeometry( 90, 110, 1, 1 );            
geometry.faces[ 0 ].materials.push( material1 );
geometry.faces[ 1 ].materials.push( material2 );

var card = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial());

Can anyone provide assistance with this? Thank you! :)

Answer №1

Was in search of a solution that didn't involve duplicating all my geometry.

Well, here it is for all the ladies and gentlemen out there...

const materials = [new THREE.MeshBasicMaterial({map: texture, side: THREE.FrontSide}),
                 new THREE.MeshBasicMaterial({map: textureBack, side: THREE.BackSide})];

const geometry = new THREE.PlaneGeometry(width, height);

for (let i = 0; i < geometry.faces.length; i++) {
    const face = geometry.faces[i].clone();
    face.materialIndex = 1;
    geometry.faces.push(face);
    geometry.faceVertexUvs[0].push(geometry.faceVertexUvs[0][i].slice(0));
}

scene.add(new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(materials)));

BOOM! A Two Faced Plane created just like that. This loop will even work with geometries containing multiple faces, replicating each face and applying the BackSide texture accordingly.

Hope you enjoy this trick!

Answer №2

To create a 3D effect, you'll need to place two plane geometries facing each other.

Start by defining a geometry for the front side.

var geometry1 = new THREE.PlaneGeometry( 90, 110, 1, 1 );

Next, create another geometry for the back side.

var geometry2 = new THREE.PlaneGeometry( 90, 110, 1, 1 );

Rotate the second geometry by 180 degrees to position it correctly.

geometry2.applyMatrix( new THREE.Matrix4().makeRotationY( Math.PI ) );

Once you load the necessary textures and materials, assemble the meshes and attach them as children of a "card" container.

// textures
var textureFront = new THREE.ImageUtils.loadTexture('images/cardFront.png' );      
var textureBack = new THREE.ImageUtils.loadTexture('images/cardBack.png' );

// material
var material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: textureFront } );
var material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: textureBack } );

// card
card = new THREE.Object3D();
scene.add( card );

// mesh
mesh1 = new THREE.Mesh( geometry1, material1 );
card.add( mesh1 );
mesh2 = new THREE.Mesh( geometry2, material2 );
card.add( mesh2 );

If you're using WebGLRenderer, the process will be smoother.

Try it out on this Fiddle link: http://jsfiddle.net/mdAb7/11/

This code is updated for three.js version r.69

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

Fetching data from an ajax call and populating a data table

I am facing an issue with ajax and bootstrap table integration. I am making an ajax JSON call using the following method: $(document).ready(function(){ $.ajax({ url: 'php/process.php?method=fetchdata', dataType: 'json' ...

What is the best way to transmit error messages from the server to the client?

I am currently working on my express server and I have a query regarding how to efficiently communicate error messages to the client side. My goal is to display the error message both on the client console as well as on the server console, but I am uncerta ...

Tips for configuring Webstorm to automatically change double quotes to single quotes when reformatting source code

After using cmd + alt + l in Webstorm to format my JavaScript code, I noticed that double quotes are used instead of single quotes. How can I configure Webstorm to automatically change the double quotes to single quotes in my code? ...

What is the best way to transfer data from an Ajax function to a controller action?

I have a button in my view that triggers a jQuery Ajax function, with parameters fetched from my model <input type="button" value="Run Check" onclick="runCheck('@actionItem.StepID', '@Model.Client.DatabaseConnectionString', '@M ...

The loading bar animation doesn't begin at a blank slate

I'm currently working on a project that utilizes Django for the backend and Bootstrap for the frontend. I have to admit, I am quite inexperienced when it comes to front-end development; JavaScript seems like magic to me. One of the key features I nee ...

Ways to avoid scrolling on a fixed element

Here is the HTML setup I have... body .top .content The issue I am facing is that when scrolling reaches the end of the ul in the .top element, the background starts to scroll. This can be quite disorienting and makes the site slow on tablets. Even ...

Finding the orientation of a Rubik's Cube may seem like a daunting task at

Currently, I am working on developing a Rubik's Cube game in WebGL using Three.js, which you can try out here. I am facing difficulties in determining the correct axis for rotating the cube based on its current orientation. For example, when the cube ...

Locating numerous words within a given string

In my quest to identify specific words within a comma-separated log, I have encountered an issue. The current code snippet effectively locates individual words, but struggles to find all three words together in the log. $log = "Left Side Turn, Left Side ...

The Vue component with the export default directive could not be located

Whenever I try to create a dashboard component, I keep encountering the same error in my command prompt. "export 'default' (imported as 'DashboardLayoutComponent') was not found in '@syncfusion/ej2-vue-layouts' Has anyo ...

Changing the value of a variable in React Native does not reflect in the child component when passed as props

Hey there, I'm facing a bit of a dilemma with this problem. The issue is that I can't use state within navigationOptions. Here's what I've attempted: I initialized my variable let isFilterVisible: boolean = false; In the navigationOpt ...

What is the best way to create a form with two inputs that redirects me to a different page based on the inputs chosen?

Seeking assistance to resolve a challenging problem that I am currently facing. Any suggestions on the technology or backend skills needed for this task would be greatly appreciated, as well as any advice that can help me progress in the right direction. ...

Changing Image to Different File Type Using Angular

In my Angular Typescript project, I am currently working on modifying a method that uploads an image using the input element of type file. However, I no longer have an input element and instead have the image file stored in the assets folder of the project ...

What causes a React Native wrapped component to re-render consistently?

I have a functional component in React native (expo) that is displaying a page using the stack navigator. On this page, there is a simple color picker (react-native-wheel-color-picker), which is a native component, and a button that updates the state. I&a ...

The focus functionality in Angular seems to be malfunctioning

Having trouble with simple focus in Angular <div class="search pull-right tab-{{ showDetails2 }}" data-ng-click="showDetails2 = !showDetails2; showDetails = false; showDetails1 = false;searchFocus();"> html <input type="text" data-ng-model="mod ...

Searching for matching strings in jQuery and eliminating the parent div element

Here's an HTML snippet: <div id="keywords"> <div id="container0"> <span id="term010"> this</span> <span id="term111"> is</span> <span id="term212"> a</span> <span ...

Using ng-value does not trigger any updates to the Ng-model

After setting the input value Array property sum, it displays the value in the input field. However, when submitting the form, the Quantity property is not being received in the Order object. I noticed that if I change the value manually, then the Quanti ...

I'm having trouble grasping the concept of this asynchronous behavior

After reviewing the output provided, my initial expectation was for working to be displayed between the lines of begin and end. Here is the rule: Is there a possible solution to achieve this outcome without modifying the app.js file? app.js const se ...

Load jQuery to display the Bootstrap modal.ORUse jQuery to load and

My attempt to use the .load() function to display a bootstrap modal immediately on my page isn't working. The modal opens but cannot be closed afterward. The goal is to load a list of players in a modal whenever a team name with the class .team is cl ...

What is the best way to create rotational movement for an object in a-frame?

Is there a way to have my entity rotate/spin on the Y-axis? I attempted the following: <a-entity position="-1 0.5 3" animation="property: rotation; to: 0 0 0 ; loop: true; elasticity:1000; dur: 10000" rotation="90 0 0"> <a-box position="1 0 ...

Having difficulty positioning the dropdown above the other elements in the body

Below, you'll notice that the dropdown menu isn't positioned correctly over other body elements like the timer. I'm utilizing bootstrap for the dropdown and redcountdown js for the timer. https://i.sstatic.net/OO8Jm.png Here is the HTML: & ...