- Home /
How to rotate child 3D object to match bend of mesh
Another new developer to Unity here so I apologize if this approach makes no sense.
So I have a project with a single flat mesh which can be bent by the user. (Think like Virtual Desktop or BigScreen and changing the curvature of the desktop)
I have 2 separate 3D models that I placed on either side of the above noted mesh. In the project I made the 2 models children of the mesh in the hierarchy.
What I am trying to do is to re-position and rotate the models as the mesh bends so they stay at the edge of the mesh and their orientation rotates to match the bend. ( Imagine a piece of paper with a paper clip on either side lying flat on your desk. Now curl the paper until the paper clips touch each other. Or think of a belt with clips on each end bending around a single radius, eventually coming together )
How do I keep my model objects attached to the edge of the mesh and have them rotate as the mesh bends?
Thank you, --flanagak--
It would be cool if you could include a simple image showing what you are trying to achieve. I can kinda see what you are looking for, but it'd be a shame to spent time on helping on the issue only to find out I misunderstood the question after all ;)
Here are a couple of pictures to help explain.
What I am attempting to do is make the objects on either side of the blue mesh appear as thought they are clamped to the edge of the mesh.
So as the mesh bends and the edges curl towards the camera, the objects stay fixed at the edge of the mesh and rotate to match the bend.
I can't think of an easy or elegant way to do it. You'd have to find one of the triangles along the edge, and use it's normal to set the position of the object. This could be done via Raycast (not ideal since it's on the very edge, and requires a collider), or purely via code.
What method are you currently using to bend the mesh? Are you modifying each vertex? If so, I agree with Cherno. Except that I think you will probably want to use the cross product of the edge-vertex normal, and the direction to the next-edge-vertex: the result will be perpendicular to both.
I am using the uDesktopDuplication project from GitHub. I have been using this for learning since I can be in VR and play around with objects in virtual space....
So I am not 100% sure how the mesh is being bent. From what I can tell it looks like it is being done in the shader. Though still new so not sure if this is whats actually doing the bending.
inline void uddBendVertex(inout float3 v, half radius, half width, half thickness)
{
#ifdef BEND_ON
half angle = width * v.x / radius;
#ifdef _FORWARD_Z
v.z *= thickness;
radius += v.z;
v.z -= radius * (1 - cos(angle));
#elif _FORWARD_Y
v.y *= thickness;
radius += v.y;
v.y += radius * (1 - cos(angle));
#endif
v.x = radius * sin(angle) / width;
#else
#ifdef _FORWARD_Z
v.y *= thickness;
#elif _FORWARD_Y
v.z *= thickness;
#endif
#endif
}
inline float3 uddRotateY(float3 n, float angle)
{
float c = cos(angle);
float s = sin(angle);
return float3(c * n.x - s * n.z, n.y, s * n.x + c * n.z);
}
inline float3 uddRotateX(float3 n, float angle)
{
float c = cos(angle);
float s = sin(angle);
return float3(n.x, c * n.y + s * n.z, -s * n.y + c * n.z);
}
inline void uddBendNormal(float4 x, inout float3 n, half radius, half width)
{
#ifdef BEND_ON
half angle = width * x / radius;
#ifdef _FORWARD_Z
n = uddRotateY(n, -angle);
#elif _FORWARD_Y
n = uddRotateX(n, -angle);
#endif
#endif
}
From what you posted, it looks like all the verticies are passed through that uddBendVertex function. Now, we need to deter$$anonymous$$e where those values are stored (after passing through that function), so you can use them to compute the placement of your other objects. The tricky part will then be deter$$anonymous$$ing WHICH of the vertices are the center edges we want: might just have to use trial and error.