Exploring Textures with Three.js

I'm attempting to add a texture to the birds example in the three.js library.

  // creating a loader instance
  var loader = new THREE.TextureLoader();

  // loading the texture resource
  loader.load('imgs/birdtexture.jpg',function (texture) {
     var material = new THREE.MeshBasicMaterial( {
     map: texture,
     side: THREE.DoubleSide
  });

  var bird = new THREE.Mesh( new Bird(), material);
);

Unfortunately, my current approach doesn't seem to be working, and I'm not receiving any error messages. It appears that I might be overlooking something crucial. Can someone point out my mistake?

UPDATE: I managed to resolve the issue by implementing UV coordinates as explained here. Below is the revised code:

  var texture = new THREE.TextureLoader().load("imgs/birdtexture.jpg");
  texture.wrapS = THREE.RepeatWrapping;
  texture.wrapT = THREE.RepeatWrapping;
  texture.repeat.set( 0.05, 0.05);

  var geometry = new Bird();

  function assignUVs(geometry) {

  geometry.faceVertexUvs[0] = [];

  geometry.faces.forEach(function(face) {

    var components = ['x', 'y', 'z'].sort(function(a, b) {
        return Math.abs(face.normal[a]) > Math.abs(face.normal[b]);
    });

    var v1 = geometry.vertices[face.a];
    var v2 = geometry.vertices[face.b];
    var v3 = geometry.vertices[face.c];

    geometry.faceVertexUvs[0].push([
        new THREE.Vector2(v1[components[0]], v1[components[1]]),
        new THREE.Vector2(v2[components[0]], v2[components[1]]),
        new THREE.Vector2(v3[components[0]], v3[components[1]])
    ]);

    });

    geometry.uvsNeedUpdate = true;
    }

    assignUVs(geometry);

    var bird = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial(     {map:texture, side: THREE.DoubleSide }));

Answer №1

Check out this (not tested yet):

  // Initialize a loader
  var loader = new THREE.TextureLoader();
  // Create a material
  var material = new THREE.MeshBasicMaterial({side: THREE.DoubleSide});

  // Load an image resource
  loader.load('imgs/birdtexture.jpg',   function (texture) {
     material.map = texture;
  });

  var bird = new THREE.Mesh( new Bird(), material);
);

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

How can I transform a JSON object into a series of nested form fields?

Can anyone suggest a reliable method for converting a JSON object to nested form fields? Let's consider the following JSON object: {'a':{'b':{'c':'1200'}}}, 'z':'foo', 'bar':{&apo ...

Curious about the power of jQuery methods?

I've been coming across codes similar to this: $(document.documentElement).keyup( function(event) { var slides = $('#slides .pagination li'), current = slides.filter('.current'); switch( event.keyCode ) { ...

The AJAX call is not being identified correctly

I am facing an issue with my search result pagination wherein the page reloads instead of loading via AJAX when using the pagination. The search results are correctly loaded through partial view with AJAX, but the pagination triggers a non-ajax request cau ...

The duration required to render DOM elements

Trying to determine the best method for measuring the rendering time of DOM elements in a web browser. Any tips? I'm currently developing an application that involves significant DOM manipulation as part of comparing the performance between Angular 1 ...

Uploading files into an array using Angular 2

Struggling to incorporate an uploader within an array: I've got an array of users displayed in a table using "ng-repeat". I want to add a column with a button to upload an image for each user. Currently, I'm utilizing ng2-file-upload, but open t ...

How do I insert a new item into the tree structure using Sapui5 CustomTreeItem?

Having an issue with the CustomTreeItem in SAPUI5. I can successfully display the tree structure from JSON, but struggling to manually add data on click of the Add button. https://i.sstatic.net/BpLOK.png In the image above, when I click the + Button, I n ...

What is the best way to determine the position of a letter within a string? (Using Python, JavaScript, Ruby, PHP, etc...)

Although I am familiar with: alphabet = 'abcdefghijklmnopqrstuvwxyz' print alphabet[0] # outputs a print alphabet[25] #outputs z I am curious about the reverse, for instance: alphabet = 'abcdefghijklmnopqrstuvwxyz' 's' = al ...

Automatically use JavaScript to send an email to my email address

I have a question I'm trying to solve: Is there a way to send myself an email notification (using a different address) when a specific event occurs in Javascript or Node.js? For example: if (100 > 90) { emailto="xxxxx.gmail.com" subject="It happ ...

Is there a way to differentiate the color of an active link from an inactive one, so users can easily identify which link they are currently viewing?

How can I customize the color of text for the active page on my website's sidebar? For example, I have two hyperlinks, one for HOME and one for ABOUT. When a user clicks on the HOME link, I want the text color to change to green while the ABOUT link r ...

By implementing Async.parallel, I ensure that the lifetime of my parameter does not exceed the duration of the asynchronous calls in NodeJS when working with MongoDB

After analyzing the code and its asynchronous behavior, it appears that the 'recipeData' array may not persist long enough to handle the asynchronous callbacks. To mitigate this, I created a copy of the data in a global array. However, I am encou ...

Obtain primary picture through PHP

Is there a way to automatically extract the main image for an article from the HTML of a webpage, similar to how Facebook generates a preview when you share a link? Given that the page and URL will vary each time this function is executed, are there any l ...

Using a jQuery dialog box containing an embedded iframe

I'm attempting to utilize the jQuery UI dialog box in order to display an iframe. Unfortunately, I'm running into some issues with getting it to work. I suspect that I may have made a syntax error, possibly related to the quotation marks within t ...

Steps for converting a tsx file into a js file in React

Currently, I am in the process of a React Project and have numerous tsx files that I aim to convert for utilization as JavaScript within my project. What would be the best approach to achieve this task? ...

Using Handlebars.js to conditionally display data within an object is not functioning as expected

I am attempting to retrieve JSON data value and only display the element if the data is present. However, I am experiencing issues with Handlebar JS. var data = { listBank: [ { "enableSavedCards":"false", "enableAxisAccount":"t ...

A guide on how to successfully send multiple arguments to a single Javascript method by utilizing thymeleaf onclick (th:onclick) functionality

I attempted to use the code below, but unfortunately it did not work as expected. Can you please provide me with a solution? Javascript Code ---> function passValue(id, name){ console.log(id) console.log(name) document.getE ...

What is the best way to retrieve a specific item from an array of objects stored in JSON format using React?

I have received a json file named data.json which contains multiple objects in an array. My goal is to extract the value of a specific key from each object. To achieve this, I am utilizing react redux to fetch these values and present them in a table forma ...

Using a JavaScript variable in conjunction with an AJAX-PHP variable

When using an ajax call to refresh an external php file "commentS.php" every 3 seconds, I encountered a problem. Despite expecting the variable in the hidden input field (name="idd") to be reflected on the "commentS.php", it did not happen as anticipated. ...

Click the button in Javascript to add new content

I am currently experimenting with JavaScript to dynamically add new content upon clicking a button. Although I have successfully implemented the JavaScript code to work when the button is clicked once, I would like it to produce a new 'hello world&ap ...

The Vue select change event is being triggered prematurely without any user interaction

I am facing an issue with my Vue app where the change event seems to trigger even before I make a selection. I tried using @input instead of @change, but encountered the same problem as described below. I have tested both @change and @input events, but th ...

Error in JavaScript Slideshow

I am attempting to create a slider that changes with a button click. Below is my JavaScript code: var img1=document.getElementById("p1"); var img2=document.getElementById("p2"); var img3=document.getElementById("p3"); function slide(){ ...