Cloud shadows in Unity

Has anyone built any simple cloud shadows for unity? I just want to create a little bit of movement in my scene. However, I have no idea how to draw moving shadows for clouds across the landscape. I dont need anything heavy. I was hoping I could just put a moving mask on my sun light or something. Is this possible? Any help would be greatly appreciated!

Somthing like this :

Giant low opacity decal maybe? :thinking:

1 Like

That would be rather expensive haha : P

If you don’t need this for mobile, it can be done with a command buffer.

You’d need a shader that can calculate the world position from the depth texture, then project your cloud texture onto that world space position and use a multiply blend mode. This would be called in a command buffer Blit() attached to the main directional light using LightEvent.AfterScreenspaceMask.

Or you can do it the easy way and apply a cookie texture onto directional light. If you want it to move then just slowly move the directional light itself, looping it’s position by the cookie size. You could even use a CustomRenderTexture asset to animate the clouds.

2 Likes

I think every production solution I’ve seen for this involves actual graphics programming, but as a quick and dirty solution one idea would be to apply an animated noise material to a translucent, shadowcasting plane and just place it out of view, as in

2 Likes

Hmmm that could actually work… Just have the Geo one sided some you cant see it from below… Though do translucent things cast shadows? As you mentioned, I feel this is normally a graphics programming thing. Sigh. Im pretty dumb when it comes to that stuff.

@bgolus , this all sounded awesome. However I have no idea what a command buffer is :" ). All of that you said almost sounded like an alien language to me. I want to do it and try it, but I have no idea how to even get started with that haha. Even the bottom one you mentioned im not super sure how to do. Any sources you can recommend I look into? Thanks heaps!

The cookie option is really straightforward. Just have a texture with the clouds in the alpha (inverted to be the cloud’s shadow so white is where the light comes through, black is where the light is blocked) and assign it as the directional light’s cookie property in the inspector.

The problem with the “big plane in the sky” option is Unity doesn’t really support semi transparent shadows, only fully opaque. The Standard shader does support dithered shadows, which as long a single you’re using soft shadows can be a decent approximation of semi-transparent shadows, but your mileage may vary.

1 Like

Hmmmm, well I will give both a try! Im guessing I set up the scrolling of the directional light with a script?

Man, I was hoping I could just plug a cloud shader in somewhere and it would work : P Ah well. Why does game stuff always have to be so tricky haha

Thanks again for all the advice!

That or an animation.

That’s what using a custom render texture would let you do.

Right click in the project view, Create New > Custom Render Texture. Assign your cloud material, assign the custom render texture to your light.

I like the idea… so simple to set up and does what you need. It shouldn’t impact performance drastically. I mean blob shadows is what you use to cheaply fake character shadows, so eventually good for one layer of clouds too?

that’s what I was thinking. Translucent deferred decal that’s unlit, with a slow panning cloud texture on it

yeah, that would basically work I think, just on a second thought, difficult to affect lit particles with that…

Sorry for bumping this, but I think I briefly worked on the thing mentioned in the OP (if it’s what I think it is; it’s hard to tell because it was done for Unity, which made the assets free, which meant lots of people have used them subsequently), but can make a pretty reasonable guess as to how that one was done.

A super easy solution is to have a tiling cloud texture that you look up using world UVs. If you assign this texture using Shader.SetGlobalTexture(), then any of your shaders can access this same texture, allowing your environments and buildings to receive this shadow too.

The down-side is that this would apply over and above your “real” shadows, so that you end up with doubled shadows – but as you can see in OP’s image, that’s what’s happening there anyway. Depending on how finicky you want to be, most people don’t seem to mind it.

2 Likes

This was the solution I wound up using during my last (mobile) unity project. As long as you have a shader register to spare, it works pretty well! It prevents a butt-load of extra verts + draw calls caused by realtime shadows, though with a single plane that’s less of an issue. If you are calculating/applying shadows in your shader, you can probably also take the min/max of your shadow & cloud value. I have not done the latter in practice.

Add a light cookie, then just move the directional light. It’s that easy.

2 Likes

Bless your soul, friend! Such a brilliant solution!