API

using AmazingAssets.VertexThicknessGenerator;

Now Unity Mesh and GameObject classes will have new VertexThicknessGenerator extension with the Generate(...) method calculating thickness per vertex.

//Example of generating vertex thickness and baking it inside vertex color

using UnityEngine;

using AmazingAssets.VertexThicknessGenerator;


public class ExampleScript : MonoBehaviour
{
    public float rayLength = 0.1f;
    [Range(1, 180)] public float fov = 90;
    public bool useSmoothNormals;


    void Start()
    {
        //Generating per vertex thickness
        float[] thicknessValues = this.gameObject.VertexThicknessGenerator().Generate(rayLength, fov, useSmoothNormals);


        if (thicknessValues != null)
        {
            //Baking thickness inside vertex color
            Color[] vertexColor = new Color[thicknessValues.Length];
            for (int i = 0; i < thicknessValues.Length; i++)
            {
                vertexColor[i] = Color.Lerp(Color.black, Color.white * thicknessValues[i], thicknessValues[i]);
            }


            //Instantiating mesh and assigning vertex colors
            Mesh mesh = Instantiate(this.gameObject.GetComponent<MeshFilter>().sharedMesh);
            mesh.colors = vertexColor;

            this.gameObject.GetComponent<MeshFilter>().sharedMesh = mesh;


            //Make sure MeshRenderer's material uses shader with vertex color support - to render thickness baked inside vertex color
        }
    }
}

float[] GenerateVertexThickness(float rayLength, float fieldOfView, bool useSmoothNormals)

Generates per-vertex thickness and returns it as the float values array, with the same size as the source mesh vertex count. Thickness 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 thickness calculation method takes into account its world space position/rotation/scale data (from Transform component).

Last updated