6 using Vintagestory.API.Client;
7 using Vintagestory.API.Common;
8 using Vintagestory.API.MathTools;
12 public class StandardRenderer : IChunkRenderer
14 private readonly int chunkSize;
18 /// Renders shards similar to the Default VS version, plus P.O.I. markings.
20 /// <param name="clientAPI">Client API.</param>
21 /// <param name="logger">Logger.</param>
22 public StandardRenderer(ICoreClientAPI clientAPI, ILogger logger) : base(clientAPI, logger)
24 chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
27 public override void GenerateChunkPngShard(Vec2i chunkPos, IMapChunk mc, ColumnMeta metaData, PngWriter pngWriter, out uint pixelCount)
30 BlockPos tmpPos = new BlockPos();
31 Vec2i localpos = new Vec2i();
33 var chunksColumn = new IWorldChunk[ClientAPI.World.BlockAccessor.MapSizeY / chunkSize];
35 int topChunkY = mc.YMax / chunkSize;//Heywaitaminute -- this isn't a highest FEATURE, if Rainmap isn't accurate!
37 for (int chunkY = 0; chunkY <= topChunkY; chunkY++)
39 chunksColumn[chunkY] = ClientAPI.World.BlockAccessor.GetChunk(chunkPos.X, chunkY, chunkPos.Y);
40 //What to do if chunk is a void? invalid?
43 // Prefetch map chunks, in pattern
44 IMapChunk[] mapChunks = new IMapChunk[]
46 ClientAPI.World.BlockAccessor.GetMapChunk(chunkPos.X - 1, chunkPos.Y - 1),
47 ClientAPI.World.BlockAccessor.GetMapChunk(chunkPos.X - 1, chunkPos.Y),
48 ClientAPI.World.BlockAccessor.GetMapChunk(chunkPos.X, chunkPos.Y - 1)
51 //pre-create PNG line slices...
52 ImageLine[] lines = Enumerable.Repeat(new object(), chunkSize).Select(l => new ImageLine(pngWriter.ImgInfo)).ToArray();
54 for (int posIndex = 0; posIndex < (chunkSize * chunkSize); posIndex++)
56 int mapY = mc.RainHeightMap[posIndex];
57 int localChunkY = mapY / chunkSize;
58 if (localChunkY >= (chunksColumn.Length)) continue;//Out of range!
60 MapUtil.PosInt2d(posIndex, chunkSize, localpos);
61 int localX = localpos.X;
62 int localZ = localpos.Y;
65 int leftTop, rightTop, leftBot;
67 IMapChunk leftTopMapChunk = mc;
68 IMapChunk rightTopMapChunk = mc;
69 IMapChunk leftBotMapChunk = mc;
71 int topX = localX - 1;
73 int leftZ = localZ - 1;
76 if (topX < 0 && leftZ < 0)
78 leftTopMapChunk = mapChunks[0];
79 rightTopMapChunk = mapChunks[1];
80 leftBotMapChunk = mapChunks[2];
86 leftTopMapChunk = mapChunks[1];
87 rightTopMapChunk = mapChunks[1];
91 leftTopMapChunk = mapChunks[2];
92 leftBotMapChunk = mapChunks[2];
96 topX = GameMath.Mod(topX, chunkSize);
97 leftZ = GameMath.Mod(leftZ, chunkSize);
99 leftTop = leftTopMapChunk == null ? 0 : Math.Sign(mapY - leftTopMapChunk.RainHeightMap[leftZ * chunkSize + topX]);
100 rightTop = rightTopMapChunk == null ? 0 : Math.Sign(mapY - rightTopMapChunk.RainHeightMap[rightZ * chunkSize + topX]);
101 leftBot = leftBotMapChunk == null ? 0 : Math.Sign(mapY - leftBotMapChunk.RainHeightMap[leftZ * chunkSize + botX]);
103 float slopeness = (leftTop + rightTop + leftBot);
105 if (slopeness > 0) b = 1.2f;
106 if (slopeness < 0) b = 0.8f;
108 b -= 0.15f; //Slope boost value
110 if (chunksColumn[localChunkY] == null)
116 chunksColumn[localChunkY].Unpack();
117 int blockId = chunksColumn[localChunkY].Blocks[MapUtil.Index3d(localpos.X, mapY % chunkSize, localpos.Y, chunkSize, chunkSize)];
119 Block block = ClientAPI.World.Blocks[blockId];
121 tmpPos.Set(chunkSize * chunkPos.X + localpos.X, mapY, chunkSize * chunkPos.Y + localpos.Y);
123 int avgCol = block.GetColor(ClientAPI, tmpPos);
124 int rndCol = block.GetRandomColor(ClientAPI, tmpPos, BlockFacing.UP);
125 int col = ColorUtil.ColorOverlay(avgCol, rndCol, 0.125f);
126 var packedFormat = ColorUtil.ColorMultiply3Clamped(col, b);
128 int red = ColorUtil.ColorB(packedFormat);
129 int green = ColorUtil.ColorG(packedFormat);
130 int blue = ColorUtil.ColorR(packedFormat);
133 //============ POI Population =================
134 if (BlockID_Designators.ContainsKey(blockId))
136 var desig = BlockID_Designators[blockId];
140 red = desig.OverwriteColor.R;
141 green = desig.OverwriteColor.G;
142 blue = desig.OverwriteColor.B;
146 ImageLineHelper.SetPixel(lines[localZ], localX, red, green, blue);
148 //chunkImage.SetPixel(localX, localZ, pixelColor);
152 for (int row = 0; row < pngWriter.ImgInfo.Rows; row++)
154 pngWriter.WriteRow(lines[row], row);