Three.js: It seems that THREE.WebGLRenderer detected the image is not a power of two, originally sized at 1600x900. It was resized to 102

As I dive into learning three.js, one of my goals is to incorporate a 16x9 photo into my scene.

Below is the snippet of code where I add an Array of images to my scene:

  const material = new MeshBasicMaterial({
    map: loader.load(images[i]),
    transparent: true,
    opacity: 1,
  });
  const plane = new Mesh(new PlaneGeometry(imageWidth, 45), material);
  plane.overdraw = true;
  plane.position.x = i * (imageWidth + imageOffset);
  plane.position.y = 0;
  this.introImages.push({
    obj: plane,
    round: 1,
  });
  this.introImagesGroup.add(this.introImages[i].obj);

A console warning has surfaced:

THREE.WebGLRenderer: image is not power of two (1600x900). Resized to 1024x512

The literature suggests that texture dimensions should be the power of two for optimization purposes. This leads me to question if my current approach to adding images to the scene in three.js is the most optimal, or if there are alternative methods for incorporating non-power of two images?

Answer №1

If you prefer not to compromise the quality of your images by scaling them down with THREE.js, it's best to scale them up to the next ^2 resolution. There are two approaches you can take:

  1. You can enlarge your image and export it at a ^2 resolution 2048 x 1024 using your preferred photo editing software.

  2. Alternatively, you can dynamically create a 2048 x 1024 canvas, draw the scaled-up image onto it, and utilize this canvas as the source for your texture:

    var imgURL = "path/to/whatever.jpg";
    
    // Create image element
    const image = document.createElement('img');
    image.src = imgURL;
    
    // Once image is loaded
    image.onload = () => {
        // Create canvas and set ^2 dimensions
        var canvas = document.createElement('canvas');
        canvas.width = 2048;
        canvas.height = 1024;
    
        // Draw image onto canvas, scaled to fit
        var ctx = canvas.getContext('2d');
        var ctx.drawImage(image, 0, 0, 2048, 1024);
    
        // Create texture
        var texture = new THREE.Texture(canvas);
    };
    

You can then assign the texture variable to the desired material, ensuring Three.js functions smoothly without issues.

Edit:

To avoid texture resizing, simply adjust your texture.minFilter to THREE.NearestFilter or THREE.LinearFilter, which will prevent engine warnings. However, bear in mind that this may lead to grainy or aliased textures when scaled down, as they will not be Mipmapped.

The outcome could either be favorable or unappealing, depending on your project. You can observe the effects of using NearestFilter in this sample:

Answer №2

Is there a different way to add images in a scene in three.js that don't necessarily need to have dimensions that are a power of two to be optimized for memory storage?

In order to use mipmapping in WebGL 1, textures should have dimensions that are a power of two. However, if you set the .minFilter property of your canvas texture to THREE.LinearFilter, you can eliminate this requirement. Keep in mind that mipmapping is not always needed for every situation.

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

Is it possible to align a div on the same line with an h1 header that spans multiple lines using CSS?

I am currently working on the following code: <h1><span>Test Heading This is the text that I want to display on the right side. This is the text that I want to display on the right side. This is the text that I want</span></h1> < ...

Display the initial page and activate the first navigation button when the page loads

Greetings to everyone. I am new to the world of jquery and currently in the process of learning. This is my script: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"> </script> <script> $(function () { $ ...

Deconstructing JavaScript scripts to incorporate HTML5/CSS3 functionality for outdated browsers such as Internet Explorer

I have been researching various resources and guides related to different scripting libraries, but none of them seem to address all the inquiries I have regarding performance and functionality. With so many scripts available, it can be overwhelming to dete ...

Incorrect handling of Unix timestamps in AngularJS

I'm facing an issue with timestamps. Let me explain - I have a timestamp coded as 1378028575, which corresponds to Sun, 01 Sep 2013 09:42:55 GMT according to this website. The problem arises when I attempt to format this timestamp using Angular's ...

Ways to eliminate the worldwide model in SAPUI5

I've been attempting to break down the global model without success. I have a filter button that's set up like this: navToSecond : function (oEvent){ var oObject = this.getView().byId("inp").getValue(); sap.ui.getCore().setModel( ...

Are there advantages to incorporating d3.js through npm in an Angular 2 TypeScript project?

Summary: It is recommended to install d3.js via npm along with the typings. The accepted answer provides more details on this issue. This question was asked during my initial stages of learning Angular. The npm process is commonly used for tree-shaking, pa ...

Strange symbols were stored in the database after saving the textarea value

After submitting a form, text entered into a text area is being saved to my database. However, in one instance, certain characters such as • are appearing in the field. For example: • Text When retrieving the data from the database using Jav ...

Populate a div using an HTML file with the help of AJAX

In my main HTML file, there is a div element: <div id="content"></div> Additionally, I have two other HTML files: login.html and signup.html. Furthermore, there is a navigation bar located at the top of the page. I am curious to know if it i ...

Cookies are strangely absent from the ajax call to the web api - a puzzling issue indeed for Web Api users

Hello, here is the code I'm working with using Ajax: function GetCurrentUserId() { return $.ajax({ type: "GET", url: rootUrl + '/api/Common/CurrentDateAndUser', dataType: 'json', ...

Troubleshooting: Issues with updating a text field in Vue test utils using Jest

Hello, I am new to Jest and unit testing. I have a question about how to set the value of a text input using Vue Test Utils. Here is the code for my custom text input component: <input v-model="local_value" @keyup.enter="submitTo ...

Struggling to get the hang of CSS animation?

Here is a code snippet that I am using: //Code for animating skills on view document.addEventListener("DOMContentLoaded", function(event) { function callback(observations, observer) { observations.forEach(observation => { if (observati ...

Store the output of a mysql query in a variable for future reference

As I work on developing a backend API for a private message system, I have encountered a challenge with one of my functions. The issue arises when I attempt to store the result of an asynchronous call in a variable for future use. Here is the code snippet ...

Transforming a series of intervals into a condensed list

I am currently working on transforming a Legend list that was initially based on a range (1-2=blue, 2-3=red, etc.) into a flat list of words ("blah"=blue, "peek"=red). The challenge I am facing is finding a way to retain the numerical system while replaci ...

Setting up paths to bypass authentication on an Express website

Currently, I am in the process of developing an application using node and express. I have implemented a passport authorization middleware for all routes as part of my highly modular approach to building the app. One challenge I have encountered is when tr ...

Issues arising during the initialization of Tinymce

After setting the editor on the frontend, I encountered an issue with initializing tinymce. Here is my code: <head> <script src="//cdn.tinymce.com/4/tinymce.min.js"></script> </head> <body> <a class=pageedit>Edit< ...

Obtain the URL for making an asynchronous request using PHP and SQL

I'm encountering some issues with a website I am attempting to update. A script named "jquery.script.js" is called in the head section containing some code. $.ajax({ url: 'function.js.php?option=urlget&id='+movie_id, ...

I encountered a blank page issue when incorporating ui.bootstrap into my controller within Angular

After attempting to utilize angular bootstrap in my project, I encountered an issue where adding the dependency in my controller and starting the server with grunt serve resulted in a blank page. Take a look at the bower components in the screenshot provid ...

button for resetting the zoom in Highcharts

Attempting to manipulate the visibility of the Zoom Button on a highstock chart through x axis zooming with the navigator feature enabled. The default behavior seems to disable the button in this scenario. While there are functions allowing for display, I ...

What is the best method for linking JavaScript to Python while exchanging data in JSON format bidirectionally?

I am currently exploring methods to establish a local connection between a Python server and a Javascript client by utilizing JSON format for the retrieval of data. Specifically, I aim to execute queries on the HTML client side, transmit these queries to t ...

Web2py exhibits varying behavior between local and online versions, where it executes server code but results in a 404 error

When I run the request on my local version of the application using the code below, it successfully executes on the server. $.ajax({ type: 'POST', url: "{{=URL('default', 'serverFunction.json')}}", data: {id: id} }); Howe ...