API
using AmazingAssets.VertexAmbientOcclusionGenerator;
Now Unity Mesh and GameObject classes will have new VertexAmbientOcclusionGenerator extension with the Generate(...) method calculating ambient occlusion per vertex.
//Example of generating vertex ambient occlusion and baking it inside vertex color
using UnityEngine;
using AmazingAssets.VertexAmbientOcclusionGenerator;
public class ExampleScript : MonoBehaviour
{
public VertexAmbientOcclusionGeneratorEnum.Solver solver;
public float rayLength = 1;
[Range(1, 180)] public float fov = 90;
public bool useSmoothNormals;
public LayerMask layerMask = 0; //Layers, meshes from which are required to be visible to this AO solver.
void Start()
{
//Generating per vertex ambient occlusion
float[] aoValues = this.gameObject.VertexAmbientOcclusionGenerator().Generate(solver, rayLength, fov, useSmoothNormals, layerMask);
if (aoValues != null)
{
//Baking AO inside vertex color
Color[] vertexColor = new Color[aoValues.Length];
for (int i = 0; i < aoValues.Length; i++)
{
vertexColor[i] = Color.Lerp(Color.black, Color.white * aoValues[i], aoValues[i]);
}
//Instantiating mesh and assigning vertex colors
Mesh aoMesh = Instantiate(this.gameObject.GetComponent<MeshFilter>().sharedMesh);
aoMesh.colors = vertexColor;
this.gameObject.GetComponent<MeshFilter>().sharedMesh = aoMesh;
//Make sure MeshRenderer's material uses shader with vertex color support - to render AO baked inside vertex color
}
}
}
float[] Generate(VertexAmbientOcclusionGeneratorEnum.Solver solver, float rayLength, float fieldOfView, bool useSmoothNormals, LayerMask layerMask)
Generates per-vertex ambient occlusion and returns it as the float values array, with the same size as the source mesh vertex count. Ambient occlusion values are in the range of [0, 1].
If using with GameObject class then mesh is read from the MeshFilter or skinnedMeshRenderer components attached to this gameObject and AO calculation method takes into account its world space position/rotation/scale data (from Transform component) and optionally can interact with other scene meshes.
Last updated