The absence of shadows being cast and received is causing issues with the scene's rendering

I've been diving into threejs through game development. I successfully rendered all models in the scene and added some lights, which turned out perfectly fine. However, when trying to cast and receive shadows on the plane, the object shadows in the scene are not rendering as expected.

I'm puzzled about where I might be going wrong.

Here's a snippet of the code:

Please take a look and assist me in resolving this issue.

import { OrbitControls } from '../assets/js/OrbitControls.js';
import { GLTFLoader } from '../assets/js/GLTFLoader.js';
import { OBJLoader } from '../assets/js/OBJLoader.js';
import { MTLLoader } from '../assets/js/MTLLoader.js';

const extrudeSettings = { depth: 8, bevelEnabled: true, bevelSegments: 2, steps: 2, bevelSize: 1, bevelThickness: 1 };
const logo = {
  position : {
    top: 750,
    back: -2000,
  },
  title: " Exhibition !!!",
  color: 0x006699
};

var camera, scene, renderer, man_walk, mixer, action, keyboard, controls, door, mixerG, mixerB;
var mixerW = new THREE.AnimationMixer();
var man_walk = new THREE.Scene();
var clock = new THREE.Clock();
var gltfLoader = new GLTFLoader();
var objLoader = new OBJLoader();

init();
animate();

function init(){

  // RENDERER
  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.gammaOutput = true
  renderer.shadowMapEnabled = true;
  renderer.shadowMapType = THREE.BasicShadowMap;
  document.body.appendChild(renderer.domElement);

  // SCENE
  scene = new THREE.Scene();
  scene.background = new THREE.Color( 0xC3D8D6 );
  // scene.fog = new THREE.Fog( 0xffffff, 0, 750 );

  // CAMERA
  camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  camera.position.y = 250;
  camera.position.z = 2090;

  // LIGHTS
  const light = new THREE.AmbientLight(0xffffff);
  light.position.set = (0,100,100);
  light.castShadow = true;
  
  scene.add(light);

  var dlight = new THREE.DirectionalLight( 0xaabbff, 0.3 );
  dlight.position.x = 0;
  dlight.position.y = 250;
  dlight.position.z = 2500;
  dlight.castShadow = true;
  scene.add( dlight );

  // KEY EVENTS
  keyboard = new THREEx.KeyboardState();
  // console.log(keyboard);

  // // ORBIT CONTROLS
  // controls = new OrbitControls(camera, renderer.dom...

To view the complete code, please visit:

https://github.com/chintuyadavsara/threejs

Answer №1

After thorough offline testing of your code, it has come to my attention that there are several issues and runtime errors present:

  • AmbientLight doesn't have the capability to cast shadows, resulting in a runtime error when setting castShadow.
  • The shadow frustum configuration for your DirectionalLight instance is not correctly set up. You may resolve this by utilizing the following code snippet:
var dlight = new THREE.DirectionalLight( 0xaabbff, 0.3 );
dlight.position.x = 0;
dlight.position.y = 750;
dlight.position.z = 0;
dlight.castShadow = true;
dlight.shadow.camera.top = 2500;
dlight.shadow.camera.bottom = -2500;
dlight.shadow.camera.left = -2500;
dlight.shadow.camera.right = 2500;
dlight.shadow.camera.near = 1;
dlight.shadow.camera.far = 1000;
dlight.shadow.mapSize.set(2048, 2048);
  • When loading a glTF asset, simply setting gltf.scene.castShadow to true will not allow shadows to be cast. It's essential to recursively set this attribute for all meshes within the scene.
gltf.scene.traverse(function (object) {

    if (object.isMesh) object.castShadow = true;

});
  • Incorrect paths to assets in the GitHub repository have been identified. For example, instead of
    /assets/3d_models/torii_gate/scene.gltf
    , the correct path should be
    ../assets/3d_models/torii_gate/scene.gltf
    .
  • Several deprecation warnings have been logged in the browser console. To address this, I recommend configuring the renderer as follows (and maintaining the default shadow map type):
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.shadowMap.enabled = true;
  • Due to the nature of being an unlit material, MeshBasicMaterial cannot receive shadows. Consider utilizing MeshPhongMaterial for tiles and floors to ensure proper shadow rendering.

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

Reasons for storing `https.get()` inside a request constant in Node.js

When using simple javascript, any content written to the console is displayed on the console itself. An example would be as follows: const name = "david"; This will display david in the console. However, in Node.js, if I store a request in a const varia ...

Navigating JSON data to retrieve a specific property in AngularJS using a select form

Struggling with AngularJS and JSON. I have a form.html view where users can select their province. I have a Province JSON file for the select tag, but when storing in MySQL, I need the province Id. I tried using ng-value="province.id" in the option tag but ...

Ways to select the element based on a specific CSS property value in the inline style

As a novice in the world of Javascript, I am currently attempting to select an element within an array of images where the opacity is set to 1. Could someone please guide me on how to achieve this? I've tried using hasAttribute, but I'm unsure ho ...

Incorporating the JQuery plugin Lightbox_me into a Ruby on Rails application

Greetings! I am currently attempting to incorporate a popup window using the Lightbox_me plugin in my Ruby On Rails application. After downloading jquery.lightbox_me.js and placing it in the app/assets/javascripts directory, I added //= require jquery.lig ...

Angular2 Window Opener

Trying to establish communication between a child window and parent window in Angular 2, but I'm stuck on how to utilize window.opener for passing a parameter to Angular 2. In my previous experience with Angular 1.5, I referenced something similar he ...

Tips on customizing Google Analytics code with Google Tag Manager

My website is currently equipped with the Google Analytics tracking code implemented through Google Tag Manager. The standard Google Analytics code typically appears as follows: <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']= ...

Discover how to shallow test for the presence of a wrapped component using Jest and Enzyme

Currently, I am in the process of testing a component that dynamically renders another wrapped component based on certain conditions. My testing tools include enzyme and jest, with the root component being rendered using the shallow() method. The main chal ...

Divide the string into several segments according to its position value

Here is a piece of text that I would like to divide into multiple sections, determined by the offset and length. If you have any questions or comments and would like to get in touch with ABC, please go to our customer support page. Below is a function ...

What is the best way to create floating text that appears when clicked, similar to the mechanics

https://i.stack.imgur.com/nRKG1.jpg Upon clicking the "big cookie" in my game, a popup appears displaying the number of cookies earned (like +276.341 septillion in this image). The popup slowly moves upward and then fades out. I successfully incorporated ...

What is the best way to connect or link to a HTML table row <tr>

Is there a way to trigger an alert when the user double clicks on the whole row? ...

The use of callbacks is ineffective in addressing the asynchronous nature of

Hello everyone, I'm currently working on a weather app and I'm facing an issue with the asynchronous behavior of useState. I've come across some suggestions on Stack Overflow that using a callback in the useState function might solve the pro ...

What steps can I take to avoid Vue.js overriding jQuery bindings within components?

After incorporating vue.js into an existing project and attaching the vue instance to the body tag with an ID of "app," everything seemed to be running smoothly. jQuery and Vue.js were cooperating well together. However, as soon as I started creating compo ...

Issues with border removal due to overflow content in CSS and AngularJS

There seems to be a peculiar issue with overflow content in my application. When you scroll horizontally on the codepen, you'll notice that the border on the .track or perhaps the .cell divs disappears. Any assistance would be greatly appreciated. HT ...

The preventDefault method is failing to prevent the default action when placed within a

I am having trouble using preventdefault to stop an action. I'm sorry if the solution is obvious, but I can't seem to locate the mistake. Why isn't it preventing the link from being followed? Here is a link to my jsfiddle: http://jsfiddle.ne ...

MUI: Autocomplete received an invalid value. None of the options correspond to the value of `0`

Currently, I am utilizing the MUI autocomplete feature in conjunction with react-hook-form. I have meticulously followed the guidance provided in this insightful response. ControlledAutoComplete.jsx import { Autocomplete, TextField } from "@mui/mater ...

Manipulate form fields using checkboxes in jQuery

I am trying to implement a feature where an input box and a button are disabled when a checkbox is clicked. This is my HTML markup: <h4>Random or Not </h4> <!-- Use Random Image --> <label><input name="o99_aufu_opti ...

Exploring methods to access specific values from an array containing multiple values using Lodash in Angular 4

Hey, I have an array that looks like this: [ 0: "Migration, MD" 1: "Lution, MD" 2: "Mover, MD" 3: "Dee" 4: "Prov10A" ] I would like to extract the values that contain the word "MD" in them. In other words, I want a result like this: [ 0: "Migratio ...

Tips for incorporating VueJS 2 global components within single file components

As I attempt to utilize a globally registered component (using Vue.component) within a single file component, I consistently encounter the following warning: vue.common.js:2611[Vue warn]: Unknown custom element: <my-component> - did you register the ...

Javascript unable to update the DOM

I've been working on a project to prototype a simple functionality using JavaScript for learning purposes. However, I've encountered an issue where the contents within the <p> tag are not updating, and I'm currently stuck. Here's ...

Calculating and displaying the output on an HTML page: A step-by-step guide

Within my HTML, I have two values stored in Session Storage: "Money" and "Time". These values are based on what the user inputted on a previous page. For example, if someone entered that they need to pay $100 in 2 days. My goal is to generate a list that ...