Search Unity

Question Created This Mesh Rendering Script But Can Not Do More Than 1

Discussion in 'Scripting' started by Sooly890, Mar 18, 2023.

  1. Sooly890

    Sooly890

    Joined:
    Aug 24, 2022
    Posts:
    107
    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MeshControl : MonoBehaviour
    6. {
    7.  
    8.     private List<Vector3> grid;
    9.     private Material[] materials;
    10.  
    11.     [SerializeField]
    12.     private Object node;
    13.  
    14.     private Vector3[] vertices;
    15.     private Vector3[] normals;
    16.     private int[] triangles;
    17.     private Mesh mesh;
    18.     private MeshFilter mf;
    19.  
    20.     private int i;
    21.  
    22.     void Start()
    23.     {
    24.         grid = new List<Vector3>();
    25.         AddMesh(new Vector3(0, 0, 0), new Vector3(3, 1, 3), new Vector3(3, 0, 4));
    26.        
    27.         LoadMeshs();
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.        
    33.     }
    34.  
    35.     void BuildMesh(Vector3 vec1, Vector3 vec2, Vector3 vec3, bool invert)
    36.     {
    37.         int a = i - 1;
    38.  
    39.         vertices[0 + a] = vec1;
    40.         vertices[1 + a] = vec2;
    41.         vertices[2 + a] = vec3;
    42.  
    43.         normals[0 + a] = new Vector3(Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))), Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.y - vec2.y))), Mathf.Cos(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))));
    44.         normals[1 + a] = new Vector3(Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))), Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.y - vec2.y))), Mathf.Cos(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))));
    45.         normals[2 + a] = new Vector3(Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))), Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.y - vec2.y))), Mathf.Cos(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))));
    46.  
    47.         triangles[0 + a] = 0 + a;
    48.         triangles[1 + a] = 1 + a;
    49.         triangles[2 + a] = 2 + a;
    50.  
    51.         mesh.vertices = vertices;
    52.         mesh.normals = normals;
    53.         mesh.triangles = triangles;
    54.  
    55.         mf = GetComponent<MeshFilter>();
    56.         mf.mesh = mesh;
    57.         Instantiate(node, vec1, transform.rotation);
    58.         Instantiate(node, vec2, transform.rotation);
    59.         Instantiate(node, vec3, transform.rotation);
    60.  
    61.     }
    62.  
    63.     float GetDir(Vector2 pos)
    64.     {
    65.         if (pos.y < 0)
    66.         {
    67.             return (Mathf.Atan(pos.x / pos.y) + 180f);
    68.         }
    69.         else
    70.         {
    71.             return (Mathf.Atan(pos.x / pos.y) + 0f);
    72.         }
    73.     }
    74.  
    75.     void AddMesh(Vector3 vec1, Vector3 vec2, Vector3 vec3)
    76.     {
    77.         grid.Add(vec1);
    78.         grid.Add(vec2);
    79.         grid.Add(vec3);
    80.     }
    81.  
    82.     void LoadMeshs()
    83.     {
    84.         mesh = new Mesh();
    85.         vertices = new Vector3[grid.Count];
    86.         normals = new Vector3[grid.Count];
    87.         triangles = new int[grid.Count];
    88.  
    89.         i = 0;
    90.         for (int a = 0; a <= grid.Count / 3; a++)
    91.         {
    92.             i++;
    93.             if (i - 1 < grid.Count / 3)
    94.             {
    95.                 BuildMesh(grid[i*3-3], grid[i*3-2], grid[i*3-1], false);
    96.             }
    97.         }
    98.     }
    99.  
    100.     void ClearAll()
    101.     {
    102.         GetComponent<MeshFilter>().mesh.Clear(false);
    103.     }
    104. }
    105.  
    This code will work but if I add another
    AddMesh(new Vector3(0, 0, 0), new Vector3(3, 1, 3), new Vector3(3, 0, 4));
    it will not render them at all.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    Your
    AddMesh
    method does nothing but add three elements to a list. You actually need to update the mesh afterwards.
     
  3. Sooly890

    Sooly890

    Joined:
    Aug 24, 2022
    Posts:
    107
    yes but after that I do
    LoadMeshs
    which will use the grid to load them
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    Would've been useful to mention that then. Can't read minds, mate.

    Are the normals facing the right way?

    You should really be debugging this on your own rather than posting a forum thread with every single issue you hit.