Is it possible to use an image loaded as a texture to create another texture mipmap in three.js?

This is not a problem, it's simply a question.

Currently, I am working on a terrain project using an atlas texture. In order to reduce color bleeding, I am planning to create mipmaps externally and then apply them to the terrain texture.

At the moment, I am utilizing...

terrainTexture = THREE.ImageUtils.loadTexture 

...to load each mipmap level and combine them with the base texture at level 0...

terrainTexture.mipmaps[mipsTexs[tex.sourceFile]] = tex.image;

...where mipsTexs is an array that helps me keep track of the position of each texture.

The issue I'm facing is that when I manually feed in the mipmaps, the terrain appears all black, while using WebGL-generated mipmaps results in colored terrain with color bleeding artifacts.

In the examples provided, there is only one instance of manual mipmapping, but the mipmaps are created as canvases. I am wondering if the all-black appearance could be due to using JS Images instead of Canvases.

Answer №1

Check out this demonstration on creating custom mipmaps:

function createMipmap( size, color ) {

    var canvasImage = document.createElement( "canvas" ),
    context = canvasImage.getContext( "2d" );

    canvasImage.width = canvasImage.height = size;

    context.fillStyle = "#222";
    context.fillRect( 0, 0, size, size );

    context.fillStyle = color;
    context.fillRect( 0, 0, size / 2, size / 2 );
    context.fillRect( size / 2, size / 2, size / 2, size / 2 );
    return canvasImage;

}

var mainCanvas = createMipmap( 128, '#f00' );
var textureCanvas = new THREE.Texture( mainCanvas, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
textureCanvas.repeat.set( 100, 100 );
textureCanvas.mipmaps[ 0 ] = mainCanvas;
textureCanvas.mipmaps[ 1 ] = createMipmap( 64, '#0f0' );
textureCanvas.mipmaps[ 2 ] = createMipmap( 32, '#00f' );
textureCanvas.mipmaps[ 3 ] = createMipmap( 16, '#400' );
textureCanvas.mipmaps[ 4 ] = createMipmap( 8,  '#040' );
textureCanvas.mipmaps[ 5 ] = createMipmap( 4,  '#004' );
textureCanvas.mipmaps[ 6 ] = createMipmap( 2,  '#044' );
textureCanvas.mipmaps[ 7 ] = createMipmap( 1,  '#404' );
textureCanvas.needsUpdate = true;

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

Can the color of text be adjusted (to either white or black) based on the background color (in any color and format)?

To achieve a text color that contrasts well with any background, I need to make sure it's either black or white. The background in my app can vary in color and format, so finding the perfect solution has been challenging. Using mix-blend-mode doesn&a ...

Incorporating a separate PHP file which includes JavaScript code

I've been struggling to make this code work. In my footer file, I have the following: footer.php: <script type="text/javascript"> $(document).ready(function(){ $.ajax({ url: '', type: 'GET', dataType: "script", success: ...

`In NestJS Nested Schema, the @Prop decorator and mongoose options are not applied as expected

I'm currently working on constructing a Schema that includes a nested object. I am trying to define default values and required properties within the nested object, but it seems like the options I set are being ignored. task.entity.ts @Schema() expor ...

Strengthening JavaScript Security

Throughout the past few years, I have delved into various javascript libraries like Raphael.js and D3, experimenting with animations sourced from different corners of the internet for my own learning. I've obtained code snippets from Git repositories ...

Map Infobox appearing on page load

I am working on a Google map project where markers and info boxes dynamically populate when the user clicks on the map. However, I now need the info box to be loaded initially when the map loads, without requiring a click event. I have tried writing some c ...

How to use a string condition to filter a list of objects in Javascript?

Below is the structure of the object: var objList = [ { "age": 19, "valueField": 34, "booleanField": false }, { "age": 15, "valueField": 5, "booleanField": false }, { "age": 22, "valueField": 17, "booleanField": true } ]; Given the condition ...

Transforming the output of a MySQL query into a JSON format organized like a tree

This question has been asked before, but no one seems to have answered yet. Imagine a scenario where a MySQL query returns results like this: name | id tarun | 1 tarun | 2 tarun | 3 If we were to standardize the JSON encoding, it would l ...

Automatically resize ui-select dropdown box to the left side

I've been tasked with incorporating AngularJS ui-select into my project, specifically creating a multiselect dropdown. Currently, the dropdown box automatically adjusts its width based on the length of the longest selectable text, causing it to extend ...

Can I use a single component for all routes in NextJS?

Just starting out with NextJS and facing a simple problem: I'm wondering if it's possible to achieve the following setup using NextJS // with react-router-dom <Router> <> <Header /> <Switch> & ...

Tips for effectively creating and appending elements using JavaScript

Code Sample var result=$.getJSON("http://search.twitter.com/search.json?callback=?",{q:query}, function(data) { ... question. }); Query: In order to display each tweet result, I want to ...

Error encountered: AngularJS routes causing 500 internal server error

I am struggling with organizing my directory structure. Here is how it currently looks - -project -public -app -app.js ( angular app module ) -server -server.js ( node root js file ) -includes -layout.jade - ...

To activate two separate click events, one for an absolute element and another for the element positioned behind it, simply click on the desired absolute element to trigger both actions

Is it possible to trigger click events for both of these elements? position: relative container ELEMENT 1: standard div ELEMENT 2: position: absolute div In real life, element 2 is a transparent backdrop designed to capture clicks on top of the header ...

JavaScript constants implemented in a script file

Similar Question: Passing a PHP string to a Javascript variable (and escaping newlines) I recently completed a project, but now I need to make one more modification by adding language constants in my JavaScript files. For example, if we include a Jav ...

I prefer not to have the entire URL visible to me

function updateDate(day, month, year) { month += ""; if (month.length <= 1) month = "0" + month; document.location.href = "<?php $_SERVER['PHP_SELF'];?>?page=events&day=" + day + "&month=" + m ...

The debounced function in a React component not triggering as expected

I am facing an issue with the following React component. Even though the raiseCriteriaChange method is being called, it seems that the line this.props.onCriteriaChange(this.state.criteria) is never reached. Do you have any insights into why this.props.onC ...

When attempting to send coordinates to the Polyline component in React Native, an error is encountered stating that NSNumber cannot be converted to

Trying to pass coordinates from BackgroundGeolocation.getLocations() is causing an issue. Upon passing the coordinates, I encounter the following error message: JSON value '-122.02235491' of type NSNumber cannot be converted to NSDictionary I ha ...

Include a class in the <code> element if it is not nested within a <pre> tag

I am incorporating the Highlight.js script on my website built with DocPad. I am looking to enhance the appearance of a basic <code> tag (equivalent to `` in Markdown), but this conflicts with the existing styles applied by Highlight.js. Highlight. ...

Do multiple AJAX calls share parameters?

I have a JavaScript function as shown below: function makeAjaxCall(outerParameter1, outerParameter2, outerDivId, paramInput){ $.ajax({ type: "POST", url: "some time taking LargeWebMethod or URL", //may take some time to return output dat ...

The Data from Req.Body Doesn't Appear After Adding Form Fields

I've been struggling to find a solution to this issue, but here's the gist of it: I have a button that allows users to add a question and answer pair one at a time by clicking on an "Add Question" button. This is achieved through the append featu ...

The error message "Unable to map props.theTodos" is displayed

I'm facing an error message while compiling: "TypeError: props.theTodos.map is not a function". I've been struggling with this issue for quite some time, but I haven't found a solution yet as I'm using hooks instead of class components. ...