jscastro76 / threebox

A Three.js plugin for Mapbox GL JS, with support for animations and advanced 3D rendering.
Other
549 stars 145 forks source link

No shadows present when 3D terrain is active #381

Closed chrispahm closed 1 year ago

chrispahm commented 1 year ago

Hey,

Describe the bug Thanks for maintaining this awesome library! I'm trying to visualise wind turbines on a 3D terrain map. Once I activate the terrain, the shadows vanish from the map though.

To Reproduce

Expected behavior Shadows should be visible on the ground, just like they are when the 3D terrain is not active.

Relevant Code or Code Sandbox Here's an Observable notebook showcasing the bug, using a slightly altered version of the add3dmodel example https://observablehq.com/d/c6c236f9bb6f21f4

Screenshots Shadows present without 3D terrain

image

Shadows not present with 3D terrain enabled

image
jscastro76 commented 1 year ago

Hi @chrispahm, Thanks for your comments. This is not really a bug, the shadows still there, but in a Geometry Plane created for projecting the shadow in the Custom Layer which is below the Terrain layer rendered by Mapbox engine. As the terrain layer has its own 3D mesh created on top of the map, there's no really a way to project the shadow of an object in a Custom Layer, over the terrain mesh in a Terrain layer, or at least, I haven't found a way to make it.

chrispahm commented 1 year ago

Hi @jscastro76, Thanks for the explanation!

jscastro76 commented 1 year ago

Hi @jscastro76, Thanks for the explanation!

I couldn't find a way yo make the terrain mesh to receive a shadow.

chrispahm commented 1 year ago

No worries, I'm just thinking about a potential way how this could be incorporated without receiving the terrain mesh from Mapbox. What do you think of the following:

  1. Query elevation data from another source, e.g. Mapzen's terrain data https://www.mapzen.com/blog/elevation/
  2. Create a 3D mesh from the terrarium elevation tiles using a method described in this blog post (or similar) https://blog.mastermaps.com/2013/10/terrain-building-with-threejs.html
  3. Add the 3D mesh to the scene, and project the shadows onto it. The mesh would need to be transparent, only the shadow should be visible. Not sure if that would work though 😄

It's quite hacky, but I guess there's no other way to get Mapbox's own terrain data / mesh into threebox, right?

One other thing that came to mind was to create a mesh on-the-fly by creating a point grid within the maps boundaries, and then query the elevation using map.queryTerrainElevation(point, { exaggerated: false }) for each point instead of re-fetching a raster DEM tile and interpolating it. I'm guessing that would be quite a performance hit though...

chrispahm commented 1 year ago

Hi @jscastro76,

I started to implement the above methodology to render shadows in a 3d terrain map: https://observablehq.com/@chrispahm/enabling-shadows-in-a-mapbox-terrain-map

image image

There are still a few open issues though:

jscastro76 commented 1 year ago

That’s quite impressive @chrispahm! I assume performance will be heavily affected but it could be applied as an option so each one could decide if use them or not. If you’re preparing any PR, let me know