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 ?

Thanks for your answers.

  • \$\begingroup\$ Computing high-quality ambient occlusion can be costly, which is why we often either pre-process it into a texture map in a 3D modelling package in advance, or approximate it with screen-space effects. But depending on how you're generating your mesh, we might be able to use some information the generator has to make a good guess (eg. if you're running marching cubes over a potential field, analyzing the field's derivatives can give us clues about whether a point is inside a shaded crevice, as is often done in raymarching rendering). Can you tell us more about your geometry/generator? \$\endgroup\$ – DMGregory Aug 28 '19 at 13:12
  • \$\begingroup\$ Folks typically won't watch a YouTube video series just for the sake of answering your question — they'll just move on to the next post. The best way to get answers is to ensure the information needed to understand your situation is described in the body of your question itself \$\endgroup\$ – DMGregory Aug 30 '19 at 12:51
  • \$\begingroup\$ Thanks for your answers and sorry, didn't mean to answer this short, I mispressed the enter key and didn't edit the comment in time ^^' I edited the original post with precisions. \$\endgroup\$ – Studio Pie Aug 30 '19 at 13:01
  • \$\begingroup\$ Knowing how you "determine the world position of the triangle" is the salient detail we need. Some methods of generating vertex positions lend themselves to analytic ambient occlusion approximations, but we need to see the guts of the function you're using to be able to tell you how. \$\endgroup\$ – DMGregory Aug 30 '19 at 13:02
  • \$\begingroup\$ Thanks, post edited. \$\endgroup\$ – Studio Pie Aug 30 '19 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.