Shadows are displaying properly on a cube within Three.js, but they are not appearing on a JSON mesh

I'm having trouble getting my racecar to cast a shadow on the floor. While the cube is successfully casting a shadow, the imported json mesh object racecar is not. I'm wondering if this has something to do with the json file baking on the materials. Should I add the code obj.castShadow = true; to a child of the obj? If so, which child should it be placed on? What mistake am I making here?

Here's the link to the json file and you can also find a demo here

Below is the relevant code snippet:

    var loader = new THREE.ObjectLoader();  
    loader.load("models/ferrari-f1-race-car.json", function (obj) {
        obj.castShadow = true;
        obj.scale.set(50,50,50);
        obj.position.x = 30;
        obj.rotation.y = Math.PI/1;
        scene.add(obj);
    });

    boxgeometry = new THREE.BoxGeometry(100, 100, 100);
    boxmaterial = new THREE.MeshLambertMaterial({
        color: 0x0aeedf
    });
    var cube = new THREE.Mesh(boxgeometry, boxmaterial);
    cube.castShadow = true;
    cube.position.x = -80;
    cube.position.y = 50;
    cube.position.z = 0;
    scene.add(cube);

    function createFloor(){ 
        floor = new THREE.Mesh(new THREE.PlaneBufferGeometry(1000,500), new THREE.MeshBasicMaterial({color: 0x8594a2}));
        floor.rotation.x = -Math.PI/2;
        floor.position.y = -0;
        floor.castShadow = false;
        floor.receiveShadow = true;
        scene.add(floor);
    }

Answer №1

Creating shadows can be resource-intensive and may not work well with custom objects, as bugs are common.

An effective solution is to generate a shadow texture on a transparent plane below the object. However, this approach may present challenges if the object or light source needs to rotate, leading to issues with maintaining accurate shadows at different angles. Authors can employ various techniques, such as rotating the camera or adjusting bias settings, to address these limitations without compromising the quality of the renderings. Examples of impressive shadow rendering can be found at , showcasing the use of simple images like:

https://i.sstatic.net/dTYWa.jpg

Moreover:

https://i.sstatic.net/aBEkr.png

To explore a photorealistic demonstration, visit

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

What is the mechanism behind AJAX functioning without the need to refresh the page?

I came across a code (not written by me) for a button that allows users to bookmark a person and add them to a shortlist. Here's how the code looks: Here is the JS & Ajax portion: self.isMarked = ko.observable(@(Model.Application.IsMarked ? "tru ...

Store live text field group in their respective index

I am working on a project that involves creating dynamic description textareas based on the value selected from a dropdown list. The dropdown list contains numbers 1 through 5, and for each number chosen, I need to generate corresponding textareas with ser ...

AngularJS is incapable of detaching forms from objects

Creating forms dynamically using ng-repeat and adding them to the vm.forms object is causing issues. When an item is removed from the array, the corresponding form is deleted but the key in the vm.forms object remains, leading to undefined errors. angul ...

Receiving output from a JavaScript function:

Can this snippet be used in JavaScript without any negative consequences? function Individual() { this.name = function() { return; } } ...

Locate the intersection point of an arc using three.js

I am working with a unique type of geometry called TorusGeometry. To achieve what I need, I must translate this geometry to a specific point labeled as C, which is the intersection point of perpendicular lines drawn from the center points of both ends labe ...

Utilizing Mongoose Schema for CSV Import

My current task involves importing a large CSV file into a mongo DB, where the order of the values will determine the key for each entry in the database: Here is an example of the CSV file structure: 9,1557,358,286,Mutantville,4368,2358026,,M,0,0,0,1,0 9 ...

Determining the top element displayed in a div: A guide

Looking for an answer on how to determine the top visible element while scrolling? You may find some insights in this post: (Check if element is visible after scrolling). My scenario is slightly different - I have a scrollable div containing multiple ele ...

Implement scroll bar functionality on canvas following the initial loading phase

I am currently working with canvas and I need to implement a scroll bar only when it is necessary. Initially, when the page loads, there isn't enough content to require a scroll bar. My project involves creating a binary search tree visualizer where u ...

What is the process for invoking a server-side C# method from AJAX while transmitting parameters to the function using CommandArgument?

My C# method is responsible for saving data to a SQL Server. It is called from an Onlick event, passing parameters using CommandArgument. Here is an example: <asp:LinkButton runat="server" onClick="save" CommandArgument='<%# Eval("post_id").ToS ...

Implementing local storage for a specific user in the initial state using React

Hey there, I'm facing an issue with my code. I'm working on a bank project where I need to store a specific user in local storage to avoid errors when the page is reloaded. The problem arises because the initial state of users is an empty array. ...

Concealing the flexslider in Angular when accessing the main URL

I need to hide a div with flexslider in it on the root page using ng-hide. The issue is that the images do not load when navigating to another path. Here is how my index.html is structured: <ul> <li><a href="#/">Root</a> ...

Assign a value of zero to the currentItem variable upon changing to a different category

Currently, I am developing a news application that utilizes a card-based layout for scrolling through news items. In the implementation, there is a variable called currentItem that keeps track of the current index being viewed. A recurring issue arises w ...

Angular 4 Password Regular Expression Not Working

My validation regex checks for at least 8 characters, including one number, one uppercase letter, one lowercase letter, and one special character. Here is the regex I am using: '(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-zd$@$!%*?& ...

use ajax to dynamically append a dropdown menu

Currently working on creating a form that includes a dropdown menu populated with elements from a database. The challenge I'm facing is ensuring that once an element is selected from the dropdown, another one appears dynamically. My goal is to use AJA ...

Uncertainty surrounding the distinction between global variables (var) and block variables (let, const) in Node.js

It's common knowledge that var is a global variable in node js and can be accessed from anywhere. However, I found myself perplexed by the following examples: In the first example, accessing global_1 poses no issues as it is a global variable. var g ...

React error: Unexpected token < in JSON when fetching local file

I've encountered an issue with my React app. I have a JSON file in the public directory that I was successfully fetching data from in my main App.js file without any errors. However, after running `npm run build`, I started getting the error message: ...

An individual in a chat App's UserList experiencing issues with incorrect CSS values. Utilizing Jquery and socketio to troubleshoot the problem

Currently, I am testing a new feature in a chat application that includes displaying a user list for those who have joined the chat. The challenge is to change the color of a specific user's name on the list when they get disconnected or leave the cha ...

PHP and AJAX for streamlined login form experience

Trying to log in through a popup login box using AJAX to determine if the login is successful. If successful, redirect to the header location, otherwise display an error message. Here is the code snippet: <script> $(document).ready(function () { ...

The transfer of JSON information from View to Controller yields no value

My goal is to create functionality where users can add and delete JQuery tabs with specific model data, and then save this data to a database. I'm attempting to use an ajax call to send JSON data to the controller, but I am encountering an issue where ...

Troubleshooting Angular JS loading problems

I'm attempting to implement the Angular-Spinner in my project: Specifically, I want to use it with http.get calls. This is what I have so far: Within controllers: $scope.loading = true; $http.get('js/data/test.json').success(function(resu ...