# Combining normal map textures via script

Hello,

I am trying to combine normal map textures via script but am running into some trouble. I looked at a lot of resources about this, but I am not well versed in the nuances of normal maps in particular, and so it has all still been leaving me unsure about the correct way to do this.

I have code that takes the colors from a base and an overlay normal map and combines them:

``````Color baseColor = baseColors[j];
Color overlayColor = overlayColors[k];

//Convert colors from normal maps into vectors
Vector3 v1 = NormalMapColorToVector(baseColor);
Vector3 v2 = NormalMapColorToVector(overlayColor);

//Blend the vectors together
Vector3 result = new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z);
result.Normalize();

//Convert back into color
baseColors[j] = VectorToNormalMapColor(result);
``````

And the functions that convert between colors and vectors are as follows:

``````/// <summary>
/// Converts the given color from a normal map to a vector.
/// </summary>
public static Vector3 NormalMapColorToVector(Color c)
{
Vector3 v = new Vector3();

v.x = c.a * 2 - 1;
v.y = c.g * 2 - 1;
v.z = Mathf.Sqrt(1 - v.x * v.x - v.y * v.y);

return v;
}

/// <summary>
/// Converts the given vector to a color for a normal map.
/// </summary>
public static Color VectorToNormalMapColor(Vector3 v)
{
Color c = new Color();

c.a = (v.x + 1) / 2;
c.g = (v.y + 1) / 2;
c.b = 1;
c.r = 1;

return c;
}
``````

Something is wrong with my blending function and/or my color-normal conversion functions, but I just don’t know what. My normal map textures are imported as normal maps and are not compressed, but I can change how they are imported if needed. I am also using the resulting combined texture in a material with the URP Lit shader.

Thanks!

Well, I figured if I posted asking for help that I would probably find the solution myself, and that’s exactly what happened!

Turns out there is nothing wrong with the code I posted above (at least as far as I can tell). The issue was coming from my other layering code introducing an invalid normal map to the blending.