Troubleshooting a shadow mapping problem in three.js involving a ShaderMaterial that alters the positions of vertices

Within my current project, I have implemented a ShaderMaterial to depict terrains. The positions of the vertices are adjusted in the vertex shader based on information from a height map texture:

vec4 mvPosition = modelViewMatrix * vec4( position + normal * heightMapScale, 1.0);
gl_Position = projectionMatrix * mvPosition;

The terrain itself appeared fine, but upon attempting to place objects onto it, I noticed some peculiarities with the shadows. It seemed like the vertices were unaware of their updated positions :(

If you're interested, please follow the links below for screenshots and a live demo (Apologies for being unable to post images due to reputation limitations)

Screenshot: https://i.sstatic.net/Xw3ZI.jpg

Live Demo: http://jsfiddle.net/b2bfm8q3/2/

Observe how the cube on the left side displays a correct shadow, while the one on the right does not due to a mismatch in the shader that shifts the face upwards.

Any thoughts on how to address this issue?

Thank you

Answer №1

Hey @gman, just wanted to extend my gratitude for your advice! I realized that more shaders should be tweaked in order to solve my issue. Specifically, the worldPosition variable in the worldpos_vertex needs to be updated as well:

// Adjusting the vertex position
"float fBump = position.x < 0. ? 0. : 350.;",
"vec4 newPosition = vec4( position + normal * fBump, 1.0);",
// Replacing THREE.ShaderChunk[ "default_vertex" ]
"vec4 mvPosition = modelViewMatrix * newPosition;",
"gl_Position = projectionMatrix * mvPosition;",
// Replacing THREE.ShaderChunk[ "worldpos_vertex" ]
"vec4 worldPosition = modelMatrix * newPosition;",

Check out the updated demo here: http://jsfiddle.net/b2bfm8q3/4/

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

I wish to trigger the function when the button with the ID "add_city" is clicked instead of it being activated upon pressing the Enter key as it currently is

Is it possible to trigger the function by clicking a button with the id "add_city", rather than just pressing Enter? function createCity(stateId, newCity, event) { if(event.keyCode == 13 || $(this).attr('id') === 'add_city') { i ...

JavaScript Scroller that keeps on scrolling

Unique Continuous JavaScript Scroller I am currently working on a JavaScript scroller script that enables left to right and right to left scrolling on the click of next and previous buttons. The script is designed to work with 1 to 3 div elements in a con ...

What is the reason for instanceof Map returning false?

Utilizing the Draft.js plugin called Mention. When accessing editorState.content.entityMap.mention, I am able to retrieve the value by: mention.get('address') However, when I attempt to verify if it is a Map using: console.log('mention&a ...

Developing a new React application with Access Control List (ACL) and encountering an issue with Casl

I've recently started working with casl and it feels like I might be overlooking something crucial. So, I created a file named can.js which closely resembles the example provided in the documentation: import { createContext } from 'react'; i ...

Generate a revised object from a function and return it to the caller

What is the most efficient way to update and return a new object from a function? I came up with this function: export const addItemToCart = (currentCart, item) => { const { name, ...otherProps } = item; // if the item is already in the cart if ...

Avoiding double entries in the shopping list using DOM selectors in JavaScript

I have been struggling with preventing duplicate items from being added to a shopping list that I created. Even though I thought the code below would solve the issue, it hasn't been effective so far. My expectation was for the JavaScript code to acces ...

Combining Arrays in AngularJS with an owl-carousel Setting

My goal is to implement an endless scrolling carousel in AngularJS using owl-carousel. The idea is to load new items every time the carousel is fully scrolled and seamlessly merge queried elements with the existing list. However, I've encountered a pr ...

Send form using ajax technology

I am in the process of developing a website using GitHub pages and Jekyll. One of the key features I want to include is a contact form that allows users to send emails. To implement this functionality, I have decided to use Formspree. After creating an ac ...

Solving template strings in a future context

I have a unique use-case scenario where I am filling the innerHTML like this. However, my issue lies in resolving the template literal within the context of a for loop. Any suggestions on how to accomplish this? var blog_entries_dom = 'blog_entries& ...

A guide to creating a JavaScript function that outputs a script in string form

Currently, I am utilizing angular and seeking to add a script to my directive's template. My goal is to create a function that can take the necessary parameters for the script and return it as a string. This approach would prevent me from having to em ...

Is it necessary to include specific versions in package.json when committing the yarn.lock file?

Do you believe it is beneficial to always commit the yarn.lock file, even though all versions are clearly specified in package.json and there should be no discrepancies among team members? I personally find it to be a time-consuming practice, especially w ...

Guide to linking two variables in Vue.js

I'm working on a project that combines Laravel with Vue. Right now, I'm passing data from the controller to the view in two variables - users and user_data. How can I link these two variables together and display them using v-for? These two tabl ...

leveraging socket.io alongside express router

As someone who is relatively new to node.js, I am currently in the process of creating a webchat application. My project consists of a server.js file and a router.js file where I have defined all my routes. Unlike many others, I am not using express-genera ...

JavaScript Bingo Game - Create Interactive Cell Selection

Below is the HTML code that I am using to create a Bingo card: ... <th class="orange">B</th> <th class="orange">I</th> <th class="orange">N</th> ...

Tips for retrieving javascript-generated HTML content

Currently, I'm attempting to retrieve article headlines from the NY Times website. Upon inspection, it appears that the HTML is being generated by Javascript since it's only visible when using the 'inspect element' feature in Firefox. ...

Leveraging Masonry.js with dynamically created divs using jQuery

Recently, I discovered Masonry.js and was excited to incorporate it into my projects. To test my skills, I decided to create a page that would display 16 divs with random heights and colors every time I clicked a button. However, I'm encountering an i ...

What is the best way to rearrange DOM elements using the output of a shuffle function?

Looking for a solution to shuffle and move around cards in an HTML memory game? Let's analyze the current setup: <ul class="deck"> <li class="card"> <i class="fa fa-diamond"></i> </li> ...

The Threejs Raycaster detects collisions with objects even when the ray is just grazing the surface

Within my Vue component, I have integrated a threejs scene and I am facing an issue with selecting objects using the mouse. I am currently using the OnPointerDown event and raycaster to locate objects under the mouse pointer. However, it seems like the ray ...

Why is my Feed2JS RSS feed functional locally but not operational after deployment on GitHub Pages?

I've been using feedtojs.org to display my Medium blog posts on my GitHub Pages website. Strangely enough, it works perfectly fine on my local server but not on the actual domain. The RSS feed is valid. Here's how it appears on my local server: ...

What is the process for converting an Angular UTC timestamp to a local timestamp?

Is it possible to convert a UTC timestamp to a local time timestamp using any function or method? I am sending the timestamp to angular moments, but the server's timestamp is in UTC. ...