Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Procedural quad, problem with texture 1 Answer

Is it possible to do procedural calculations in shaders? 1 Answer

C# Proceducal Mesh terrain 2 Answers

Work around for nested prefabs? (for my use case - random levels) 1 Answer

Generating Chunks of Ore Using Perlin Noise 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges