Skip to content

Commit

Permalink
[Writer] Fixed height when color is enabled for generating heightmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarbuz committed Jan 20, 2019
1 parent fc7b966 commit 684510a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion SchematicToVox/SchematicToVox.csproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartArguments>--i heightmaps/JS-Wyre-2018-12-29-17-05-56.png --o vox/heightmap --hm --e --way=1</StartArguments>
<StartArguments>--i color.png --o color3 --hm 100</StartArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<StartArguments>
Expand Down
54 changes: 47 additions & 7 deletions SchematicToVox/Schematics/SchematicWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -16,6 +17,7 @@ public static class SchematicWriter
private static int _heightmap;
private static bool _color;


public static Schematic WriteSchematic(string path, int heightmap, bool excavate, bool color)
{
_excavate = excavate;
Expand Down Expand Up @@ -44,6 +46,8 @@ private static Schematic WriteSchematicFromImage(string path)
SchematicReader.WidthSchematic = schematic.Width;
SchematicReader.HeightSchematic = schematic.Heigth;

Bitmap grayScale = MakeGrayscale3(bitmap);

schematic.Blocks.Add(new HashSet<Block>());
using (var progressbar = new ProgressBar())
{
Expand All @@ -55,30 +59,31 @@ private static Schematic WriteSchematicFromImage(string path)
int x = i % schematic.Width;
int y = i / schematic.Width;
var color = bitmap.GetPixel(x, y);
var colorGray = grayScale.GetPixel(x, y);
if (color.A != 0)
{
if (_heightmap != 1)
{
int intensity = color.R + color.G + color.B;
int intensity = colorGray.R + colorGray.G + colorGray.B;
float position = intensity / (float)765;
int height = (int)(position * _heightmap);

if (_excavate)
{
if (CheckCornerPixels(bitmap, color, x, y))
{
AddMultipleBlocks(ref schematic, ref global, height, x, y, color);
AddMultipleBlocks(ref schematic, ref global, height, x, y, color, colorGray);
}
else
{
Block block = (_color) ? new Block(x, height - 1, y, color) :
new Block(x, height - 1, y, new Tools.Color32(211, 211, 211, 255));
Block block = (_color) ? new Block(x, height - 1, y, color) :
new Block(x, height - 1, y, colorGray);
AddBlock(ref schematic, ref global, block);
}
}
else
{
AddMultipleBlocks(ref schematic, ref global, height, x, y, color);
AddMultipleBlocks(ref schematic, ref global, height, x, y, color, colorGray);
}
}
else
Expand All @@ -94,12 +99,47 @@ private static Schematic WriteSchematicFromImage(string path)
return schematic;
}

private static void AddMultipleBlocks(ref Schematic schematic, ref int global, int height, int x, int y, Color color)
private static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);

//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);

//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});

//create some image attributes
ImageAttributes attributes = new ImageAttributes();

//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);

//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

//dispose the Graphics object
g.Dispose();
return newBitmap;
}

private static void AddMultipleBlocks(ref Schematic schematic, ref int global, int height, int x, int y, Color color, Color colorGray)
{
for (int z = 0; z < height; z++)
{
Block block = (_color) ? new Block(x, z, y, color) :
new Block(x, z, y, new Tools.Color32(211, 211, 211, 255));
new Block(x, z, y, colorGray);
AddBlock(ref schematic, ref global, block);
}
}
Expand Down

0 comments on commit 684510a

Please sign in to comment.