public MyTileTexture <byte> GetTerrainBlendTexture(MyPlanetMaterialBlendSettings settings)
        {
            MyTileTexture <byte> tex;

            string path     = settings.Texture;
            int    cellSize = settings.CellSize;

            if (!m_ditherTilesets.TryGetValue(path, out tex))
            {
                string fullPath = Path.Combine(MyFileSystem.ContentPath, path) + ".png";
                if (!File.Exists(fullPath))
                {
                    fullPath = Path.Combine(MyFileSystem.ContentPath, path) + ".dds";
                }

                SharpDXImage image = null;
                try
                {
                    image = SharpDXImage.Load(fullPath);
                }
                catch (Exception e)
                {
                    MyLog.Default.WriteLine(e.Message);
                }

                if (image == null)
                {
                    return(MyTileTexture <byte> .Default);
                }

                PixelBuffer buffer = image.GetPixelBuffer(0, 0, 0);

                Debug.Assert(buffer.Format == Format.R8_UNorm);

                if (buffer.Format != Format.R8_UNorm)
                {
                    return(MyTileTexture <byte> .Default);
                }

                tex = new MyTileTexture <byte>(buffer, cellSize);
                image.Dispose();
            }

            return(tex);
        }
Exemple #2
0
        /// <summary>
        /// Converts or copies image data from pPixels into scratch image data
        /// </summary>
        /// <param name="pDDS"></param>
        /// <param name="offset"></param>
        /// <param name="size"></param>
        /// <param name="metadata"></param>
        /// <param name="cpFlags"></param>
        /// <param name="convFlags"></param>
        /// <param name="pal8"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        private static unsafe Image CreateImageFromDDS(IntPtr pDDS, int offset, int size, ImageDescription metadata, Image.PitchFlags cpFlags, ConversionFlags convFlags, int* pal8, GCHandle? handle)
        {
            if ((convFlags & ConversionFlags.Expand) != 0)
            {
                if ((convFlags & ConversionFlags.Format888) != 0)
                    cpFlags |= Image.PitchFlags.Bpp24;
                else if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551 | ConversionFlags.Format4444 | ConversionFlags.Format8332 | ConversionFlags.FormatA8P8)) != 0)
                    cpFlags |= Image.PitchFlags.Bpp16;
                else if ((convFlags & (ConversionFlags.Format44 | ConversionFlags.Format332 | ConversionFlags.Pal8)) != 0)
                    cpFlags |= Image.PitchFlags.Bpp8;
            }

            // If source image == dest image and no swizzle/alpha is required, we can return it as-is
            var isCopyNeeded = (convFlags & (ConversionFlags.Expand | ConversionFlags.CopyMemory)) != 0 || ((cpFlags & Image.PitchFlags.LegacyDword) != 0);

            var image = new Image(metadata, pDDS, offset, handle, !isCopyNeeded, cpFlags);

            // Size must be inferior to destination size.
            Debug.Assert(size <= image.TotalSizeInBytes);

            if (!isCopyNeeded && (convFlags & (ConversionFlags.Swizzle | ConversionFlags.NoAlpha)) == 0)
                return image;

            var imageDst = isCopyNeeded ? new Image(metadata, IntPtr.Zero, 0, null, false) : image;

            var images = image.PixelBuffer;
            var imagesDst = imageDst.PixelBuffer;

            ScanlineFlags tflags = (convFlags & ConversionFlags.NoAlpha) != 0 ? ScanlineFlags.SetAlpha : ScanlineFlags.None;
            if ((convFlags & ConversionFlags.Swizzle) != 0)
                tflags |= ScanlineFlags.Legacy;

            int index = 0;

            int checkSize = size;

            for (int arrayIndex = 0; arrayIndex < metadata.ArraySize; arrayIndex++)
            {
                int d = metadata.Depth;
                // Else we need to go through each mips/depth slice to convert all scanlines.
                for (int level = 0; level < metadata.MipLevels; ++level)
                {
                    for (int slice = 0; slice < d; ++slice, ++index)
                    {
                        IntPtr pSrc = images[index].DataPointer;
                        IntPtr pDest = imagesDst[index].DataPointer;
                        checkSize -= images[index].BufferStride;
                        if (checkSize < 0)
                            throw new InvalidOperationException("Unexpected end of buffer");

                        if (FormatHelper.IsCompressed(metadata.Format))
                        {
                            Utilities.CopyMemory(pDest, pSrc, Math.Min(images[index].BufferStride, imagesDst[index].BufferStride));
                        }
                        else
                        {
                            int spitch = images[index].RowStride;
                            int dpitch = imagesDst[index].RowStride;

                            for (int h = 0; h < images[index].Height; ++h)
                            {
                                if ((convFlags & ConversionFlags.Expand) != 0)
                                {
#if DIRECTX11_1
                                if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551 | ConversionFlags.Format4444)) != 0)
#else
                                    if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551)) != 0)
#endif
                                    {
                                        ExpandScanline(pDest, dpitch, pSrc, spitch, (convFlags & ConversionFlags.Format565) != 0 ? DXGI.Format.B5G6R5_UNorm : DXGI.Format.B5G5R5A1_UNorm, tflags);
                                    }
                                    else
                                    {
                                        var lformat = FindLegacyFormat(convFlags);
                                        LegacyExpandScanline(pDest, dpitch, metadata.Format, pSrc, spitch, lformat, pal8, tflags);
                                    }
                                }
                                else if ((convFlags & ConversionFlags.Swizzle) != 0)
                                {
                                    SwizzleScanline(pDest, dpitch, pSrc, spitch, metadata.Format, tflags);
                                }
                                else
                                {
                                    if (pSrc != pDest)
                                        CopyScanline(pDest, dpitch, pSrc, spitch, metadata.Format, tflags);
                                }

                                pSrc = (IntPtr) ((byte*) pSrc + spitch);
                                pDest = (IntPtr) ((byte*) pDest + dpitch);
                            }
                        }
                    }

                    if (d > 1)
                        d >>= 1;
                }
            }

            // Return the imageDst or the original image
            if (isCopyNeeded)
            {
                image.Dispose();
                image = imageDst;
            }
            return image;
        }
Exemple #3
0
        public MyHeightmapFace GetHeightMap(string folderName, string faceName, MyModContext context)
        {
            ProfilerShort.Begin("MyHeightmapLoadingSystem::GetHeightMap()");
            if (m_first)
            {
                PreloadCrashingData();
                m_first = false;
            }
            string fullPath = null;
            bool   found    = false;


            // Look for modded textures
            if (!context.IsBaseGame)
            {
                fullPath = Path.Combine(Path.Combine(context.ModPathData, "PlanetDataFiles"), folderName, faceName);
                if (MyFileSystem.FileExists(fullPath + ".png"))
                {
                    found     = true;
                    fullPath += ".png";
                }
                else if (MyFileSystem.FileExists(fullPath + ".dds"))
                {
                    found     = true;
                    fullPath += ".dds";
                }
            }

            // Use default ones
            if (!found)
            {
                fullPath = Path.Combine(m_planetDataFolder, folderName, faceName);
                if (MyFileSystem.FileExists(fullPath + ".png"))
                {
                    found     = true;
                    fullPath += ".png";
                }
                else if (MyFileSystem.FileExists(fullPath + ".dds"))
                {
                    fullPath += ".dds";
                }
            }

            MyHeightmapFace value;

            if (m_heightMaps.TryGetValue(fullPath, out value))
            {
                ProfilerShort.End();
                return(value);
            }
            try
            {
                using (SharpDXImage image = LoadTexture(fullPath))
                {
                    if (image == null)
                    {
                        MyLog.Default.WriteLine("Could not load texture {0}, no suitable format found. " + fullPath);
                    }
                    else
                    {
                        PixelBuffer buffer = image.GetPixelBuffer(0, 0, 0);

                        value = new MyHeightmapFace(buffer.Height);

                        if (buffer.Format == Format.R16_UNorm)
                        {
                            PrepareHeightMap(value, buffer);
                        }
                        else if (buffer.Format == Format.R8_UNorm)
                        {
                            PrepareHeightMap8Bit(value, buffer);
                        }
                        else
                        {
                            MyDebug.FailRelease(String.Format("Heighmap texture {0}: Invalid format {1} (expecting R16_UNorm or R8_UNorm).", fullPath, buffer.Format));
                        }
                        buffer = null;
                        image.Dispose();
                    }
                }
                m_heightMaps[fullPath] = value;
            }
            catch (Exception e)
            {
                MyLog.Default.WriteLine(e.Message);
            }
            ProfilerShort.End();

            return(value);
        }