Illumination scope for directional lights in three.js

In the world of three.js, calculating shadows for directional lights involves setting a range based on a bounding box that extends from the light source. This means that if I want to limit how far shadows are rendered, I need to adjust the bounding box geometry. For instance:

var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.set( 50, 50, 50 );
directionalLight.intensity = 0.5;
directionalLight.castShadow = true;
directionalLight.shadowCameraLeft = -10;
directionalLight.shadowCameraRight = 10;
directionalLight.shadowCameraTop = 10;
directionalLight.shadowCameraBottom = -10;

The shadowCamera parameters can be modified to determine the distance at which shadows are generated from the light source's field of view. My goal is to have this bounding box follow the user camera so that shadows are visible up to a certain distance away from the camera.

However, since the bounding box's geometry is tied to the light's position relative to the scene's origin or target, simply moving the bounding box to match the camera's location won't achieve the desired effect.

I tried moving the light source along with the camera in the hopes that the bounding geometry would adapt to the camera's position. Unfortunately, this approach failed because I couldn't set a target for the directional light – it always aimed towards the origin, leading to changes in its direction whenever I adjusted the light's position.

Even attempting something like the following had no effect:

directionalLight.target.position.set(100,100,100);

If this method had worked, I could theoretically maintain a constant angle between the directional light's position and target by adjusting them relative to each other, potentially achieving my objective. But, as mentioned, it didn't work.

Do you have any suggestions on how to establish a shadow range or insight into why the above approach was unsuccessful?

Answer №1

DirectionalLight.target is a unique property associated with the light source, distinct from the scene graph and thus not automatically updated in terms of matrix values.

To address this issue, you can assign an object within your scene as the target, which should resolve any discrepancies.

light.target = myObject;

Implemented in three.js version 73.

Answer №2

To assist with troubleshooting, consider enabling this option:

ambientLight.shadowCastingEnabled = true;

Also, ensure you are adjusting all necessary parameters, such as:

ambientLight.shadowNearPlane = -5;
ambientLight.shadowFarPlane = 15;

In regards to the statement "does nothing," I am able to modify the light target using

ambientLight.target.position.set(a, b, c); 

which has worked for me in the past. Perhaps turning on shadowCastingEnabled will provide more clarity. Trusting this information proves beneficial.

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

"Combining JSON, JavaScript, and HTML for dynamic web development

I am a junior computer programmer facing challenges with our JSON project. The objective is to store an object in local storage, but my HTML and JS code are not working as intended. It seems like nothing happens at all. Any suggestions or feedback would ...

Enhance Your AngularJS Application with Data Transfer Object Models

Is there a way to implement a Data Transfer Object (DTO)? In my backend code, I have clearly defined domains such as the Client class: class Client { protected $firstName; protected $lastName; } This class contains specific properties that I wan ...

Assistance Required for Making a Delicious Cookie

Within my interface, there are two buttons displayed - one is labeled yes while the other is called no. <input type="button" name="yes" onclick="button()"> <input type="button" name="no"> In order to enhance user experience, I am looking to i ...

Is there a way to check if a user has previously visited a URL and automatically redirect them back to that page if they have

I'm looking for a way to detect if a user has already opened a specific link or URL in their browser tab. Is it possible to redirect the link to an active tab if the URL is already open? For example, if a user clicks on a link and JS code opens a new ...

Steps for triggering a re-render in a React component when an external value changes

I am currently working on a project that involves using Meteor and React. In my code, I have a class called WebRTC which handles all WebRTC-related logic: class WebRTC { this.isCalling = false; ... } In addition to the WebRTC class, there is ...

Exploring the power of tRPC for creating dynamic routes in NextJs

Recently, I embarked on a new project using the complete t3 stack (Nextjs, prisma, tailwind, tRPC), and encountered a minor hiccup. To provide some context, within my database, I have an "artists" table containing fields such as name, email, address, id, ...

angularjs ng-repeat utilizing the index instead of names

Using ng-repat to populate an HTML table with an array has been causing some naming normalization issues for me. Dealing with stubborn users, I am now in search of a quick workaround. Sometimes my array AgendaItems appears as: {"Agenda Item":&q ...

I'm looking for a way to set up a PropType that accepts a boolean value, but also allows for

Currently, my code includes a Modal component with a prop called disableEscapeKeyDown. The PropType defines it as boolean | null, but when trying to use it in the ModalWindow function, I receive an error stating Type 'boolean | null' is not assig ...

The webpage is missing a rendered React component even though it should be displayed

I am facing an issue where a React component is not appearing on the webpage despite being rendered. I have provided the code and screenshots of the components below for reference. Below is the snippet from the "App.jsx" file: function createCard ...

A tutorial on dynamically adding fields with a dropdown list (populated from a database) and a text box using PHP and JQuery

I have multiple form components that I need to add dynamically, allowing users to add more than one. Firstly, there is a dropdown list with values populated from MySQL, followed by a text box for inquiries entry. The dropdown list displays a list of users, ...

How to retrieve a JSON item without knowing the name of the parent key

When requesting JSON from Wikipedia's API, the URL is: http://en.wikipedia.org/w/api.php?action=query&prop=description&titles=WTO&prop=extracts&exsentences&explaintext&format=json This is how the response is structured: { ...

Positioning of Cloned Element in jQuery

When a user presses enter while in one of the text input fields provided, I want to add a new empty text input field directly below it - and before the other existing ones. The code snippet below currently adds the new field at the end of the container, bu ...

Is it possible to display or hide a spinner within a span using Javascript and AJAX?

When submitting a contact form, I want to display and hide a spinner on the submit button. The spinner should be visible while the form is being submitted and hidden once the submission is complete. Below is the code for my submit button with the spinner: ...

What is the best way to send data from a React.js application to AWS Lambda?

I'm having trouble sending data from my React application to an AWS Lambda function through API Gateway. Here is the code snippet from my React app: const exampleObj = { firstName: 'Test', lastName: 'Person' }; fetch(process.env.R ...

What is preventing the data property from updating in setInterval?

Looking for a way to increase the speed of the props while my marker is moving? Currently, the speed only updates when the pause button is clicked. How can I automatically update this.speed when clicking the speed button? I have defined the speed prop in ...

I am having trouble with my append function even though I checked the console log and did not see any errors (beginner's inquiry)

As I practice working with functions and dynamically creating input fields, I have encountered an issue where I am unable to append my input field to the form. Despite using console.log() and not seeing any errors, I can't figure out what mistake I ma ...

Is AJAX.call functioning properly in every browser except for Firefox?

I encountered an issue with an ajax load in Firefox. Every time I try to load, I keep receiving a message that says 'unreachable code after return statement'. Despite my efforts to find a solution, I have not been successful in resolving it. Inte ...

Applying Styles to Cells Using the Google Sheets API (v4)

Having encountered an issue while using the Google Sheets API (v4) for programmatically creating or updating spreadsheets, I have come across the following problem: According to the documentation (https://developers.google.com/sheets/api/reference/rest/v4 ...

Creating a dynamic list in HTML by populating it with randomly selected words using JavaScript

Check out my awesome code snippet function wordSelect() { var words = ["Vancouver", "Whistler", "Surrey", "Victoria", "Kelowna"]; //List of randomly-selected words from above var selectedWords = []; for(let i = 0; i &l ...

Is it advisable to Utilize ES6 Classes in Javascript for Managing React State?

Is it recommended to use ES6 classes directly as React state? I am interested in creating an ES6 class that: Contains member variables that will trigger re-renders on the frontend when changed. Includes methods that sync these member variables with the ...