Exemplo n.º 1
0
        public static bool DetectTextureType(byte[] data, Vector2I size, PixelFormat format, out bool hasAlpha, out bool normalMap)
        {
            hasAlpha  = false;
            normalMap = size.X > 64 && size.Y > 64;

            int normalMapCount = 0;
            int totalCount     = 0;

            for (int y = 0; y < size.Y; y++)
            {
                for (int x = 0; x < size.X; x++)
                {
                    byte r;
                    byte g;
                    byte b;
                    byte a = 255;
                    int  offset;

                    switch (format)
                    {
                    case PixelFormat.R8G8B8A8:
                        offset = (y * size.X + x) * 4;
                        r      = data[offset + 3];
                        g      = data[offset + 2];
                        b      = data[offset + 1];
                        a      = data[offset + 0];
                        break;

                    case PixelFormat.R8G8B8:
                        offset = (y * size.X + x) * 3;
                        r      = data[offset + 2];
                        g      = data[offset + 1];
                        b      = data[offset + 0];
                        break;

                    case PixelFormat.X8R8G8B8:
                        offset = (y * size.X + x) * 4;
                        r      = data[offset + 2];
                        g      = data[offset + 1];
                        b      = data[offset + 0];
                        break;

                    case PixelFormat.A8R8G8B8:
                        offset = (y * size.X + x) * 4;
                        a      = data[offset + 3];
                        r      = data[offset + 2];
                        g      = data[offset + 1];
                        b      = data[offset + 0];
                        break;

                    default:
                        hasAlpha  = false;
                        normalMap = false;
                        return(false);
                    }

                    if (a != 255)
                    {
                        hasAlpha = true;
                    }

                    if (normalMap)
                    {
                        var v = new Vector3F(r, g, b) / 255.0f;
                        v *= 2;
                        v -= new Vector3F(1, 1, 1);
                        var l = v.Length();
                        if (l > 0.75f && l < 1.25f)
                        {
                            normalMapCount++;
                        }
                        else
                        {
                            if (totalCount > 10000)
                            {
                                float p = (float)normalMapCount / totalCount;
                                if (p < .95f)
                                {
                                    normalMap = false;
                                }
                            }
                        }
                    }

                    totalCount++;
                }
            }

            if (normalMap)
            {
                float p = (float)normalMapCount / totalCount;
                if (p < .95f)
                    normalMap = false; }
            }