Include backdrop in 3 scenes

I've been struggling to add a background to my THREE scene, but I can't seem to get it right. Despite following the suggestions mentioned here and here, I still haven't had any luck.

Here are the lines of code I inserted into my rather complex script:

// Load the background texture
var loader = new THREE.TextureLoader();
var texture = loader.load( 'textures/stars_texture2956.jpg' );
var backgroundMesh = new THREE.Mesh(
    new THREE.PlaneGeometry(2, 2, 0),
    new THREE.MeshBasicMaterial({
        map: texture
    }));

backgroundMesh.material.depthTest = false;
backgroundMesh.material.depthWrite = false;

// Create your background scene
backgroundScene = new THREE.Scene();
backgroundCamera = new THREE.Camera();
backgroundScene.add(backgroundCamera );
backgroundScene.add(backgroundMesh );

and the render function is as follows:

function render() {
renderer.render(backgroundScene, backgroundCamera);
renderer.render(scene, camera );
}

Despite all this, I still do not see the background (it remains white), although everything else functions as expected. Is there a solution to this issue?

Answer №1

To easily incorporate a static background into your scene, consider making the background transparent and placing an image underneath the canvas:

let renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setClearColor( 0xffffff, 0);

If you're looking to create a panoramic background that changes as you rotate the camera, you'll need to set up a skybox. This involves creating a large mesh around your scene textured with a series of images that cover a full 360 degrees of view. Check out this example for more detail: http://threejs.org/examples/#webgl_materials_envmaps

Answer №2

The issue arises as the map is not defined due to the parameter expected by the TextureLoader Constructor being a manager rather than a URL.

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

// load the texture from a specified path
var textureMap = imageLoader.load( 'textures/land_ocean_ice_cloud_2048.jpg' );

Visit this link for more information on TextureLoader in Three.js

If you are facing issues with multiple scenes, try disabling autoClear to prevent the second renderer from clearing the first one. After initialization, set: renderer.autoClear = false;. Then manually clear in your render function like so:

function render() {
    renderer.clear(); // <-
    renderer.render(backgroundScene, backgroundCamera);
    renderer.render(scene, camera);
}

Answer №3

experiment with coding in this style

    THREE.ImageUtils.crossOrigin = '';
  var image = 'http://example.com/image.jpg';
  var texture = THREE.ImageUtils.loadTexture(image);
new THREE.MeshLambertMaterial({  map: texture });

this method has been successful for me

Answer №4

Here is a helpful tip: Rather than creating a separate scene and struggling to correctly render it with cameras/other elements, simply incorporate the background mesh into your existing scene.

Below is the code snippet:

// Load the background texture
var loader = new THREE.TextureLoader();
var texture = loader.load( 'textures/messier-41.jpg' );               
var backgroundMesh = new THREE.Mesh( 
    new THREE.PlaneGeometry(2048, 2048,8,8),
    new THREE.MeshBasicMaterial({
         map: texture
    }));

backgroundMesh.material.depthTest = false;
backgroundMesh.material.depthWrite = false;

This block of code should be placed before any code that adds other elements to the scene.

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

Uncertainty about the integration of a JavaScript file into a PHP file

Having two PHP files where one is executed through a URL and makes AJAX calls to the second PHP file can present challenges. Sometimes, the AJAX results return HTML content with JavaScript events that do not work as expected. To solve this issue, I have in ...

The Protractor identifier for the dynamically generated element

Is it possible to locate an element using a partial id? The page I am testing has a dynamic id where the first part contains a variable number, making it difficult to predict the complete id beforehand. ...

Prevent specific fields from being saved in Node MongoDB native

When working with MongoDB and using the node mongodb native driver to insert documents, I have encountered an issue. The objects I am inserting have fields that I do not want to be saved in the database: var x = { field: 'value', _nonPersist ...

Retrieve the Table Element's unique identifier and use it to trigger the opening of a modal containing corresponding data

Hello, I am using a Bootstrap table with data fetched from PHP and need some advice: <div class="table-responsive"> <table class="table table-bordered table-hover"> <tr> <th>First Name< ...

Navigating external pages with Vue Router

Could really use some assistance. I've got a JSON file filled with various URL links, some internal and some external. This is what the JSON structure looks like: [ {stuff..., "Url":"https://www.google.com/", stuff..}, {stuff... ...

Tips for refreshing an html table without affecting the scroll location?

HTML: <div class="html_table"></div> # Within the html body tag. Utilizing Ajax function to retrieve table data. var $html_table= $('.html_table'); function ajaxCallFunction() { $.ajax({ type: 'POST', ...

I'm attempting to integrate the map function into my React Redux application, but after implementing useDispatch, I'm encountering an error message in the console

I am currently troubleshooting an app I'm working on, but I keep encountering errors in the console. Included below is a picture of the error as well as the code snippet triggering the issue. Can anyone provide insight into what might be causing this ...

Utilizing conditional statements like if/else and for loops within a switch statement in JavaScript allows for

I am currently developing a request portal that involves dynamic checkboxes, labels, and textboxes that are dependent on an option list. As a beginner in javascript, I am facing challenges with creating conditional statements. I have managed to make progr ...

Problem with reordering rows in a PHP DataTable

While attempting to retrieve data from a database table and display it in a DataTable, I encountered the following error: DataTables warning: table id=example - Invalid JSON response. To learn more about this error, refer to http://datatables.net/tn/1 ...

What is the best way to set a boolean value for a checkbox in a React project with Typescript?

Currently, I am working on a project involving a to-do list and I am facing an issue with assigning a boolean value to my checkbox. After array mapping my to-dos, the checkbox object displays 'on' when it is unchecked and a 'Synthetic Base E ...

Tips for efficiently managing variable item quantities and orders when making selections from an unsorted list

When extracting innerText properties from list items within a ul element, the length of the list can vary greatly. Certain items like profile name, age, and location are always present, while others such as current term and prior degree may or may not be i ...

Is the check for 'is integer' satisfactory?

Is the following 'is integer' function sufficient: function isInteger( input ) { return Number(input) === parseInt(input); } I want this function to return true for inputs like 12, 13, '14', '-1', Number.MAX_VALUE (round ...

What is the best method for deleting automatically added connection proxies in XCC?

Is there a way to make an ajax request from IBM Connections XCC without it being proxied? let api = 'https://my-server2/api.xml' var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = () => { if (xmlhttp.readyState == XMLHttpRe ...

The callback syntax used in the TypeORM Relations decorator is completely baffling to me

Recently, I've been exploring TypeORM and the relationships that can be defined with different decorators. For instance - @OneToOne(type => Profile, profile => profile.user) @JoinColumn() profile: Profile; I'm struggling to understand the ...

Use jQuery to create rows within each div

Can someone assist me with the following issue? I have created an HTML grid where objects are displayed on the left side, and a row is shown for each object on the right side. I am looking to use jQuery to add 8 items in a row on the right side for each o ...

How to Extract a URL from an Anchor Tag without an HREF Attribute Using Selenium

How can I make a link, which normally opens in a new window when clicked, open in the current window instead? The link does not have an href attribute, only an id and a class. For example: <a id="thisLink" class="linkOut">someLinkText</a> I r ...

Is there a way to access a local file upon clicking a button with the HTML5 File API?

I am currently working on a project where I am using jQuery along with the HTML5 File API to extract data from a local file. My goal is to retrieve text from the file only when the user clicks on a specific button, rather than automatically detecting chang ...

Can Vue allow for the inclusion of HTML elements to store data seamlessly?

One example involves displaying array elements in an <ul>, with each element starting with <li> and ending with </li>. Here is the attempted code: clearedtaskslist: function(){ this.template='<ul>' for(let i=0;i<t ...

Tips for adding content to a textarea with JavaScript without disrupting the editing history

I have a specific requirement where I want the user to be able to highlight text in a textarea, then press ctrl + b to automatically surround that selected text with stars. Here is what I envision happening: 1) The initial content of the textarea is: "he ...

The dynamic form is programmed to display identical values for both the initial and subsequent inputs

Currently, I am developing a personalized dynamic form utilizing the Material UI library as the component for my React Js application. You can find more information about this library at https://mui.com/. In front of you is the initial setup of the dynami ...