- Home /
Filling the space between bezier curves with a texture,
I want to dynamically draw 2D road segments in Unity with scripts. Each segment is defined by 4 points and the required control points to draw bezier curves. This means a segments outer shape consists of 2 "ends" wich are just simple lines, and 2 bezier curves.
I am using this https://github.com/dbrizov/NaughtyBezierCurves to create the bezier curves. This results into an outer shape of a segment like this:
Creating the outer shape of a segment is pretty simple. Now I want to fill the segment with a texture. I dont know how I can create planes or a mesh with the same shape as the outer shape of the segment. This is what I want to achive: I first thought about going along the bezier curves and creating slim splanes all along the curve, but this would lead to overlapping or gaps between them. Plus, I dont know how to orient the planes towards the other curve.
Maybe someone has a simple solution how I can add a texture to the road with a script.
,
Answer by landings · Jan 13, 2020 at 04:22 PM
Discretize your curves into two point lists. Build the mesh like this (Not tested but should not be too wrong):
// Discretize the curves
// You have verticesOnCurve1[n] and verticesOnCurve2[n]
// float width is your road's width
//// Vertices ////
List<Vector3> vertexBuffer = new List<Vector3>();
for (int i = 0; i < n; i++) {
vertexBuffer.Add(verticesOnCurve1[i]); // Top points
}
for (int i = 0; i < n; i++) {
vertexBuffer.Add(verticesOnCurve2[i]); // Bottom points
}
//// Indices ////
List<int> indexBuffer = new List<int>();
for (int i = 0; i < n - 1; i++) {
indexBuffer.Add(i);
indexBuffer.Add(i + 1);
indexBuffer.Add(n + i);
indexBuffer.Add(n + i);
indexBuffer.Add(i + 1);
indexBuffer.Add(n + i + 1);
}
//// UVs ////
List<float> pointMileages = new List<float>(); // Mileages are calculated from the top curve
pointMileages.Add(0); // Starting point
for (int i = 1; i < n; i++) {
var distance = (verticesOnCurve1[i] - verticesOnCurve1[i - 1]).magnitude;
pointMileages.Add(pointMileages[i - 1] + distance); // Other points
}
List<Vector2> uvs = new List<Vector2>();
for (int i = 0; i < n; i++) {
uvs.Add(new Vector2(pointMileages[i] / width, 0)); // Top points
}
for (int i = 0; i < n; i++) {
uvs.Add(new Vector2(pointMileages[i] / width, 1)); // Bottom points
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612221506im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Curving a cuboid mesh 1 Answer
Curve vertex array 1 Answer
Combine N different meshes with M different materials on each 0 Answers
how can i connvert mesh line into a dashed line mesh 0 Answers
iTween for Dynamically Moving Objects around a Sphere 0 Answers