Hi guys, I am proceduraly building a spherized cube based on a number of edges on a corner.
However after all of the building/moving of vertices, my submeshes are flat. I know my function to modify a cube works, however when it is applied to the combined mesh of my generated sides it is invisible. The vertices via the log that they are spherized for World, and the triangle count is correct. Even when I delete the submeshes they appear to be correctly positioned…
I am attaching some code. It’s pretty sloppy so far, but it seems fairly simple to understand.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spherizer : MonoBehaviour {
public Transform[] CubeSides;
public Transform Side;
public Transform World;
public float radius = 5f; //IDE
public float units = 5; //IDE
public int smooth = 50; // 0-100 bigger = smoother
Vector3 cubeOrigin;
List<Vector3> vertices;
Transform newSide;
List<Vector2> uvs;
//public Transform newSide = new Transform();
Mesh mesh;
// Use this for initialization
void Start () {
cubeOrigin = transform.position;
//vertices = new Vector3[mesh.vertexCount];
MakeCube();
MakeSphere();
Debug.Log("Loaded");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("space"))
Debug.Log(mesh.vertexCount);
}
public void MakeCube(){
//instantiate global variables
World = GameObject.FindGameObjectWithTag("World").transform;
CubeSides = new Transform[6];
vertices = new List<Vector3>();
uvs = new List<Vector2>();
//instantiate local variables
List<MeshFilter> meshFilters = new List<MeshFilter>();
List<Vector3> sideVertices = new List<Vector3>();
List<int> triangles = new List<int>();
//Create Sides of Cube
for (int thisSide = 0; thisSide < CubeSides.Length; thisSide++) {
newSide = (Transform)Instantiate(Side,cubeOrigin,Quaternion.identity);
newSide.parent = World;
newSide.name = ("Side "+(thisSide+1));
//Create Vertices of Side
for (int x = 0;x<=units;x++){
for (int z = 0;z<=units;z++){
sideVertices.Add(new Vector3(x-(units/2),units/2,z-(units/2)));
}
}
//Create Triangles for Side
for(int i = 0;i<(sideVertices.Count-(units+1));i++){
if ((i+1)%(units+1)!=0) {
triangles.Add(i);
triangles.Add(i+1);
triangles.Add(i+(int)(units+1f));
triangles.Add(i+1);
triangles.Add(i+(int)(units+1f)+1);
triangles.Add(i+(int)(units+1f));
}
}
//Create UVs for Side
for (int i = 0; i < sideVertices.Count; i++) {
uvs.Add (new Vector2(sideVertices_.x,sideVertices*.z));*_
* }*
* CubeSides[thisSide] = newSide;*
* //Assign collections to Side*
* newSide.gameObject.GetComponent().sharedMesh = newSide.gameObject.GetComponent().mesh;*
* mesh = newSide.gameObject.GetComponent().sharedMesh;*
* mesh.vertices = sideVertices.ToArray();*
* mesh.triangles = triangles.ToArray();*
* mesh.uv = uvs.ToArray();*
* mesh.RecalculateNormals();*
* meshFilters.Insert(0,newSide.GetComponent());*
* //Clear variables*
* sideVertices = new List();*
* triangles = new List();*
* uvs = new List();*
* } *
* //Rotate Sides into Position (Is there a more eloquent way to do this?)*
* CubeSides[1].Rotate(new Vector3(270,180,0));*
* CubeSides[2].Rotate(new Vector3(270,90,0));*
* CubeSides[3].Rotate(new Vector3(270,0,0));*
* CubeSides[4].Rotate(new Vector3(270,270,0));*
* CubeSides[5].Rotate(new Vector3(180,180,0)); *
* //Concatenate Sides into Cube*
_ /foreach(Transform side in CubeSides){_
_ MeshFilter filter = side.gameObject.GetComponent();_
_ for(int i = 0;i<filter.mesh.vertices.Length;i++){_
_ Vector3 vert = filter.mesh.vertices;
vert = filter.transform.TransformPoint(vert);
vertices.Add(vert);
}
}/
* mesh = World.gameObject.GetComponent().mesh;
//mesh.vertices = vertices.ToArray();
CombineInstance[] combine = new CombineInstance[meshFilters.Count];
Debug.Log(meshFilters.Count);*_
for (int h = 0;h < meshFilters.Count;h++) {
combine.mesh = meshFilters.mesh;
combine.transform = meshFilters.transform.localToWorldMatrix;
meshFilters.gameObject.SetActive(false);
}
mesh = new Mesh();
mesh.CombineMeshes(combine,true,true);
Debug.Log(“Loaded Cube”);
}
public void MakeSphere(){ // done
for (int i = 0; i<mesh.vertexCount;i++){
Vector3 vert = mesh.vertices*;*
* //Debug.Log(i+1 +“,”+ vert);*
* float distance = (cubeOrigin-vert).magnitude;*
* Vector3 vect = cubeOrigin - vert;*
* vect = vect.normalized;*
_ vect = (distance - radius + Random.Range(-radius/smooth, radius/smooth));
vert += vect;
vertices.Add(vert);
//Debug.Log(i+1 +“,”+ vert);
}
mesh.vertices = vertices.ToArray();
for (int i = 0; i < mesh.vertices.Length; i++) {
Debug.Log(i+1 +“,”+ mesh.vertices);
}
for (int i = 0; i < mesh.triangles.Length-2; i+=2) {
Debug.Log(mesh.triangles+“,”+mesh.triangles[i+1]+“,”+mesh.triangles[i+2]);
}
vertices.Clear();
vertices.AddRange(mesh.vertices);
Debug.Log(vertices.Count);
uvs = new List(vertices.Count);
for (int i = 0; i < vertices.Count ; i++) {
uvs.Add(new Vector2(vertices.x, vertices.z));
//Debug.Log(vertices.x+“,”+ vertices.z);
}
mesh.uv = uvs.ToArray();
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.Optimize();
World.gameObject.SetActive(true);
Debug.Log("uv count "+mesh.uv.Length);
Debug.Log("triangle count "+mesh.triangles.Length/3);
}
}
*_