Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Mesh is readable then not readable??

Discussion in 'Scripting' started by prattmyster, Oct 20, 2022.

  1. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    Hey i am currently working on a script that changes the geometry of a mesh.

    I am using a scriptable object to store a mesh as a base data reference, i am then creating a new mesh from this data and changing it according to position ect.

    The problem is when I originally create the mesh from the data it lets me read the verticies from the scriptable object mesh. However when i go to update the mesh the scriptable object mesh is now not letting me read any data. Giving the error message:

    Not allowed to access vertices on mesh 'Wall' (isReadable is false; Read/Write must be enabled in import settings)
    UnityEngine.Mesh:get_vertices ()

    it seems odd that it will let me read the data once but not a second time. I've checked through my code and i have not replaced the Mesh in the scriptable object. Anyone has any ideas would love the help
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You'd have to show your code.
     
  3. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    sorry forgot about that

    code for creating and updating the Mesh

    Code (csharp):
    1.  
    2.     Network_Mesh meshes;
    3.     public Network_Mesh MeshInformation => meshes;
    4.     NetworkFence data;
    5.  
    6.     GameObject mainMeshGO;
    7.     [SerializeField]
    8.     Mesh mainMesh;
    9.     MeshFilter mainMeshMF;
    10.     GameObject LODMeshGO;
    11.     protected Mesh LODMesh;
    12.     MeshFilter lodMeshMF;
    13.     FenceSegment parent;
    14.  
    15.     public void Create(Vector3 startPos, Vector3 endPos, Vector3 anchorPos, Network_Mesh mesh, NetworkFence info)
    16.     {
    17.         parent = this.transform.parent.GetComponent<FenceSegment>();
    18.         meshes = mesh;
    19.         data = info;
    20.         LOD = this.gameObject.AddComponent<LODGroup>();
    21.         mainMeshGO = new GameObject("MainMesh");
    22.         mainMeshGO.transform.SetParent(this.transform);
    23.         mainMeshGO.transform.localPosition = Vector3.zero;
    24.         mainMeshGO.transform.localEulerAngles = Vector3.zero;
    25.         mainMesh = new Mesh();
    26.         Material mat = new Material(Game_Tools.ChangeMaterial(mesh.Shader));
    27.         Game_Tools.ApplyTextures(mesh.Shader, mesh.TextureSets, mat);
    28.         mainMeshGO.AddComponent<MeshRenderer>().material = mat;
    29.         mainMesh = updateMesh(startPos, endPos, anchorPos, MeshInformation.MainMesh, mainMeshGO.transform);
    30.         mainMeshMF = mainMeshGO.AddComponent<MeshFilter>();
    31.         mainMeshMF.sharedMesh = mainMesh;
    32.         mainMesh.Optimize();
    33.         if (mesh.HasLod)
    34.         {
    35.             LODMeshGO = new GameObject("LODMesh");
    36.             LODMeshGO.transform.SetParent(this.transform);
    37.             LODMeshGO.transform.localPosition = Vector3.zero;
    38.             LODMeshGO.transform.localEulerAngles = Vector3.zero;
    39.             LODMesh = mesh.LODMesh;
    40.             Material lodMat = new Material(Game_Tools.ChangeMaterial(mesh.Shader));
    41.             Game_Tools.ApplyTextures(mesh.Shader, mesh.LODTextureSets, lodMat);
    42.             LODMeshGO.AddComponent<MeshRenderer>().material = lodMat;
    43.             LODMesh = updateMesh(startPos, endPos, anchorPos, mesh.MainMesh, LODMeshGO.transform);
    44.             LODMeshGO.AddComponent<MeshFilter>().sharedMesh = LODMesh;
    45.             LODMesh.Optimize();
    46.             LOD main = new LOD(mesh.Lod001Value, new Renderer[] { mainMeshGO.GetComponent<Renderer>() });
    47.             LOD lod = new LOD(mesh.Lod002Value, new Renderer[] { LODMeshGO.GetComponent<Renderer>() });
    48.             AddLod(main, lod);
    49.             col.sharedMesh = LODMesh;
    50.             col.convex = true;
    51.             col.isTrigger = true;
    52.         }
    53.         else
    54.         {
    55.             LOD main = new LOD(mesh.Lod001Value, new Renderer[] { mainMeshGO.GetComponent<Renderer>() });
    56.             AddLod(main);
    57.             col.sharedMesh = mainMesh;
    58.             col.convex = true;
    59.             col.isTrigger = true;
    60.         }
    61.     }
    62.  
    63.     public void UpdateMesh(Vector3 startPos, Vector3 endPos, Vector3 anchorPos)
    64.     {
    65.         mainMesh.Clear();
    66.         mainMesh = updateMesh(startPos, endPos, anchorPos, MeshInformation.MainMesh, mainMeshGO.transform);
    67.         Game_Tools.ApplyTextures(MeshInformation.Shader, MeshInformation.TextureSets, mainMeshGO.GetComponent<MeshRenderer>().sharedMaterial);
    68.         mainMeshMF.sharedMesh = mainMesh;
    69.         mainMesh.Optimize();
    70.         if (MeshInformation.HasLod)
    71.         {
    72.             LODMesh = MeshInformation.LODMesh;
    73.             LODMesh = updateMesh(startPos, endPos, anchorPos, MeshInformation.MainMesh, LODMeshGO.transform);
    74.             Game_Tools.ApplyTextures(MeshInformation.Shader, MeshInformation.TextureSets, LODMeshGO.GetComponent<MeshRenderer>().sharedMaterial);
    75.             LODMesh.Optimize();
    76.             col.sharedMesh = LODMesh;
    77.             return;
    78.         }
    79.         col.sharedMesh = mainMesh;
    80.     }
    81.  
    82.     Mesh updateMesh(Vector3 startPos, Vector3 endPos, Vector3 anchorPos, Mesh mesh, Transform parent)
    83.     {
    84.         Mesh Mesh = new Mesh();
    85.         Mesh.name = mesh.name;
    86.         Mesh m = new Mesh();
    87.         m.vertices = mesh.vertices;
    88.         m.triangles = mesh.triangles;
    89.         m.uv = mesh.uv;
    90.         m.normals = mesh.normals;
    91.         int verticiesPerSection = m.vertexCount;
    92.         float length = Game_Tools.GetMeshBounds(meshes).z;
    93.         float distance = 0;
    94.         distance = data.SmoothBuild ? Equations.Distance(m, startPos, endPos, anchorPos) : Vector3.Distance(startPos, endPos);
    95.  
    96.         int segmentsNeeded = Mathf.CeilToInt(distance / length); // How many meshes needed between Nodes
    97.         float segmentLength = distance / segmentsNeeded; //Length of each Mesh
    98.         List<MeshVertexPos> Verts = Game_Tools.GetMeshVerticiesZ(m);
    99.         List<GameObject> startPoints = new List<GameObject>();
    100.         float divisions = 1f / (float)segmentsNeeded;
    101.         List<Vector3> newVerts = new List<Vector3>();
    102.         List<Vector2> newUVS = new List<Vector2>();
    103.         List<int> newTris = new List<int>();
    104.  
    105.         if (data.SmoothBuild)
    106.         {
    107.             GameObject startPoint = new GameObject("Start Point");
    108.             startPoint.transform.SetParent(this.transform);
    109.             startPoint.transform.localPosition = Vector3.zero;
    110.             startPoint.transform.LookAt(this.parent.AnchorPoint.transform);
    111.             GameObject anchorS = new GameObject("Anchor");
    112.             anchorS.transform.SetParent(startPoint.transform);
    113.             anchorS.transform.localPosition = Vector3.zero;
    114.             anchorS.transform.localEulerAngles = Vector3.zero;
    115.             GameObject endPoint = new GameObject("End Point");
    116.             endPoint.transform.SetParent(this.transform);
    117.             endPoint.transform.localPosition = endPos;
    118.             endPoint.transform.LookAt(this.parent.AnchorPoint.transform);
    119.             GameObject anchorE = new GameObject("Anchor");
    120.             anchorE.transform.SetParent(endPoint.transform);
    121.             anchorE.transform.localPosition = Vector3.zero;
    122.             anchorE.transform.localEulerAngles = Vector3.zero;
    123.             GameObject anchorPoint = new GameObject();
    124.             anchorPoint.transform.SetParent(this.transform);
    125.             anchorPoint.transform.position = this.parent.AnchorPoint.transform.position;
    126.             anchorPoint.transform.LookAt(Vector3.Lerp(this.parent.StartPoint.transform.position, this.parent.EndPoint.transform.position, 0.5f));
    127.  
    128.             float anchorOffset = Equations.BuildOffset(anchorS.transform, anchorE.transform, out bool isStraight);
    129.             if (isStraight)
    130.             {
    131.                 anchorPoint.transform.localEulerAngles = Vector3.zero;
    132.             }
    133.  
    134.             for (int i = 0; i < m.vertexCount; i++)
    135.             {
    136.                 float percentage = 0;
    137.                 for (int p = 0; p < Verts.Count; p++)
    138.                 {
    139.                     bool found = false;
    140.                     foreach (int id in Verts[p].verticieID)
    141.                     {
    142.                         if (id == i)
    143.                         {
    144.                             found = true;
    145.                             percentage = Verts[p].Percentage;
    146.                             break;
    147.                         }
    148.                     }
    149.                     if (found) break;
    150.                 }
    151.                 float newOffset;
    152.                 newOffset = m.vertices[i].x * anchorOffset;
    153.                 Vector3 baseAnchor = anchorPoint.transform.localPosition;
    154.                 Vector3 baseStart = startPoint.transform.localPosition;
    155.                 anchorS.transform.localPosition += new Vector3(m.vertices[i].x, m.vertices[i].y, 0);
    156.                 startPoint.transform.position = anchorS.transform.position;
    157.                 Vector3 start = startPoint.transform.localPosition;
    158.                 Vector3 baseEnd = endPoint.transform.localPosition;
    159.                 anchorE.transform.localPosition -= new Vector3(m.vertices[i].x, -m.vertices[i].y, 0);
    160.                 endPoint.transform.position = anchorE.transform.position;
    161.                 Vector3 end = endPoint.transform.localPosition;
    162.                 if (isStraight)
    163.                 {
    164.                     anchorPoint.transform.localPosition += new Vector3(newOffset, m.vertices[i].y, 0);
    165.                 }
    166.                 else
    167.                 {
    168.                     anchorPoint.transform.position = Equations.Interception(startPoint.transform.position, startPoint.transform, endPoint.transform.position, endPoint.transform);
    169.                 }
    170.                 Vector3 anchor = anchorPoint.transform.localPosition;
    171.                 Vector3 newVert = Equations.CurveLerp(start, end, anchor, percentage);
    172.                 newVerts.Add(newVert);
    173.                 anchorPoint.transform.localPosition = baseAnchor;
    174.                 startPoint.transform.localPosition = baseStart;
    175.                 anchorS.transform.localPosition = Vector3.zero;
    176.                 endPoint.transform.localPosition = baseEnd;
    177.                 anchorE.transform.localPosition = Vector3.zero;
    178.                 anchorE.transform.localEulerAngles = Vector3.zero;
    179.             }
    180.             float uvOffset = distance / data.SegmentLength;
    181.             //Generate UVS
    182.             for (int u = 0; u < m.vertexCount; u++)
    183.             {
    184.                 float y = m.uv[u].y * uvOffset;
    185.                 newUVS.Add(new Vector2(m.uv[u].x, y));
    186.             }
    187.             //GenerateTriangles
    188.             newTris = new List<int>(m.triangles);
    189.             //Update Mesh
    190.             Mesh.vertices = newVerts.ToArray();
    191.             Mesh.triangles = newTris.ToArray();
    192.             Mesh.uv = newUVS.ToArray();
    193.             Mesh.normals = m.normals;
    194.             Mesh.RecalculateNormals();
    195. #if (UNITY_EDITOR)
    196.             DestroyImmediate(startPoint);
    197.             DestroyImmediate(endPoint);
    198.             DestroyImmediate(anchorPoint);
    199.             return Mesh;
    200. #endif
    201.             Destroy(startPoint);
    202.             Destroy(endPoint);
    203.             Destroy(anchorPoint);
    204.             return Mesh;
    205.         }
    206.         else
    207.         {
    208.             //CreatePoints
    209.             for (int i = 0; i <= segmentsNeeded; i++)
    210.             {
    211.                 GameObject newPoint = new GameObject("startPoint_" + i);
    212.                 GameObject anchor = new GameObject("SegmentAnchor");
    213.                 newPoint.transform.SetParent(this.transform);
    214.                 newPoint.transform.localPosition = Equations.CurveLerp(startPos, endPos, anchorPos, i * divisions);
    215.                 anchor.transform.SetParent(newPoint.transform);
    216.                 anchor.transform.localPosition = Vector3.zero;
    217.                 anchor.transform.localEulerAngles = Vector3.zero;
    218.                 startPoints.Add(newPoint);
    219.             }
    220.             //RotatePoints
    221.             for (int i = 1; i < startPoints.Count; i++)
    222.             {
    223.                     startPoints[i].transform.LookAt(startPoints[i - 1].transform.position);
    224.                     startPoints[i].transform.localEulerAngles += new Vector3(0, 180, 0);
    225.             }
    226.             for (int i = 0; i < segmentsNeeded; i++)
    227.             {
    228.                 //Generate Verticies
    229.                 for (int v = 0; v < m.vertexCount; v++)
    230.                 {
    231.                     float percentage = 0;
    232.                     for (int p = 0; p < Verts.Count; p++)
    233.                     {
    234.                         bool found = false;
    235.                         foreach (int id in Verts[p].verticieID)
    236.                         {
    237.                             if (id == v)
    238.                             {
    239.                                 found = true;
    240.                                 percentage = Verts[p].Percentage;
    241.                                 break;
    242.                             }
    243.                         }
    244.                         if (found) break;
    245.                     }
    246.                     startPoints[i].transform.LookAt(startPoints[i + 1].transform.position);
    247.                     Vector3 baseStartPoint = startPoints[i].transform.localPosition;
    248.                     Vector3 baseEndPoint = startPoints[i+1].transform.localPosition;
    249.                     startPoints[i].transform.GetChild(0).transform.localPosition += new Vector3(m.vertices[v].x, m.vertices[v].y, 0);
    250.                     startPoints[i].transform.position = startPoints[i].transform.GetChild(0).transform.position;
    251.                     Vector3 startPoint = startPoints[i].transform.localPosition;
    252.                     startPoints[i + 1].transform.GetChild(0).transform.localPosition += new Vector3(m.vertices[v].x, m.vertices[v].y, 0);
    253.                     startPoints[i + 1].transform.position = startPoints[i + 1].transform.GetChild(0).transform.position;
    254.                     Vector3 endPoint = startPoints[i + 1].transform.localPosition;
    255.                     Vector3 point = Vector3.Lerp(startPoint, endPoint, percentage);
    256.                     startPoints[i].transform.localPosition = baseStartPoint;
    257.                     startPoints[i].transform.GetChild(0).transform.localPosition = Vector3.zero;
    258.                     startPoints[i + 1].transform.localPosition = baseEndPoint;
    259.                     startPoints[i + 1].transform.GetChild(0).transform.localPosition = Vector3.zero;
    260.                     newVerts.Add(point);
    261.                 }
    262.                 //Generate UVS
    263.                 for (int u = 0; u < m.vertexCount; u++)
    264.                 {
    265.                     newUVS.Add(m.uv[u]);
    266.                 }
    267.                 //GenerateTriangles
    268.                 for (int t = 0; t < m.triangles.Length; t++)
    269.                 {
    270.                     int a = m.triangles[t] + (i * verticiesPerSection);
    271.                     newTris.Add(a);
    272.                 }
    273.                 Mesh.vertices = newVerts.ToArray();
    274.                 Mesh.triangles = newTris.ToArray();
    275.                 Mesh.uv = newUVS.ToArray();
    276.                 Mesh.normals = m.normals;
    277.                 Mesh.RecalculateNormals();
    278.             }
    279.             //Destroy points
    280.             foreach (GameObject go in startPoints)
    281.             {
    282. #if (UNITY_EDITOR)
    283.                 DestroyImmediate(go);
    284.                 continue;
    285. #endif
    286.                 Destroy(go);
    287.                 continue;
    288.             }
    289.         }
    290.         return Mesh;
    291.     }
    292. }
    293.