Geometry Permanently Disappearing after Editing Cubes

Hi all,

I’m trying to programatically render a few shapes as visualization for a project of mine, and I’m trying to use Unity to do it. All I need to render are cubes and irregular Hexahdera. I’m looking to do this by simply using the Cube primtiive, and editing it.

Unfortunately, any edits to the cube completely break the rendering.

For Hexahedra, I have:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HexahedronElement : MonoBehaviour {

    //visualization primitive
    Mesh visualization;
    MeshFilter meshFilter;
    MeshRenderer meshRenderer;

    public Vector3 xyz; //the COM
    public Vector3 scale;

    HexahedronElement()
    { }

    public void Initialize(Vector3 xyz, Vector3 scale)
    {
        this.xyz = xyz;
        this.scale = scale;
    }

    public void UpdateGeometry(Vector3[] vertices, Vector3 scale)
    {
        this.transform.localScale = scale; 

        /*The vertex ordering is:
         * 6 101
2 001
8 111
4 011
7 110
3 010
5 100
1 000
8 111
4 011
7 110
3 010
5 100
6 101
2 001
1 000
2 001
4 011
3 010
1 000
5 100
7 110
8 111
6 101
         * 
         * 
         * 
         * 
         * 
         * 
         * */

        if (visualization == null) return; 
        
        Vector3[] visVertices = visualization.vertices;

        visVertices[7] = vertices[0];
        visVertices[15] = vertices[0];
        visVertices[19] = vertices[0];

        visVertices[1] = vertices[1];
        visVertices[14] = vertices[1];
        visVertices[16] = vertices[1];

        visVertices[5] = vertices[2];
        visVertices[11] = vertices[2];
        visVertices[18] = vertices[2];

        visVertices[3] = vertices[3];
        visVertices[9] = vertices[3];
        visVertices[17] = vertices[3];

        visVertices[6] = vertices[4];
        visVertices[12] = vertices[4];
        visVertices[20] = vertices[4];

        visVertices[0] = vertices[5];
        visVertices[13] = vertices[5];
        visVertices[23] = vertices[5];

        visVertices[4] = vertices[6];
        visVertices[10] = vertices[6];
        visVertices[21] = vertices[6];

        visVertices[2] = vertices[7];
        visVertices[8] = vertices[7];
        visVertices[22] = vertices[7];

        visualization.vertices = visVertices;
        visualization.RecalculateNormals();

        Debug.Log("Updated!");
        
    }

    // Use this for initialization
    void Start () {

        

        visualization = PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Cube);
        meshFilter = gameObject.AddComponent<MeshFilter>();
        meshFilter.mesh = visualization;
        meshRenderer = gameObject.AddComponent<MeshRenderer>();
        meshRenderer.material = new Material(Shader.Find("Specular"));
        meshRenderer.material.color = Color.gray;

        this.transform.localScale = scale; //TODO: how can I make thse global?
        this.transform.position = xyz;



    }
	
	// Update is called once per frame
	void Update () {
		
	}
}

The PrimitiveHelper is from this answer. reproduced below for convenience.

using System.Collections.Generic;
using UnityEngine;

public static class PrimitiveHelper
{
	private static Dictionary<PrimitiveType, Mesh> primitiveMeshes = new Dictionary<PrimitiveType, Mesh>();

	public static GameObject CreatePrimitive(PrimitiveType type, bool withCollider)
	{
		if (withCollider) { return GameObject.CreatePrimitive(type); }

		GameObject gameObject = new GameObject(type.ToString());
		MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
		meshFilter.sharedMesh = PrimitiveHelper.GetPrimitiveMesh(type);
		gameObject.AddComponent<MeshRenderer>();

		return gameObject;
	}

	public static Mesh GetPrimitiveMesh(PrimitiveType type)
	{
		if (!PrimitiveHelper.primitiveMeshes.ContainsKey(type))
		{
			PrimitiveHelper.CreatePrimitiveMesh(type);
		}

		return PrimitiveHelper.primitiveMeshes[type];
	}

	private static Mesh CreatePrimitiveMesh(PrimitiveType type)
	{
		GameObject gameObject = GameObject.CreatePrimitive(type);
		Mesh mesh = gameObject.GetComponent<MeshFilter>().sharedMesh;
		GameObject.Destroy(gameObject);

		PrimitiveHelper.primitiveMeshes[type] = mesh;
		return mesh;
	}
}

In short, there are two modes I run my visualization in: Mode 1 is with only regular cubes, and Mode 2 is with irregular hexahedra in addition to the cubes.

Running in Mode 1 works fine.

If I try to run in Mode 2, I get problems. I’ve attached pictures of how it should look (Mode 1) and how it’s rendering when I try to add a few irregular Hexahedra (Mode 2) to that set below (Mode 1 on top). After I run Mode 2, and it has problems, I then end my application, and restart it in Mode 1 (which, note, should work fine), and it will THEN fail. After that, if I completely restart Unity Mode 1 works again.

I could understand if I misedited the hexahedra or something, but why would that clobber my regular cubes? And why would that require a restart of unity?

I am wrapping the cubes in another class - let me know if that would be useful to include here.

Okay, so it seems like all the meshes might be the same reference - it seems they share vertices and ID and this might be the whole problem.

How do I create new meshes?