How Does Unity Assign Pivot Point Location on Script Generated Meshes - unity3d

I have tried to find any information on how the Unity assigns pivot points to object but all I keep finding is threads on how to move pivot points and that it can't be done. I am creating a 2D game with a background that is randomly created with meshes that are wrapped in empty GameObjects. These objects are organically shaped but they have a property that returns a rectangle that bounds the object so that they can be placed in a way that they are not overlapping. The trouble is that the algorithm assumes that the pivot point is going to be the center of the object. What I would like to know is how does Unity decide where the pivot point will be set to so that I can predict how much I will need to move my mesh inside the parent object so that the pivot point will be in the center of the bounding rectangle.

Possible fix:
Try create the meshes during runtime and see if it always places the pivot points at a certain corner or at least relatively speaking the same location.
If it does that you would know where the pivot point is and could take it into account in your code, if you also know the size of the mesh you spawn.

So I think most general and correct answer that I can come up with is that unity assigns the pivot point to the center of the GameObject that you apply the Mesh to. The local coordinates of the vertices of the mesh depending on how you create them mighht place your mesh so that its logical center is not the same as the that of the empty GameObject that it is attached to. What I did to fix the issue was to make a vector from local point (0,0,0) to the center of bounding rectangle and translate the vertices I use to make my mesh by that vector inverted. It wasn't perfect but by far close enough to ensure that I won't have any overlapping meshes.

Related

Hide parts of mesh overlapping another mesh in Unity

I have thoses two meshes:
In my game, I put the hat on the hair at runtime:
As you can see, as expected, the hair is visible outise the hat part.
How can I achieve this in Unity (what kind of mask shader should I use?):
I've tryed to make a depth mask but it hides every meshes in my scene. I just want to hide the hair, not others meshes.
And what if I have two player having the same case? Would player mask hide player 2 hair? How can I avoid that?
What I would do:
write a C# code that gets the pivot position (bottom part of the hat) and its up vector every frame.
build a plane with these values. The up vector would be the normal vector of the plane and a plane can be defined by a point and a normal vector.
I would pass the equation of the plane to the shader (via Material.SetFloat or Material.SetVector) and evaluate if the world positions of the hair vertices are in the correct or in the wrong side of the plane.

Unity3D dynamic mesh with hole

Dynamically creating a mesh with a hole in it from two lists of vertices
I am currently attempting to dynamically create a mesh (2D) with a hole in it. I have a list of Vector3 vertices for both the outline and the hole's outline.
My question:
How would I go about merging these two lists of vertices into a single mesh?
More detail: I have two meshes that overlap, and I'm trying to do a boolean difference between the two, to create a new mesh that will eventually replace the bigger one, to get rid of clipping. Example
Using the Clipper-Library (see http://www.angusj.com/delphi/clipper.php) is of no use, because it returns the same two sets of vertices that I set as input.
I'm guessing I need to somehow fix the triangles for the mesh to create triangles between the outer and inner vertices? (The meshes can be any shape/size, so finding out which vertexes to combine into triangles is no easy task).
Can anybody tell me how I would create a single mesh out of the two vertex-loops?
If you need a generic boolean algorithm this is a very hard problem, for example 3D Studio Max has two seperate boolean mesh creators, each failing at different sets of objects.
If you only need to subtract rectangular, aligned shapes, which do not touch, its simpler. For your specific case you can just join the list of vertexes, and fill new list of triangles - you'll need two tris per quad, so thats eight triangles stretched across eight verices.
It gets a bit harder if they start to touch, as you need to find intersection points and basically re-triangulate the outline.

How to change pivot of a node

I tried this
xxxx.pivot = SCNMatrix4MakeTranslation(0.5, 0.5, 0.5)
But it does not make it centered. What is the best way of doing that with code?
Center the pivot to the center of the object, and the object to the center of the world, in Blender, Cheeatah 3D, 3D studio Max or any other suitable software and then re-export it with the pivot in the proper position. Changing the pivot in code does not have the same effect and will quite likely lead to other issues once you want to translate, rotate, scale etc. Offsetting the pivot through code is not really meant as a fix for an incorrectly placed pivot which is actually the result of the vertices having the wrong positions. Rather than trying to patch that in code, fix it properly in a 3D modeling package.
A little confused about the question. Are you trying to center the couch on the coordinate origin? Why are you using pivot instead of translation? The pivot property moves the local coordinate system origin relative to the object itself (i.e. setting the upper left corner of the couch to be 0,0, instead of the center). Whereas translation moves the object in the scene but maintains the object's position relative to its own coordinate system. Which are you trying to accomplish?
https://developer.apple.com/documentation/scenekit/scnnode/1408044-pivot

How do I center a mesh on a GameObject?

I have a 2D game where the users can create cars by clicking on the screen to add vertices and then drag these around as they see fit. These vertices are then used to draw a mesh. Users can also add wheels to the vertices (1 per vertex).
In the game there is also an option to have the computer generate a random car, which it does by creating eight vertices at random points inside of a unit circle.
These cars are stored and it is possible for the user to load them and re-use them. The two pictures below show a square button with a computer generated car (The green one with red wheels) which sits inside of the square as intended, and a user generated car (The white one with pink wheels) which sits outside of the square.
I know that this is because the mesh of the computer generated one draws triangles from Vector3.zero which is the center of the object, while the user created one only triangles between the vertices, which aren't necessarily placed around the center of the object (As in this example where they are all placed above and to the right of the center).
How would I go about centering the mesh of the user created car?
I could perhaps calculate the center of the mesh and then subtract that from the position of all the vertices. Or I could subtract that position from the position of the GameObject that holds the mesh when presenting it in the gui. Or are there better alternative solutions?
Using Mesh.Bounds
Transform car;
Bounds carBounds = car.GetComponent<MeshFilter>().mesh.bounds;
Vector3 whereYouWantMe;
Vector3 offset = car.transform.position - car.transform.TransformPoint(carBounds.center);
car.transform.position = whereYouWantMe + offset;
This will also give you access to a bunch of cool fields like center min max and size

Open GL - ES 2.0 : Touch detection

Hi Guys I am doing some work on iOS and the work requires use of OpenGL es. So now I have a bunch of squares, cubes and triangles on the screen. Some of these geometries might overlap. Any ideas/ approaches for touch detection?
Regards
To follow up on the answer already given, squares, cubes and triangles are convex shapes so you can perform ray-object intersection quite easily, even directly from the geometry rather than from the mathematical description of the perfect object.
You're going to need to be able to calculate the distance of a point from the plane and the intersection of a ray with the plane. As a simple test you can implement yourself very quickly, for each polygon on the convex shape work out the intersection between the ray and the plane. Then check whether that point is behind all the planes defined by polygons that share an edge with the one you just tested. If so then the hit is on the surface of the object — though you should be careful about coplanar adjoining polygons and rounding errors.
Once you've found a collision you can easily get the length of the ray to the point of collision. The object with the shortest distance is the one that's in front.
If that's fast enough then great, otherwise you'll probably want to look into partitioning the world or breaking objects down to their silhouettes. Convex objects are really simple — consider all the edges that run between one polygon and the next. If only exactly one of those polygons is front facing then the edge is part of the silhouette. All the silhouettes edges together can be projected to a convex 2d shape on the view plane. You can then test touches by performing a 2d point-in-polygon from that.
A further common alternative that eliminates most of the maths is picking. You'd render the scene to an invisible buffer with each object appearing as a solid blob in a suitably unique colour. To test for touch, you'd just do a glReadPixels and inspect the colour.
For the purposes of glu on the iPhone, you can grab SGI's implementation (as used by MESA). I've used its tessellator in a shipping, production project before.
I had that problem in the past. What I have used is an implementation of glu unproject that you can find on google (it uses the inverse of the model view projection matrix and the viewport size). This allows you to map the 2D screen coordinates to a 3D vector into the world. Then, you can use this vector to intersect with your objects and see which one intersects (or comes really close to doing so).
I do hope there are better ways of doing this, so I look forward to other answers as well!
Once you get the inverse-modelview and cast your ray (vector), you still need to know if the ray intersects your geometry. One approach would be to grab the depth (z in view coordinate system) of the object's center and extend (stretch) your vector just that far. Then see if the vector's "head" ends within the volume of your object or not (you need the objects center and e.g. Its radius, if it's a sphere)