示例#1
0
    IReadOnlyTexture <byte> Write(IReadOnlyTexture <byte> existing, AssetInfo info, AssetMapping mapping, ISerializer s, IJsonUtil jsonUtil)
    {
        if (existing == null)
        {
            throw new ArgumentNullException(nameof(existing));
        }
        int width  = info.Width;
        int height = info.Height;

        if (width == 0 || height == 0)
        {
            throw new ArgumentException("Explicit width and height must be defined when using FontSpriteLoader", nameof(info));
        }

        var repacked = new SimpleTexture <byte>(existing.Id, existing.Name, width, height * existing.Regions.Count);

        for (int i = 0; i < existing.Regions.Count; i++)
        {
            var oldFrame = existing.GetRegionBuffer(i);
            repacked.AddRegion(0, i * height, width, height);
            BlitUtil.BlitDirect(oldFrame, repacked.GetMutableRegionBuffer(i));
        }

        var font = _loader.Serdes(repacked, info, mapping, s, jsonUtil);

        return(font == null ? null : existing);
    }
 public SimpleTerrainMaterial(TerrainData terrain)
 {
     alphamapWidth              = terrain.alphamapWidth;
     alphamapHeight             = terrain.alphamapHeight;
     alphamapLayers             = terrain.alphamapLayers;
     alphamaps                  = new List <float[, ]>();
     float[,,] terrainAlphamaps = terrain.GetAlphamaps(0, 0, terrain.alphamapWidth, terrain.alphamapHeight);
     for (int l = 0; l < alphamapLayers; l++)
     {
         float[,] alphamap = new float[alphamapWidth, alphamapHeight];
         for (int i = 0; i < alphamapWidth; i++)
         {
             for (int j = 0; j < alphamapHeight; j++)
             {
                 alphamap[i, j] = terrainAlphamaps[i, j, l];
             }
         }
         alphamaps.Add(alphamap);
     }
     textures = new SimpleTexture[terrain.splatPrototypes.Length];
     for (int i = 0; i < textures.Length; i++)
     {
         textures[i] = new SimpleTexture(terrain.splatPrototypes[i].texture);
         Vector2 ts = terrain.splatPrototypes[i].tileSize;
         textures[i].scale = new Vector2(terrain.size.x / ts.x, terrain.size.z / ts.y);
     }
 }
示例#3
0
        internal static void UnloadContent()
        {
            DefaultRasterizerState.Dispose();
            DebugRasterizerState.Dispose();

            SimpleTexture.Dispose();
            SimpleNormalTexture.Dispose();
        }
    public void Render()
    {
        //RayTraceTool.GetRandomDirs_BlinnPhong_Importance2(Vector3.up, new Vector3(1, 0.1f, 0).normalized, 0.7f, 1000);
        //return;

        updateProgress(0);
        Camera cam = GetComponent <Camera>();

        if (cam)
        {
            Vector3       left          = Vector3.Cross(cam.transform.forward, cam.transform.up);
            Vector3       up            = Vector3.Cross(left, cam.transform.forward);
            float         lengthY       = Mathf.Tan(Mathf.Deg2Rad * cam.fieldOfView / 2) * 2;
            float         lengthX       = lengthY * width / height;
            int           waitNum       = width * height;
            int           curIndex      = 0;
            SimpleTexture targetTexture = new SimpleTexture(width, height);

            float startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    int x = i - width / 2;
                    int y = height / 2 - j;

                    Color color      = Color.black;
                    int   splitPixel = antiAliasing ? 3 : 1;
                    for (int ax = 0; ax < splitPixel; ax++)
                    {
                        for (int ay = 0; ay < splitPixel; ay++)
                        {
                            float   fx           = (lengthX * (x + ax / (float)splitPixel)) / width;
                            float   fy           = (lengthY * (y + ay / (float)splitPixel)) / height;
                            Vector3 temp         = cam.transform.position + cam.transform.forward + left * fx + up * fy;
                            Vector3 tempDir      = Vector3.Normalize(temp - cam.transform.position);
                            int     subSampleNum = sampleNum / (splitPixel * splitPixel);
                            color += TraceColor(cam.transform.position, tempDir, subSampleNum, 3);
                        }
                    }
                    color  /= splitPixel * splitPixel;
                    color.a = 1;
                    targetTexture.SetColor(width - i - 1, height - j - 1, RayTraceTool.LinearToGammaSpace(color));

                    curIndex += 1;
                }
                if (updateProgress(curIndex / (float)waitNum))
                {
                    break;
                }
            }
            //TaskPipeLine.Flush();
            Debug.Log("render time : " + (Time.realtimeSinceStartup - startTime));
            texture.SetPixels(targetTexture.colors);
            texture.Apply(true);
        }
    }
示例#5
0
 public PaletteManager()
 {
     _paletteTexture = new SimpleTexture <uint>(AssetId.None, "Palette", 256, 1).AddRegion(0, 0, 256, 1);
     On <SlowClockEvent>(e => OnTick(e.Delta));
     On <LoadPaletteEvent>(e => SetPalette(e.PaletteId));
     On <LoadRawPaletteEvent>(e =>
     {
         Palette = null;
         GeneratePalette(e.Entries, null);
     });
 }
示例#6
0
    static IReadOnlyTexture <byte> Read(AssetInfo info, ISerializer s)
    {
        var streamLength = s.BytesRemaining;

        if (streamLength == 0)
        {
            return(null);
        }

        int width  = info.Width;
        int height = info.Height;

        if (width == 0)
        {
            width = (int)Math.Sqrt(streamLength);
        }

        int totalHeight = (int)streamLength / width;

        if (height == 0)
        {
            height = totalHeight;
        }

        int spriteCount = Math.Max(1, unchecked ((int)(streamLength / (width * height))));

        height = (int)streamLength / (width * spriteCount);

        byte[] pixelData          = s.Bytes(null, null, (int)streamLength);
        int    expectedPixelCount = width * height * spriteCount;

        ApiUtil.Assert(expectedPixelCount == (int)streamLength,
                       $"Extra pixels found when loading fixed size sprite {info.AssetId} " +
                       $"({streamLength} bytes for a {width}x{height}x{spriteCount} image, expected {expectedPixelCount}");

        var frames = new Region[spriteCount];

        for (int n = 0; n < spriteCount; n++)
        {
            frames[n] = new Region(0, height * n, width, height, width, totalHeight, 0);
        }

        var sprite = new SimpleTexture <byte>(
            info.AssetId,
            info.AssetId.ToString(),
            width,
            height * spriteCount,
            pixelData.AsSpan(0, expectedPixelCount), // May be less than the streamlength
            frames);

        return(info.Get(AssetProperty.Transposed, false) ? Transpose(sprite) : sprite);
    }
    public SimpleMaterial(Material mat)
    {
        unityTexture = mat.mainTexture as Texture2D;

        if (mat.mainTexture != null)
        {
            Int64 key = mat.mainTexture.GetNativeTexturePtr().ToInt64();
            if (!textures.TryGetValue(key, out texture))
            {
                texture       = new SimpleTexture(mat.mainTexture as Texture2D);
                textures[key] = texture;
            }
        }
        else
        {
            texture = new SimpleTexture(null);
        }
        if (mat.HasProperty("_Color"))
        {
            this.color = RayTraceTool.GammaToLinearSpace(mat.color);
        }
        if (mat.HasProperty("_Glossiness"))
        {
            glossiness = mat.GetFloat("_Glossiness");
        }
        if (mat.HasProperty("_Metallic"))
        {
            metallic = mat.GetFloat("_Metallic");
        }
        if (mat.HasProperty("_Cutoff"))
        {
            renderMode = RenderMode.Alphablend;
        }
        else if (mat.shader.name.ToLower().Contains("standard"))
        {
            if (mat.HasProperty("_Mode"))
            {
                if (mat.GetInt("_Mode") == 0)
                {
                    renderMode = RenderMode.Opaque;
                }
            }
        }
        else if (mat.shader.name.ToLower().Contains("multiply"))
        {
            renderMode = RenderMode.Multiply;
        }
        else if (mat.shader.name.ToLower().Contains("transparent"))
        {
            renderMode = RenderMode.Alphablend;
        }
    }
        public object Process(object asset, AssetInfo info)
        {
            if (asset == null)
            {
                throw new ArgumentNullException(nameof(asset));
            }
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            var bitmap  = (InterlacedBitmap)asset;
            var texture =
                new SimpleTexture <uint>(info.AssetId, bitmap.Width, bitmap.Height)
                .AddRegion(0, 0, bitmap.Width, bitmap.Height);

            var imageBuffer = new ReadOnlyImageBuffer <byte>(bitmap.Width, bitmap.Height, bitmap.Width, bitmap.ImageData);

            BlitUtil.BlitTiled8To32(imageBuffer, texture.GetMutableRegionBuffer(0), bitmap.Palette, 255, null);
            return(texture);
        }
示例#9
0
        static IReadOnlyTexture <byte> Read(AssetInfo info, ISerializer s)
        {
            ushort width     = s.UInt16("Width", 0);
            ushort height    = s.UInt16("Height", 0);
            int    something = s.UInt8(null, 0);

            ApiUtil.Assert(something == 0);
            byte frameCount = s.UInt8("Frames", 1);

            var result = new SimpleTexture <byte>(info.AssetId, width, height * frameCount);

            for (int i = 0; i < frameCount; i++)
            {
                byte[] frameBytes = s.Bytes("Frame" + i, null, width * height);
                result.AddRegion(0, i * height, width, height);
                BlitUtil.BlitDirect(
                    new ReadOnlyImageBuffer <byte>(width, height, width, frameBytes),
                    result.GetMutableRegionBuffer(i));
            }

            s.Check();
            return(result);
        }
示例#10
0
    public static IReadOnlyTexture <T> CombineFramesVertically <T>(IAssetId id, IList <IReadOnlyTexture <T> > frames) where T : unmanaged
    {
        if (frames == null)
        {
            throw new ArgumentNullException(nameof(frames));
        }
        int[] yOffsets   = new int[frames.Count];
        int   currentY   = 0;
        int   totalWidth = 0;

        for (int i = 0; i < frames.Count; i++)
        {
            yOffsets[i] = currentY;
            currentY   += frames[i].Height;
            if (frames[i].Width > totalWidth)
            {
                totalWidth = frames[i].Width;
            }
        }

        if (totalWidth == 0 || currentY == 0)
        {
            throw new InvalidOperationException($"Tried to combine frames, but the width or height was 0");
        }

        var result = new SimpleTexture <T>(id, totalWidth, currentY);

        for (int i = 0; i < frames.Count; i++)
        {
            var fy    = yOffsets[i];
            var frame = frames[i];
            result.AddRegion(0, fy, frame.Width, frame.Height);
            BlitDirect(frame.GetLayerBuffer(0), result.GetMutableRegionBuffer(i));
        }

        return(result);
    }
示例#11
0
        public IReadOnlyTexture <byte> Serdes(IReadOnlyTexture <byte> existing, AssetInfo info, AssetMapping mapping, ISerializer s, IJsonUtil jsonUtil)
        {
            IReadOnlyTexture <byte> singleFrame = null;

            if (s.IsWriting())
            {
                if (existing == null)
                {
                    throw new ArgumentNullException(nameof(existing));
                }
                singleFrame =
                    new SimpleTexture <byte>(
                        existing.Id,
                        existing.Name,
                        existing.Width,
                        existing.Height,
                        existing.PixelData.ToArray())
                    .AddRegion(existing.Regions[0].X, existing.Regions[0].Y, existing.Regions[0].Width, existing.Regions[0].Height);
            }

            var sprite = new FixedSizeSpriteLoader().Serdes(singleFrame, info, mapping, s, jsonUtil);

            if (sprite == null)
            {
                return(null);
            }

            return(new SimpleTexture <byte>(
                       sprite.Id,
                       sprite.Name,
                       sprite.Width,
                       sprite.Height,
                       sprite.PixelData.ToArray())
                   .AddRegion(0, 0, sprite.Width, sprite.Height)
                   .AddRegion(0, sprite.Height - StatusBarHeight, sprite.Width, StatusBarHeight));
        }
示例#12
0
        public void Load(LabyrinthData labyrinthData, AssetInfo info, IsometricMode mode, TilemapRequest request, int?paletteNumber, IAssetManager assets)
        {
            if (labyrinthData == null)
            {
                throw new ArgumentNullException(nameof(labyrinthData));
            }
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (assets == null)
            {
                throw new ArgumentNullException(nameof(assets));
            }

            RemoveAllChildren();

            bool floors   = mode is IsometricMode.Floors or IsometricMode.All;
            bool ceilings = mode is IsometricMode.Ceilings or IsometricMode.All;
            bool walls    = mode is IsometricMode.Walls or IsometricMode.All;
            bool contents = mode is IsometricMode.Contents or IsometricMode.All;

            paletteNumber ??= info.Get(AssetProperty.PaletteId, 0);
            var paletteId = new PaletteId(AssetType.Palette, paletteNumber.Value);
            var palette   = assets.LoadPalette(paletteId);

            if (palette == null)
            {
                Error($"Could not load palette {paletteNumber}");
                palette = assets.LoadPalette(Base.Palette.Common);
            }
            else
            {
                Raise(new LoadPaletteEvent(paletteId));
            }

            var etmManager = Resolve <IEtmManager>();

            request.Pipeline   = DungeonTilemapPipeline.NoCulling;
            request.DayPalette = palette;
            _tilemap           = etmManager.CreateTilemap(request /*labyrinthData.Id, 0, request, palette, null, DungeonTilemapPipeline.NoCulling */);

            // Layout:
            // [Empty] [First frame of all floors] [First frame of all ceilings] [First frame of all walls] [Additional floor frames] [Additional ceiling frames] [Additional wall frames]

            int totalTiles = 1;

            if (floors || ceilings)
            {
                for (int i = 0; i < labyrinthData.FloorAndCeilings.Count; i++)
                {
                    var floorInfo = labyrinthData.FloorAndCeilings[i];
                    var floor     = floorInfo == null ? null : assets.LoadTexture(floorInfo.SpriteId);
                    _tilemap.DefineFloor(i + 1, floor);
                    if (floors)
                    {
                        totalTiles += _tilemap.DayFloors.GetFrameCountForLogicalId(i + 1);
                    }
                    if (ceilings)
                    {
                        totalTiles += _tilemap.DayFloors.GetFrameCountForLogicalId(i + 1);
                    }
                }
            }

            if (walls)
            {
                for (int i = 0; i < labyrinthData.Walls.Count; i++)
                {
                    var  wallInfo      = labyrinthData.Walls[i];
                    bool isAlphaTested = wallInfo != null && (wallInfo.Properties & Wall.WallFlags.AlphaTested) != 0;
                    var  wall          = wallInfo == null ? null : assets.LoadTexture(wallInfo.SpriteId);
                    _tilemap.DefineWall(i + 1, wall, 0, 0, wallInfo?.TransparentColour ?? 0, isAlphaTested);

                    foreach (var overlayInfo in wallInfo?.Overlays ?? Array.Empty <Overlay>())
                    {
                        if (overlayInfo.SpriteId.IsNone)
                        {
                            continue;
                        }

                        var overlay = assets.LoadTexture(overlayInfo.SpriteId);
                        _tilemap.DefineWall(i + 1,
                                            overlay,
                                            overlayInfo.XOffset, overlayInfo.YOffset,
                                            wallInfo?.TransparentColour ?? 0, isAlphaTested);
                    }

                    totalTiles += _tilemap.DayWalls.GetFrameCountForLogicalId(i + 1);
                }
            }

            if (contents)
            {
                var transparent = new SimpleTexture <byte>(AssetId.None, "Transparent", 1, 1, new byte[] { 0 })
                                  .AddRegion(Vector2.Zero, Vector2.One, 0);

                for (byte i = 1; i <= labyrinthData.ObjectGroups.Count; i++)
                {
                    _tilemap.DefineWall(i, transparent, 0, 0, 0, true);
                }

                totalTiles += labyrinthData.ObjectGroups.Count;
            }

            _wallCount = labyrinthData.Walls.Count;
            _floors    = new byte[totalTiles];
            _ceilings  = new byte[totalTiles];
            _contents  = new byte[totalTiles];
            var frames = new int[totalTiles];

            int index = 1;

            // Add initial frames
            if (floors)
            {
                FloorFrames    = new List <int> [labyrinthData.FloorAndCeilings.Count + 1];
                FloorFrames[0] = new List <int> {
                    0
                };
                for (byte i = 1; i <= labyrinthData.FloorAndCeilings.Count; i++)
                {
                    _floors[index] = i;
                    FloorFrames[i] = new List <int> {
                        index
                    };
                    index++;
                }
            }

            if (ceilings)
            {
                CeilingFrames    = new List <int> [labyrinthData.FloorAndCeilings.Count + 1];
                CeilingFrames[0] = new List <int> {
                    0
                };
                for (byte i = 1; i <= labyrinthData.FloorAndCeilings.Count; i++)
                {
                    _ceilings[index] = i;
                    CeilingFrames[i] = new List <int> {
                        index
                    };
                    index++;
                }
            }

            if (walls)
            {
                WallFrames    = new List <int> [labyrinthData.Walls.Count + 1];
                WallFrames[0] = new List <int> {
                    0
                };
                for (byte i = 1; i <= labyrinthData.Walls.Count; i++)
                {
                    _contents[index] = (byte)(i + 100);
                    WallFrames[i]    = new List <int> {
                        index
                    };
                    index++;
                }
            }

            if (contents)
            {
                ContentsFrames    = new List <int> [labyrinthData.ObjectGroups.Count + 1];
                ContentsFrames[0] = new List <int> {
                    0
                };
                for (byte i = 1; i <= labyrinthData.ObjectGroups.Count; i++)
                {
                    _contents[index]  = i;
                    ContentsFrames[i] = new List <int> {
                        index
                    };
                    index++;
                }
            }

            // Add animation frames
            if (floors)
            {
                for (byte i = 1; i <= labyrinthData.FloorAndCeilings.Count; i++)
                {
                    int frameCount = _tilemap.DayFloors.GetFrameCountForLogicalId(i);
                    for (int j = 1; j < frameCount; j++)
                    {
                        _floors[index] = i;
                        FloorFrames[i].Add(index);
                        frames[index++] = j;
                    }
                }
            }

            if (ceilings)
            {
                for (byte i = 1; i <= labyrinthData.FloorAndCeilings.Count; i++)
                {
                    int frameCount = _tilemap.DayFloors.GetFrameCountForLogicalId(i);
                    for (int j = 1; j < frameCount; j++)
                    {
                        _ceilings[index] = i;
                        CeilingFrames[i].Add(index);
                        frames[index++] = j;
                    }
                }
            }

            if (walls)
            {
                for (byte i = 1; i <= labyrinthData.Walls.Count; i++)
                {
                    int frameCount = _tilemap.DayWalls.GetFrameCountForLogicalId(i);
                    for (int j = 1; j < frameCount; j++)
                    {
                        _contents[index] = (byte)(i + 100);
                        WallFrames[i].Add(index);
                        frames[index++] = j;
                    }
                }
            }

            _tilemap.TileCount = totalTiles;
            for (int i = 0; i < totalTiles; i++)
            {
                SetTile(i, i, frames[i]);
            }

            for (int i = 0; i < totalTiles; i++)
            {
                AddSprites(labyrinthData, i, request);
            }
        }