Setting up a spotlight in three.js

After following the initial tutorial for three.js, I hit a roadblock when attempting to incorporate a point light into the scene. Despite numerous attempts to tweak my code, the cube remains unlit by the point light.

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, 
            window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);

document.body.appendChild(renderer.domElement);

camera.position.z = 55;

var light = new THREE.PointLight( 0xff0000, 1, 100, 2);
light.position.set(20,0,20);
light.castShadow = true;

scene.add(light);

var spheresize = 1;
var pointLightHelper = new THREE.PointLightHelper( light, spheresize );
scene.add( pointLightHelper );

var ambient = new THREE.AmbientLight( 0x303030);
scene.add(ambient);

var cube_geometry = new THREE.BoxGeometry(10,10,10);
var cube_material = new THREE.MeshLambertMaterial({color:0x00ff00});
var cube = new THREE.Mesh(cube_geometry, cube_material);
scene.add(cube);

animate();

function animate(){
    requestAnimationFrame( animate);

    cube.rotation.y += 0.01;

    renderer.render(scene, camera);
}

Although the code displays the light helper and the green cube is affected by ambient light, the point light fails to illuminate the cube (refer to the attached image). What am I overlooking? How can I ensure that the point light properly lights up the cube? https://i.sstatic.net/3Er5p.png

Answer №1

The current light source is functioning properly, but it would be beneficial to adjust either the color of the light or the material being illuminated.

At the moment, a pure red light is being directed onto a pure green material. However, the color of the material dictates which color components are reflected and in what proportions. Since pure red light lacks any green component, the object appears as if it is not being illuminated at all.

This situation may seem perplexing because in reality, instances of purely red light and solely green materials are rare in our physical environment.

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

Analyzing data visualization within CSS styling

I have the desire to create something similar to this, but I am unsure of where to start. Although I have a concept in mind, I am struggling to make it functional and visually appealing. <div id="data"> <div id="men" class="shape"></di ...

Unexpected error when using Slack SDK's `client.conversations.open()` function: "User Not Found"

I am currently utilizing the Slack node SDK in an attempt to send private messages through a bot using user IDs: const client = new WebClient(process.env.SLACK_TOKEN); const sendMessage = async (userId) => { try { await client.conversations.open( ...

What is the reason for the request body being undefined?

I have a JavaScript file named index.js that contains: const express = require('express'); const bodyParser = require('body-parser'); const cors = require('cors'); const db = require('./db'); const movieRouter = re ...

Load website once AJAX has finished

Currently, I am using an ajax call to retrieve the color scheme of my website from the database due to having multiple clients with different schemes. My goal is to ensure that the page only loads after the ajax call has completed. Despite an expected dela ...

Issue with relative templateUrl in Angular 2 not resolving paths

As I embark on creating my first Angular 2 app, I'm faced with the task of setting my template in an HTML file using `templateUrl`. Currently, both the component.ts and view.html are stored in the same folder under src/ directory. Here's what I ...

"Keep an eye on the server with Backbone.js by running periodic checks

In an effort to keep my backbone application constantly checking the server for model updates, I aim to create a system similar to Twitter's auto-refresh feature for new tweets. Currently, I am connecting to an external application through their API ...

Retrieving Vue data from parent components in a nested getter/setter context

<template> <div id="app"> {{ foo.bar }} <button @click="meaning++">click</button> <!--not reactive--> <button @click="foo.bar++">click2</button> </div> </templ ...

Setting up a nodejs application on a web server

As a newcomer to NodeJS, I am currently learning how to create a sample app using this technology. I have successfully tested it on my localhost, but now I am facing issues when trying to host it on a public server. var http = require('http'); ...

Attempting to calculate a property of the scope is ineffective

I am brand new to the world of AngularJS and I am struggling with a specific calculation. I have created a form where users can enter a volume number, which will be used in a calculation to populate a field in my MongoDB document. However, I am having issu ...

Zone Constraints for Dragging and Dropping in Angular 8

Seeking help to solve a puzzling issue that has been occupying my thoughts for the past few days. The Challenge I am attempting to incorporate a drag-and-drop functionality that allows users to drag up to 10 items and place them in specified spots (each ...

Tips for preventing route changes when clicking on a hash tag in AngularJS

I have a link in a small carousel on the page, and I am utilizing AngularJS routing to create a Single Page App. The tiny carousel uses anchor tags as buttons to navigate between images. <div class="carousel-controls-mini"> <a href=" ...

Angular - ng-class - dynamic progress tracker

I am currently developing a multi-step wizard in AngularJS that consists of three states. Each state should correspond to specific steps being active in the wizard. I have attempted to achieve this by assigning a class of 'active' to each step ba ...

How can I use Angular 6 to design an interactive user interface with drag-and-drop functionality?

I am looking to make a dynamic form using Angular 7 with drag and drop functionality. The controls I need for building the form are: Check Boxes Multiple Headings Sub Headings Table + Formatted Tables + Unformulated Checkbox + text Text Fields + formatte ...

The PDFKIT feature ensures that any overflowing data in a row is automatically moved to a new page

A function in my code generates a row of data based on an array. It works perfectly fine for the first page, but as soon as the data overflows somewhere around doc.text("example",70,560), it jumps to the next page. The issue arises when the Y coo ...

Issues with the History API in AJAX Requests

Recently, I have come across a dynamically generated PHP code: <script> parent.document.title = 'Members | UrbanRanks'; $("#cwrocket_button").click(function(){ $("#module").load("modu ...

Using Three.js to apply multiple materials to a single object

I'm interested in learning how to apply two different materials to a single object... I have a shader material that I use for coloring, but I also want to include basic Lambert shading. Here's the code snippet : var material1 = new THREE.Shader ...

Retrieving the data-id attribute from dynamically rendered div elements

How can I modify my checkPair function to retrieve the data-id attribute when clicking on a dynamically created card? Currently, I am encountering an error stating "The name 'getAttribute' could not be found." function createHTMLElementFromHtml( ...

Providing data from a javascript xml file upon page initialization

I am aiming to extract multiple values from an XML file as soon as an HTML page is loaded. The XML file remains constant and will not change over time. It will be stored in the same directory as the HTML page. The purpose of this XML file is to fill severa ...

Initializing an Express app with JSON file loading

My movie-finding application relies on backend API calls to function. As part of the initialization process, I need to load two JSON files: one containing a list of languages for searching (lang.json) and another stored in the config variable that provides ...

iOS users have reported that the dictation feature in React Native abruptly cuts off words

While utilizing dictation on a TextInput in iOS, the dictation abruptly stops between words. This was not a problem in React Native 53. However, upgrading to version 54 or higher triggers this issue. Below is an example of code that triggers the bug: imp ...