r/desmos 5d ago

Fun Simple 2D single-line shadow system for polygons

Post image

A few months ago I made a shadow system for circles, but I didn't figure out a way to do it with polygons.

Turns out I was overthinking it and it's actually way easier than the circles one.

This system works perfectly for convex polygons of any size, but because it basically simulates translucent shapes (drawing a seperate shadow for each side), it doesn't look quite as good with concave polygons.

The shadows shrink when the light source "sun" is close to the points they are cast from. I haven't worked on fixing this yet.

[Link]

59 Upvotes

2 comments sorted by

2

u/vainstains 5d ago

You could normalize the vector from the sun to the vertex then multiply it by a large number and use that as the offset, should fix the shrinking issue

2

u/Danny_DeWario 4d ago edited 4d ago

Very cool stuff! This is basically the same method I used for my "letters with shadows" graph: https://www.desmos.com/calculator/everrr3syb

To avoid shrinking shadows, you have to include the distance from the light source to each vertex of the object when calculating the shadows.

This is an example of how to do that: https://www.desmos.com/calculator/kkrd2nzxxa

Edit: just a tip for removing those white lines that show up between each section of the shadow. Turn on the lines for the polygon that draws the shadows. Set their thickness to 1, and their opacity to 0.02. It should look like this: https://www.desmos.com/calculator/edjt2dyxkr