Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="address"></param>
        /// <param name="storage"></param>
        /// <param name="tile"></param>
        public void SaveTile(VTAddress address, IStorage storage, VTTile tile)
        {
            var name = address.GetFileNameWithoutExtension("") + ".tile";

            tile.Write(storage.OpenWrite(name));
            //tile.WriteDebug( storage.OpenWrite(name + ".tga") );
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="pageTable"></param>
        public void SplitIntoPages(BuildContext context, VTTextureTable pageTable, IStorage storage)
        {
            var pageSize       = VTConfig.PageSize;
            var pageSizeBorder = VTConfig.PageSizeBordered;
            var border         = VTConfig.PageBorderWidth;

            var colorMap  = LoadTexture(BaseColor, Color.Gray);
            var normalMap = LoadTexture(NormalMap, Color.FlatNormals);
            var roughness = LoadTexture(Roughness, Color.Black);
            var metallic  = LoadTexture(Metallic, Color.Black);
            var emission  = LoadTexture(Emission, Color.Black);

            var pageCountX = colorMap.Width / pageSize;
            var pageCountY = colorMap.Height / pageSize;

            for (int x = 0; x < pageCountX; x++)
            {
                for (int y = 0; y < pageCountY; y++)
                {
                    var pageC = new Image(pageSizeBorder, pageSizeBorder);
                    var pageN = new Image(pageSizeBorder, pageSizeBorder);
                    var pageS = new Image(pageSizeBorder, pageSizeBorder);

                    for (int i = 0; i < pageSizeBorder; i++)
                    {
                        for (int j = 0; j < pageSizeBorder; j++)
                        {
                            int srcX = x * pageSize + i - border;
                            int srcY = y * pageSize + j - border;

                            var c = colorMap.SampleClamp(srcX, srcY);
                            var n = normalMap.SampleClamp(srcX, srcY);
                            var r = roughness.SampleClamp(srcX, srcY).R;
                            var m = metallic.SampleClamp(srcX, srcY).R;
                            var e = emission.SampleClamp(srcX, srcY).R;

                            pageC.Write(i, j, c);
                            pageN.Write(i, j, n);
                            pageS.Write(i, j, new Color(r, m, e, (byte)255));
                        }
                    }

                    var address = new VTAddress((short)(x + AddressX), (short)(y + AddressY), 0);
                    var tile    = new VTTile(address, pageC, pageN, pageS);
                    pageTable.SaveTile(address, storage, tile);
                }
            }
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public VTTile LoadImage(VTAddress address)
        {
            VTTile tile;

            if (!cache.TryGetValue(address, out tile))
            {
                var path = address.GetFileNameWithoutExtension(".tile");

                if (storage.FileExists(path))
                {
                    tile = new VTTile(address);
                    tile.Read(storage.OpenFile(path, FileMode.Open, FileAccess.Read));
                }
                else
                {
                    tile = new VTTile(address);
                    tile.Clear(Color.Black);
                }

                cache.Add(address, tile);
            }

            return(tile);
        }
Пример #4
0
        /// <summary>
        /// Generates mip levels for all tiles.
        /// </summary>
        /// <param name="buildContext"></param>
        /// <param name="pageTable"></param>
        /// <param name="sourceMipLevel"></param>
        /// <param name="mapStorage"></param>
        void GenerateMipLevels(BuildContext buildContext, VTTextureTable pageTable, int sourceMipLevel, IStorage mapStorage)
        {
            if (sourceMipLevel >= VTConfig.MipCount)
            {
                throw new ArgumentOutOfRangeException("mipLevel");
            }

            //int count   = VTConfig.VirtualPageCount >> sourceMipLevel;
            int sizeB = VTConfig.PageSizeBordered;
            var cache = new TileSamplerCache(mapStorage);

            foreach (var vttex in pageTable.SourceTextures)
            {
                if (!vttex.TilesDirty)
                {
                    continue;
                }

                int startX = RoundDown2(vttex.AddressX, sourceMipLevel);
                int startY = RoundDown2(vttex.AddressY, sourceMipLevel);

                int wTiles = (vttex.Width / VTConfig.PageSize);
                int hTiles = (vttex.Width / VTConfig.PageSize);

                int endExX = RoundUp2(vttex.AddressX + wTiles, sourceMipLevel);
                int endExY = RoundUp2(vttex.AddressY + hTiles, sourceMipLevel);

                for (int pageX = startX; pageX < endExX; pageX += 2)
                {
                    for (int pageY = startY; pageY < endExY; pageY += 2)
                    {
                        var address00 = new VTAddress(pageX + 0, pageY + 0, sourceMipLevel);
                        var address01 = new VTAddress(pageX + 0, pageY + 1, sourceMipLevel);
                        var address10 = new VTAddress(pageX + 1, pageY + 0, sourceMipLevel);
                        var address11 = new VTAddress(pageX + 1, pageY + 1, sourceMipLevel);

                        //	there are no images touching target mip-level.
                        //	NOTE: we can skip images that are touched by border.
                        //if ( !pageTable.IsAnyExists( address00, address01, address10, address11 ) ) {
                        //	continue;
                        //}

                        var address = new VTAddress(pageX / 2, pageY / 2, sourceMipLevel + 1);

                        var tile = new VTTile(address);

                        var offsetX = (pageX) * VTConfig.PageSize;
                        var offsetY = (pageY) * VTConfig.PageSize;
                        var border  = VTConfig.PageBorderWidth;

                        var colorValue    = Color.Zero;
                        var normalValue   = Color.Zero;
                        var specularValue = Color.Zero;

                        for (int x = 0; x < sizeB; x++)
                        {
                            for (int y = 0; y < sizeB; y++)
                            {
                                int srcX = offsetX + x * 2 - border * 2;
                                int srcY = offsetY + y * 2 - border * 2;

                                SampleMegatextureQ4(cache, srcX, srcY, sourceMipLevel, ref colorValue, ref normalValue, ref specularValue);

                                tile.SetValues(x, y, ref colorValue, ref normalValue, ref specularValue);
                            }
                        }

                        pageTable.SaveTile(address, mapStorage, tile);
                    }
                }
            }
        }