Unfinished silhouette using THREE.js

I am new to JavaScript and THREE.js and seeking guidance.

Below is the visual output I managed to create: https://i.sstatic.net/P1hWA.png

Here's the code snippet:

// Initializing the renderer
renderer = new THREE.WebGLRenderer();

renderer.setSize( window.innerWidth, window.innerHeight );
document.getElementById('container').appendChild(renderer.domElement);
    
// Setting up the scene
scene = new THREE.Scene();
    
// Creating and positioning the camera on the scene
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 100000 );
camera.position.set(0, 0, 1000);
scene.add(camera);
    
// Creating a plane with material and adding it to the scene 
me = new THREE.Mesh(new THREE.PlaneGeometry(900,550), new THREE.MeshPhongMaterial({color: 0xffffff}));
scene.add( me );
    
me.position.x = 130;
me.position.y = 10;
me.rotation.y = -20;
    
// Creating a cube with material and adding it to the scene
cube = new THREE.Mesh( new THREE.CubeGeometry( 100, 100, 100 ), new THREE.MeshPhongMaterial({color:0x00ffff}) );
scene.add( cube );
    
cube.position.y = 0;
cube.position.x = 20;
cube.position.z = 0;
cube.rotation.y = 0;
    
scene.add( new THREE.AmbientLight( 0x212223) );
      
light = new THREE.SpotLight(0xffffff, 1);
light.position.set(-300,-100,20); 
light.angle = Math.PI/5;
light.shadowCameraVisible = true;
scene.add(light);
    
renderer.shadowMap.enabled = true;
renderer.shadowMapDarkness = 1;
    
light.castShadow = true;
light.intensity = 0.8;
cube.castShadow = true;
cube.receiveShadow = true;
me.receiveShadow = true;
    
lightHelper = new THREE.SpotLightHelper( light );
scene.add(lightHelper);
    
renderer.render( scene, camera );

When I try to change the x and y values of the light or cube in my code, the shadow of the cube sometimes appears incomplete. As a beginner, I believe there might be an error in my code that I am unable to identify.

Answer №1

When I set the position.x to 300, it works fine. However, for other x values, it doesn't work and I'm baffled as to why (since the rest of my animation modifies the x value).

Typically, shadow mapping operates as follows:

You establish a new orthographic or perspective camera (based on whether you're using a directional or point light), capture the scene's z-buffer onto a texture, and then during object rendering, cross-reference the z-buffer point corresponding to the current fragment with the current fragment's z value. If the former is higher, then the fragment is within shadow. If they have nearly matching values (+- some margin due to depth buffer precision), then the fragment is in the light.

Every camera used for screen rendering has a minimum distance at which it can render objects.

In short, your limitation this time around appears to be the clipping distance.

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

Implementing dynamic width with ng-style in AngularJS

I am trying to dynamically resize a div when an event is fired from S3. In the code below, I pass the progress variable to the updateProgress function as a parameter. This function resides in the top scope of my Angular controller. s3.upload(params).on(& ...

Whenever I utilize paesrInt in the POST request, an error occurs. I have included the code below for reference

const express = require("express"); const bodyParser = require("body-parser"); const app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.get("/", function (req, res) { res.sendFile( ...

Select a single option from the group to include in the array

I'm currently developing a new soccer betting application. My goal is to allow users to choose the result of a match - whether it's a win, loss, or draw - and then save that selection in a list of chosen bets. https://i.stack.imgur.com/hO3uV.png ...

Error in VueJS when attempting to call a method within a v-for loop: 'not defined on the instance but referenced during render'

I’m still getting the hang of Vue.js (Vuebie?), and while I know this question has been asked before, I’ve not quite stumbled upon a solution myself. My current challenge involves passing an object to a method in order to increment a value and then di ...

"Cannot access files using jQuery $_FILES array with blueimp file upload plugin in Internet Explorer versions 8 and

While browsing StackOverflow, I noticed numerous questions regarding the Blueimp file upload plugin in IE8/9. Unfortunately, none of them seem to address the specific issue I am facing. Currently, I am using IE10 in IE8/9 simulator mode. However, each tim ...

Sending various values via a click in an HTML link

Hello, I am attempting to pass multiple values using the HTML onclick function. I am utilizing Javascript to generate a table dynamically. var user = element.UserName; var valuationId = element.ValuationId; $('#ValuationAssignedTable').append(&a ...

Pinchin opts for alternative methods over utilizing the Hammer library for calls

After downloading Hammers.js version-1.0.10, I have been trying to call the 'pinch in' function from the JavaScript file but it is not executing. I suspect there may be a problem with the library as I have tried downloading another one, but the s ...

Exploring the fundamentals of fragment shaders in three.js: utilizing position data to color individual fragments

I am experimenting with coloring the fragments in my shader based on their position between the highest and lowest vertex. Take a look at the shaders below: <script type="x-shader/x-vertex" id="vertexshader"> uniform float span; attr ...

Changing the color of placeholder text in MUI 5 TextField

Looking to customize the text color and placeholder text color in my MUI TextField component to be green https://i.sstatic.net/NZmsi.png The documentation doesn't provide clear instructions, so I attempted a solution that didn't work: <TextF ...

Lodash provides an array of values when the specified path is verified as valid

When using Node.js with the following object and lodash "queries" in the terminal, I encounter the following: var obj = { a: [{ b: [{ c: "apple" }, { d: "not apple" }, { c: "pineapple" }] }] }; > _.get(obj ...

Prevent inputting values into the text field

Below is the code I wrote to prevent users from entering certain values in a textbox: $(".block-keypress").keypress(function(e){ return false; }); While the code is functioning properly, the backspace key is not blocked. ...

Vuetify's v-list-item no longer has spacing issues when using the v-slot:prepend feature

Currently, I am in the process of designing a side navigation using v-navigation-drawer. While most of my items utilize an icon, I also want to create some custom behaviors. Unfortunately, the title in v-slot:title does not align with the title in the v- ...

Is there a way to receive a JSON request in Express delete using Restangular?

Below is the Restangular code that I am using for deleting an object: $scope.delO = (id){ Restangular .one("footer", id) .get() .then((ob) => { ob.remove(); } .catch.... } The deletion request is being successfully sent, co ...

What is the best way to handle multiple responses in Ajax within a single function?

Here is a simple code snippet: $.ajax({ url:'action.php', method: 'POST', data:{getcart:1}, success:function(response){ $('#getcart').html(response);//want to ...

Sending an Ajax POST request to a different server: navigating around cross-domain limitations

Is the only way to accomplish this task through the use of a proxy server? Currently, I am developing a clicktail clone for tracking web user interactions and mouse movements. The script I have created successfully tracks all user actions and sends them to ...

Incrementally add a new object to an existing array of objects

I have an array of objects below. When I do a console.log, this is what I see: [Object, Object, Object] 0:Object name: "Rick" Contact: "Yes" 1:Object name:"Anjie" Contact:"No" 2:Object name:"dillan" Contact:"Maybe" Now, I wa ...

The Blender model imported into Three.js appears to have been rendered in a lower resolution than expected

I recently brought a gltf model into my Three.js project, which was exported from Blender. The model appears perfectly rendered in , but in my Three.js project, it seems to have lower quality as depicted in these screenshots: https://ibb.co/qrqX8dF (donm ...

What is the best way to erase the contents of a pop-up window?

Whenever I click on an event in the calendar, a popup window appears displaying textboxes and a list of items. The issue arises when I close the popup after selecting an event with items in the list; the same items show up for other events without refreshi ...

Utilize a pair of drop-down menus to concurrently filter a data table

I am interested in implementing two select option filters on a datatable at the same time. Currently, they function well when individually selected, but do not work together for filtering by two variables simultaneously. Currently, selecting a second filt ...

What is the recommended approach for conducting backend validation?

As I develop a CRUD application using express.js and node.js, here is the backend API code that I have written: app.post("/signup", (req, res) => { const { name, email, password } = req.body; if (!name) { res.status(401).json("Please provide a n ...