"In the most recent version of THREE.js (r82), the shadow is not detectable

I am having trouble casting a shadow from a cube onto a plane using MeshLambertMaterial in my code. Despite setting up the scene correctly, the shadows are not appearing as expected.

Most solutions I have come across suggest that objects should be within the frustum for shadows to work properly. However, upon inspection, it seems that the object is indeed inside the frustum.

If you can identify any mistakes or issues in my implementation, please let me know. Any guidance or assistance would be highly appreciated. I have provided a link to a fiddle showcasing my code: https://jsfiddle.net/44jwvbt6/

// import Ticker from 'timing/Ticker';

class World {

    constructor() {

        this._renderer = new THREE.WebGLRenderer({ antialias: true });
        this._renderer.shadowMap.enabled = true;
        this._renderer.shadowMap.type = THREE.PCFShadowMap;
        // this._renderer.shadowMap.cullFace = THREE.CullFaceBack;

        this._scene = new THREE.Scene();
        this._scene.background = new THREE.Color( 0x333333 );

        const width = window.innerWidth;
        const height = window.innerHeight;

        this._camera = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, -400, 400 );
        this._camera.position.set(0, 0, 100);

        const geometry = new THREE.BoxGeometry(100, 100, 100);
        const material = new THREE.MeshLambertMaterial({ color: 0xff0000 });

        const light = new THREE.DirectionalLight(0xffffff, 1);
        light.position.set( 0, 200, 400 );
        light.castShadow = true;
        light.shadow.camera.near = -1000;
        light.shadow.camera.far = 2000;
        // light.shadow.camera.fov = 30;
        light.shadow.bias = 0.0038;
        light.shadow.mapSize.width = 1024;
        light.shadow.mapSize.height = 1024;
        // light.shadow.camera.left = -width / 2;
        // light.shadow.camera.right = width / 2;
        // light.shadow.camera.top = -height / 2;
        // light.shadow.camera.bottom = height / 2;
        this._scene.add(light);
        this._scene.add(new THREE.CameraHelper( light.shadow.camera ));

        const mesh1 = new THREE.Mesh(geometry, material);
        mesh1.position.set(0, 0, 100);
        mesh1.castShadow = true;
        this._scene.add(mesh1);

        const plane = new THREE.PlaneGeometry(width, height);
        const mesh4 = new THREE.Mesh(plane, material);

        // mesh2.castShadow = true;
        // mesh3.castShadow = true;
        mesh4.receiveShadow = true;
        this._scene.add(mesh4);

        const controls = new THREE.OrbitControls( this._camera, this._renderer.domElement );

        window.addEventListener('resize', this._resize.bind(this));

        this._resize();

        this.animate();
        // this._ticker = new Ticker(1000 / 60);
        // this._ticker.onTick(this.animate.bind(this));

    }

    get element() {
        return this._renderer.domElement;
    }

    get scene() {
        return this._scene;
    }

    _resize() {
        this._renderer.setSize(window.innerWidth, window.innerHeight);
    }

    animate() {
        this._renderer.render(this._scene, this._camera);
        requestAnimationFrame(this.animate.bind(this));
    }

}

const world = new World;
document.body.appendChild(world.element);

// export default World;

Answer №1

There are two issues to address here: first, the re-use of materials is causing a problem that needs solving.

const mesh4 = new THREE.Mesh(plane, new THREE.MeshLambertMaterial({ color: 0xff0000}));

The second issue is related to the directional light. Make sure you uncomment the lines that allow the light to cast shadows within a specific area defined by properties like light.shadow.camera.*.

light.shadow.camera.left = -width / 2;
light.shadow.camera.right = width / 2;
light.shadow.camera.top = -height / 2;
light.shadow.camera.bottom = height / 2;

Try making these adjustments and see how it affects your project: https://jsfiddle.net/nrbojkoo/1/

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

Locked first row and first column in HTML table

I'm struggling with freezing the first row and column in an HTML file exported from Microsoft Excel. When attempting to add position:fixed; to achieve this, I noticed that it changes the size and alignment of the headers. Can someone please advise me ...

You have encountered an error: Uncaught TypeError - the function (intermediate value).findOne is not defined

Encountering an error when attempting to call the getStocks function from a Vue component. smileCalc: import User from "../models/user.js"; let userID = "62e6d96a51186be0ad2864f9"; let userStocks; async function getUserStocks() { ...

Guide on transforming UTC time from the server to the local time of users during a GET request

I am currently facing a challenge where I need to verify if the date of the last time an element was clicked matches the current date. Due to my server generating the current date which is 5 hours ahead of my local time, there is a discrepancy causing the ...

Nested JSON array is being shown as [Object Object] within a TextField

I am facing an issue with mapping two sets of JSON data, which I will refer to as Set 1 and Set 2. In Set 1, the data (named sizingData) is being mapped to text fields using the following function: const fillTextField = () => { let result = []; ...

What is the method for retrieving a value from my Node.js module once it has been modified by an asynchronous function?

Apologies, but as a beginner developer, I'm struggling to see how this relates directly to the questions already mentioned. I have no understanding of ajax and I'm unsure how to adapt the solutions provided to my own situation. Currently, I&apos ...

Ways to embed one block of javascript code within another block of javascript code

Can you help me with inserting the following code into a specific part of my JavaScript code? The issue I am facing is that the code contains all JavaScript, and when I directly add it, the gallery crashes. <div id='gallerysharebar'> & ...

Is it possible to dynamically assign trigger and target divs to bPopup?

On our ColdFusion page, we have a dynamic list of paired divs created using a loop over a query. For example, each pair consists of internshipHandleX and internshipHiddenX: <div id="internshipHidden#internship.currentrow#" class="hidden pop-up"> We ...

One jQuery plugin was functioning perfectly, while the other one was failing to work as expected

I am working on a simple HTML file that includes a heading and two empty div elements. <h1>Blablabla.</h1> <div id="div1"></div> <div id="div2"></div> These two divs need to be populated with SVG content generated usin ...

Tips for updating text in a freshly opened window using JQuery or JavaScript

I have a function that is triggered by a button click to open a new window or tab, display an alert, and update text. Here is the code snippet: function nWin(p) { var setStyle = "<style rel='stylesheet'>\ .vTop {\ ...

Exploring the process of setting up reactive forms with AngularJS to handle arrays

I am working on setting up a reactive form for AngularJS and need to initialize certain fields: initializeBusinessEposForm(): void { this.businessEposForm = this.formBuilder.group({ custom_pos_priority: new FormControl(false), custom_float_ ...

Unable to specify a particular <div> for fetching data using jQuery AJAX in ASP.NET

While working on the following Javascript code, I encountered an issue where the text specified inside the CommentItem div I am appending is not displaying. However, my main concern revolves around targeting ContainerPh which is located above the txtCommen ...

The PDF file cannot be displayed due to the dynamic loading of the path and filename

Currently, I am working on an AngularJS and Java application. The following HTML code is utilized to open a PDF file in the browser. However, there seems to be an issue where the PDF does not open when dynamically passing the value for the data attribute. ...

Chart showing a Google Timeline: Allow for each bar to be colored differently when there are multiple bars on the same line

It appears that the Google Timeline charts have a feature to color individual blocks on the timeline as indicated in the documentation at However, there seems to be an issue when two bars overlap on the same line, which is evident in this fiddle: http://j ...

Animating transitions on a dynamic Bootstrap navbar using jQuery

I have the code attached here, and I successfully changed the color of the navbar and logo when scrolling down. However, I am looking to add a transition effect when this change occurs. I experimented with various transition options in CSS, but unfortunat ...

Adjust the text color based on the background image or color

Here on this site, I have designed the first div to display a dark image, while the second one shows a light background. I am aiming to adjust the sidebar text color based on whether it is displayed against the dark or light background. How can I achieve ...

Laravel 5.0 facing issues with AJAX requests

For my Laravel 5.0 project, I am utilizing Google API's for Google Oauth login. After successfully retrieving the email and id_token of the currently logged-in user, I aim to send this data to the SigninController to access our own API. The goal is to ...

Ajax Syntax Error: Unexpected Token U

I have been struggling all day with an issue while trying to send json data via ajax to Express. Here is how my ajax code looks like: $('#saveClause').click(function () { var username = document.getElementById('postUserName').inne ...

Misunderstanding the Variable Scope Concept in Node.js

I am struggling to comprehend why the return of an array from another function is confined to only one block of code. For example: exports.join = function(req, res){ User.findById(req.user._id, function(err, user) { var dupe = []; //placeholder arr ...

Why is it that useEffect is functioning correctly only on the first occasion?

Recently, I've been facing significant challenges with React's useEffect and States. The issue arises when I redirect to the main page of my app and then attempt to use them again. In the following component, everything seems to function correct ...

Error: Attempting to retrieve a refresh token from the Google API resulted in an Uncaught ReferenceError, as the

I am currently working on obtaining a refresh token once a user authorizes with Google in order to prevent the need for re-authorization. After studying Google's documentation, I learned that setting the access type to offline is necessary. Now, I am ...