- Home /
Question by
OcularCash · May 09, 2014 at 03:34 AM ·
procedural-generation
Procedural Line Mesh Vert Compensation
I made a procedural line mesh to recreate, in a sense, the line renderer component since it has a pinching problem. It is all done but I am having problems finding the proper method to compensate corners and adjusting the vertices based on the angle. I currently have it setting the y position to half of the width but I don't have any compensation for angles so it thins out if say, the line goes from horizontal to vertical.
So my question is, what is the best approach to adding in that compensation procedurally? Here is the current code. It's for a 2D project and only needs to create on start. Did some comment overloading as well:
//Width in units of our line
public var width : float;
//Start, Elbows and End Positions for this line
public var linePositions : Vector3[];
//The texture we want to use
public var texture : Texture;
//The shader we want to use
public var shader : Shader;
//The color we want to use
public var color : Color;
//The stored variable of segments
private var segments : int;
/////Calls when the scene is loaded/////
function Start()
{
///Generate the mesh///
GenerateMesh();
}
/////Function that generates the mesh
function GenerateMesh()
{
///////////////////////Setup///////////////////////////////////
var obj : GameObject = new GameObject("New Line", MeshFilter, MeshRenderer);
//If a shader has been chosen in the inspector
if(shader)
{
//Assign it to the newly created line
obj.renderer.material = new Material(shader);
}
//If a shader has not been chosen in the inspector
if(!shader)
{
//Assign a default diffuse shader to the newly created line
obj.renderer.material = new Material(Shader.Find("Diffuse"));
}
//If a texture has been chosen in the inspector
if(texture)
{
//Assign it to the newly created line
obj.renderer.material.mainTexture = texture;
}
//Assign our chosen color to the line
obj.renderer.material.color = color;
//Add a mesh filter to it
var mf : MeshFilter = obj.GetComponent.<MeshFilter>();
//Create the new mesh
var mesh : Mesh = new Mesh();
//Assign the new mesh to the mesh filter
mf.mesh = mesh;
//And store the amount of segment we will be creating
segments = linePositions.Length - 1;
//Create a stored iterator variable
var i : int;
///////////////////////Vertices////////////////////////////////
//Store our vert positions in an array that is twice the size of our line positions
var vertices : Vector3[] = new Vector3[linePositions.Length * 2];
//For half our vertices
for(i = 0; i < vertices.Length/2; i++)
{
//Store the center value minus half the line width of this position for even numbers
vertices[i * 2] = linePositions[i] - Vector3(0, width/2, 0);
//Store the center value minus half the line width of this position for odd numbers
vertices[i * 2 + 1] = linePositions[i] + Vector3(0, width/2, 0);
}
//And send the vert array to the mesh
mesh.vertices = vertices;
///////////////////////Triangles//////////////////////////////
//Store our triangle points in an array that is 6 times larger than our segments
var tri : int[] = new int[segments * 6];
//For every segment
for(i = 0; i < segments; i++)
{
//Begin the first triangle corner in the bottom left
tri[i * 6] = i * 2;
//Then the top left
tri[i * 6 + 1] = i * 2 + 1;
//And end the first triangle corner in the bottom right
tri[i * 6 + 2] = i * 2 + 2;
//Begin the second triangle corner in the top left
tri[i * 6 + 3] = i * 2 + 1;
//Then the top right
tri[i * 6 + 4] = i * 2 + 3;
//And end the second triangle corner in the bottom right
tri[i * 6 + 5] = i * 2 + 2;
}
//And send our triangle array to the mesh
mesh.triangles = tri;
///////////////////////Normals//////////////////////////////
//Store our normals in an array that is the same size as our vertices array
var normals : Vector3[] = new Vector3[vertices.Length];
//For every normal
for(i = 0; i < normals.Length; i++)
{
//Face it south (-Z Direction)
normals[i] = -Vector3.forward;
}
//And send our normals to the mesh
mesh.normals = normals;
///////////////////////UVs(Isn't perfect)//////////////////////////////
//Store our UV coordinates in an array that is the same size as our vertices array
var uv : Vector2[] = new Vector2[vertices.Length];
//For every 4 vertices
for(i = 0; i < vertices.Length/4; i++)
{
//Start the uv in the bottom left
uv[i * 4] = new Vector2(i * 4, i * 4);
//Then the top left
uv[i * 4 + 1] = new Vector2(i * 4 + 1, i * 4);
//Then the bottom right
uv[i * 4 + 2] = new Vector2(i * 4, i * 4 + 1);
//And end the uv in the top right
uv[i * 4 + 3] = new Vector2(i * 4 + 1, i * 4 + 1);
}
//And send the UV to the mesh
mesh.uv = uv;
mesh.RecalculateBounds();
mesh.Optimize();
}
Comment
Your answer