0
I’ve got a problem, not yet assimilated how to make the UVs of a cube here is the code can someone help me?
using UnityEngine; using System.Collections;
[RequireComponent (typeof (MeshCollider))] [RequireComponent (typeof (MeshFilter))] [RequireComponent (typeof (MeshRenderer))]
public class Create_Cube : MonoBehaviour {
public Color ColorBloc; public Texture2D Textura;
// Use this for initialization void Start () {
Mesh mesh = new Mesh ();
mesh=GetComponent().mesh;
Vector3 p0 = new Vector3(0,0,0); Vector3 p1 = new Vector3(1,0,0); Vector3 p2 = new Vector3(0,1,0); Vector3 p3 = new Vector3(0,0,1); Vector3 p4 = new Vector3(1,0,1); Vector3 p5 = new Vector3(1,1,0); Vector3 p6 = new Vector3(0,1,1); Vector3 p7 = new Vector3(1,1,1);
mesh.vertices = new Vector3{p0,p1,p2,p3,p4,p5,p6,p7};
mesh.triangles = new int{ 0,2,1, 1,2,5, //face 1 3,0,1, 1,4,3, //face 2 0,3,2, 2,3,6, //face 3 1,5,4, 5,7,4, //face 4 6,3,4, 6,4,7, //face 5 6,5,2, 7,5,6 //face 6 };
//mesh.uv = new Vector2{new Vector2(0.0f,0.0f),new Vector2(1.0f,0.0f),new Vector2(0.0f,1.0f),new Vector2(0.0f,0.0f),new Vector2(1.0f,0.0f),new Vector2(1.0f,1.0f),new Vector2(0.0f,1.0f),new Vector2(1.0f,1.0f)};
mesh.uv = new Vector2{
new Vector2(0,0), new Vector2(0,1),
new Vector2(1,0),new Vector2(0,1),
new Vector2(1,1), new Vector2(0,0),
new Vector2(0,0), new Vector2(1,0)
};
renderer.material = new Material(Shader.Find(“Diffuse”));
renderer.material.mainTexture = Textura; renderer.material.color = ColorBloc;
mesh.RecalculateNormals(); mesh.RecalculateBounds (); mesh.Optimize();
} // Update is called once per frame void Update () {
} }
+1 for all that, more if I could. First things first, is there a guide on creating these meshes or determining which are hull/interior cubes anywhere or is this just something I should experiment with? Any suggestions? My textures are only 16x16 and I'm not making a minecraft clone, so I don't need a huge area with millions and millions of cubes, so just minor optimization might do it, though the test I ran with around 14k cubes basically lagged me out of commission, so I do still obviously need some optimization.
– KoderWell the determination of the cube hull is pretty easy. For a given cube at x,y,z you just check if there exist a cube at x+1, x-1, y+1, y-1, z+1, z-1 (all 6 sides), and for each side there is no cube, you generate the triangles for that face. You'll need to know how to create meshes procedurally. Check Unity3D website for their procedural tutorial/project to get started. It's a bit complex to write efficient code (like I make use of cache friendly space curves to squeeze out as much performance as I can, but I'm a nerd) but the basics are surprisingly simple.
– StatementBy the way, for performance geeks and general boasting: 2400 fps is in editor. In a standalone I got about 3500 fps :)
– Statement-1 for boasting.. Kidding. I can appreciate solid code ;) Thanks for the help.
– Koder