Open boxes with walls that don't create shadows

Currently, I am facing an issue with an open-sided box created using MeshStandardMaterial and BoxGeometry. I have configured the box to cast and receive shadows, but it is not behaving as expected. I anticipated the interior of the box to darken when the point light shines through the open side.

(I've embedded it as a snippet, albeit with OrbitControls disabled because I couldn't integrate it from the CDN)

https://i.sstatic.net/isFbl.gif

/**
 * Debug Panel
 */
const dat = lil;
 
const gui = new dat.GUI({width: 500})


/**
 * Base
 */
// Canvas
const canvas = document.querySelector('canvas.webgl')

// Scene
const scene = new THREE.Scene()




/**
 * Open Box
 */
var material1 = new THREE.MeshStandardMaterial( { color: 0xff0000, side: THREE.DoubleSide, shadowSide:THREE.DoubleSide  } );
var material2 = new THREE.MeshStandardMaterial( { color: 0x00ff00, side: THREE.DoubleSide, shadowSide:THREE.DoubleSide } );
var material3 = new THREE.MeshStandardMaterial( { color: 0x0000ff, side: THREE.DoubleSide, shadowSide:THREE.DoubleSide } );
var material4 = new THREE.MeshStandardMaterial( { color: 0xffff00, side: THREE.DoubleSide, shadowSide:THREE.DoubleSide } );
var material5 = new THREE.MeshStandardMaterial( { color: 0x00ffff, side: THREE.DoubleSide, shadowSide:THREE.DoubleSide } );
var materialTransparent =  new THREE.MeshStandardMaterial( { transparent: true, opacity: 0, wireframe: true, side: THREE.DoubleSide} );

var geometry = new THREE.BoxGeometry( 5, 1, 1 );
var materials = [ materialTransparent, material1, material2, material3, material4, material5 ]

var openBox = new THREE.Mesh( geometry, materials );
openBox.receiveShadow = true;
openBox.castShadow = true
scene.add( openBox );

gui.add(openBox.position, 'y', -3, 3, 0.001)
    .name("Open Box Y-pos")


/**
 * light
 */

const ambientLight = new THREE.AmbientLight(0xffffff, 0.2)
scene.add(ambientLight)

const pointLight = new THREE.PointLight(0xffffff, 1);
pointLight.position.x = 3
pointLight.castShadow = true
scene.add(pointLight)

const sphereSize = 1;
const pointLightHelper = new THREE.PointLightHelper( pointLight, sphereSize );
scene.add( pointLightHelper );


/**
 * Sizes
 */
const sizes = {
    width: window.innerWidth,
    height: window.innerHeight
}



const lights = {
    pointLight: pointLight,
    toggleLight: function(){
        this.pointLight.visible = !this.pointLight.visible
    },
    intensity: pointLight.intensity
}
// const directionalLightHelper = new THREE.DirectionalLightHelper( directionalLight );
// scene.add( directionalLightHelper );

gui.add(lights, 'toggleLight').name("Toggle light")
gui.add(pointLight, 'intensity', 0, 3, 0.05)
gui.add(pointLight, 'decay', 0, 100, 0.5)
gui.add(pointLight.position, 'x', 0, 10, 0.05)



window.addEventListener('resize', () =>
{
    // Update sizes
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    // Update camera
    cameras.cam1.aspect = sizes.width / sizes.height
    cameras.cam1.updateProjectionMatrix()

    // Update renderer
    renderer.setSize(sizes.width, sizes.height)
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})

/**
 * Cameras
 */

const cameraFov = 50;
const boxHeight = 1;

const cam1 = new THREE.PerspectiveCamera(cameraFov, sizes.width / sizes.height, 0.1, 100)
const cam2 = new THREE.PerspectiveCamera(cameraFov, sizes.width / sizes.height, 0.1, 100)


//object used to change between the two cameras
const cameras = {
    cam1: cam1,
    cam2: cam2,
    currentCamera: cam1,
    changeCamera: function(){
        this.currentCamera = (this.currentCamera === cam1) ? cam2 : cam1;
    }
}
gui.add(cameras, 'changeCamera')

cam1.position.z = 3
cam2.position.x = boxHeight / 2 / Math.tan(Math.PI * cameraFov / 360);
cameras.cam2.lookAt( 0, 0, 0 );

scene.add(cameras.cam1)
scene.add(cameras.cam2)

const helper1 = new THREE.CameraHelper( cameras.cam1 );
const helper2 = new THREE.CameraHelper( cameras.cam2 );
scene.add( helper1 );
// scene.add( helper2 );


// Controls
//const controls1 = new OrbitControls(cameras.cam1, canvas)
//const controls2 = new OrbitControls(cameras.cam2, canvas)
//controls1.enableDamping = true
//controls2.enableDamping = true

/**
 * Renderer
 */
const renderer = new THREE.WebGLRenderer({
    canvas: canvas
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))

/**
 * Animate
 */
const clock = new THREE.Clock()

const tick = () =>
{
    const elapsedTime = clock.getElapsedTime()

    // Update controls
    if (cameras.currentCamera === cam1){
        //controls2.enabled = false;
        //controls1.enabled = true;
        //controls1.update()
    } else {
        //controls1.enabled = false;
        //controls2.enabled = true;
        //controls2.update()
    }

    // Render
    renderer.render(scene, cameras.currentCamera)

    // Call tick again on the next frame
    window.requestAnimationFrame(tick)
}

tick()
*
{
    margin: 0;
    padding: 0;
}

html,
body
{
    overflow: hidden;
}

.webgl
{
    position: fixed;
    top: 0;
    left: 0;
    outline: none;
}
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9df1f4f1b0fae8f4ddadb3acabb3ac">[email protected]</a>/dist/lil-gui.umd.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Open Box</title>
</head>
<body>
    <canvas class="webgl"></canvas>
</body>
</html>

Answer №1

The issue arose from the absence of:

renderer.shadowMap.enabled = true

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

"Enhancing User Experience with Bootstrap's masonry layout feature while retaining scroll position

I'm faced with a puzzling situation and I can't seem to find the solution. Currently, I am utilizing Bootstrap 5 along with the masonry layout as recommended here: https://getbootstrap.com/docs/5.0/examples/masonry/, and it is working perfectly. ...

Ways to rejuvenate an angular component

I am in the process of developing a mobile application using ionic 4. The app supports two languages, namely ar and en. The menu drawer is a pre-built component included within the app. In order to ensure that the drawer component displays the correct sty ...

Cleaning a string of word characters in Javascript: a step-by-step guide

I have been working on cleaning strings that were transformed from word text, but I am facing an issue with removing the special character '…' When I click on the "clean" button, the script currently removes all dots and only one special ...

Position an HTML canvas at the very top of the webpage

Can someone help me figure out how to align a canvas element to the very top (0, 0) of a webpage? I attempted using margin: 0; padding: 0px;, but it didn't work. There always seems to be some white space at the top that I can't eliminate. ...

What is the correct way to update the state in an array of objects using useState?

I'm struggling to figure out how to use the React useState hook properly. In my todolist, I have checkboxes and I want to update the 'done' property to 'true' for the item that has the same id as the checkbox being clicked. Even th ...

Unable to send a String to the controller via ajax

Exploring web development using play framework and ajax. I'm looking to pass a string from a form to a controller using ajax, but unsure of how to go about it. Can anyone assist me with this? Here's my code snippet: html: <form onsubmit="retu ...

When the margin-left changes, SVG begins to flicker and shake in a marquee effect

I've been experimenting with a marquee effect using vanilla JS. The effect is working, but I'm encountering some shaking issues with SVG and images as they move. <div class="marquee"> <h1>Nepal <svg version="1.1&qu ...

Asynchronously retrieve the result from a previous NodeJS chain and forward it to a nested function for processing

When uploading an image from the client as Base64 to the server, I need to: Save the file to disk Get the result of the save (first chain) and pass it to the next chain Check the result in the next function using that(), if it's true, update the dat ...

Implementing dynamic checkbox values depending on a selection from a dropdown menu in Angular

In my checkbox list, I have various Samsung mobile models and two offers available. $scope.offers = [ { id: "as23456", Store: "samsung", Offer_message:"1500rs off", modalname: "Samsung Galaxy You ...

The fixed positioned div with jQuery disappears when scrolling in Firefox but functions properly in Chrome, IE, and Safari

Click here to see a div located at the bottom of the right sidebar that is supposed to behave as follows: As you scroll down the page, the div should change its class and become fixed at the top of the screen until you reach the bottom of the parent el ...

Combine various input data and store it in a variable

I am looking for a way to add multiple input text values together and store the sum in a variable. Currently, I can add the values and display it in an id, but I want to assign it to a variable instead. The condition for this variable is described below af ...

Utilizing Google Maps to generate outcomes for an online platform

My approach with Google Maps is a bit unconventional. Instead of rendering images, I aim to execute JavaScript code that processes data and returns a text response. However, I soon realized that running JavaScript as a remote web service might not be pos ...

Enhance the interactivity of Javascript results by making them clickable

I'm new to JavaScript and facing a challenge. I have a JSON file that I'm using to retrieve data. I now want to make the search results clickable so they can be directly inserted into an input field. Eventually, I plan on incorporating them into ...

Guide on integrating jQuery list filtering in Ionic 2

I stumbled upon a jQuery function that filters data in HTML, found it online. Now, I want to incorporate a similar feature into my Ionic app. However, I am unsure about the modifications required in list-search-min.js. Here is a snippet of my index.html: ...

"Enhance Your Website's User Experience with jQuery Aut

One of the challenges I am facing involves an Ajax call that retrieves a JSON representation of data created using PHP's json_encode method: ["Montérégie","Montréal - North Shore","Montréal - South Shore"] These values are extracted from a &apos ...

Is there a way to streamline this generator without using recursion?

I need to develop a unique value generator that produces values within a specified range. The criteria are: all generated values must be distinct the order of values remains consistent upon each run of the generator each value should be significantly diff ...

What is the best way to create an impact?

Need help fixing the parallax effect on this page. I attempted to implement a parallax effect but encountered some issues. Here is the JavaScript code: $objWindow = $(window); $('section[data-type="background"]').each(function(){ var $bgOb ...

Basic use of AJAX for sending the value from jQuery's datepicker

As a novice in JavaScript and AJAX, I'm hoping for your patience as I navigate my way through. In the scope of this project, I am utilizing the CodeIgniter framework. My objective is to implement a datepicker and transmit its value via AJAX. Below is ...

Choose a particular character in a text that corresponds with a regular expression in Javascript

I am looking to replace the symbols < and > within the text. I have constructed a regular expression as follows: (<span[^>]+class\s*=\s*("|')subValue\2[^>]*>)[^<]*(<\/span>)|(<br(\/*)>) This ...

React Component - Element with an undefined value

When I tried implementing an Ionic Modal as a React Component, I encountered the following error message: Type '({ onClose, tipo }: PropsWithChildren<{ onClose: any; tipo: number; }>) => Element | undefined' is not assignable to type & ...