# How to add ambiant occlusion to a mesh created by script

We created a mesh point by point with a script (a huge mesh), and we would like to add ambiant occlusion to this mesh. We use Unity 2019.1.6 and the LWRP template.

we already tested the unity ambiant occlusion post-processing , but it's not working on any gameobject... We searched for tutorials but on Unity 2019 it's pretty uncommon.

Edit: High quality AO isn't our goal, we just want to better see the difference between two faces of same colour. We create the whole mesh (vertices and triangles) in script following the Sebastian Lague tutorial about planet generation. (Here's the first ep. link )

Our code is basically the same : loop over whole array, determine the world position of the vertex and save triangle indexs, then give all of this to the mesh component.

We "patched" the problem for now by adding a flat shader to it.

Edit 2 : How we determine world position of each vertex.

We are creating a plane mesh of [resolution * resolution] vertices. Then we apply noises to create a landscape.

(localUp, axisA and axisB can be replaced by x, y and z, we use them because we create 6 meshes for the 6 sides of the planet.)

Here's the loop for creating the vertices :

    for (int y = 0; y < resolution; y++)
{
for (int x = 0; x < resolution; x++)
{
int i = x + y * resolution; //index of this vertex on mesh
//pointOnUnitCube determines the position of the vertex on the mesh
Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - 0.5) * 2 * axisB;
//Transforms the cube to a sphere by normalizing
Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
//the ShapeGenerator uses noises to change the "height" of this pointOnUnitSphere
vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

//Add to the triangles index array the 2 triangles this point is part of
if (x != resolution - 1 && y != resolution - 1)
{
triangles[triIndex] = i;
triangles[triIndex + 1] = i + resolution + 1;
triangles[triIndex + 2] = i + resolution;

triangles[triIndex + 3] = i;
triangles[triIndex + 4] = i + 1;
triangles[triIndex + 5] = i + resolution + 1;
triIndex += 6;
}
}
}


Have you any idea how we could achieve this ?