This means you have to iterate over every light every frame, remove all of their command buffers, compare the light's position & radius against the position & bounds radius of every object that you want to have sorted, and add those that interact to that light's LightEvent.AfterShadowMapPass command buffer. Next you need to add the command buffer for each object to each shadow casting light it might affect, and in the order you want them to render in. You might be able to stick both in the shadow caster pass and use a pass index of -1, but I've never tried that when rendering to depth maps. One to render the mesh with your shadow caster material, with the pass index of the shadow caster pass, and one that's the stencil pass from the other material. Then you’ll need a second shader that is just a shadow caster pass w/ the stencil test, and manually render objects in the correct order into shadow casting lights using command buffers for both the shadow caster depth pass and the stencil only pass.įor each of your objects you'll want to create a command buffer that's basically two cmd.DrawRenderer() commands. But don’t give it a Fallback, which will mean it won’t have a shadow caster pass. Then you render your objects top to bottom with that shader (use a single late opaque queue for all gear, and the RendererComponent’s sortingOrder value from script to control their order). ![]() Then a second pass after the Surface Shader that’s empty except for ColorMask 0 and writing to that stencil value. But it would fix most of the issues you’re having.īasically you need your current Surface Shader, but with a Stencil check that skips any pixels already rendered to w/ a specific ref value. So you’d have to manually inject your object’s shadow pass & stencil pass into lights’ shadow map rendering to ensure their draw order. But this won’t fix the shadow problem, especially since Unity totally ignores the draw order when rendering shadow maps and only allows a single shadow caster pass per object. You could use a stencil pass to prevent objects from drawing, forcing a specific order to the visuals w/o needing to use a z bias. There’s no way to do this in a way that doesn’t look like crap. If you modify the shadow caster pass to do another offsets there, either the shadow positions move with the objects, meaning they cast in the wrong place (because they too have been moved in 3D space towards the camera) or they self shadow / have no shadow because they’re being pushed away / towards the light. But now it’ll be receiving shadows as if it’s in the original position, and those other objects are presumably penetrating each other in the original position (which is where the shadows are being calculated as) which means you’ll see stuff like the shield shadowing the sword when the sword is supposed to be under it. ![]() If you use a vertex fragment shader instead you can use the original position instead of the offset position to calculate the shadow receiving position. That means the shadow receiving for lights (apart from the main directional light) are going to be calculated as if it’s in that different position. If you use a vertex offset of any kind in a Surface Shader, it’s going to actually move the object in 3D space. They’ll always give away the “real” order because they don’t give a f what your draw order or what kind of bias towards the camera you have. Yeah, the shadow receiving makes this kind of impossible. If I could fix that, this approach would be fine.ĭo you know if there's another approach I could take that would better achieve what I'm after or is this just not really possible? The objects is really clipping through it and making a shadow but rendered underneath so only the shadow is visible. ![]() Here you can see the shadow on the shield in the bottom right is cast by an object that's underneath it. ![]() I tried turning off Z Writing and this mostly works but I get this artifact where the shadows clip through the objects. Essentially I want them to act like 2D sprites stacked on top of each other but I'm not using sprites because I want full PBR and dynamic lighting/shadows. What I'm actually trying to do is I want the objects to fully render over each other based on a controllable sort order and not intersect even if they are in the same place. Here you can see the white rectangle is casting a long shadow on the ground but a very short shadow on the shield because it's biased. That kind of works in perspective, but I have an orthographic camera and the object appears to move as it's bias changes.Įven in perspective though it messes with the lighting, the shadows cast on the biased object are visibly in the wrong place.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |