Having trouble getting Three.js JSON models to cast shadows properly?

Recently, I've been experimenting with blender exported models in Three.js. I have successfully imported a model and observed how light interacts with the object. While a directionalLight is illuminating the front-facing parts, I'm facing an issue with casting shadows.

Here's what I've set up:

renderer.shadowMapEnabled = true;
renderer.shadowMapType = THREE.PCFSoftShadowMap;
(...)
var directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.position.set(30,10,-10);
directionalLight.castShadow = true;
directionalLight.shadowDarkness = 0.5;
directionalLight.shadowCameraVisible = true;
directionalLight.shadowBias = 0.1;
directionalLight.shadowMapWidth = 1024;
directionalLight.shadowMapHeight = 1024;
directionalLight.shadowCameraRight = 10;
directionalLight.shadowCameraLeft = -10;
directionalLight.shadowCameraTop = 10;
directionalLight.shadowCameraBottom = -10;
directionalLight.shadowCameraFar = 10;
directionalLight.shadowCameraNear = 10;
scene.add(directionalLight);

var loader = new THREE.ObjectLoader();
loader.load("island.json", function(geometry) {
    islandMesh = geometry;
    for(k in islandMesh.children-1){
        islandMesh.children[k].castShadow = true;
        islandMesh.children[k].receiveShadow = true;
    }
    islandMesh.castShadow = true;
    islandMesh.receiveShadow = true;
    scene.add(islandMesh);
    render();
});

Despite setting up what I believe to be the necessary configurations for shadow casting, I am unable to see any shadows being cast. See the screenshot below:

https://i.stack.imgur.com/9pvxC.jpg

Even though the light source is positioned to the right of the island, the mountains are not casting the expected shadows.

Any help or insight on this matter would be greatly appreciated!

Thank you so much in advance!

Answer №1

To successfully implement your code, it is crucial that the data in your json file follows a specific structure where there is one parent and all other elements are children of that parent. (It is curious why you are decrementing in the for loop using -1.)

A more robust approach for all scenarios would be to navigate through the generated geometry tree:

islandMesh.traverse ( function (child) {
    if (child instanceof THREE.Mesh) {
        child.castShadow = true;
        child.receiveShadow = true;
    }
}

Update

In regards to the directional light being utilized, adjustments may need to be made to the

light.shadowCameraNear, light.shadowCameraFar, light.shadowCameraLeft,
light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom

The default values for CameraNear might suffice, but consider setting it back to 1. As for CameraFar, the value should depend on the distance between the camera and the farthest point in the scene.

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

Custom Serialization and Deserialization with Gson in Java for Unique Requirements

Understanding how to implement JsonDeserializer and JsonSerializer is one thing, but my goal goes beyond that: Let's outline the scenario: public class classA { public String a; public classB clazzB; } public class classB { public String c; public ...

A step-by-step guide on transforming objects into arrays using jq

Can anyone help me transform this JSON using jq? [ { "one": 37, "two": "2017-09-15T19:31:55" } ] I need it to look like this: [ { "one": 37 }, { "two": "2017-09-15T19:31:55" } ] If you know how to do this, please share your ...

The login process in Next-auth is currently halted on the /api/auth/providers endpoint when attempting to log in with the

My Next-auth logIn() function appears to be stuck endlessly on /api/auth/providers, as shown in this image. It seems that the async authorize(credentials) part is not being executed at all, as none of the console.log statements are working. /pages/api/au ...

Give drawn elements a touch of fuzziness and experiment with the color of the fragment shader

I am currently experimenting with creating an animated gradient effect by blending three separate blobs together in a melting-like fashion, with each blob moving independently. The desired result can be seen in the image provided below. So far, I have bee ...

Can you use an ajax post to search for a boolean in MongoDB without converting on the server side?

I am facing an issue with my mongo collection that has documents containing a boolean field: { name : "Tom", active : true } { name : "Jerry", active : false } In my application's client side, there is an element that triggers an AJAX po ...

When trying to export a JSON file from an Angular app, the URL is prefixed with 'unsafe' and the download fails with a network error message

Looking for guidance on exporting a JSON file from an angular app. Below is the code snippet I am using: app.js file: app.config(['$compileProvider', function ($compileProvider) { $compileProvider.aHrefSanitizationWhitelist(/^\s*(htt ...

I am trying to showcase a collection of images on my homepage, but unfortunately, it is not functioning as expected

Does anyone know how to display images using Angular? I am currently utilizing the pic controller in an AngularJS file. Any assistance would be greatly appreciated. HTML CODE <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...

the modal body is taking longer than expected to load with ajax

I have a unique modal that I'm filling with dynamic data through an Ajax GET request upon modal load. Interestingly, the data is not fetched or added to the modal body unless I trigger an alert first. The structure of my modal is as follows: <div ...

A Guide to Effortlessly Implementing moment.js into a React TypeScript Application

I'm attempting to implement code splitting and one of the specific packages I want to separate into its own chunk is moment.js. Here's how I'm doing it: const myFunc = async (currentNumber) => { const moment = (await import('momen ...

Interactive planetary visualization using three.js in real-time

As I work on my react project, I'm developing a globe and looking to initialize it accurately with respect to a specific time, whether it be the current time or a future time. The goal is for this globe to correctly align itself with the day and night ...

The redesigned pie chart animations in d3 are experiencing glitches

Previously, I had a pie chart with fantastic animations. You can view it here: https://jsfiddle.net/tk5xog0g/29/ I tried to rebuild the chart and improve it based on my needs, but the animations no longer work as intended. I suspect this might be due to ...

Convert a single JSON array into two separate jquery arrays

I'm currently working on extracting data from a JSON file using my jQuery script. Below is my jQuery code snippet: $.ajax({ type: "GET", url: "settings.json", dataType: "json", cache: false, error: function(){ $('.sl ...

Adding Material-UI icons dynamically in a React TypeScript project requires understanding the integration of imported icons

I have a collection of menu buttons with icons, stored in an array of objects. The icon names are saved as strings that correspond to Material UI icons: interface MenuButton { text: string, onClickFunction: Function icon: string } export defau ...

Using Javascript outside of the AngularJS environment

I am trying to utilize Javascript functions outside the controller in Angular JS instead of using a service within a module. Is this allowed? For instance: var UrlPath="http://www.w3schools.com//angular//customers.php" //this section will store all the f ...

Removing the hash symbol in Angular: A step-by-step guide

My experience with AngularJS is new, and I am currently working on removing the # from the URLs without utilizing NODE or Express. The project is being hosted locally on MAMP, with index.html acting as the main entry point. Within the structure, there are ...

Consistently encountering the message 'Error: timeout of 2000ms exceeded' while using Selenium

Good morning, Currently, I am in the process of learning how to use Selenium with JavaScript (specifically using Mocha). I have created a very basic test that is causing some issues during runtime. Whenever I run the test, a new instance of Chrome opens a ...

What could be causing Typed.js to not function properly in my situation?

Having an issue with typed.js not functioning properly. Here is the code snippet: <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="js/jquery-1.11.3.min.js"></script> <!-- Include all compiled plugins ...

Create a navigation link in Vue-bootstrap that directs to an 'external program'

After deciding to use vue-bootstrap instead of just bootstrap for its additional features like tabs, I made the choice to rewrite the navigation using it as well. However, I encountered an issue where the links in the navigation menu are pointing to the co ...

A guide on executing multiple Post Requests in Node.js

Currently, I am facing some issues with my code while attempting to make multiple post requests based on certain conditions. The goal is to retrieve data from an online database (Firebase), save it locally, and then delete the online data. Here's wha ...

Stop Ajax from activating jQuery function

Upon examining our Drupal site, we discovered a straightforward jQuery script that inserts a div class containing content: (function($) { Drupal.behaviors.myHelpText = { attach: function (context, settings) { //code begins //adjusting placeholder ...