Mesh missing in prefab
Hi, I'm trying to store informations about a Cube that is build out of 6 separate faces in a different GameObject. so I can have multiple versions of this cube, all with different parameters like color, size, resolution of each face etc. It is important that in the end I have n versions or copies of that one Object with totaly different values. The faces are generated as child Objects and just contains a Mesh Renderer and Mesh Filter component.
My first attempt was to make a prefab out of the existing cubeObject and then instantiate this prefab somewhere else (like on a placeholder transform in a different scene). When I create a prefab out of the existing cube the MeshFilter of each face is empty/ null.
another try was to think about saving all parameters somehow and then applying them to another cube. but I'm not sure how to do this in a proper way? maybe Making the hole thing a scriptable object OR I don't know.
The other core issue is if i dublicate the cube and then change the value of one of them ALL cubes get changed because of the way the cube is generated. Any suggestiones how to solve this would help a ton!
FACES.cs:
private Mesh mesh;
private int resolution;
private Vector3 localUp;
private Vector3 axisA, axisB;
public Face(Mesh mesh, int resolution, Vector3 localUp)
{
this.mesh = mesh;
this.resolution = resolution;
this.localUp = localUp;
axisA = new Vector3(localUp.y, localUp.z, localUp.x);
axisB = Vector3.Cross(localUp, axisA);
}
public void ConstructMesh()
{
Vector3[] vertices = new Vector3[resolution * resolution];
// getting nb of vertices; nmb of faces = (r-1)^2; *2 for triangles; *3 to get all vertices for every triangle
int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
int triangleIndex = 0;
for (int y = 0; y < resolution; y++)
{
for (int x = 0; x < resolution; x++)
{
// index of vertices
int i = x + y * resolution;
Vector2 percent = new Vector2(x, y) / (resolution - 1);
Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
// get vertices same distance to center of cube
Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
// calculate elevation and set them to the vertices
vertices[i] = pointOnUnitSphere;
// check if index is not outside the face
if (x != resolution - 1 && y != resolution - 1)
{
// first triangle, clockwise
triangles[triangleIndex] = i;
triangles[triangleIndex + 1] = i + resolution + 1;
triangles[triangleIndex + 2] = i + resolution;
// second triangle, clockwise
triangles[triangleIndex + 3] = i;
triangles[triangleIndex + 4] = i + 1;
triangles[triangleIndex + 5] = i + resolution + 1;
// update triangleIndex by 6 because we created 6 triangle
triangleIndex += 6;
}
}
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
CUBE.CS:
public int resolution = 5;
[SerializeField, HideInInspector]
private MeshFilter[] meshFilters;
private Face[] faces;
void Initialize()
{
if (meshFilters == null || meshFilters.Length == 0)
{
meshFilters = new MeshFilter[6];
}
faces = new Face[6];
// all cardinal directions
Vector3[] directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };
for (int i = 0; i < 6; i++)
{
if(meshFilters[i] == null)
{
GameObject meshObj = new GameObject("mesh");
meshObj.transform.parent = transform;
meshObj.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Universal Render Pipeline/Lit"));
meshFilters[i] = meshObj.AddComponent<MeshFilter>();
meshFilters[i].sharedMesh = new Mesh();
}
// generate Faces
faces[i] = new Face(meshFilters[i].sharedMesh, resolution, directions[i]);
}
}
void GenerateMesh()
{
foreach (Face face in faces)
{
face.ConstructMesh();
}
}
private void OnValidate()
{
Initialize();
GenerateMesh();
}
Your answer
Follow this Question
Related Questions
Changing a mesh with C# and Resource.Load() 0 Answers
Why is my mesh not showing outside of an area? 0 Answers
Can you turn a prefab/gameobject into a mesh? 0 Answers
[SOLVED] How to convert Mesh from local to World Space? CODE FIXED! 0 Answers
ArgumentException: The prefab you want to instantiate is null. 2 Answers