Displaying a single image on various surfaces of a BoxGeometry in Three.js

I am currently utilizing three.js to generate 3D Objects. My goal is to develop a 3D canvas similar to the one showcased here in my project. I aim to display a single image on all sides of the object (box) except for the back.

I came across a relevant example at: .

My plan involves using BoxGeometry along with 2D faceVertexUvs (custom alignment) to cover surfaces. Is there an easier way to achieve this without working with Vector2 (2D)? Can I utilize Vector3 (3D) or specify pixels for easier management?

Since I am new to Three.js, any suggestions for a better approach would be greatly appreciated.

Answer №1

An example showcasing the use of BoxBufferGeometry:

body {
  overflow: hidden;
  margin: 0;
}
<script type="module">
import * as THREE from "https://threejs.org/build/three.module.js";
import {OrbitControls} from "https://threejs.org/examples/jsm/controls/OrbitControls.js";

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 1, 100);
camera.position.set(0, 0, 10);
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);

var controls = new OrbitControls(camera, renderer.domElement);

var geometry = new THREE.BoxBufferGeometry(10, 5, 1);
var uvs = geometry.getAttribute("uv");

// Setting UV coordinates for different faces
uvs.setXY(0, 1, 1); // +x
uvs.setXY(1, 0.9, 1);
uvs.setXY(2, 1, 0);
uvs.setXY(3, 0.9, 0);

uvs.setXY(4, 0.1, 1); // -x
uvs.setXY(5, 0, 1);
uvs.setXY(6, 0.1, 0);
uvs.setXY(7, 0, 0);

uvs.setXY(8, 0, 0.8); // +y
uvs.setXY(9, 1, 0.8);
uvs.setXY(10, 0, 1);
uvs.setXY(11, 1, 1);

uvs.setXY(12, 0, 0); // -y
uvs.setXY(13, 1, 0);
uvs.setXY(14, 0, 0.2);
uvs.setXY(15, 1, 0.2);

uvs.setXY(20, 0, 0); // -z ("back")
uvs.setXY(21, 0, 0);
uvs.setXY(22, 0, 0);
uvs.setXY(23, 0, 0);

var material = new THREE.MeshBasicMaterial({map: new THREE.TextureLoader().load("https://threejs.org/examples/textures/uv_grid_opengl.jpg")});
var object = new THREE.Mesh(geometry, material);
scene.add(object);

renderer.setAnimationLoop(()=>{
  renderer.render(scene, camera);
});
</script>

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

Controller encounters a error when requiring a module

Struggling to set up Stripe for my app, I've encountered some issues with the module implementation. Typically, I would require a module at the top of the file to use it. However, in the paymentCtrl file, when I do this, it doesn't work and I rec ...

What is the most effective way to implement a single modal throughout my web application without having to duplicate HTML code on each page?

After realizing I was repetitively adding the same div to every page for a modal dialog, I decided to place a single div in the site.master page and call it when needed. This method worked fine until I began implementing ajax with partial page updates. H ...

What is the correct way to properly wrap the div component in this code snippet?

I am currently working on implementing a Javascript component that displays pinned locations and related information on a map. I have made some progress with the implementation, but unfortunately, it does not appear correctly in the actual site. There is a ...

Issue with MomentJS inBetween function consistently displaying incorrect results

I'm currently working on Vue Datatable and I have a specific requirement to search for records between two dates. I am using the moment.js library and the inBetween function to achieve this. Strangely, when I hardcode the dates, the function returns t ...

Is there a way to update the input box value with a variable using jquery?

I am currently facing an issue with changing the value attribute of an input box in a form using jquery. Although I am able to change the value, it does not reflect in the outer html. Below is my current code snippet: $("a").click(function(event) { va ...

Struggling to implement a Datepicker in the date column of a table created using HTML and JavaScript

I am encountering an issue while creating a table in html and javascript. I am unable to use a Datepicker in the date column when inserting a new row, even though it works fine when using in normal html code. Below is the javascript function: //Script fo ...

Retrieving Remote Headers in Loopback.io Inbound HTTP Method

In my remote method, I have placed the application logic as shown below: module.exports = function(Entity) { HcpEntity.retrieveProfile = function(body, cb) { process.nextTick(function() { //TODO: Add Application Logic here } } } Also, he ...

How to append a JSON object to an existing .json file

UPDATE: Despite successfully executing the PHP code, my JSON file remains unchanged. I must apologize in advance for covering old ground, but I have spent countless hours exploring different solutions with no success. Perhaps sharing my challenge could as ...

The onload event for embedding SVG files is not functioning as expected

I created an angular directive: <div> <embed ng-src="{{url}}" id="map" type="image/svg+xml/> </div> I am trying to trigger a function when the svg is fully loaded. To achieve this, I have added an onload action listener durin ...

What methods can I employ with JavaScript to catalog data collected from an HTML form?

Currently, my form requires users to input a username that cannot start or end with a period (.). I have implemented some code but I believe there is an issue with the .value[0] parts. //Checking Username if (document.getElementById("uName&quo ...

Organizing form data using Vue

One of the features of my code is the ability to create dynamic input and select elements: <div v-for="(index) in rows"> <select> <option selected="true" disabled>Select Type</option> <option>Name</opti ...

Tips for locating all events linked to a specific text box using JQUERY

I'm currently encountering a major issue with textboxes on a page that I've been tasked to update. Whenever I attempt to input text into the textboxes, the span element next to them disappears. My application is built using ASP.NET and relies on ...

Tips for capturing a jQuery trigger in traditional JavaScript

I am trying to trigger an event using jQuery and I want to bind to it in a non-jQuery script. It appears that while I can bind to the event in jQuery using on, I am unable to do so with addEventListener. Check out this jsFiddle for a demonstration: http: ...

What could be the root cause behind the error encountered while trying to authenticate a JWT?

I've been working on integrating a Google OAuth login feature. Once the user successfully logs in with their Google account, a JWT token is sent to this endpoint on my Express server, where it is then decoded using jsonwebtoken: app.post('/login/ ...

What is the best way to iterate through arrays in javascript?

Could someone please assist me with looping through an array of variable sizes? Here is the array: var x = [[1,2,3],[8],[10,11,12],[13]]; I am trying to create combinations in a new array. For example: y = [[1,8,10,13],[2,8,10,13],[3,8,10,13], [1,8,11,1 ...

Is it considered fashionable to utilize HTML5 data attributes in conjunction with JavaScript?

Currently, I am utilizing HTML5 data attributes to save information such as the targeted DOM element and to initialize events using jQuery's delegation method. An example of this would be: <a href="#" data-target="#target" data-action="/update"> ...

Set the android camera resolution to a lower setting automatically when a user attempts to upload a file from their browser

In my current application, I have implemented a feature that allows users to upload files. When the user clicks on the input field, they are presented with options to either select a video from their gallery or open the camera to record a new video and u ...

The animation unexpectedly resets to 0 just before it begins

Currently, I am developing a coverflow image slider with jQuery animate. However, there are two issues that I am facing. Firstly, when the animation runs for the first time, it starts at `0` instead of `-500`. Secondly, after reaching the end and looping b ...

Issues with Braintree webhooks and CSRF protection causing malfunction

I have successfully set up recurring payments with Braintree and everything is functioning properly. Below is an example of my code: app.post("/create_customer", function (req, res) { var customerRequest = { firstName: req.body.first_name, lastN ...

Is there a way to determine if a div is positioned 100px from the bottom edge?

When using the sticky class to stick a div, I encountered an issue where it sticks correctly from the top but overlaps the footer at the bottom. https://i.sstatic.net/28A3A.jpg Here is the HTML code: <div class="tab-content"> <div role="tab ...