Design a dynamic backdrop using three.js

Hey, I'm currently working on incorporating a background image into my Three JS project.

Although the code is functioning properly, the background isn't being displayed. Here is my init function:

function init(){
// Creating a new scene
scene = new THREE.Scene();
var WIDTH = window.innerWidth,
    HEIGHT = window.innerHeight,
    BOXWIDTH = 20;

// Renderer 
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(WIDTH,HEIGHT);
document.body.appendChild(renderer.domElement);

// PerspectiveCamera (Field of view, Aspect ratio, Near distance, Far distance) 
camera = new THREE.PerspectiveCamera(45,WIDTH/HEIGHT,1,20000000);
camera.position.set(0,8000,0);
scene.add(camera);

// Resizer
window.addEventListener('resize', function() {
  var WIDTH = window.innerWidth,
      HEIGHT = window.innerHeight;
  renderer.setSize(WIDTH, HEIGHT);
  camera.aspect = WIDTH / HEIGHT;
  camera.updateProjectionMatrix();
});

// Setting the background color of the scene.

// Loading the background texture
var texture = THREE.ImageUtils.loadTexture( 'images/background.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;

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

// Creating a light, setting its position, and adding it to the scene.
var light = new THREE.PointLight(0xaaaaaa);
light.position.set(-100,200,100);
scene.add(light);

// Initializing object for world/screen calculations
projector = new THREE.Projector();

// Adding OrbitControls for mouse panning.
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.minDistance = 0;
controls.maxDistance = 7000000;

// To update mouse position on move
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
}

Here is my render function

function animate() {

    // Learn more about requestAnimationFrame at http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
    requestAnimationFrame(animate);
    // Rendering the scene
    renderer.autoClear = false;
    renderer.clear();
    renderer.render(backgroundScene, backgroundCamera);
    renderer.render(scene, camera);
    controls.update();
    // Updating tweens from TWEEN library
    TWEEN.update();
    update();
}

Could there be a step that I overlooked?

Answer №1

It may be a bit late to mention this, but upon initial inspection, it appears there is a flaw in your rendering process.

renderer.render(backgroundScene , backgroundCamera );
renderer.render(scene, camera);

The second line seems to overwrite the first one. Instead of creating a separate background scene and camera, consider adding your background mesh directly to the main scene.

If incorporating the background into the main scene is not feasible based on your requirements, you could explore rendering to a texture using WebGLRenderTarget.

For more information, visit:

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

Ways to resolve the problem of connecting Provider with my store and efficiently transmitting data to components in my Redux-React application

Encountering an error that reads: Uncaught Error: Could not find "store" in either the context or props of "Connect(WebShop)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(WebShop)". Despite havin ...

Possible revision: "Methods for updating Bootstrap language settings?"

Currently, I am working on a project using ASP.NET Core in combination with Bootstrap. Recently, I successfully integrated localization support by following the guidance provided in this documentation. However, I encountered an issue where the Bootstrap fr ...

Utilizing JavaScript scope effectively in Express.js within a Node environment

I'm currently diving into the world of JavaScript and full-stack development and one concept that's giving me some trouble is understanding how to properly utilize express with JavaScript .... Here is my question: In all the tutorials I've ...

What is a way to pass refs from a parent component to a child component without relying on Typescript?

I am currently working on an App.vue file that has a property called "clicked" which I am trying to pass to different child components. App.vue <template> <NavBar :clicked="clicked"/> <BackDrop :clicked="clicked"/> ...

Is there a way to produce a unique color using Stylus CSS?

Currently, I'm utilizing Express for Node.js and Stylus as the CSS engine. While Stylus is great, I'm facing some challenges in passing color variables or generating random colors. Despite attempting to use the javascript API for stylus, I find m ...

Retrieve the properties from within a closure function in a functional component

I have developed a simple React application using create-react-app. The app consists of a single component that takes in a value and an onClick callback. When the callback is triggered, the value increments. import React, { useState } from 'react&apos ...

Experiencing difficulty converting a JSON array to a C# list during deserialization

With so many options available for serializing and deserializing JSON, it can be confusing to determine which one is the best choice. It's curious why there are multiple tools that seem to accomplish the same task. Some examples include JsonConvert, J ...

Link a timestamp to a datetime-local input using ng-model

My challenge is to display an <input type="datetime-local> field using the ng-model attribute to represent a timestamp: <input type="datetime-local" ng-model="object.value"> This is being achieved with: $scope.object.value = 1433109600000; ...

Utilizing React.js components for Parsing JSON Objects

As a beginner in reactjs, I am faced with the challenge of parsing JSON response fetched from an API in index.html and passing it to a component called Card.js as props. Despite being able to access the response in the console log, I am unable to render it ...

What is the best way to add information stored in localStorage to an element on the webpage?

At the moment, I am developing a Bookmark manager that stores data in localstorage and then adds the saved data from localstorage to the DOM. However, I am facing an issue where the code inside fetchUrl() is not getting appended to the DOM. Here is what I ...

Guide on making a persistent sidebar using CSS and JavaScript

I'm in the process of developing a website that features a main content area and a sidebar, similar to what you see on Stack Overflow. The challenge I am facing is figuring out how to make the sidebar remain visible as a user scrolls down the page. T ...

Incorrect center point selected for specific fbx models when imported into THREEJS

Currently, I am working on a project that involves utilizing fbx models in a THREEJS viewer. While most models have the correct pivot points, some do not, causing a challenge in identifying the root cause of the issue. To convert fbx to json, I am using t ...

selecting a div element with a label using the nth-child property

I recently discovered the CSS3 :nth-child() Selector and decided to create a sample Here is the HTML snippet: <body> <div> <label>dsdf</label></div> <div>seconddiv</div> </body> And here's the CSS par ...

The server is being bottlenecked by the calculation of distances between numerous Nodejs coordinates

With a list containing hundreds of coordinates (lat,lon), I face the challenge of calculating the distance between each pair of points for every client request. This process is currently blocking my nodejs server as it takes 500ms to do so with a list of 1 ...

Troubleshooting problems with the Quora pixel after refreshing the page

Having a strange issue with the Quora pixel integration on Angular. I have added the script in the HTML code as follows: ! function(q, e, v, n, t, s) { if (q.qp) return; n = q.qp = function() { n.qp ? n.qp.apply(n, arguments) : n.queue.push ...

Caution: The `<input>` tag is displaying unknown props `input` and `meta`. Please remove these props from the element

I have been working on creating a form using redux-form, but I keep encountering a warning and an error that I am struggling to resolve. Here are the warning and error messages: Warning: Unknown props input and meta on the <Field> tag. Please remov ...

Is there a problem with the map() function in React.js?

Every time I try to add a new To Do without modifying the HTML at all and push() the array, everything works smoothly. However, once I trigger the onChange event listener, I encounter the error "this.state.tasks.map is not a function". I'm struggling ...

What steps should be taken to ensure that Google effectively crawls through an AngularJS application?

According to a source at Allotment Digital, it is unnecessary to provide different or pre-rendered content to Google when using html5mode and removing hashtags from URLs. While some websites state that ajax crawling documents are deprecated, others such a ...

Having trouble with the side menu navigation in Bootstrap?

I have a total of 5 pages on my website. Out of these, 2 main pages contain submenus/pages as well. To make users aware of the presence of submenus, I am using plus and minus icons. However, there is an issue where clicking on one submenu toggle icon doe ...

What is the purpose of using a single pipe character within a Vue.js template handlebar expression?

Here is a sample code snippet: <div> {{ name | capitalize }} </div> I have searched through the documentation for vuejs and handlebars, but couldn't find any relevant information. ...