# Beginner problem: Making heatmap from shadows/sunlight

Hi All,

I have a directional light (the sun), some meshes (buildings) and a plane (the ground).
When I move the sun, the buildings cast shadows on the ground. I’ve added code to the sun so it will update it’s position based on the date and time of day.

I’d like to create a heatmap - e.g. set the sun’s position at 1300hours, 1400 hours, 1500 hours and 1600 hours and then color the ground based on which part of the plane that received the most sun during these 4 passes.

I’m starting to look into shaders and how to get /set color values for a mesh from the c# side. This doesn’t seem straight forward, at least not for a newbie.

Any input on how to get this done is much appreciated.

Best,
Johannes

A little more information would help us advise you. For example, how often is this going to be calculated? How much detail do you want - like every pixel on the texture detail, or every vertex on the mesh detail?

If it’s only going to be calculated say once every several minutes, then you can easily loop through all the vertices in the mesh, raycast towards the sun’s position at each time, and just count the number of times the raycast hit something, and I can give you some sample code to get you started on that. OTOH, If you need texture-density-level detail in this, or if you need it done every frame, then you’re going to have to get into something much more complex.

Hi StarManta,

I’ve uploaded a screenshot so you can see what it’s about. In my country, Denmark, a bunch of free geodata has been released freely available to the public, so I downloaded a 1x1 kilometer square and used an algorithm to convert the data to an obj file, which I’ve imported into Unity. I’ve attached some sliders, which you can use to control the position of the sun. My idea is to generate the heatmap when hitting the test button. You can see the test plane (red) I’ve added for this. I guess moving the sun in 10 minutes intervals a few times and then doing the raycasting on per vertex basis would be fine.

Earlier I did try the Physics.Raycast method but I couldn’t get it to hit anything. Could it be my mesh not being ok as it’s generated by an algorithm? Or do I need to set it up to accept collision? Maybe I should start out with unity-made game objects just to confirm I can get the setup going as working with vectors is new to me.

Thank you for your quick reply and thanks for pointing me towards the raycasting! I’ll explore this and not spend more time digging into shaders.

Best,
Johannes