- Home /
Lightmapping: Excluding materials from AO?
Hello,
I have a model which uses a custom alphablend shader to render decals on top of solid geometry. However, when I lightmap the geometry, the decal casts ambient occlusion onto the model beneath it.
Is there a way of preventing the decal from casting AO? For example, is there a shader constant that I can enable? I tried using the _Transmission attribute and including "Transparent" in the shader path/name, and while this uses the transparency to mask the AO, it doesn't remove it completely.
Answer by Pangamini · Jan 07, 2015 at 10:37 AM
AO is rendered as a post process from the depth buffer. Decals usually render with depth bias (so in the depth buffer they appear closer to the camera), causing your problem. What you need to do is to use the bias when checking for depth, but not write the decal depth. You can change these properties in the shader that renders your decals.
Hi Panga$$anonymous$$i,
How do I not write the decal depth? I tried adding "ZWrite Off" to the shader, but it had no effect on the lightmapping.
Here's the shader to explain what I mean.
Shader "$$anonymous$$yShader/Transparent/$$anonymous$$yDecalShader"
{
Properties
{
_$$anonymous$$ainTex( "RGB", 2D ) = "white" {}
}
SubShader {
Fog { $$anonymous$$ode Off }
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Blend SrcAlpha One$$anonymous$$inusSrcAlpha
ZWrite Off
Pass {
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#include "TheRestOfTheShader.cginc"
ENDCG
}
}
}
$$anonymous$$y bad, i completelly missed the fact that you are talking about the lightmaps.... so are you creating decals as static objects before baking? (Otherwise i can't see how dynamic decals could cast static AO)
In which case, i can only think about souliton where decals are generated together with corresponing underlaying geometry UVs and applied the underlaying lightmap
The decal and the underlying surface are part of the same model. The model is set as static.
Well i don't ha ve a ready solution then... but my assumption is that decal shouldn't be lightmap static, because that would mean it receives its own lightmap and casts lightmap shadows, while it's obvious that solution lies in a world where decals use the light information from the surface they are applied to. So that's the direction i'd go: make decals read the underlying lightmap data and apply it to itself
Answer by scottharber · Jan 08, 2015 at 02:23 PM
Never mind. I found a solution by altering the placement of the decal.
Unity's lightmapper lets you place a decal between 0m and 0.00029m from a surface before it generates AO. Adding an offset to the decal shader prevents any Z fighting issues from such a close placement.
And will that apply lightmap to both the decal and underlying geometry?
Yes. Since both are static, this will provide lightmaps for both meshes, but not generate AO between them as they are so close together.