- Home /
Unity distort a sprite to match a mesh
I am relatively new to Unity, so I am not sure how to do things with graphics very much. I have generated a procedural mesh along a spline. It's a race track where each piece is made of two triangles
Because of the nature of the track, the piece sides are deformed based on the angle of the next vector in the path like so:
When I apply the piece texture it gets applied straight, as it is meant for my first example. However, I would like to have the sprite curved with respect to the sides of my mesh piece, so that the transition of graphics between the pieces is not chopped.
Is that something that is possible?
Feel free to ask me more questions in case I didn't explain it well.
Thanks!
Answer by Eno-Khaon · May 28, 2015 at 08:23 PM
As far as realtime 3D rendering is generally concerned, anything being displayed only makes sense as triangles. With this in mind, the way your ground is constructed is at risk. Some easy ways of mitigating this are to either add more vertices/triangles/faces to the segments (which would reduce the distortion, but not eliminate it) or modify the uv coordinates to resemble the distorted shape of the mesh (which eliminates simple texture tiling).
As an example, if the uv coordinates are shifted, though, the texture could instead come in the form of a projection (i.e. texture coordinates are based on worldspace position rather than texture coordinates on the object), but making it look reasonable is still very limited.
Thanks!
I will try playing with adding a bit more triangles in each piece. The problem is not really critical at this stage but you can totally see choppiness in the track pieces as they slide out behind the car and into the camera. I was just wondering if there is an obvious way to do this skewing of the texture. I will mark your answer as correct since I will most likely end up using your suggestions.
Answer by hu90 · May 28, 2015 at 08:40 PM
A mesh contains, along with the vertices and triangles, an array of uvs. See http://docs.unity3d.com/ScriptReference/Mesh-uv.html .
The uv
array consists of uv coordinates, that I'll call uvs for convenience.
A uv tells to what point on the texture a vertex is attached. That's why its type is Vector2.
Vector2.zero
is the bottom-left point on the texture, andVector2.one
is the top-right point. It typically loops so that between Vector(0, 0) and Vector3(2, 0) the texture will repeat twice on x.Each uv is associated with a vertex. This association is one-to-one.
myMesh.uv [n]
is associated with ``myMesh.vertices [n]``. It follows that Mesh.uv should always have the same length as Mesh.vertices.
So where on the texture each vertex is attached is all the graphic cards needs to know to work out what each triagle contains. You need to write to Mesh.uv to override the default "flat" texture-mapping.
EDIT: The image will be interpolated in the most simple way possible. This means that your two-triangle example might not look very good. Solution: more triangles (so more vertices and more uvs).
I will definitely try this! I accepted the other answer earlier since it was something I was more familiar with and it seems to do the job to a certain extent. $$anonymous$$aybe this will work better. I will try both. Thanks for the help!
Your answer
Follow this Question
Related Questions
Patch on a sphere of varying size 0 Answers
multiple textures on one mesh + different Albedos 1 Answer
Procedual quad doesnt display texture - only single color 1 Answer
How to scroll a region of texture (which is an atlas) on a mesh (which is a sprite) 2 Answers
Converting 2d sprite to mesh - looking better algorithm 1 Answer