Editor.SpriteNormalsToolWindow.GenerateNormalsMap C# (CSharp) Method

GenerateNormalsMap() private method

private GenerateNormalsMap ( Texture2D horizontals, Texture2D verticals ) : void
horizontals UnityEngine.Texture2D
verticals UnityEngine.Texture2D
return void
        private void GenerateNormalsMap(Texture2D horizontals, Texture2D verticals)
        {
            // Create the asset if not existing yet
            if (OutputTexture == null)
            {
                OutputTexture = new Texture2D(Mathf.Max(horizontals.width, verticals.width), Mathf.Max(horizontals.height, verticals.height), TextureFormat.RGBA32, false);
            }

            // Actual map generation
            Color[] outputPixels = new Color[OutputTexture.width * OutputTexture.height];
            float uNormalized = 0.0f, vNormalized = 0.0f;
            Color ch, cv;
            float r, g, b;
            Vector2 xyNormal;
            Vector3 normal;
            for (int v = 0; v < OutputTexture.height; ++v)
            {
                vNormalized = (float)v / OutputTexture.height;
                for (int u = 0; u < OutputTexture.width; ++u)
                {
                    uNormalized = (float)u / OutputTexture.width;
                    ch = horizontals.GetPixelBilinear(uNormalized, vNormalized);
                    r = ch.r * 2.0f - 1.0f;
                    cv = verticals.GetPixelBilinear(uNormalized, vNormalized);
                    g = cv.g * 2.0f - 1.0f;
                    xyNormal = Vector2.ClampMagnitude(new Vector2(r, g), 0.999f);
                    r = xyNormal.x;
                    g = xyNormal.y;
                    b = (float)Math.Sqrt(1.0f - (double)(r * r) - (double)(g * g)); // z = sqrt (1 - x^2 - y^2)
                    normal = new Vector3(r, g, b).normalized;
                    outputPixels[u + v * OutputTexture.width] = new Color(normal.x * 0.5f + 0.5f, normal.y * 0.5f + 0.5f, normal.z * 0.5f + 0.5f, 1.0f);
                }
            }
            OutputTexture.SetPixels(outputPixels);
            OutputTexture.Apply();

            File.WriteAllBytes(Path.GetDirectoryName(Application.dataPath) + "/Assets/SpriteNormals.png", OutputTexture.EncodeToPNG());
            AssetDatabase.ImportAsset("Assets/SpriteNormals.png");
            OutputTexture = AssetDatabase.LoadMainAssetAtPath("Assets/SpriteNormals.png") as Texture2D;
        }
    }