Three.js is capable of displaying a scene background, but it does not currently show any

I feel like I'm at my wit's end. I've been struggling to make anything render in this simple three.js program and it's driving me insane. I've tried everything - adding cubes, copying geometries, adjusting lights, moving the camera, but nothing works. I can't see anything but the background color of the scene. It's like every time I try to work on a project, I'm met with obstacle after obstacle, and it's frustrating beyond belief. It's like hitting a brick wall over and over again...

My HTML is fine, the scene renders perfectly in the background. But no matter what I do, I can't get any shaded geometries to show up. There are no console errors, no issues in VS Code, and other projects run perfectly. It's like a curse has been placed on this project, stopping me in my tracks...

Everything seems to be set up correctly - I have the context, renderer, loader, scene, and camera all defined. I even have lights and a red sphere object in the scene. Yet, no matter what I do, it just won't render properly. I'm running the web app on Node liveserver, and my browser is up to date, so I'm at a loss...


var context = null,
    renderer = null,
    loader = null,
    scene = null,
    camera = null;

var redSphere;
var cube;

function onLoad() {

    //CONTEXT
    context = document.getElementById("deep");
    context.style.height = "100vh";
    context.style.width = "100vw";

    //RENDERER
    renderer = new THREE.WebGLRenderer( { antialias: true } );
    renderer.setSize(window.innerWidth, window.innerHeight);
    context.appendChild(renderer.domElement);

    //CAMERA
    camera = new THREE.PerspectiveCamera(45, context.offsetWidth/context.offsetHeight, 1, 100);
    camera.position.set(0, 0, 0);

    //SCENE
    scene = new THREE.Scene();
    scene.background = new THREE.Color("rgb(20, 0, 20)");

    //LIGHTS
    var sun1 = new THREE.AmbientLight(0xffffff, 1.0);
    var sun2 = new THREE.DirectionalLight(0xffffff, 1.0);
    var sun3 = new THREE.DirectionalLight(0xffffff, 1.0);
    sun1.position.set(0, 0, 0);
    sun2.position.set(-3, 3, 2);
    sun3.position.set(0, -3, 2);
    scene.add(sun1, sun2, sun3);

    //RED OBJECT
    var sphereGeo = new THREE.BoxGeometry(1, 1, 1);
    var redSphereMat = new THREE.MeshPhongMaterial({color: "rgb(255, 0, 0)" });
    redSphere = new THREE.Mesh(sphereGeo, redSphereMat);
    redSphere.position.set(0, 5, 0);
    scene.add(redSphere);

    draw();
}

function draw() {
    renderer.render(scene, camera);
    camera.lookAt(redSphere);
    redSphere.rotation.y -= 0.1;
    requestAnimationFrame(draw);
}

Answer №1

For the lookAt() function, be sure to provide a Vector3 as an argument instead of a Mesh. You can use the following code snippet:

camera.lookAt(redSphere.position);

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

Discovering the Nearest Point to the Mouse Cursor using Three JS Ray Casting

Three.js version r85 While using raycasting in Three JS, a list of points is generated, and I am interested in identifying the point closest to the cursor. It appears that the first point returned is typically the one nearest to the camera. Is there a me ...

Blending ASP.NET Core 2.0 Razor with Angular 4 for a Dynamic Web Experience

I am currently running an application on ASP.NET Core 2.0 with the Razor Engine (.cshtml) and I am interested in integrating Angular 4 to improve data binding from AJAX calls, moving away from traditional jQuery methods. What are the necessary steps I need ...

Extracting data from a Wikipedia table using web scraping in JavaScript

I'm attempting to extract information from a specific website. <script> var convertToInt; var allData = []; $.ajax({ url: "http://en.wikipedia.org/wiki/Demographics_of_Europe", type: 'GET', cache: false, success: func ...

Issue with React.useEffect not functioning in Express application

I'm having trouble with the state not updating or re-rendering in my code. I've tried logging from inside the useEffect function but nothing seems to happen. Any suggestions on how to make the useEffect work properly? app.js var createError = re ...

What are the steps to run and test the renovate bot on your local machine

Before setting up renovate to work with my Azure pipeline, I wanted to test it locally using npx renovate to ensure everything is working as expected and that my config file is properly configured. I ran npx renovate --platform local command. My project i ...

Uploading a large number of images to GCP Bucket results in a failure to connect to the oauth2 token

After writing a NodeJS upload function for Google Cloud bucket, I encountered an issue when trying to upload a larger dataset of over 3000 images. Initially, the uploading process seemed smooth, but then suddenly an error occurred and only some of the imag ...

Having trouble with the Moment.js diff function in your React Native project?

Within my React Native application, I have implemented Moment.js and included the following code snippet: const expDate = moment(new Date(val)).format('MM-DD-YYYY'); const nowDate = moment().format('MM-DD-YYYY'); const diff = nowDate.d ...

Using AJAX to call a PHP function within a PHP script

I have successfully implemented an AJAX file that can execute content from a PHP file. However, my concern is how to specifically call a particular function within the PHP file if there are multiple functions present. Below is my current AJAX code: $(doc ...

When working with Node.js and Express, I encountered an issue where the req.session object was not defined within

It's peculiar to me that req.session.username is undefined in the tag >>>DOESNT WORK<<< while it works in the tag >>>THIS DOES WORK<<<. I passed req as an argument to my module, but it seems like there might be some ...

Issues encountered when trying to retrieve data from an Express server

I have set up a NodeJS server with Express.js and I am attempting to send a request to it using fetch. fetch("http://localhost:3001/api", { method: "POST", headers: { "Content-Type": "application/json", ...

Sorting data in Javascript can be done efficiently by utilizing the .filter method

Can someone help me identify what I might be doing incorrectly? I have a chained filter under computed that is giving me an error message stating 'product.topic.sort' is not a function. My intention is to use 'select' to provide sortin ...

Timezones not synchronizing properly with Moment.js

Hello everyone, I am currently exploring the world of moment.js along with the timezone add-on. I'm encountering some issues with a world clock project that involves using moment.js and moment-timezone.js together on a page where highcharts are also p ...

Tips for obtaining the combined outcome of multiple arrays (3 to 5 arrays) in JavaScript

How can we transform an array of objects with nested arrays into a new array of objects with mixed values? Consider the following input: var all = [ { name: "size", value: [20, 10, 5], }, { name: "color", value: [ ...

Redux Persist causes the redux state to become undefined

I recently added redux-persist to my project and now I am facing an issue where all of my Redux states are returning undefined. Previously, all the Redux states were functioning properly. However, after incorporating redux-persist, they are no longer work ...

Display the returned view following an AJAX request

Currently, I am trying to search for a specific date in the database using ajax to trigger the function. However, I am encountering a 404 error when I should be receiving the desired outcome, and the ajax call is entering an error mode. Here is the snippet ...

Implementing an ES6 class in a next.js application: A step-by-step guide

Currently, I am in the process of developing a next.js application and am looking to extract the code that interacts with the database from the API. To achieve this, I have developed a class that serves as a manager: class UserManager { async findUser ...

Tips for sending formData (file upload) via jQuery ajax with MVC model

My Web Development Challenge : //IMAGE UPLOAD var formData = new FormData(); var totalImages = document.getElementById("ImageUpload").files.length; for (var x = 0; x < totalImages; x++) { ...

Calculating the number of days between two given dates, including both the start and end dates

I need to calculate the number of days between two dates, including both of the dates. My current method for this task is as follows: numDaysBetweenDates(startDate, endDate) { let millisecondsPerDay = 24 * 60 * 60 * 1000; return (endDate - startDate) ...

How to capture a change event in a dynamically loaded element using JQuery

I am facing an issue with dynamically added rows in my table. Each row contains a select option, and I need to trigger an action when the select is changed. However, since the rows are loaded after the page load, my function does not work as expected. < ...

Change the appearance of text with a button click using JavaScript

Currently mastering JavaScript but encountering an issue. How do I change the text to underline when clicking on "Click Me"? <p id="demo" style=" text-decoration:none; ">Hello JavaScript!</p> <button type="button" onclick="document.getE ...