Generates per-vertex ambient occlusion and returns it as the float values array, with the same size as the source mesh vertex count. AO values are in the range of [0, 1].
If using with class then mesh is read from the or components attached to this gameObject and AO calculation method takes into account its world space position/rotation/scale data (from component) and optionally can interact with other scene meshes.
//Example script demonstrating usage of the GenerateAmbientOcclusion method
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()
{
float[] aoValues = this.gameObject.GenerateVertexAmbientOcclusion(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
}
}
}