- Home /
Imported mesh bounds size is alwasy too big and pivot point is not aligned
Hi,
I'm having a bit of difficulty with importing custom meshes to Unity. I"m trying to create a custom mesh with vertices being drawn in screen coordinates. I've created an OBJ file with the stored mesh vertices and imported that into Unity and attached it to a game object. The mesh appears correctly however the mesh/render bounds are far too big and my pivot point is off in space somewhere and not connected to the mesh.
After some reading Ive become aware that Unity has a limitation with being able to change the pivot point of a mesh. I've tried offsets but because I have different meshes/sizes this wont work every time.
I'm wondering if someone knows why Unity draws the mesh a certain size (visually) but the entire game object size is so much bigger? This is making it difficult for me to draw the mesh at the appropriate space. Does anyone have any ideas?
EDIT: I've been able to offset the pivot point using the mesh bounds however, now the problem seems that the bounds are actually bigger than the mesh itself. I've drawn a debug line from the bounds min/max points and it appears that the line is in 3d space going through the center of the mesh (extending into Z).
Is there anyway to create a 2D mesh in unity? simply a plane that always faces towards the camera and whose bounds reflect the mesh vertices instead of some world points? I'm working in 2D and so far have found Unity quite difficult to work with on account of trying to convert everything from 3D to a 2D application of it.
Thanks
Setting up sprites is not so difficult. How is your camera set up? I'm assu$$anonymous$$g an orthographic setup, but your requirements may differ.
Answer by Fattie · Nov 11, 2012 at 09:18 AM
Firstly I'm not sure if you should be doing anything at all with meshes in relation to what you're trying to achieve, see below. But re your specific question, two possibilities come to mind (A) a stray vert (B) recalculating the bounds.
"The mesh appears correctly however the mesh/render bounds are far too big"
are you ABSOLUTELY SURE you do not have any stray verts? one will do it, an easy mistake to make! use this simple technique:
http://answers.unity3d.com/questions/307429/simply-display-or-highlight-empty-game-objects-in.html
to run through your vertices array and draw a big red ball at every single one. you'll soon see if there's an error.
(PS if you are new to working w/ mesh in unity is always worth remembering that the .vertices call CREATES A COPY of the verts, in distinction from all other properties in Unity.
http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html http://docs.unity3d.com/Documentation/ScriptReference/Mesh-vertices.html
function showAllMyBallsClearly()
{
var copyOfVerts : Vector3[] = GetComponent(MeshFilter).mesh;
for (var i = 0; i < copyOfVerts.Length; i++)
.. draw a ball there
}
also...
did you know about this function,
http://docs.unity3d.com/Documentation/ScriptReference/Mesh.RecalculateBounds.html
maybe that's the secret here.
regarding 2D ...
you ask a separate question about 2D. Realistically for 2D in Unity you need to grab "2DToolkit" (asset store)
there;s no other realistic way to do 2D in Unity -- the time saving is staggering. you just drop all your sprites in the 2DTK system, and it completely handles everything for you, from packing atlases to completely handling "flat meshes" as you say, to animations, to doing everything in one draw call.
Fattie,
Thanks a lot for the reply.
I'm actually using 2Dtoolkit but what I"m trying to do is not simply render a sprite to the screen. I actually am trying to render custom sized meshes for nav purposes. I'm using a tool that outputs the verts in screen coordinates so that is why I am using the screenToWorldPoint function to convert the vertices of the raw mesh I've imported as an asset. I also wasn't aware that calling the mesh.vertices property duplicates it, so thanks for that, I've made a temp variable ins$$anonymous$$d of calling it in the loop.
I've also implemented your idea of using the Gizmo balls to adorn the vertices and it works great! there aren't any stray verts but still something seems off.
Here is where I'm at:
I've drawn the mesh in blue for visibility purposes.
The yellow balls are the Gizmos that mark the mesh verts. The red rectangle is supposed a cube drawn around the mesh collider using:
Gizmos.DrawWireCube(mc.bounds.center, mc.bounds.size)
And the pink line is a debug line drawn from mesh collider's $$anonymous$$ to max points.
As you can see the line is drawn right through the center of the mesh to the bounding box's corners. I was under the impression that the mesh collider was suposed to be exact to the shape of the mesh itself, but here it shows up as equivalent to a box collider. Is there something that I'm missing about how mesh colliders work?
Thanks
hola! it's very possible you have not actually set the mesh collider! it's separate from the render mesh.
SECONDLY -- just TBC, you are aware that bounding boxes IN FACT WOR$$anonymous$$ LI$$anonymous$$E THAT. they are like a $$anonymous$$/max ON THE NATURAL AXIS of the universe.
check out AABB bounding box examples on the internet
(sorry if you know all this already - it's impossible to know if someone's a 70 yr old expert or a new chum on this site)
what do you mean by the "mesh collider's $$anonymous$$ to max points" what call are you using there?
hopefuly these quick notes bring a solution, if not more tomorrow!!!! :)
I'm exceptionally proud you were able to use two of my mentions! :)
Thanks for the information. I've been playing around a bit more and reading the documentaion and it clarified a few things. It seems I was confused between the actual mesh bounds and the bounding box. the mesh collider/renderer seems to detect the "actual" bounds of the mesh; that is the blue polygon. But the rectangle and the $$anonymous$$/max points are drawn I'm guessing to the AABB bounds.
Regardless I've managed to get the offset and pivot point correct using the mesh vertices and I can detect the correct bounds of the actual polygon mesh so it seems to be working. I haven't been able to finalize my solution as of yet but I'm getting close. I've closed out the question however as it has been answered; thanks for all the help.
hola @user1337
is there something else you need on this?
could you CLOSE OUT the questin please .. TIC$$anonymous$$ any useful answer Cheees