Three.js: Troubleshooting a Black Screen Issue During Rendering

Despite setting the body background color to #135462, the code below is displaying a black screen. It seems like there might be an issue with the rendering code preventing the background color from showing up properly.

Additionally, the object is not appearing on the screen as expected. You can find the link to my object here:

Does anyone have any insights into why this could be happening?

    <!DOCTYPE html>
<html lang="en">
    <head>
        <title>My three.js Project</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                background:#135462;
                color:#fff;
                padding:0;
                margin:0;
                overflow:hidden;
                font-family:georgia;
                text-align:center;
            }


        </style>
    </head>

    <body>          

            <script src="js/three.min.js"></script>
            <script src="js/loaders/BinaryLoader.js"></script>

            <script src="js/loaders/ObjectLoader.js"></script>
            <script src="js/Detector.js"></script>

          <script>
          if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

                        var container;
            var camera, scene, renderer;

            var windowHalfX = window.innerWidth / 2;
            var windowHalfY = window.innerHeight / 2;

            init();
            animate();

            function init() {
                container = document.createElement( 'div' );
                document.body.appendChild( container );

                camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 10000 );
                camera.position.z = 5;

                scene = new THREE.Scene();
                scene.add( camera );

                var loader = new THREE.ObjectLoader();
                loader.load( "obj/superman/SupermanSmall.json", function(obj){

                scene.add(obj);

                });

                renderer = new THREE.WebGLRenderer();
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                container.appendChild( renderer.domElement );



            }

            function animate() {

                    requestAnimationFrame( animate );
                    camera.lookAt( scene.position );

                    renderer.render( scene, camera );

            }

          </script>

    </body>
</html>

Answer №1

After resolving the errors, there is a possibility that you may still encounter a black screen. To address this issue, consider adding the following code:

renderer.setClearColor(0xEEEEEE);

You can also choose any other color to set as the background. Another option would be:

renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));

Implementing this should modify the background color accordingly.

Answer №2

If you want to achieve transparency and show the background defined in your CSS, one solution is to set the alpha property of the renderer to true. This will allow the background to shine through, as demonstrated below:

CSS

body {
    background: linear-gradient(#e4e0ba, #f7d9aa);
    margin: 0px;
    overflow: hidden;
}

Javascript

// Initialize the renderer with alpha transparency
renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio( window.devicePixelRatio );
document.body.appendChild( renderer.domElement );

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

Enhance your Angular2 Directive

Looking to customize the angular2-clipboard npm package by extending its functionalities. Specifically, I aim to access and modify its ngOnInit() function to cater to a specific copying use case. Being new to angular2, I am uncertain about the process of ...

Show singular array element upon click

I need help with a specific task. When a button is clicked, I want to display a random item from an array. Each array item should only be displayed once. Currently, my code is set up as follows: When the button is clicked, a random array item is displ ...

Incorporate extra padding for raised text on canvas

I have a project in progress where I am working on implementing live text engraving on a bracelet using a canvas overlay. Here is the link to my code snippet: var first = true; startIt(); function startIt() { const canvasDiv = document.getElement ...

Tips for validating multiple inputs of the same type with identical names in JavaScript

I have limited experience with Javascript and am facing an issue with a HTML form that contains multiple input types with the same names occurring more than once. My goal is to validate the form before inserting the data into the database. I have managed t ...

React form submissions result in FormData returning blank data

I am having trouble retrieving the key-value pair object of form data when the form is submitted, using the new FormData() constructor. Unfortunately, it always returns empty data. Despite trying event.persist() to prevent react event pooling, I have not ...

Create essential CSS for every .html page using Gulp

Recently, I came across a useful npm package recommended by Google for generating critical CSS: var critical = require('critical'); gulp.task('critical', function (cb) { critical.generate({ base: 'app/', ...

Is NextJS Route Handler in Version 13 Really Secure?

Within my forthcoming NextJS 13 web application, I am in the process of integrating an API through route handlers to facilitate functions like user registration and login procedures. Is it considered safe when transmitting sensitive data like a user's ...

Encountering the following issue: "ERROR TypeError: Argument is required in IE 11"

The code below is functioning properly in all internet browsers except for IE11. When running the code in IE, an error is thrown stating "ERROR TypeError: Argument not optional." The project being developed is using Angular 10. private togglePageClass(mod ...

Error message "npm install is causing a warning due to an outdated lockfile"

npm 8.1.2 | node 16.13.1 Upon running npm install, I encountered the error message below. It seems to be related to version compatibility issues, even though I tried installing npm version 7.19.1, the same error persists. Any suggestions on why this is ha ...

Exploring the possibilities of infinite scroll in JavaScript using the Backbone framework

I've been grappling with this problem for three days straight. I've been attempting to incorporate scrolling into my backbone project using the https://github.com/paulirish/infinite-scroll plugin. Despite my best efforts to find a solution throu ...

What category does React.js fall under - library or framework?

Hey there! I've noticed that sometimes people refer to React JS as a library, while others call it a framework. Can you shed some light on which term is more accurate? ...

react.js function that returns 'undefined'

Having trouble with my class function that returns undefined when I try to use the return value. Main.js: let db = new Database() let username = db.get() return( //returns undefined <p>{username}</p> ) database.js: class Database { [... ...

Here's the step-by-step process: Access the specific item in the object by referencing `obj[i]["name of desired attribute"]

I tried seeking advice and consulting multiple sources but none provided a suitable answer. Is there someone out there who can assist me? obj[i].["name of thing in object"] Here's the array: [ { "name": "DISBOARD#2760" ...

establishConnection(); ^ TypeError: establishConnection is undefined

While attempting to connect to MongoDB, I encountered an error in my index.js file. require('dotenv').config(); const express = require('express') const app = express(); const cors = require('cors'); const connection = require ...

Ways to retrieve the related file in Angular service files?

I am looking to retrieve an array from another service file (chart-serv.js) in my return-serv.js service file using AngularJS. How can I reference the dependent file enclosed within double braces [ ], in line 1? return-serv.js var app = angular.module(& ...

Incorporate a unique identifier $id within the ajax request

Whenever I make changes to my product, I would like the categories to show up in a dropdown menu. Currently, it only displays: -- Select your category -- For instance, if my $current_category_id = 2, I want the dropdown to show the relevant categories. ...

Unable to locate index.html file in Docker container while dockerizing React application

I'm a newcomer to Docker and I'm looking to containerize my react app. The index.html file is located in the public folder within my react project. However, when I try to run the docker image, it fails with an error indicating that the index.html ...

Navigate through FAQ items with sliders using Owl Carousel 2 - Easily switch between different chapters

Exploring the creation of a FAQ section with individual carousels for each item. My primary objective is for the carousel to transition to the next chapter when advancing beyond the last slide (backwards navigation would be a future enhancement). I'v ...

I am uncertain about the current status of this project; it functions correctly on my local machine but is encountering issues when deployed on the server

I'm facing an issue while transferring a project from local to server. The project works fine locally but not on the server, specifically when I try to edit products. The database is not displaying the correct information, as it shows two crucial numb ...

Creating a simple Node.js project using material web components - a step-by-step guide

I have recently started experimenting with Node.js. This is a basic program I created to test the integration of material-components-web. I followed the instructions provided in the readme file and here's what I have implemented so far: package.json ...