Synchronizing Trackball Control with Directional Lighting in ThreeJS

In my current project, I am utilizing three.js to render objects. The setup includes directional lighting and trackball controls. I understand that the light is static while the camera moves with the controls.

However, I want the light to move along with the camera's movement so that the lighting conditions remain constant at every viewpoint. This will create the illusion of the object rotating instead of the camera.

This is what I have implemented:

camera = new THREE.PerspectiveCamera(45, width / height, 0.01, 1000);
camera.position.z = 3;

light = new THREE.DirectionalLight(0xAAAAAA, 1);
light.position = camera.position;
scene.add(light);

Attempts made:

  1. Exploring various approaches using different types of lighting techniques (though favoring the current one).
  2. Trying to update the light position within the Render loop, without success.

The result is that the light remains stationary on the opposite side of the object, creating an odd effect. It's expected that since the light's position matches the camera's, it would illuminate from the same direction.

Update:

After adding the following lines:

light.castShadow = true;
light.shadowCameraVisible = true;

I noticed the frames outlining the cast lights. While the light's position changes, the actual illumination does not move accordingly. Is there a setting needed to refresh the light's position?

Any assistance would be greatly appreciated.

Answer №1

To give the appearance of spinning, consider using a special technique:

Try incorporating a PointLight as a child element of the camera rather than the scene by using the following code: camera.add( light );, and adjust the position of the point light like this: light.position.set( 10, 10, 0 );

Additionally, remember to include the camera as a child of the scene as well with: scene.add( camera );

This tip applies to three.js version r.66

Answer №2

During my experiment, I successfully linked the directional light to the camera, resulting in a light source emanating from the side. By utilizing the 'directional light' helper, one can visualize the effect of this illumination.

  var directionalLight = new THREE.DirectionalLight( 0xffffff, 1.2); 
  directionalLight.position.set( 500, 100, 0 ); 
  directionalLight.target.position.set( 0, 0, 0 ); 
  
  dlightHelper = new THREE.DirectionalLightHelper(directionalLight, 50); 
  camera.add( dlightHelper);
  
  camera.add( directionalLight );

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

Unchecked box

Is there a way to create a checkbox that triggers a timing event when checked, and then stops the event when unchecked? Currently, it seems like the function continues even after unchecking the box, causing the variable to increment at a faster rate. Live ...

Adding dynamic values to nested form groups in Angular Form Array

After following a tutorial on creating a reactive form in an Angular application, I managed to implement it successfully. However, I encountered an issue when trying to add an additional control called "setNumber" to the form array. I want this control to ...

The Colorbox class is not compatible with AJAX or DOM objects

Typically, I utilize the colorbox tool to open "popup windows" on my page and everything works smoothly. However, in my latest project, things are a bit different as I am using js/ajax/dom to dynamically create my objects in the handleRequestStateChange() ...

Error Message: Unable to Modify Headers Once Sent - NodeJS

I encountered an issue with my node.js app where one of the routes keeps throwing a "Can't set headers after they are sent error". Description of the route: In my application, users have different access levels. This specific route checks the user&a ...

Avoiding a constantly repeating video to prevent the browser from running out of memory

Using HTML5, I created a video that loops and draws the frames to canvas. I decided to create multiple canvases and draw different parts of the video on each one. However, after some time, I encountered an issue where Google Chrome would run out of memory. ...

Retrieve the scrollTop, scrollLeft properties, and other scroll-related data from an element using Selenium

When testing a Python/Django element that is scrolled using scrollTop, I am trying to retrieve the value of scrollTop. In JavaScript, I can access this value with: element.scrollTop I attempted to do this in Python using: element.get_attribute('sc ...

Assigning a date input value during a Protractor test

I am currently working on an Ionic hybrid mobile application and in the process of creating some protractor tests. For testing against a simulator version of the app, I am relying on Appium. One particular challenge I've encountered involves a regist ...

Creating interactive tooltips in Shiny applications with the help of ShinyBS

I am attempting to incorporate the shinyBS package into my basic app. My goal is to generate dynamic tooltip text based on each radioButton selection. To better illustrate my issue, I have drafted a simple code snippet in HTML & JS. While I came acro ...

Tips on employing useState within useEffect?

Attempting to refactor my component from react.component to hooks has been a bit challenging. I am struggling to properly utilize the state variable offsetTop. It seems like the value is not being set when needed. In my first attempt: const [offsetTop, se ...

What is the best way to implement a user-customizable dynamic URL that incorporates API-generated content in a NextJS and React application?

Seeking assistance with implementing customizable dynamic URLs in Next.js with React. My current project involves a Next.js+React application that uses a custom server.js for routing and handling 'static' dynamic URLs. The goal now is to transiti ...

Issues with processing the $ajax URL in Ajax and jQuery calls are causing malfunctions

Allow me to provide a clearer explanation. My goal is to dynamically update the content of an HTML element based on data received from a REST API. The process involves making an AJAX call to the API URL and then updating the content accordingly. <butt ...

JavaScript triggers page reload upon selection box click

Here is a sample URL for reference: [link removed] The code snippet I am currently using is: $('.jump-submit').on('change', function(e) { var $this = $(this); $this.closest('form').submit(); }); However, when attemp ...

How to access a function defined within the jQuery ready function externally

My ASPX page content:- <script src="js/jquery-1.4.2.js" type="text/javascript"></script> <script src="js/jquery-ui-1.8.2.custom.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready ...

Show image in ReactJS using flask send_file method

Using Flask's send_file function, I send an image to the client in the following way: @app.route('/get-cut-image',methods=["GET"]) def get_cut_img(): response = make_response(send_file(file_path,mimetype='image/png')) respon ...

When using the Ng --version command on a development package, it throws an error

I encounter an error with a development package when cloning a repository. I would greatly appreciate any advice on how to resolve this issue. https://i.stack.imgur.com/DBp5r.png ...

Generating a fresh array by filtering out elements with specific properties using the .map() method

Within my React application, there exists an array containing key value pairs where each pair corresponds to a checkbox. Upon checking the checkbox, the value of the corresponding key switches between true and false. The structure of the data retrieved is ...

Tips for automatically expanding all nodes with children when the page loads in the Wix Angular tree control

Is there a way to automatically expand all nodes with children when the page loads in an Angular tree control? The tree control is full of useful features, but this specific functionality seems to be missing. It does have a property for expanded nodes. Do ...

The distinction between storing data and component data becomes apparent when using Vuex in conjunction with a persisted state

Below is my post.js file in the store directory: import axios from 'axios' import createPersistedState from "vuex-persistedstate" export default { namespaced: true, state: { sample_data: 'Welcome!!', l ...

What is the process for incorporating an animated gif into a scene?

I'm trying to incorporate an animated gif in three.js. What's the best way to do it? var materialTextured = new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture('images/pin.gif'), transparent: true, ...

Ways to avoid storing repeating paragraphs in JavaScript Array?

Can someone help me with my code? I am struggling to prevent duplicate data from being saved into an array. When I click on any two paragraph elements, the text inside them gets added to an array named `test`. However, I want to avoid saving the same tex ...