What is causing my Three.js scene to appear pitch black?

I am embarking on my first three.js project, but unfortunately my scene is appearing entirely black. I am utilizing Vite as my local server.

Below is the JavaScript code I am currently using:

import * as THREE from 'three'

//Scene
const scene = new THREE.Scene()

//Create a sphere
const geometry = new THREE.SphereGeometry(3, 64, 64)
const material = new THREE.MeshStandardMaterial({
    color: "#00ff83",
})
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh);

//Light
const light = new THREE.PointLight(0xffffff, 1, 100)
light.position.set = (0, 10, 10)
scene.add(light)

//Camera
const camera = new THREE.PerspectiveCamera(45, 800 / 600)
camera.position.z = 20
scene.add(camera)

//Renderer
const canvas = document.querySelector(".webgl")
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(800, 600)
renderer.render(scene, camera)

To include the canvas in my HTML file, I used the following code:

<canvas class="webgl"></canvas>

Answer №1

Make sure to include the AmbientLight

const illumination = new THREE.AmbientLight( 0x404040 ); // gentle white light
scene.add( illumination );

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

Passing parameters from a div to a single page component in Vue.js: A complete guide

There is code that appears on multiple pages: <div id="contact-us" class="section md-padding bg-grey"> <div id="contact"></div> <script src="/dist/build.js"></script> </div> Included in main.js is: im ...

Information sent by the Firefox TCP socket using the socket.send() method cannot be retrieved until the socket is closed

I am experiencing an issue while trying to send data from Firefox to a Java desktop application. My Java class functions as a server, and the Firefox script acts as a client. When I test it using another Java class called client.java, the data is successfu ...

Creating a new table based on an if statement from an HTML table index value of x

Hey everyone, I could really use your assistance. I've been attempting to create an if statement based on the values in my HTML table's index-x (where x represents the column index). I've successfully managed to convert the table into an arr ...

What is the best way to reveal information from an object in Javascript?

For debugging purposes, I need to extract the variables stored in connectData. Despite checking the type with 'typeof', I have found that when I try to access connectData[1] thinking it's an array, it returns undefined. If I simply want to ...

What adjustments can I make to my smooth-scrolling JavaScript code in order to exclude my Bootstrap Carousel from the scrolling

On my website, I have incorporated JavaScript to create a smooth-scrolling effect when a user clicks on a hyperlink. This feature is essential as the landing page includes a chevron-down icon that prompts the user to navigate to the next section of the pag ...

Is it more beneficial to categorize REST-based modules/controllers by resource or by specific action/feature?

I'm facing a scenario that goes like this: Endpoint 1: Retrieve all books in the United States owned by John with a GET Request to /country/us/person/john/books Endpoint 2: Get all books owned by John in every country with a GET Request to /person/j ...

Select the date from the drop-down menu or choose a date from the

How can I implement a date picker with dropdowns for date, month, and year, while also enforcing a minimum age of 18 years with JavaScript and frontend validation? ...

Passing data retrieved from fetch requests in ReactJS using context: Best practices

Just started learning React and need some help. I'm trying to pass variables with json data to a component for further use but running into errors. What changes should I make to use variables with json data from Store.js in the product.js component? T ...

Using JavaScript to extract the metadata of an image from a remote location

Is there a way to extract XMP metadata from a JPEG file using JavaScript? I came across a method for doing it in PHP (How can I read XMP data from a JPG with PHP?) which can be adapted for JavaScript using AJAX. However, the issue arises when trying to acc ...

What is the best way to adjust the width of an image?

Can someone assist me with a script for implementing an auto read more feature on my blog? I have specified values for image thumbnail height and width in the script, but they are not being applied to my blog. Any help would be appreciated. To view my blo ...

How to incorporate both image and text links within an HTML div container using JavaScript

I am trying to create a clickable image and text within a div named "films" that both link to the same webpage. However, I am experiencing an issue where only the text link works and the image link is disabled. If I remove the text link, then the image l ...

Using Vue.js with Vue Router to handle login functionality and automatically refreshing data

I am currently working on a Vue.js application and have successfully implemented authentication functionality, including checking if the user is logged in. After logging in, I want to fetch some data and store it in localStorage. However, I have encounter ...

Enhancing Highcharts: Extending Tooltip Functionality

My dataset contains a collection of data points that I want to display in a Highcharts chart. Here is an example of how the data looks: mydataset = [{ x: 1, y: 3, names: ["John", "Jane"] }, { x: 2, y: 4, names: ["Alice", "Bob"] }] ...

IE8 and IE9 encountering "Access is denied" error due to XML causing XDomainRequest (CORS) issue

Sorry if this seems repetitive, but I am unable to find a definitive answer to similar questions. Whenever I attempt to make a CORS request for XML, I consistently encounter an "Access is denied" JavaScript error in IE8. The code I am using is based on t ...

What is the best way to load an image file using JavaScript and store it in a separate directory?

Is there a way to transfer an image file from one directory to another by clicking a button using Javascript that will work on all browsers (IE, FF, Chrome)? I already have code for this: <input type="button" name="button" value="Move image to another ...

What is the best way to implement a scrollbar in a specific div rather than the entire window?

Hey everyone! So, I have this window in electronJS with a div where I'm dynamically adding elements using Javascript and the function --> document.createElement('section'). Here's the loop in Javascript to add these elements: for ( ...

Safari is causing issues with HTML5 Video playback

I have a client with a media-heavy website containing numerous video and audio files. While the videos load perfectly on Chrome, Firefox, and IE, they do not load on Safari for Windows. Here's a snippet of the code: <video controls="controls" type ...

Calling gtag("event") from an API route in NextJS

Is there a way to log an event on Google Analytics when an API route is accessed? Currently, my gtag implementation looks like this: export const logEvent = ({ action, category, label, value }: LogEventProps) => { (window as any).gtag("event&quo ...

What is the best way to integrate properties subsets into your classes?

In my code, I am working with 3 classes ... class1 { constructor(a, b, c) { this.a = a; this.b = b; this.c = c; this.toClass2 = function() { // TODO: return this as an instance of class2; // the conversion would remove the un ...

Tips on how to eliminate a dimension in a cube

I'm attempting to create a cube, but I'd like the ability to eliminate one dimension, transforming it into a two-dimensional shape like a sheet of paper. My approach: let cubeGeometry = new THREE.BoxGeometry(10, 0, 10); let cubeMaterial = new T ...