I am having a problem with displaying maps on screen. I am giving the developer the choice between square tiles and hexagonal tiles. The map is saved as a .txt
The square tiled map displays correctly as shown
where as the hexagonal map is upside down
I am completely stumped as to how to go about fixing the issue I have supplied the entire function bellow
void displayMap(){
//Draw Tiles on the Screen
if (useSquareTiles == true && useHexagonTiles == false) {
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
GameObject.Instantiate (mapArray [y] [x], new Vector3 (x, mapHeight - y, 0),
Quaternion.Euler (-90, 0, 0));
}
}
}
float hexWidth = 0.85f; //The width of the HexTiles
if (useHexagonTiles == true && useSquareTiles == false) {
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
GameObject.Instantiate (mapArray[y][x], new Vector3 (x * hexWidth, y + (0.5f * Mathf.Abs (x) % 1), 0),
Quaternion.Euler (-90, 0, 0));
}
}
}
}
Related
The codes below generate 10000cubes(using gizmos). I'm stuck on how to further expand the code and using gizmos to illustrate the sorting process(movement of the cubes when mouse button is click), appreciate if can give me some hint on how to do it.
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
int x;
int y;
int z;
int[,] array1 = new int[100, 100];
int temp;
void Start()
{
for (x = 0; x < 100; x++)
{
for (y = 0; y < 100; y++)
{
array1[x, y] = Random.Range(0, 2);
Debug.Log(string.Format("{0},{1},{2}", x, y, array1[x, y]));
}
}
}
void OnDrawGizmos()
{
for (x = 0; x < 100; x++)
{
for (y = 0; y < 100; y++)
{
Vector2 pos1 = new Vector2(0 + x, 0 + y);
Gizmos.DrawCube(pos1, transform.position);
Gizmos.color = (array1[x, y] == 1) ? Color.black : Color.white;
}
}
}
void OnMouseDown()
{
for (z = 0; z < 100; z++)
{
for (x = 0; x < 100; x++)
{
for (y = 0; y < 100-z; y++)
{
if (array1[x, y] > array1[x, y + 1])
temp = array1[x, y];
array1[x + 1, y] = array1[x, y];
array1[x, y] = temp;
}
}
}
}
}
1.Possible Solution
I provide an answer in this part, and suggest better alternatives below.
The problem with your code is that the sorting happens synchronously, within one frame. What you can do is move the sorting code to Update, and sort a little between each frame. This will let the Gizmos call draw the correct state.
2.Use Progress Bar instead
You may use EditorUtility.DisplayProgressBar to display a progress bar in the Editor. Since you're using Gizmos which don't show up in the Build, this solution assumes you're working in the Editor.
3.Use Compute Buffers
CatLikeCoding has this fantastic article on using Compute Shaders to draw a Graph in high resolution. You can follow it and draw your sorting progress in a very high resolution. Furthermore, this will work in both editor and runtime.
I'm attempting to get the bounds of the edges of interest in a binary image using the following method. Sadly my maths seems to be letting me down and I'm only getting a rectangle 2px smaller in each dimension than the original image.
Can someone show me where I have gone wrong?
Note. FastBitmap is a class that allows fast access to pixel data.
private Rectangle FindBox(Bitmap bitmap, byte indexToRemove)
{
int width = bitmap.Width;
int height = bitmap.Height;
int minX = width;
int minY = height;
int maxX = 0;
int maxY = 0;
using (FastBitmap fastBitmap = new FastBitmap(bitmap))
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (fastBitmap.GetPixel(x, y).B == indexToRemove)
{
if (x < minX)
{
minX = x;
}
if (x > maxX)
{
maxX = x;
}
if (y < minY)
{
minY = y;
}
if (y > maxY)
{
maxY = y;
}
}
}
}
}
// check
if ((minX == width) && (minY == height) && (maxX == 0) && (maxY == 0))
{
minX = minY = 0;
}
return new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
}
The image I'm testing.
You appear to be checking EVERY pixel to see if a match exists in any x and y. Instead, what you want to do is check the minx, maxx, miny, and maxy separately.
For the minY, you want start at the top and check each row down until you hit a y row that has a matching pixel.
For the maxY, you want start at the bottom and check each row up until you hit a y row that has a matching pixel.
For the minX, you want start at the left and check each column until you hit a x column that has a matching pixel.
For the maxX, you want start at the right and check each column until you hit a x column that has a matching pixel.
Something like this:
minY = getMinY(fastBitmap, indexToRemove);
maxY = getMinY(fastBitmap, indexToRemove);
minX = getMinY(fastBitmap, indexToRemove);
maxX = getMinY(fastBitmap, indexToRemove);
int getMinY(Bitmap bitmap, byte indexToRemove)
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (fastBitmap.GetPixel(x, y).B == indexToRemove)
{
return y;
}
}
}
return 0;
}
int getMaxY(Bitmap bitmap, byte indexToRemove)
{
for (int y = height; y > 0; y--)
{
for (int x = 0; x < width; x++)
{
if (fastBitmap.GetPixel(x, y).B == indexToRemove)
{
return y;
}
}
}
return height;
}
etc...
You should be able to write the getMinX and getMaxY yourself.
I want to generate something like this:
I use Perlin Noise with sharp curve, my code produces those cliffs:
.
for (int x = 0; x < sizeX; x++)
{
for (int z = 0; z < sizeZ; z++)
{
int floorY = map.GetMaxYNotWater(x, z);
float n = hillsNoise.Noise(x, z);
int hillY = (int)(curveHills.Evaluate(n) * 80f);
if (hillY > floorY + 5)
{
for (int y = hillY; y > floorY; y--)
{
map.SetBlock(GetBlock(y), new Vector3i(x, y, z));
}
}
}
}
How can I "cut" them to make hanging things?
I tried to do it like this with additional curve:
for (int x = 0; x < sizeX; x++)
{
for (int z = 0; z < sizeZ; z++)
{
int floorY = map.GetMaxYNotWater(x, z);
float n = hillsNoise.Noise(x, z);
int hillY = (int)(curveHills.Evaluate(n) * 80f);
if (hillY > floorY + 5)
{
int c = 0;
int max = hillY - floorY;
max = (int)(max * curveHillsFull.Evaluate(n)) + 1;
for (int y = hillY; y > floorY && c < max; y--, c++)
{
map.SetBlock(GetBlock(y), new Vector3i(x, y, z));
}
}
}
}
But it produces flying islands.
So what can I do to achieve the first screenshot results?
I can't say how Minecraft does it, but from my own experience with voxel terrain, the best way to approach it is to think of the voxel grid as something like a cloud: each voxel has a density, and when that density is high enough, it becomes a 'visible' part of the cloud and you fill the voxel.
So rather than calculating the min and max Y levels, work on calculating the density value, something like this:
for (int x = 0; x < sizeX; x++)
{
for (int y = 0; y > sizeY; y--)
{
for (int z = 0; z < sizeZ; z++)
{
//This means less density at higher elevations, great for turning
//a uniform cloud into a terrain. Multiply this for flatter worlds
float flatWorldDensity = y;
//This calculates 3d Noise: you will probably have to tweak this
//heavily. Multiplying input co-ordinates will allow you to scale
//terrain features, while multiplying the noise itself will make the
//features stronger and more or less apparent
float xNoise = hillsNoise.Noise(x, y);
float yNoise = hillsNoise.Noise(x, z);
float zNoise = hillsNoise.Noise(y, z);
float 3dNoiseDensity = (xNoise + yNoise + zNoise) / 3;
//And this adds them together. Change the constant "1" to get more or
//less land material. Simple!
float ActualDensity = flatWorldDensity + 3dNoiseDensity;
if (ActualDensity > 1)
{
map.SetBlock(GetBlock(y), new Vector3i(x, y, z));
}
}
}
}
I am creating a game after working through a XNA 4.0 book. It will be 3D, but I am already stuck in creating the terrain...
UPDATE: Everything starting from here is an update...
Terrain Update:
public void Update(Matrix view, Matrix projection)
{
View = view;
Projection = projection;
World = Matrix.CreateTranslation(-Width / 2f, 0, Height / 2f);
}
Terrain Draw:
public void Draw(GraphicsDevice g)
{
effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
effect.Parameters["xView"].SetValue(View);
effect.Parameters["xProjection"].SetValue(Projection);
effect.Parameters["xWorld"].SetValue(World);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
//g.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColorNormal.VertexDeclaration);
g.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
}
}
The commented line is working, in the both cases I am able to see the terrain...
The following code is to initialize Vertex and Index Buffer:
private void SetUpVertices(GraphicsDevice g)
{
float currentH;
int currentI;
vertices = new VertexPositionColorNormal[Width * Height];
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
currentH = heightData[x,y];
currentI = x + y * Width;
vertices[currentI].Position = new Vector3(x, currentH , -y);
if (currentH < minH + (maxH - minH) / 3)
vertices[currentI].Color = Color.ForestGreen;
else if (currentH < maxH - (maxH - minH) / 3)
vertices[currentI].Color = Color.LawnGreen;
else
vertices[currentI].Color = Color.White;
}
}
SetUpIndices(g);
}
private void SetUpIndices(GraphicsDevice g)
{
indices = new int[(Width - 1) * (Height - 1) * 6];
int counter = 0;
for (int y = 0; y < Height - 1; y++)
{
for (int x = 0; x < Width - 1; x++)
{
int lowerLeft = x + y * Width;
int lowerRight = (x + 1) + y * Width;
int topLeft = x + (y + 1) * Width;
int topRight = (x + 1) + (y + 1) * Width;
indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;
indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}
SetUpNormals(g);
}
private void SetUpNormals(GraphicsDevice g)
{
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].Normal = Vector3.Zero;
}
int[] index = new int[3];
Vector3 s1, s2, n;
for (int i = 0; i < vertices.Length / 3; i++)
{
for (int y = 0; y < 3; y++)
index[y] = indices[i * 3 + y];
s1 = vertices[index[0]].Position - vertices[index[2]].Position;
s2 = vertices[index[0]].Position - vertices[index[1]].Position;
n = Vector3.Cross(s1, s2);
for (int y = 0; y < 3; y++)
{
vertices[index[y]].Normal += n;
vertices[index[y]].Normal.Normalize();
}
}
FillBuffers(g);
}
private void FillBuffers(GraphicsDevice g)
{
VertexBuffer = new VertexBuffer(g, VertexPositionColorNormal.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
VertexBuffer.SetData(vertices);
IndexBuffer = new IndexBuffer(g, typeof(int), indices.Length, BufferUsage.WriteOnly);
IndexBuffer.SetData(indices);
g.Indices = IndexBuffer;
g.SetVertexBuffer(VertexBuffer);
}
I don't think, that there is a mistake, because it is working with the other line. Might there be an error with the .fx file I am using. If you think so, I am going to switch to BasicEffects...
(You might notice, that the code is from http://www.riemers.net/eng/Tutorials/XNA/Csharp/series1.php )
Thanks for your help...
Yours,
Florian
(Answer to original revision of the question.)
You're not setting your vertex buffer and index buffer onto the graphics device. These two lines of code (untested) should do what you need:
g.GraphicsDevice.Indices = indexBuffer;
g.GraphicsDevice.SetVertexBuffer(vertexBuffer);
Place them just after you set the parameters on your effect (ef), before the loop.
The vertex buffer provides the vertex declaration that the exception message is asking for.
Edit after question update: In your new version you're setting the vertex and index buffers - but it's in the wrong place. You need to set them onto the graphics device each frame. Your code would only work if nothing changes them after you set them in FillBuffers. But I'm guessing that stuff is being drawn outside your class's Draw method?
If that something else is a SpriteBatch, even it works using vertex buffers and index buffers. So it will reset your settings. (It's worth adding that it also sets render states - in which case you might need to see this article.)
In an assignment for school do we need to do some image recognizing, where we have to find a path for a robot.
So far have we been able to find all the polygons in the image, but now we need to generate a pixel map, that be used for an astar algorithm later. We have found a way to do this, show below, but the problem is that is very slow, as we go though each pixel and test if it is inside the polygon. So my question is, are there a way that we can generate this pixel map faster?
We have a list of coordinates for the polygon
private List<IntPoint> hull;
The function "getMap" is called to get the pixel map
public Point[] getMap()
{
List<Point> points = new List<Point>();
lock (hull)
{
Rectangle rect = getRectangle();
for (int x = rect.X; x <= rect.X + rect.Width; x++)
{
for (int y = rect.Y; y <= rect.Y + rect.Height; y++)
{
if (inPoly(x, y))
points.Add(new Point(x, y));
}
}
}
return points.ToArray();
}
Get Rectangle is used to limit the search, se we don't have to go thoug the whole image
public Rectangle getRectangle()
{
int x = -1, y = -1, width = -1, height = -1;
foreach (IntPoint item in hull)
{
if (item.X < x || x == -1)
x = item.X;
if (item.Y < y || y == -1)
y = item.Y;
if (item.X > width || width == -1)
width = item.X;
if (item.Y > height || height == -1)
height = item.Y;
}
return new Rectangle(x, y, width-x, height-y);
}
And atlast this is how we check to see if a pixel is inside the polygon
public bool inPoly(int x, int y)
{
int i, j = hull.Count - 1;
bool oddNodes = false;
for (i = 0; i < hull.Count; i++)
{
if (hull[i].Y < y && hull[j].Y >= y
|| hull[j].Y < y && hull[i].Y >= y)
{
try
{
if (hull[i].X + (y - hull[i].X) / (hull[j].X - hull[i].X) * (hull[j].X - hull[i].X) < x)
{
oddNodes = !oddNodes;
}
}
catch (DivideByZeroException e)
{
if (0 < x)
{
oddNodes = !oddNodes;
}
}
}
j = i;
}
return oddNodes;
}
There are some interesting discussion here on polygon hit tests, but it sounds to me as if you might be better off with a polygon fill.
You may want to look for a Plygon Triangulation algorithm.
Also, note that catching an exception is far more time-consuming that checking the right condition. So i suggest you to convert your existing code in:
public bool inPoly(int x, int y)
{
int i, j = hull.Count - 1;
var oddNodes = false;
for (i = 0; i < hull.Count; i++)
{
if (hull[i].Y < y && hull[j].Y >= y
|| hull[j].Y < y && hull[i].Y >= y)
{
var delta = (hull[j].X - hull[i].X);
if (delta == 0)
{
if (0 < x) oddNodes = !oddNodes;
}
else if (hull[i].X + (y - hull[i].X) / delta * delta < x)
{
oddNodes = !oddNodes;
}
}
j = i;
}
return oddNodes;
}