I want to create a mesh from a silhouette
Asked Answered
L

1

1

I'm working in Unity and thus coding in C#, but any idea or a place to start is welcome.

I don't really know how to describe my problem, and if there is a 'simple' solution for it, but I'll try. I have an object (probably going to limit myself to simple shapes) that casts 2 shadows. I'd like to generate a mesh that is the shape of that shadow. As you can see on the image below, I drew the desired meshes in green.

desired meshes drawn in green

I've messed around with altering the vertices of my initial mesh, and in some specific cases (objects with no rotation) found a solution, but I haven't found one that's works well enough. Does anyone have an idea that could work?

Thanks in advance, Bart

Leaven answered 3/6, 2016 at 13:29 Comment(4)
You could try using Raycasts from the light sources through the corners of your red cube and check where the ray hits the wallSuperelevation
how do you know how "thick" the object should be? a shadow is 2DMarijuana
It is gonna be fixed width. 1 Unit for example.Leaven
you do not have to "generate a mesh". simply, take a box, and change the length/width (that is to say, by changing the scale) and you're done. it's that simple.Bunghole
R
2

I took the time to create a project that does exactly what you said: Preview it here

The Method

Using raycasts I calculated the projected vertices of a specific object from a light source. The method may seem inefficient but as long as the specified mesh has a low vert count everything should be fine.

Then by taking the average of projected vertices I calculated the position of the projected cube. Vector3 averagePosition = new Vector3(verticies.Average(vector => vector.x), verticies.Average(vector => vector.y), verticies.Average(vector => vector.z));

And by taking the range each of the projected vertex position components (x,y,z) I calculated the scale of the cube.Vector3 averageScale = new Vector3(verticies.Max(vector => vector.x) - verticies.Min(vector => vector.x), verticies.Max(vector => vector.y) - verticies.Min(vector => vector.y), normalScale);

Note: I am not generating a whole new mesh. I am just manipulating the transform of a pre-made cube with a script attached.

Downside is that this method is only limited to one axis so far. Can be fixed.

Download the project from Github

GitHub link: https://github.com/MyIsaak/Shadow-Mesh/tree/master

Would be great if you could commit any improvements you make to help the community. You are free to use this project for commercial and non commercial use.

Rosenberg answered 4/6, 2016 at 14:0 Comment(1)
I really appreciate the time and effort you spend trying to help, thanks. After a lot of trail and error I came to a similar result, but not nearly as clean and straightforward as your code, so this is really helpful. Now, the main problem with morphing a mesh that has the shape of a shadow is that a shadow doesn't always have the same shape or as much vertices as the original object. (example in link) But I'm confident that I can solve that using your code, as I'm able to find the needed vertices to build the desired mesh from. !Example and outlineLeaven

© 2022 - 2024 — McMap. All rights reserved.