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

47194-example-1.png

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:

47195-example-2.png

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!

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.

A mesh contains, along with the vertices and triangles, an array of uvs. See Unity - Scripting API: Mesh.uv .

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, and Vector2.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).