Troubleshooting: Three.js Lighting Issue - Object appears to be excessively illuminated

I'm currently learning from a tutorial on three.js at this link: https://www.youtube.com/watch?v=_OwJV2xL8M8

My project is supposed to display a canvas with a green sphere in the center, illuminated by a point light positioned above it. However, my point light doesn't seem to be working properly, and the sphere appears to be evenly lit. I started this project using vite.

Here is the HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="icon" type="image/svg+xml" href="/vite.svg">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vite Project</title>
</head>
<body>
    <canvas class="webgl"></canvas>
    <script type="module" src="/main.js"></script>
</body>
</html>

And here's my javascript code:

import * as THREE from 'three'

const scene = new THREE.Scene()

const geometry = new THREE.SphereGeometry(3, 64, 64);
const material = new THREE.MeshBasicMaterial({
    color: '#c0ff83'
});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

const light = new THREE.PointLight(0xfcfcff, 4, 60);
light.position.set(0, 10, 10);
scene.add(light);

const camera = new THREE.PerspectiveCamera(45, 800 / 600); // updated
camera.position.z = 16;
scene.add(camera);

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

I have attempted to troubleshoot by commenting out the lighting code, but the issue persists.

Below are the errors I encountered. Any suggestions for resolving them?

Errors

Answer №1

MeshBasicMaterial is considered an "unlit" material, meaning it does not interact with lights. For a different effect, try using a material that is affected by light, such as MeshLambertMaterial or MeshPhongMaterial.

const scene = new THREE.Scene()

const geometry = new THREE.SphereGeometry(3, 64, 64);
const material = new THREE.MeshLambertMaterial({color: '#c0ff83'});

const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

const light = new THREE.PointLight(0xfcfcff);
light.position.set(0, 10, 10);
scene.add(light);

const camera = new THREE.PerspectiveCamera(45,  window.innerWidth / window.innerHeight); // updated
camera.position.z = 16;
scene.add(camera);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild(renderer.domElement)

renderer.render(scene, camera);
body {
      margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4b574d5a5a7f0f110e0b07">[email protected]</a>/build/three.min.js"></script>

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

Allow for the ability to choose a specific option for every individual line that is echoed in

I have researched several similar questions, but none of them address exactly what I am attempting to achieve. My goal is to use AJAX to fetch a PHP page that will display the contents of a folder on my server. Currently, the files are being listed line by ...

Area range in a column plot using Highcharts

Is there a way to set the area range (blue horizontal bar) only on the red bar by adjusting its width? I've tried using plot bands, but they cover the full width of the x-axis. Can anyone offer a solution? In my latest attempt, I successfully set the ...

Date Boxes in a Tangled Web

Looking to showcase multiple dates on a screen, I'm encountering an issue where clicking on a date button opens a datepicker. If the user clicks out of the box, the datepicker closes properly. However, when another datepicker button is clicked, instea ...

Issue with sound not playing on iOS for video and audio elements (not related to autoplay)

My latest creation is a unique "app" that features a cozy fireplace video and a festive Christmas song playing on repeat. I shared it with a friend who uses an iPhone, but unfortunately, the audio wouldn't play from either the video or the song. I&ap ...

What is the best way to create an `isHook` function in my code?

Is there a way to determine if a function passed is a React Hook? isHook(useState); // returns true isHook(() => {}); // returns false; I am looking for a method to distinguish whether a function attached to an object property is a hook or not. In cas ...

Combining two sets of JSON data through mathematical operations

Two JSON files are available: one containing team names and codes, and the other with match details and scores. The goal is to create another JSON file that aggregates total matches won-lost and total goals. The JSON files are located at: JSON 1 ...

Error: An issue occurred in the driver.execute_script function causing a JavascriptException. The error message indicates

When attempting to run some JavaScript code on the Chrome driver, I encountered a JavascriptException. Despite my confidence in the correctness of the JS code, the following error message was displayed: raise exception_class(message, screen, stacktrace) s ...

Encountering a problem with CORS in the jQuery API while trying to

Currently, I am initiating an API request from a separate domain. There is currently no authorization check in place, however, we are considering implementing one in the future. When I make a request to this API without any headers, I receive a response. ...

I am currently facing an issue with my Next JS app where the search results page is not showing up when a user enters a query

I am currently working on a search application using Next.js. Within my app directory, I have 3 main files - layout.js, page.js, and searchResults.js Here is the code in layout.js: export const metadata = { title: 'xyz Search', description: ...

Implementing relationships in microservices with Prisma and NestJS

Schema for User Management service: model User { id String @id @default(uuid()) username String @unique email String @unique password String } Schema for File Management service: model File ...

Unable to use the res.send() function

I've been working on a Node.js project. Within the validate.js file, I have defined a class Validate with a static method validateTicket and exported the class at the end. validate.js file const request = require("request"); const config = { urlBas ...

Creating custom functions within views using Sencha Touch 2

I'm having trouble creating my own function in Sencha Touch 2 and I keep receiving an error: Uncaught ReferenceError: function22 is not defined The issue seems to be coming from my Position.js file located in the View directory. Ext.define(' ...

Exploring the world of technology with jQuery, harnessing the power of JSON objects

I'm seeking assistance with creating JSON using JQuery, sending it via Ajax to a .NET webservice, and then deserializing it in .NET in order to save the data to a database. If anyone could provide guidance on where I might be going wrong, that would ...

hide popup when button is clicked

As we develop a CMS for a website, I have listed all posts in a datatable. When a user clicks on the delete button, I want to remove that post. I have called my service and successfully deleted the row from the database, but the frontend is not displaying ...

Having trouble uploading data to the database using a combination of Vue.js and Laravel backend framework

I've been facing an issue where my attempt to submit a record to my SQL database from a Vue component using Laravel API is not yielding any results. Despite comparing my code with other functioning examples, I have yet to find a solution. Below is th ...

Display error page when unable to load ng-view template

Is there a way to display a standard error page or template if a certain template cannot be loaded using ngRoute in Angular? I've come across suggestions that subscribing to the $routeChangeError event might help, but I'm not sure what steps to ...

Looking to deactivate a particular checkbox in a chosen mode while expanding the tree branches

I encountered an issue with a checkbox tree view where I needed to disable the first two checkboxes in selected mode. While I was able to achieve this using the checked and readonly properties, I found that I could still uncheck the checkboxes, which is no ...

Encountering issues with saving information to MongoDB

I recently started working with the MERN stack and I am trying to save user information in MongoDB, which includes two string attributes: "name" and "role". However, I encountered an error in the browser console stating "Failed to load resource: Request t ...

Utilize JavaScript to eliminate tags surrounding a text node

If my HTML code looks something like this: <div id="text"> This is some text that has an area <span class="highlight">highlighted with different formatting</span> and then some more text. </div> I am trying to figure ...

What is the process for displaying images fetched from the API?

Currently, my front-end is built using React/Redux and the API side with AdonisJS. All of my images are stored within the API, and I need to find a way to display them on the front-end. Can anyone provide guidance on accomplishing this task? ...