- Home /
Deleting/restoring mesh vertices
I'm sorry if a similar question has been asked already...
I have recently been trying to create a script that can turn a cube into separate planes. (not whole as new gameObjects, just isolated faces of the cube.) For use in a voxel engine. For example, it creates a plane where the top of the cube would be, giving the illusion that only the top of the block is being rendered. But I can't figure out how to create multiple faces at once. Like creating the top,left, and bottom sides all at the same time. Can anybody give me any tips on how this is done?
Here is my code to create the top side of the box:
import System.Collections.Generic;
private var width: float = 1;
private var height: float = 1;
var verts: Vector3[] = new Vector3[4];
var normals: Vector3[] = null;
var uv: Vector2[] = null;
var tri: int[] = null;
var mf: MeshFilter = null;
var basex : float = 0.0;
var basey : float = 0.0;
var basez : float = 0.0;
var mesh: Mesh = null;
verts = new Vector3[4];
normals = new Vector3[4];
uv = new Vector2[4];
tri = new int[6];
mf = GetComponent(MeshFilter);
basex = transform.position.x - height / 2;
basey = transform.position.y + height / 2;
basez = transform.position.z - width / 2;
verts[0] = new Vector3(basex, basey, basez);
verts[1] = new Vector3(basex + width, basey, basez);
verts[2] = new Vector3(basex, basey, basez + height);
verts[3] = new Vector3(basex + width, basey, basez + height);
for (i = 0; i < normals.Length; i++) {
normals[i] = Vector3.up;
}
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
tri[0] = 0;
tri[1] = 2;
tri[2] = 3;
tri[3] = 0;
tri[4] = 3;
tri[5] = 1;
mesh = new Mesh();
mesh.vertices = verts;
mesh.triangles = tri;
mesh.uv = uv;
mesh.normals = normals;
mf.mesh = mesh;
mesh.RecalculateNormals();
Answer by robertbu · Aug 01, 2013 at 08:30 PM
There is code in this answer that produces cubes out of six independent faces:
http://answers.unity3d.com/questions/391561/create-a-mesh-and-color-cubes.html
I don't do a lot of mesh work, so there might be better code posted on this list. You might Google "unity3d voxels" to see what else might help you.
I've tried the link, but I just don't understand how the vertexes, triangles, and uv's are mapped out in a cube. Plus I'm using JS, so I have no idea what the code in the link does. :/ I've also tried Google, but couldn't find anything.
Below is the $$anonymous$$akeCube() portion of the script translated to Javascript with the color stuff stripped out. Attach it to any visible game object with a mesh and run. Right now it does not work with an empty game object because there is no code to create and setup a $$anonymous$$eshRenderer. Without a Renderer, nothing shows. You will see one line defining the four vertices for each side of the cube. You could leave out any of the lines defining the vertices for a side, and the code would still run and produce a partial block without that side. The 'for' loop defines the two triangles for each side:
#pragma strict
function Start () {
var mf = GetComponent($$anonymous$$eshFilter);
if (mf == null)
mf = gameObject.AddComponent($$anonymous$$eshFilter);
var mesh = $$anonymous$$akeCube(1.0f);
mf.mesh = mesh;
}
function $$anonymous$$akeCube(fSize : float) : $$anonymous$$esh
{
var fHS = fSize / 2.0f;
var mesh = new $$anonymous$$esh();
mesh.vertices = [
Vector3(-fHS, fHS, -fHS), new Vector3( fHS, fHS, -fHS), new Vector3( fHS, -fHS, -fHS), new Vector3(-fHS, -fHS, -fHS), // Front
Vector3(-fHS, fHS, fHS), new Vector3( fHS, fHS, fHS), new Vector3( fHS, fHS, -fHS), new Vector3(-fHS, fHS, -fHS), // Top
Vector3(-fHS, -fHS, fHS), new Vector3( fHS, -fHS, fHS), new Vector3( fHS, fHS, fHS), new Vector3(-fHS, fHS, fHS), // Back
Vector3(-fHS, -fHS, -fHS), new Vector3( fHS, -fHS, -fHS), new Vector3( fHS, -fHS, fHS), new Vector3(-fHS, -fHS, fHS), // Bottom
Vector3(-fHS, fHS, fHS), new Vector3(-fHS, fHS, -fHS), new Vector3(-fHS, -fHS, -fHS), new Vector3(-fHS, -fHS, fHS), // Left
Vector3( fHS, fHS, -fHS), new Vector3( fHS, fHS, fHS), new Vector3( fHS, -fHS, fHS), new Vector3( fHS, -fHS, -fHS)]; // right
var triangles : int[] = new int[mesh.vertices.Length / 4 * 2 * 3];
var iPos = 0;
for (var i = 0; i < mesh.vertices.Length; i = i + 4) {
triangles[iPos++] = i;
triangles[iPos++] = i+1;
triangles[iPos++] = i+2;
triangles[iPos++] = i;
triangles[iPos++] = i+2;
triangles[iPos++] = i+3;
}
mesh.triangles = triangles;
mesh.RecalculateNormals();
return mesh;
}
Thanks a bunch! This is exactly what I needed. Now I know what to do. :) And it's fine that it doesn't set up a renderer, since I will be putting this script on a cube.
Answer by IgorAherne · Aug 01, 2013 at 08:30 PM
Hello! I am yet to begin studying this topic, yet here is a very good article, that will hopefully be useful. http://catlikecoding.com/unity/tutorials/star/
Answer by Dave-Carlile · Aug 01, 2013 at 08:30 PM
You just need to add more vertices and triangles to the mesh. Instead of arrays of 4, create arrays of 24 (6 faces * 4 vertices each).
(This is a big comment, sorry.) Thanks for responding so quickly, guys. :)
Dave, I've done what you said and created bigger arrays, and I am now able to create multiple faces at once. But I have run into a small problem. I can't quite figure out how to recreate the sides of the cube exactly how they were. Like, I'm not sure how to get the new planes to have the same vertexes, triangles, and uv's as the cube would normally have on that side. I've tried printing out the values from a normal cube's mesh and pasting them into my code, but the faces get deformed for some reason. This is important because if I try to make my own face from scratch, the texture is usually rotated. Do you know where I could find the values I need to make the faces look normal? (I've tried looking at the above links, but I don't understand C#...)
Your answer
![](https://koobas.hobune.stream/wayback/20220613111356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Selecting a single polygon / face at runtime 1 Answer
UV-ing a sphere procedurally 1 Answer
Highmap on a cube. 0 Answers
mesh-morph scaling badly 1 Answer
reading vertices from a mesh always returns Vector3.zero 2 Answers