mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Can buildings make shadows?

Open BTF-Sang opened this issue 5 years ago • 13 comments

    I saw the example of the official website. The building has no shadow and it feels very fake. 
    The API did not find an example of building shadows. 
    How can I achieve this? Thank you....

BTF-Sang avatar Mar 01 '19 09:03 BTF-Sang

There's currently no easy way to do this. I did an experiment on this with CustomerLayer interface here, although it's a very fragile approach (not sure if it works with the current version): https://gist.github.com/mourner/e24fab0dd579402b0a6a3bbeaf916a02

I'd still be interested in seeing shadows as a part of core — perhaps we'll implement it some day.

mourner avatar Mar 01 '19 09:03 mourner

That's very awesome @mourner. I had to make some tweaks to get it working, so I hope you don't mind I've published it at https://bl.ocks.org/andrewharvey/9490afae78301c047adddfb06523f6f1

andrewharvey avatar Mar 01 '19 12:03 andrewharvey

Awesome! One thing that didn't carryover from the demo is the semi-transparency — I hoped to get opacity in the custom layer implementation but that got scrubbed. Not sure if there is a good workaround. cc @ansis

mourner avatar Mar 01 '19 15:03 mourner

@mourner @andrewharvey thank you for the help I still have a problem,I want mapbox to combine osm map to achieve 3D effect, and load raster-json building data, how to achieve?

BTF-Sang avatar Mar 02 '19 02:03 BTF-Sang

One thing that didn't carryover from the demo is the semi-transparency — I hoped to get opacity in the custom layer implementation but that got scrubbed. Not sure if there is a good workaround. cc @ansis

I think opacity will have to be in the hands of the custom layer implementer now that we aren't rendering into a texture first. Two approaches to implement opacity here:

  • render into a texture and copy with an opacity
  • use the stencil or depth buffer to prevent double-drawing and draw with opacity directly

ansis avatar Mar 04 '19 16:03 ansis

@ansis thanks! This makes sense. Maybe we can even get away with double-drawing for the building shadows layer (it may have a cool effect).

mourner avatar Mar 05 '19 08:03 mourner

@mourner Hi! Thanks for the example :). I tried setting this up in my project which uses v2.1.0 from mapbox. Since v2 this example doesn't seem to work anymore, I'm getting the following error:

GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 0

When I change the drawElements part to something like this:

gl.drawElements( gl.TRIANGLES, segment.primitiveLength * 0.003, gl.UNSIGNED_SHORT, segment.primitiveOffset * 3 * 2 );

the shadows do render, but they're really off. Any idea what has changed since v2 of mapbox which causes this error? This is the full code which I'm using to test: https://gist.github.com/Stefwint/121dbb209bd273e52d87542ba77204dc

Stefwint avatar Feb 16 '21 08:02 Stefwint

@Stefwint were you able to figure anything out for this? We're struggling with the same issue at the moment.

BraydenKC avatar Feb 23 '21 15:02 BraydenKC

@BraydenKC Unfortunately not. The owner of this repository is giving some more in depth explanation about the issue: https://github.com/jscastro76/threebox/issues/146#issuecomment-780521627

So let's hope there will be a solution soon..

Stefwint avatar Feb 23 '21 15:02 Stefwint

i solved this like below

gl.vertexAttribPointer(this.aPosition2d, 2, gl.SHORT, false, 8, 8 * vertexOffset)
gl.vertexAttribPointer(this.aNormal, 4, gl.SHORT, false, 8, 4 + 8 * vertexOffset)

satorbs avatar Feb 26 '21 06:02 satorbs

@satorbs Do you have a full example? It looks better with your changes, but it's not quite there yet.

Stefwint avatar Feb 26 '21 07:02 Stefwint

Awesome work @mourner I got it working without problem with libregl and custom json buildings Thanks

phpia avatar Oct 21 '21 08:10 phpia

For Mapbox GL JS version 2.0 and up, here is the updated example

https://gist.github.com/ted-piotrowski/420a31bf3c157664fdda14bf45692785

...and the revision if you're wondering what changed

https://gist.github.com/ted-piotrowski/420a31bf3c157664fdda14bf45692785/revisions

ted-piotrowski avatar Sep 20 '22 22:09 ted-piotrowski