- Home /
Ambient Occlusion Map in Shaders
Hi,
I'm trying to modify the builtin bumped specular shader to support an Ambient Occlusion map.
I usually extract the AO map using Filter Forge software.
It is a black and white map where it is darkest when there is the shadow and brightest when there is only light.
The shader code so far is as follows, please pay attention to the line with the comment:
Shader "Bumped Specular Ambient Occlusion" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
_MainTex ("Base (RGB)", 2D) = "white" {}
_GlossTex ("Gloss (A)",2D) = "white" {}
_AOTex ("Ambient Occlusion", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 400
CGPROGRAM
#pragma surface surf BlinnPhong
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _GlossTex;
sampler2D _AOTex;
fixed4 _Color;
half _Shininess;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float2 uv_GlossTex;
float2 uv__AOTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
fixed4 gloss = tex2D(_GlossTex, IN.uv_GlossTex);
fixed4 aocclusion = tex2D(_AOTex, IN.uv__AOTex);
The Interesting Line -->
o.Albedo = tex.rgb * aocclusion.a * _Color.rgb;
o.Gloss = tex.a;
o.Alpha = gloss.a * _Color.a;
o.Specular = _Shininess;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
FallBack "Specular"
}
This shader causes my material to appear very dark and almost not possible to lit in the scene.
My question is that how can I modify this shader to properly support the AO map?
Note: that the infamous SSAO does not take the texture normals and bumps into consideration while this map does.
Hello,
Did you ever get this figured out and working properly?
Thanks
Answer by Liam-Lam · Jul 27, 2019 at 07:12 AM
Try to change o.Albedo = tex.rgb aocclusion.a _Color.rgb; --> o.Albedo = tex.rgb lerp(1, aocclusion.a, _occStr) _Color.rgb; _occStr is property range from 0->1. Also make sure your occlusion info store in alpha channel. It usually store in r or g.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072354im_/https://answers.unity.com/themes/thub/images/avi.jpg)