Run-time API

Vertex Forge extension methods can be brought into scope with this using directive:

using AmazingAssets.VertexForge;

Now Unity mesh class will have new VertexForge extension allowing texture baking per vertex.

// Example of baking texture into a mesh

using AmazingAssets.VertexForge;

void BakeTextureToVertexColor(ref Mesh mesh, Texture2D texture)
{
    Color[] colors = mesh.VertexForge(false).BakeTexture(texture, new Vector4(1, 1, 0, 0), 0, 0);

    //Save calculated results inside vertex color
    mesh.colors = colors;
}
// Example of baking lightmap into a mesh

using AmazingAssets.VertexForge;

void BakeLightmapToUVBuffer(ref Mesh mesh, Renderer renderer)
{
    Color[] lightmapColors = mesh.VertexForge(false).BakeLightmapColor(renderer, 0, 0);

    //Converting color to vector4
    Vector4[] ligthmapData = lightmapColors.Select(c => new Vector4(c.r, c.g, c.b, c.a)).ToArray();

    //Save calculated results inside UV3 buffer
    mesh.SetUVs(3, ligthmapData);
}

VertexForge extension expects only one argument (bool keepResources) that defines how methods used later arrange their used resources. If there are multiple meshes requiring color baking then it's better for VertexForge to keep those resource and save time on their repetitive calculations. This results in faster data baking.

If bool keepResources is set to false, then after using color baking method VertexForge releases all used resources, frees up memory and no farther actions are required.

If bool keepResources is set to true, then after using all required VertexForge methods, manually should be called VertexForge.ReleaseResources().

BakeColor

Color[] BakeTexture(Texture[] textures, 
                    Vector4[] texturesTilingOffset, 
                    int uvIndex, 
                    float mipmap)

Bakes Texture2D and RenderTextures per-vertex and returns result as the color array, which is same size as the mesh vertex count. Source mesh is not modified.

textures

Array of the baked textures with the same size as the mesh’s sub-mesh count. Each texture will be baked to the corresponding sub-mesh by its index. In the case of providing only one texture, it will be used for all sub-meshes.

texturesTilingOffset

Baked textures tiling and offset values. Array must be the same size as the textures.

Pass null to use default (1, 1, 0, 0) values.

uvIndex

Mesh UV index in the range of [0, 7] used for texture sampling. By default it is 0.

mipmap

Baked texture’s mimap value in the range of [0, 1]. By default it is 0.

BakeCubemap

Color[] BakeCubemap(Cubemap[] cubemaps, 
                    bool useSmoothNormals, 
                    Quaternion rotation, 
                    float mipmap)

Bakes cubemaps per-vertex and returns result as the color array, which is the same size as the mesh vertex count. Source mesh is not modified.

cubemaps

Array of the baked cubemaps with the same size as the mesh’s sub-mesh count. Each cubemap will be baked to the corresponding sub-mesh by its index. In the case of providing only one cubemap, it will be used for all sub-meshes.

useSmoothNormals

Reads provided cubmaps colors using mesh smooth normals, which is calculated by averaging all normals at the vertex.

rotation

Mesh rotation

mipmap

Baked texture’s mimap value in the range of [0, 1]. By default it is 0.

BakeCameraTargetTexture

Color[] BakeCameraTargetTexture(Camera camera, int resolution, float mipmap)

Bakes target texture of the provided camera and returns result as a color array, same size as the mesh vertex count. Source mesh is not modified.

BakeCameraCubemap

Color[] BakeCameraCubemap(Camera camera, bool useSmoothNormals, int resolution, float mipmap)

Bakes cubemap of the provided camera and returns result as a color array, same size as the mesh vertex count. Source mesh is not modified.

BakeLightmapColor

Color[] BakeLightmapColor(Renderer renderer, float exposure, float mipmap)

Bakes lightmap color texture using provided renderer and returns result as a color array, same size as the mesh vertex count. Source mesh is not modified.

renderer

Lightmap color texture is associated with this renderer.

exposure

Exposure of the baked color in the range of [-16, 16]. By default it is 0.

mipmap

Baked texture’s mimap value in the range of [0, 1]. By default it is 0.

BakeLightmapDirection

Color[] BakeLightmapDirection(Renderer renderer, float mipmap)

Bakes lightmap direction texture using provided renderer and returns result as a color array, same size as the mesh vertex count. Source mesh is not modified.

renderer

Lightmap color texture is associated with this renderer.

mipmap

Baked texture’s mimap value in the range of [0, 1]. By default it is 0.

Last updated