Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode]
  4. public class ProceduralCylinder : MonoBehaviour
  5. {
  6. public Mesh m_CylinderMesh = null;
  7. public Material m_Material = null;
  8. public int m_RadiusSubdivisions = 10;
  9. public int m_HeightSubdivisions = 5;
  10. public float m_Radius = 1.0f;
  11. public float m_Height = 5.0f;
  12.  
  13. private Vector3[] m_Vertices;
  14. private Vector2[] m_UVs;
  15. private int[] m_Triangles;
  16.  
  17. [ContextMenu("Generate Mesh")]
  18. public void GenerateMesh()
  19. {
  20. if(m_CylinderMesh != null)
  21. {
  22. if(Application.isPlaying)
  23. {
  24. Destroy(m_CylinderMesh);
  25. }
  26. else
  27. {
  28. DestroyImmediate(m_CylinderMesh);
  29. }
  30. }
  31.  
  32. m_CylinderMesh = new Mesh();
  33. m_CylinderMesh.name = "Cylinder_" + gameObject.name;
  34.  
  35. m_Vertices = new Vector3[(m_HeightSubdivisions + 1) * m_RadiusSubdivisions];
  36. m_UVs = new Vector2[m_Vertices.Length];
  37. m_Triangles = new int[6 * m_HeightSubdivisions * m_RadiusSubdivisions];
  38. int currentTriangle = 0;
  39.  
  40. for(int i = 0; i < m_HeightSubdivisions + 1; i++)
  41. {
  42. float yUV = ((float)i / (float)m_HeightSubdivisions);
  43. float height = yUV * m_Height;
  44.  
  45. for(int j = 0; j < m_RadiusSubdivisions; j++)
  46. {
  47. float xUV = ((float)j / (float)m_RadiusSubdivisions);
  48. float rad = xUV * Mathf.PI * 2.0f;
  49. Vector3 pos = new Vector3(Mathf.Cos(rad) * m_Radius, height, Mathf.Sin(rad) * m_Radius);
  50. int idx0 = (i * m_RadiusSubdivisions + j);
  51. m_Vertices[idx0] = pos;
  52. m_UVs[idx0] = new Vector2(xUV, yUV);
  53.  
  54. if(i < m_HeightSubdivisions)
  55. {
  56. int idx1 = ((i + 1) * m_RadiusSubdivisions + j);
  57. int idx2 = ((i + 1) * m_RadiusSubdivisions + ((j + 1) % m_RadiusSubdivisions));
  58. int idx3 = (i * m_RadiusSubdivisions + ((j + 1) % m_RadiusSubdivisions));
  59.  
  60. m_Triangles[currentTriangle + 0] = idx0;
  61. m_Triangles[currentTriangle + 1] = idx1;
  62. m_Triangles[currentTriangle + 2] = idx2;
  63.  
  64. m_Triangles[currentTriangle + 3] = idx0;
  65. m_Triangles[currentTriangle + 4] = idx2;
  66. m_Triangles[currentTriangle + 5] = idx3;
  67.  
  68. currentTriangle += 6;
  69. }
  70. }
  71. }
  72.  
  73. m_CylinderMesh.vertices = m_Vertices;
  74. m_CylinderMesh.uv = m_UVs;
  75. m_CylinderMesh.triangles = m_Triangles;
  76.  
  77. m_CylinderMesh.RecalculateBounds();
  78. m_CylinderMesh.RecalculateNormals();
  79. m_CylinderMesh.RecalculateTangents();
  80. }
  81.  
  82. private void Update()
  83. {
  84. Camera camera = Camera.main;
  85. if(m_CylinderMesh != null)
  86. {
  87. Graphics.DrawMesh(m_CylinderMesh, transform.localToWorldMatrix, m_Material, gameObject.layer);
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement