I want to find the 2D bounding box (BB) of the visible part of an Game-object in Unity3D.**
Thanks to having no reputation i cant post images, so here are links to Imgur:
Starting Point: Image
Showing the initial scene camera. For example i want to get the BB of the wheels of the passing car.
Goal: Image
Showing the quite small BB i want to get in an efficient way. The method used is very costly and i want to avoid it. Its works on a segmented version of the input picture, looking similar to this: Image.
Current Approach:
Image
This uses the bounds of all GameObjects (GO) tagged as wheels and calls a function similar like this:
Rect GetBoundingBox(GameObject go, Camera camera, float margin = 0f)
{
Rect rect = new Rect(-1f, -1f, -1f, -1f);
Vector3[] v; // Vertices
// Object is behind camera
if (camera.WorldToScreenPoint(go.GetComponentInChildren<Renderer>().bounds.center).z < 0)
return rect;
// get first mesh filter
v = go.GetComponentInChildren<MeshFilter>().mesh.vertices;
// maybe the game object hast no mesh yet... and is empty
if (v.Length < 1 || v == null)
return rect;
for (int i = 0; i < v.Length; i++)
{
//World space
v[i] = go.transform.TransformPoint(v[i]);
//GUI space
v[i] = camera.WorldToScreenPoint(v[i]);
v[i].y = Screen.height - v[i].y;
}
Vector3 min = v[0];
Vector3 max = v[0];
for (int i = 1; i < v.Length; i++)
{
min = Vector3.Min(min, v[i]);
max = Vector3.Max(max, v[i]);
}
//Construct a rect of the min and max positions and apply some margin
rect = Rect.MinMaxRect(min.x, min.y, max.x, max.y);
// apply optional margin
rect.xMin -= margin;
rect.xMax += margin;
rect.yMin -= margin;
rect.yMax += margin;
return rect;
}
Two problems appear.
Its getting all BB even if the GO is not visible by the camera.
Even if its visible, the boundaries are as large as the GO it self, not as the visible part.
I am very happy about every hint in a good direction!!
For interested people the background is the development of an application to generate/simulate labeled training data for an deep learning project.
Related
I followed a unity tutorial to make a memory game but after adding more cards, they now go off the right of the screen.
The code uses a single card in the Unity designer and all other cards are instantiated and positioned from that (using startpos)
public const float offset = 2.5f;
Vector3 startpos = originalCard.transform.position;
for (int i = 0; i < numCols; i++)
{
for (int j = 0; j < numRows; j++)
{
card = Instantiate(originalCard) as MemoryCard;
float posX = (offset * i) + startpos.x;
float posY = - (offset * j) + startpos.y;
card.transform.position = new Vector3(posX, posY, startpos.z);
}
}
I would like to center the cards in the screen but not sure how given that the screen dimensions via Screen.width are in pixels but the Vector code is not and the x component of the original card (startpos.x) is actually negative.
Easiest way is to move the original card in your scene view. If you click your camera u can see it's bounds so click the camera, then move you starting card as far left as you can and still see it. If they still don't all fit in camera, you can change the card width and do some math to see how many cards you can fit.
The alternative solution, but also easy, is to adjust your camera bounds to encompass the cards where they are.
So I'm new to XNA and am attempting to draw multiple sprites to the screen in the easiest way. I want each sprite to be incremented in the X axis so that I am left with multiple sprites drawn across the screen.
I have defined:
Vector2 pos;
In the LoadContent function I have:
pos = new Vector2(0, 0);
and in Draw I have:
spriteBatch.Draw (circle, pos, Color.White); //Draws sprite to screen in correct position
spriteBatch.Draw(circle, (pos.X += 1), Color.White); //causes error and doesnt draw
Hopefully I've explained this well enough and you can see what I'm trying to do, the compiler doesn't agree with (pos.X += 50) (me trying to increment the X position by 50).
I know I could go about this a longer way and create a new Vector2 for each draw but that would create multiple lines of what I think surely is unnecessary code and there must be a quick way like this to go about doing it?
The method signature of Draw expects the second parameter to be a Vector2, right?
If so, then (pos.X += 1) isn't a Vector2. It's a statement which increments the X parameter of the pos Vector2 variable, but the statement doesn't return an instance of a Vector2 object.
Edit: Code is as follows:
public void DrawSprites()
{
// setup circle here
// setup spritebatch here
// setup initial pos here
// setup MAX_ITERATIONS here
var INCREMENT_VALUE = 50;
for (var i = 0; i < MAX_ITERATIONS; i++) {
var iteratedPos = pos + new Vector2((INCREMENT_VALUE * i), 0); // per Nikola's comment
spriteBatch.Draw(circle, iteratedPos, 0), Color.White);
}
}
You need to make a list of the draw rectangles of your textures.
//implement Texture2D (called "image") and SpriteBatch (called "spriteBatch")
List<Rectangle> rectangles = new List<Rectangle>();
const int ITERATIONS = 10; //or whatever you want the iterations to be
const int INCREMENT_VALUE = 50; //again, whatever you want it to be
for (int i = 0; i < ITERATIONS; i++)
{
for (int j = 0; j < rectangles.Count; j++)
{
rectangles[j].X += INCREMENT_VALUE * i;
spriteBatch.Draw(image, rectangles[j], Color.White);
}
}
Like said earlier, you need a list of Rectangles that have all the image rectangles in them. Hope I helped, and good luck!
here is my code for displaying bounding box
Vector3[] corners = box.GetCorners();
for (int i = 0; i < 8; i++)
{
verts[i].Position = Vector3.Transform(corners[i],modelMatrix);
verts[i].Color = Color.White;
}
vbo.SetData(verts);
ibo.SetData(indices);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
effect.World = Matrix.Identity;
effect.View = view;
effect.Projection = projection;
pass.Apply();
ContentLoader.SetBuffers(ibo, vbo);
}
I'd like to achieve same result using BoundingBox class.
I tried to do it like this,but it doesn't work
for (int i = 0; i < boundingBoxes.Count; i++)
{
Vector3 min = Vector3.Transform(boundingBoxes[i].Min, modelMatrix);
Vector3 max = Vector3.Transform(boundingBoxes[i].Max, modelMatrix);
boundingBoxes[i] = new BoundingBox(min, max);
}
the code above works if there is no rotation.With rotation things get messed up.Any idea why and how to fix it?
You can not rotate a BoundingBox object in Xna. The built in collision detection methods of the BoundingBox class will always be calculated from min & max for a box in axis alignment only. By transforming min & max, you are not rotating the box, you are only changing the x,y,z dimensions of the axis aligned box.
You might be better off studying up on "oriented bounding boxes". You would draw an oriented box by using the corners as verts and choosing 'LineList' as your PrimitiveType instead of 'TriangleList' in the 'DrawIndexedPrimitives' method. Collision detection for an oriented box is different & more complex than for an axis aligned box.
I have some problems on the display of my model with texture.
Everything works perfectly, however, I use a loop to repeat the texture to represent a floor 20 X 20 on the screen. My texture is repeated correctly. But I do not understand why all my textures generate a flicker ...
I noticed that images are superimposed on each other. I'm sure I checked my loop is coded correctly.
see screenshot:
my code (loop function generation ground):
//Function draw - ground land
private void draw_groundLand(Vector3 position_model_origin)
{
//example generation mode 4x4 cubes
int[,,] MatriceWorldCube = new int[1,2,2];
MatriceWorldCube[0, 0, 0] = 1;
MatriceWorldCube[0, 0, 1] = 1;
MatriceWorldCube[0, 1, 0] = 2;
MatriceWorldCube[0, 1, 1] = 1;
int height = MatriceWorldCube.GetLength(0);
int width = MatriceWorldCube.GetLength(1);
int length = MatriceWorldCube.GetLength(2);
Vector3 pos_reference = position_model_origin;
for (int thickness = 0; thickness < height; thickness ++)
{
for (int column = 0; column < width; column ++)
{
for (int line = 0; line < length ; line ++)
{
// Copy any parent transforms.
Matrix[] transforms = new Matrix[model_ground_land1.Bones.Count];
model_ground_land1.CopyAbsoluteBoneTransformsTo(transforms);
// Draw the model. A model can have multiple meshes, so loop.
foreach (ModelMesh mesh in model_ground_land1.Meshes)
{
// This is where the mesh orientation is set, as well
// as our camera and projection.
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] *
Matrix.CreateRotationY(cubeGroundLand1_modelRotation) * Matrix.CreateTranslation(position_model_origin);
effect.View = View;
effect.Projection = Projection;
}
// Draw the mesh, using the effects set above.
mesh.Draw();
}
position_model_origin.X = (float)(line +1);
}
position_model_origin.X = pos_reference.X;
position_model_origin.Z = (float)(column +1);
}
position_model_origin.Z = pos_reference.Z;
position_model_origin.Y = (float)(thickness+1);
}
position_model_origin.Y = pos_reference.Y;
position_model_origin = pos_reference;
}
Thank you in advance for your help. I lose patience (over a whole weekend ^ ^)
It's Z-fighting. Z-buffer precision falling with distance so far objects have more "flickers" GPU can't figure out which polygon is on top because tail of the z-buffer value isn't precise enough to distinguish 2 almost equal values.
You have 6 ways to fix it:
Move geometry or do not display part that is below.
Use something like polygon offset in OpenGL
Use CPU z-sorting instead of z-buffer.
Use only one object with 2 textures instead of 2 objects + some shaders (i don't know what exactly you are trying to achieve)
Use larger z-buffer.
Move clip planes closer to each other it will increase precision.
This method that draws my tiles seems to be quite slow, Im not sure exactly whats wrong, it belive my culling method isnt working and is drawing stuff offscreen, but im not completeley sure. Here it is:
// Calculate the visible range of tiles.
int left = (int)Math.Floor(cameraPosition.X / 16);
int right = left + spriteBatch.GraphicsDevice.Viewport.Width / 16;
right = Math.Min(right, Width) + 1; // Width -1 originally - didn't look good as tiles drawn on screen
if (right > tiles.GetUpperBound(0))
right = tiles.GetUpperBound(0) + 1; // adding 1 to get the last right tile drawn
int top = (int)Math.Floor(cameraPosition.Y / 16);
int bottom = left + spriteBatch.GraphicsDevice.Viewport.Height/ 16;
bottom = Math.Min(bottom, Height) + 1; // Height -1 originally - didn't look good as tiles drawn on screen
if (bottom > tiles.GetUpperBound(1))
bottom = tiles.GetUpperBound(1) + 1; // adding 1 to get the last bottom tile drawn
// For each tile position
for (int y = top; y < bottom; ++y)
{
for (int x = left; x < right; ++x)
{
// If there is a visible tile in that position, draw it
if (tiles[x, y].BlockType.Name != "Blank")
{
Texture2D texture = tileContent["DirtBlock_" + getTileSetType(tiles,x,y)];
spriteBatch.Draw(texture, new Vector2(x * 16, y * 16), Color.White);
if (isMinimap)
spriteBatch.Draw(pixel, new Vector2(30+x, 30+y), Color.White);
}
}
}
GetTileSetTypes is a function to get what tiles are around it, for different textures, like DirtBlock_North, DirtBlock_Center, etc.
Tile content is just a class with my block textures.
Try changing SpriteBatch.Begin to defered and combining all of the tiles onto one texture.
See this GameDev question for info about why deferred is most likely the fastest option for you.
Also realize that every time you draw a new texture you have to take the old one out of the GPU and put the new one in. This process is called texture swapping and usually isn't an issue but you are swapping textures twice per tile which is likely to impact performance noticeably.
This can be fixed by combining multiple sprites onto one texture and using the source rectangle argument. This allows you to draw multiple sprites without a texture swap. There are a few OSS libraries for this. Sprite Sheet Packer is my personal favorite.
Unfortunantly without the project and a profiler I'm just guessing; however, these are the two biggest gotchas for rendering tilemaps I know of. I can't really see anything wrong from here. Below is the code I use to draw my tile maps and as you see its very similar to yours.
If all else fails I would suggest using a profiler to figure out which bits are running slowly.
//Init the holder
_holder = new Rectangle(0, 0, TileWidth, TileHeight);
//Figure out the min and max tile indices to draw
var minX = Math.Max((int)Math.Floor((float)worldArea.Left / TileWidth), 0);
var maxX = Math.Min((int)Math.Ceiling((float)worldArea.Right / TileWidth), Width);
var minY = Math.Max((int)Math.Floor((float)worldArea.Top / TileHeight), 0);
var maxY = Math.Min((int)Math.Ceiling((float)worldArea.Bottom / TileHeight), Height);
for (var y = minY; y < maxY; y++) {
for (var x = minX; x < maxX; x++) {
_holder.X = x * TileWidth;
_holder.Y = y * TileHeight;
var t = tileLayer[y * Width + x];
spriteBatch.Draw(
t.Texture,
_holder,
t.SourceRectangle,
Color.White,
0,
Vector2.Zero,
t.SpriteEffects,
0);
}
}