Retrieve Textures From an Assortment

Greetings to all the readers here!

I am in the process of developing a website where I want to implement a feature that allows me to scroll through "cards" arranged in a single line, visible from an isometric side view.

I have managed to set up the scene using THREE.js, however, I am facing a challenge in creating a collection of elements that can be automatically used as textures for these cards.

To elaborate further, my objectives are:

  • To create and maintain an updatable collection of elements (images/videos)
  • To use each element from this collection as the texture for the front face of the boxes
  • To have the size of the boxes adjust automatically according to the aspect ratio of the image texture
  • To avoid manually linking textures to each box; instead, each new element added to the collection should create a new card or box in the scene

I would greatly appreciate any assistance or guidance on how to achieve this.

Thank you in advance for your help!

Here is the current code snippet:

import './style.css'
import * as THREE from 'three'

// Rest of the code goes here

I am willing to give it a try myself, but my knowledge of JavaScript is limited, and I am unsure where to begin with implementing this feature.

Answer №1

One idea is to create a curated collection of image URLs and dynamically load textures to generate boxes with accurate aspect ratios for each image. You could even extend this functionality by implementing a function to introduce new elements - simply by specifying a new image URL and generating a corresponding box with the texture. The function would handle loading the new texture, creating a new box, and appending the URL to the imageUrls collection as shown below:

    import './style.css';
    import * as THREE from 'three';
    const canvas = document.querySelector('.webgl');
    const scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff); 
    const ambientLight = new THREE.AmbientLight(0xffffff, 1);
    scene.add(ambientLight);


    const renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);

    const zoom = 300; // Adjust this value to control the zoom
    const sizes = {
      width: window.innerWidth,
      height: window.innerHeight
    }; 
    const aspect = window.innerWidth / window.innerHeight; 
    const camera = new THREE.OrthographicCamera(
      -sizes.width / zoom,
      sizes.width / zoom,
      sizes.height / zoom,
      -sizes.height / zoom,
      -10,
      1000
    );

 
    camera.position.set(2, 2, 5);
    camera.lookAt(0, 0, 0);

 
    window.addEventListener('resize', () => {
 
      sizes.width = window.innerWidth;
      sizes.height = window.innerHeight;

 
      camera.left = -sizes.width / zoom;
      camera.right = sizes.width / zoom;
      camera.top = sizes.height / zoom;
      camera.bottom = -sizes.height / zoom;
      camera.updateProjectionMatrix();

 
      renderer.setSize(sizes.width, sizes.height);
      renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
    });

 
    function createBoxWithTexture(texture, positionZ) {
      const aspect = texture.image.width / texture.image.height;
      const boxGeometry = new THREE.BoxGeometry(1 * aspect, 1, 0.01);
      const boxMaterial = new THREE.MeshStandardMaterial({
        map: texture,
        transparent: true,
        opacity: 0.7
     });
      const box = new THREE.Mesh(boxGeometry, boxMaterial);
      box.position.set(0, 0, -positionZ);
      scene.add(box);
    }


    const loader = new THREE.TextureLoader();
    const imageUrls = [
      'path/to/image1.jpg',
      'path/to/image2.jpg',
      'path/to/image3.jpg',
  
    ];
    imageUrls.forEach((url, index) => {
      loader.load(url, (texture) => {
        createBoxWithTexture(texture, index * 1.5); 
      });
    });


    function addNewImage(url) {
      loader.load(url, (texture) => {
        createBoxWithTexture(texture, imageUrls.length * 1.5); 
        imageUrls.push(url); // Add the URL to the collection
      });
    }

// Demonstrative usage:

addNewImage('path/to/newImage.jpg');

// Render the scene

function animate() {

  requestAnimationFrame(animate);

  renderer.render(scene, camera);

}

requestAnimationFrame(animate);

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

obtain the image number by extracting a portion of the text

How can I extract the image number from a string using the sub-string function? I have applied the sub-string function to the property below. It returns 3 at the end in Chrome, but it does not work properly in Mozilla. Issue : After "-->", when I chec ...

AngularJS factory or filter failing to update properly

I have been working on a system that manages translations in my factory. I set the language as a string and then use a filter to update the view when the language changes. Everything works fine if I define the language in the view beforehand, but when I t ...

Create compelling visual representations by utilizing Google charts to draw customized charts based on variable

I have been attempting to create a chart using Google Charts by passing some parameters to the function. I am trying to use these parameters to populate the data, but it doesn't seem to be working as expected. Is it possible to include parameters in t ...

Bootstrap carousel button that tracks the number of clicks

Unfortunately, I am facing a challenging issue with creating a click counter for the Bootstrap carousel button. The problem seems to be related to the span element for the previous and next icons. The button counter is not registering clicks on the respec ...

Contrasting .queue() with jquery.queue()

Can someone clarify the distinction between .queue() with .dequeue() and $.queue() OR jquery.queue()? If they serve the same purpose, why did jQuery provide them in two separate documentations? Could someone provide examples to illustrate their difference ...

Update the chosen option in a dropdown list with jQuery and javascript

I need to set the value of a column in a repeater so that once it's assigned, the column will display the current selection from the dropdown. I have the correct value to assign in $('#myname_' + rowIndex).text(), but when I try to assign t ...

Tips for implementing automatic line wrapping in a PDF document using React-PDF

I am experiencing an issue with a PDF rendering where very long words are not wrapping onto a new line, instead overflowing the container and disappearing off the page. Below is the setup causing this problem. The styles are listed followed by the actual ...

Discovering the wonders of Angular: fetching and utilizing data

Recently delved into the world of Angular and I've hit a roadblock. I'm struggling to grasp how Angular accesses data in the DOM. In all the examples I've come across, data is initialized within a controller: phonecatApp.controller(' ...

Transmit the identification to angularjs for the genuine content to be displayed

I have a hidden field where I store an Id, which can also be 2, 3, 4, or 59. I need to send this Id from the hidden field to my opgaver.js file so it can download the content. However, I am facing difficulty in figuring out how to pass the Id to the opgav ...

In Three.js, FBX bones exhibit smooth rotation, but GLTF bones display strange rotation behavior

Currently, I have successfully implemented a dynamic model that works with an FBX file using the Three.js FBXLoader. However, for convenience sake, I decided to switch to using a GLTF/GLB file as it contains textures within the same file. To achieve this, ...

Using dynamic tag names within React JSX can greatly enhance the flexibility and

I'm working on creating a React component for HTML heading tags (h1, h2, h3, etc.), where the heading level is determined by a prop. Here is how I attempted to approach it: <h{this.props.level}>Hello</h{this.props.level}> My expected out ...

jquery events such as onSubmit, onComplete, etc. are commonly used

It's interesting to observe that many tools in jQuery, especially those involving ajax, contain hooks like onSubmit, onComplete, and more. Is there a specific term for this practice and the standards associated with it? Does this practice belong sol ...

Can a SVG Animation be created featuring a progress ring with both the right and left dash offsets moving simultaneously?

Currently in my codepen, I am working on a project where I am using GSAP. If you are unfamiliar with that, no worries - just focus on the dashoffset and dasharray in the SVG circulation path. You can find this project in Codepen which is part of our premi ...

javascript: update hidden field if date is past January 15th

Having a bit of trouble with this one after a full day! The form contains a hidden field called 'grant_cycle'. If the form is submitted after January 15th, it should have the value 'Spring, [year]', or if after July 15th, it should be ...

Guide to using Vue.js and Vue Router to create a sub menu for the children of the current route

I am currently working on customizing the navigation for my Vue application. My goal is to create a main menu at the top that displays all the root level routes, and a side menu that appears when navigating through child routes. Here is an example of what ...

What is the best way to implement an AppBar that fades in and out when scrolling within a div?

I'm trying to implement a Scrollable AppBar that hides on scroll down and reappears when scrolling up. Check out this image for reference To achieve this functionality, I am following the guidelines provided by Material-UI documentation export defa ...

How to make an HTTPS REST API request in Node.js with JSON body payload

Currently, I am attempting to make a secure HTTPS REST request in Node.js by utilizing the following code: var querystring = require('querystring'); var https = require('https'); var postData = { 'Value1' : 'abc1&ap ...

Send out Chrome Desktop notifications to reach all users

I recently integrated Chrome Desktop notifications into my website by following the steps outlined in this helpful guide: Chrome desktop notification example After adding the script code to my site, I was able to get it working smoothly. However, I encou ...

What is the process for the event loop moving into the poll phase?

There is a scenario outlined in the event loop explanation on the Node.js official website. When setTimeout is triggered, and the callback queue for the timer phase isn't empty, why does the event loop move on to the poll phase? The site mentions that ...

Encountering an issue while trying to pass hidden value data in array format to the server side

I am currently learning how to use Handlebars as my templating engine and encountering an issue with passing over data from an API (specifically the Edamam recipe search API). I am trying to send back the array of ingredients attached to each recipe card u ...