ProceduralPrimitives

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Vertices fixed for cone.)
(Cone finished. Remove normals manual calculation from others, redundant with RecalculateNormals(). Fixed a mistake on plane's triangle loop (n instead of face).)
Line 37: Line 37:
 
}
 
}
 
#endregion
 
#endregion
 
#region Normals
 
Vector3[] normals = new Vector3[ vertices.Length ];
 
{
 
for(int i = 0; i < normals.Length; i++)
 
normals[i] = Vector3.up;
 
};
 
#endregion
 
  
 
#region UVs
 
#region UVs
Line 61: Line 53:
 
int[] triangles = new int[ nbFaces * 6 ];
 
int[] triangles = new int[ nbFaces * 6 ];
 
int t = 0;
 
int t = 0;
for(int face = 0; face < nbFaces; n++ )
+
for(int face = 0; face < nbFaces; face++ )
 
{
 
{
 
// Retrieve lower left corner from face ind
 
// Retrieve lower left corner from face ind
Line 77: Line 69:
  
 
mesh.vertices = vertices;
 
mesh.vertices = vertices;
mesh.normals = normals;
 
 
mesh.uv = uvs;
 
mesh.uv = uvs;
 
mesh.triangles = triangles;
 
mesh.triangles = triangles;
Line 131: Line 122:
 
};
 
};
 
#endregion
 
#endregion
 
#region Normals
 
Vector3 up = Vector3.up;
 
Vector3 down = Vector3.down;
 
Vector3 front = Vector3.forward;
 
Vector3 back = Vector3.back;
 
Vector3 left = Vector3.left;
 
Vector3 right = Vector3.right;
 
 
Vector3[] normals = new Vector3[]
 
{
 
// Bottom
 
down, down, down, down,
 
 
// Left
 
left, left, left, left,
 
 
// Front
 
front, front, front, front,
 
 
// Back
 
back, back, back, back,
 
 
// Right
 
right, right, right, right,
 
 
// Top
 
up, up, up, up
 
};
 
#endregion
 
  
 
#region UVs
 
#region UVs
Line 221: Line 182:
  
 
mesh.vertices = vertices;
 
mesh.vertices = vertices;
mesh.normals = normals;
 
 
mesh.uv = uvs;
 
mesh.uv = uvs;
 
mesh.triangles = triangles;
 
mesh.triangles = triangles;
Line 231: Line 191:
  
  
== C# - Cone (in progress) ==  
+
== C# - Cone ==  
  
<syntaxhighlight lang="csharp">
+
<syntaxhighlight lang="csharp">MeshFilter filter = gameObject.AddComponent<MeshFilter>();
MeshFilter filter = gameObject.AddComponent<MeshFilter>();
+
 
Mesh mesh = filter.mesh;
 
Mesh mesh = filter.mesh;
 
mesh.Clear();
 
mesh.Clear();
 
+
 
float height = 1f;
 
float height = 1f;
 
float bottomRadius = .25f;
 
float bottomRadius = .25f;
 
float topRadius = .05f;
 
float topRadius = .05f;
 
int nbSides = 18;
 
int nbSides = 18;
int nbHeightSeg = 1;
+
int nbHeightSeg = 1; // Not implemented yet
 
+
 
int nbVerticesCap = nbSides + 1;
 
int nbVerticesCap = nbSides + 1;
 
#region Vertices
 
#region Vertices
 
+
 
// bottom + top + sides
 
// bottom + top + sides
Vector3[] vertices = new Vector3[nbVerticesCap + nbVerticesCap + nbSides * nbHeightSeg * 2];
+
Vector3[] vertices = new Vector3[nbVerticesCap + nbVerticesCap + nbSides * nbHeightSeg * 2 + 2];
 
int vert = 0;
 
int vert = 0;
 
float _2pi = Mathf.PI * 2f;
 
float _2pi = Mathf.PI * 2f;
 
+
 
// Bottom cap
 
// Bottom cap
 
vertices[vert++] = new Vector3(0f, 0f, 0f);
 
vertices[vert++] = new Vector3(0f, 0f, 0f);
 
while( vert <= nbSides )
 
while( vert <= nbSides )
 
{
 
{
    float rad = (float)vert / nbSides * _2pi;
+
float rad = (float)vert / nbSides * _2pi;
    vertices[vert] = new Vector3(Mathf.Cos(rad) * bottomRadius, 0f, Mathf.Sin(rad) * bottomRadius);
+
vertices[vert] = new Vector3(Mathf.Cos(rad) * bottomRadius, 0f, Mathf.Sin(rad) * bottomRadius);
    vert++;
+
vert++;
 
}
 
}
 
+
 
// Top cap
 
// Top cap
 
vertices[vert++] = new Vector3(0f, height, 0f);
 
vertices[vert++] = new Vector3(0f, height, 0f);
 
while (vert <= nbSides * 2 + 1)
 
while (vert <= nbSides * 2 + 1)
 
{
 
{
    float rad = (float)(vert - nbSides - 1)  / nbSides * _2pi;
+
float rad = (float)(vert - nbSides - 1)  / nbSides * _2pi;
    vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, height, Mathf.Sin(rad) * topRadius);
+
vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, height, Mathf.Sin(rad) * topRadius);
    vert++;
+
vert++;
 
}
 
}
 
+
 
// Sides
 
// Sides
 
int v = 0;
 
int v = 0;
while (vert <= nbSides * 2 + nbSides * 2 + 1)
+
while (vert <= vertices.Length - 4 )
 
{
 
{
    float rad = (float)v / nbSides * _2pi;
+
float rad = (float)v / nbSides * _2pi;
    vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, height, Mathf.Sin(rad) * topRadius);
+
vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, height, Mathf.Sin(rad) * topRadius);
    vertices[vert + 1] = new Vector3(Mathf.Cos(rad) * bottomRadius, 0, Mathf.Sin(rad) * bottomRadius);
+
vertices[vert + 1] = new Vector3(Mathf.Cos(rad) * bottomRadius, 0, Mathf.Sin(rad) * bottomRadius);
    vert+=2;
+
vert+=2;
    v++;
+
v++;
 
}
 
}
 +
vertices[vert] = vertices[ nbSides * 2 + 2 ];
 +
vertices[vert + 1] = vertices[nbSides * 2 + 3 ];
 
#endregion
 
#endregion
 +
       
 +
#region UVs
 +
Vector2[] uvs = new Vector2[vertices.Length];
  
/*#region Normals
+
// Bottom cap
Vector3 up = Vector3.up;
+
int u = 0;
Vector3 down = Vector3.down;
+
uvs[u++] = new Vector2(0.5f, 0.5f);
Vector3 front = Vector3.forward;
+
while (u <= nbSides)
Vector3 back = Vector3.back;
+
Vector3 left = Vector3.left;
+
Vector3 right = Vector3.right;
+
 
+
Vector3[] normals = new Vector3[]
+
 
{
 
{
};
+
    float rad = (float)u / nbSides * _2pi;
#endregion
+
    uvs[u] = new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
 
+
    u++;
#region UVs
+
}
Vector2 _00 = new Vector2(0f, 0f);
+
Vector2 _10 = new Vector2(1f, 0f);
+
Vector2 _01 = new Vector2(0f, 1f);
+
Vector2 _11 = new Vector2(1f, 1f);
+
  
Vector2[] uvs = new Vector2[]
+
// Top cap
 +
uvs[u++] = new Vector2(0.5f, 0.5f);
 +
while (u <= nbSides * 2 + 1)
 
{
 
{
};
+
    float rad = (float)u / nbSides * _2pi;
#endregion*/
+
    uvs[u] = new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
 +
    u++;
 +
}
  
 +
// Sides
 +
int u_sides = 0;
 +
while (u <= uvs.Length - 4 )
 +
{
 +
    float t = (float)u_sides / nbSides;
 +
    uvs[u] = new Vector3(t, 1f);
 +
    uvs[u + 1] = new Vector3(t, 0f);
 +
    u += 2;
 +
    u_sides++;
 +
}
 +
uvs[u] = new Vector2(1f, 1f);
 +
uvs[u + 1] = new Vector2(1f, 0f);
 +
#endregion
 +
 
#region Triangles
 
#region Triangles
 
int nbTriangles = nbSides + nbSides + nbSides*2;
 
int nbTriangles = nbSides + nbSides + nbSides*2;
 
int[] triangles = new int[nbTriangles * 3 + 3];
 
int[] triangles = new int[nbTriangles * 3 + 3];
       
+
       
 
// Bottom cap
 
// Bottom cap
 
int tri = 0;
 
int tri = 0;
Line 315: Line 288:
 
while (tri < nbSides - 1)
 
while (tri < nbSides - 1)
 
{
 
{
    triangles[ i ] = 0;
+
triangles[ i ] = 0;
    triangles[ i+1 ] = tri + 1;
+
triangles[ i+1 ] = tri + 1;
    triangles[ i+2 ] = tri + 2;
+
triangles[ i+2 ] = tri + 2;
    tri++;
+
tri++;
    i += 3;
+
i += 3;
 
}
 
}
 
triangles[i] = 0;
 
triangles[i] = 0;
Line 326: Line 299:
 
tri++;
 
tri++;
 
i += 3;
 
i += 3;
 
+
 
// Top cap
 
// Top cap
 
//tri++;
 
//tri++;
 
while (tri < nbSides*2)
 
while (tri < nbSides*2)
 
{
 
{
    triangles[ i ] = tri + 2;
+
triangles[ i ] = tri + 2;
    triangles[i + 1] = tri + 1;
+
triangles[i + 1] = tri + 1;
    triangles[i + 2] = nbVerticesCap;
+
triangles[i + 2] = nbVerticesCap;
    tri++;
+
tri++;
    i += 3;
+
i += 3;
 
}
 
}
 
+
 
triangles[i] = nbVerticesCap + 1;
 
triangles[i] = nbVerticesCap + 1;
 
triangles[i + 1] = tri + 1;
 
triangles[i + 1] = tri + 1;
Line 344: Line 317:
 
i += 3;
 
i += 3;
 
tri++;
 
tri++;
//i += 3;
+
 
+
 
+
 
+
 
// Sides
 
// Sides
while (tri < nbTriangles - 1)
+
while( tri <= nbTriangles )
 
{
 
{
    triangles[ i ] = tri + 2;
+
triangles[ i ] = tri + 2;
    triangles[ i+1 ] = tri + 1;
+
triangles[ i+1 ] = tri + 1;
    triangles[ i+2 ] = tri + 0;
+
triangles[ i+2 ] = tri + 0;
    tri++;
+
tri++;
    i += 3;
+
i += 3;
 
+
    triangles[ i ] = tri + 1;
+
triangles[ i ] = tri + 1;
    triangles[ i+1 ] = tri + 2;
+
triangles[ i+1 ] = tri + 2;
    triangles[ i+2 ] = tri + 0;
+
triangles[ i+2 ] = tri + 0;
    tri++;
+
tri++;
    i += 3;
+
i += 3;
 
}
 
}
 
triangles[i] = nbVerticesCap * 2;
 
triangles[i + 1] = tri+1;
 
triangles[i + 2] = tri + 0;
 
tri++;
 
i += 3;
 
 
triangles[i] = tri;
 
triangles[i + 1] = nbVerticesCap * 2;
 
triangles[i + 2] = nbVerticesCap * 2 + 1;
 
tri++;
 
 
#endregion
 
#endregion
 
+
 
mesh.vertices = vertices;
 
mesh.vertices = vertices;
//mesh.normals = normals;
+
mesh.uv = uvs;
//mesh.uv = uvs;
+
 
mesh.triangles = triangles;
 
mesh.triangles = triangles;
 
+
 
mesh.RecalculateNormals();
 
mesh.RecalculateNormals();
 
mesh.RecalculateBounds();
 
mesh.RecalculateBounds();
 
mesh.Optimize();
 
mesh.Optimize();
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:59, 25 January 2013

Author: Bérenger.

Contents

Description

Use those scripts to procedurally create primitive meshes, with custom parameters. More to come later. If anyone has the code for a teapot, that'd be great. I guess it's a tiny bit more complicated though.

Usage

The entry point of those script must be a reference to a meshfilter's mesh. The return value is that mesh filled up.


C# - Plane

// You can change that line to provide another MeshFilter
MeshFilter filter = gameObject.AddComponent< MeshFilter >();
Mesh mesh = filter.mesh;
mesh.Clear();
 
float length = 1f;
float width = 1f;
int resX = 2; // 2 minimum
int resZ = 2;
 
#region Vertices		
Vector3[] vertices = new Vector3[ resX * resZ ];
for(int z = 0; z < resZ; z++)
{
	// [ -length / 2, length / 2 ]
	float zPos = ((float)z / (resZ - 1) - .5f) * length;
	for(int x = 0; x < resX; x++)
	{
		// [ -width / 2, width / 2 ]
		float xPos = ((float)x / (resX - 1) - .5f) * width;
		vertices[ x + z * resX ] = new Vector3( xPos, 0f, zPos );
	}
}
#endregion
 
#region UVs		
Vector2[] uvs = new Vector2[ vertices.Length ];
for(int v = 0; v < resZ; v++)
{
	for(int u = 0; u < resX; u++)
	{
		uvs[ u + v * resX ] = new Vector2( (float)u / (resX - 1), (float)v / (resZ - 1) );
	}
}
#endregion
 
#region Triangles
int nbFaces = (resX - 1) * (resZ - 1);
int[] triangles = new int[ nbFaces * 6 ];
int t = 0;
for(int face = 0; face < nbFaces; face++ )
{
	// Retrieve lower left corner from face ind
	int i = face % (resX - 1) + (face / (resZ - 1) * resX);
 
	triangles[t++] = i + resX;
	triangles[t++] = i + 1;
	triangles[t++] = i;
 
	triangles[t++] = i + resX;	
	triangles[t++] = i + resX + 1;
	triangles[t++] = i + 1; 
}
#endregion
 
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = triangles;
 
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();


C# - Box

// You can change that line to provide another MeshFilter
MeshFilter filter = gameObject.AddComponent< MeshFilter >();
Mesh mesh = filter.mesh;
mesh.Clear();
 
float length = 1f;
float width = 1f;
float height = 1f;
 
#region Vertices
Vector3 p0 = new Vector3( -length * .5f,	-width * .5f, height * .5f );
Vector3 p1 = new Vector3( length * .5f, 	-width * .5f, height * .5f );
Vector3 p2 = new Vector3( length * .5f, 	-width * .5f, -height * .5f );
Vector3 p3 = new Vector3( -length * .5f,	-width * .5f, -height * .5f );	
 
Vector3 p4 = new Vector3( -length * .5f,	width * .5f,  height * .5f );
Vector3 p5 = new Vector3( length * .5f, 	width * .5f,  height * .5f );
Vector3 p6 = new Vector3( length * .5f, 	width * .5f,  -height * .5f );
Vector3 p7 = new Vector3( -length * .5f,	width * .5f,  -height * .5f );
 
Vector3[] vertices = new Vector3[]
{
	// Bottom
	p0, p1, p2, p3,
 
	// Left
	p7, p4, p0, p3,
 
	// Front
	p4, p5, p1, p0,
 
	// Back
	p6, p7, p3, p2,
 
	// Right
	p5, p6, p2, p1,
 
	// Top
	p7, p6, p5, p4
};
#endregion
 
#region UVs
Vector2 _00 = new Vector2( 0f, 0f );
Vector2 _10 = new Vector2( 1f, 0f );
Vector2 _01 = new Vector2( 0f, 1f );
Vector2 _11 = new Vector2( 1f, 1f );
 
Vector2[] uvs = new Vector2[]
{
	// Bottom
	_11, _01, _00, _10,
 
	// Left
	_11, _01, _00, _10,
 
	// Front
	_11, _01, _00, _10,
 
	// Back
	_11, _01, _00, _10,
 
	// Right
	_11, _01, _00, _10,
 
	// Top
	_11, _01, _00, _10,
};
#endregion
 
#region Triangles
int[] triangles = new int[]
{
	// Bottom
	3, 1, 0,
	3, 2, 1,			
 
	// Left
	3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
	3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
 
	// Front
	3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
	3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
 
	// Back
	3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
	3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
 
	// Right
	3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
	3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
 
	// Top
	3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
	3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
 
};
#endregion
 
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = triangles;
 
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();


C# - Cone

MeshFilter filter = gameObject.AddComponent<MeshFilter>();
Mesh mesh = filter.mesh;
mesh.Clear();
 
float height = 1f;
float bottomRadius = .25f;
float topRadius = .05f;
int nbSides = 18;
int nbHeightSeg = 1; // Not implemented yet
 
int nbVerticesCap = nbSides + 1;
#region Vertices
 
// bottom + top + sides
Vector3[] vertices = new Vector3[nbVerticesCap + nbVerticesCap + nbSides * nbHeightSeg * 2 + 2];
int vert = 0;
float _2pi = Mathf.PI * 2f;
 
// Bottom cap
vertices[vert++] = new Vector3(0f, 0f, 0f);
while( vert <= nbSides )
{
	float rad = (float)vert / nbSides * _2pi;
	vertices[vert] = new Vector3(Mathf.Cos(rad) * bottomRadius, 0f, Mathf.Sin(rad) * bottomRadius);
	vert++;
}
 
// Top cap
vertices[vert++] = new Vector3(0f, height, 0f);
while (vert <= nbSides * 2 + 1)
{
	float rad = (float)(vert - nbSides - 1)  / nbSides * _2pi;
	vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, height, Mathf.Sin(rad) * topRadius);
	vert++;
}
 
// Sides
int v = 0;
while (vert <= vertices.Length - 4 )
{
	float rad = (float)v / nbSides * _2pi;
	vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, height, Mathf.Sin(rad) * topRadius);
	vertices[vert + 1] = new Vector3(Mathf.Cos(rad) * bottomRadius, 0, Mathf.Sin(rad) * bottomRadius);
	vert+=2;
	v++;
}
vertices[vert] = vertices[ nbSides * 2 + 2 ];
vertices[vert + 1] = vertices[nbSides * 2 + 3 ];
#endregion
 
#region UVs
Vector2[] uvs = new Vector2[vertices.Length];
 
// Bottom cap
int u = 0;
uvs[u++] = new Vector2(0.5f, 0.5f);
while (u <= nbSides)
{
    float rad = (float)u / nbSides * _2pi;
    uvs[u] = new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
    u++;
}
 
// Top cap
uvs[u++] = new Vector2(0.5f, 0.5f);
while (u <= nbSides * 2 + 1)
{
    float rad = (float)u / nbSides * _2pi;
    uvs[u] = new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
    u++;
}
 
// Sides
int u_sides = 0;
while (u <= uvs.Length - 4 )
{
    float t = (float)u_sides / nbSides;
    uvs[u] = new Vector3(t, 1f);
    uvs[u + 1] = new Vector3(t, 0f);
    u += 2;
    u_sides++;
}
uvs[u] = new Vector2(1f, 1f);
uvs[u + 1] = new Vector2(1f, 0f);
#endregion
 
#region Triangles
int nbTriangles = nbSides + nbSides + nbSides*2;
int[] triangles = new int[nbTriangles * 3 + 3];
 
// Bottom cap
int tri = 0;
int i = 0;
while (tri < nbSides - 1)
{
	triangles[ i ] = 0;
	triangles[ i+1 ] = tri + 1;
	triangles[ i+2 ] = tri + 2;
	tri++;
	i += 3;
}
triangles[i] = 0;
triangles[i + 1] = tri + 1;
triangles[i + 2] = 1;
tri++;
i += 3;
 
// Top cap
//tri++;
while (tri < nbSides*2)
{
	triangles[ i ] = tri + 2;
	triangles[i + 1] = tri + 1;
	triangles[i + 2] = nbVerticesCap;
	tri++;
	i += 3;
}
 
triangles[i] = nbVerticesCap + 1;
triangles[i + 1] = tri + 1;
triangles[i + 2] = nbVerticesCap;		
tri++;
i += 3;
tri++;
 
// Sides
while( tri <= nbTriangles )
{
	triangles[ i ] = tri + 2;
	triangles[ i+1 ] = tri + 1;
	triangles[ i+2 ] = tri + 0;
	tri++;
	i += 3;
 
	triangles[ i ] = tri + 1;
	triangles[ i+1 ] = tri + 2;
	triangles[ i+2 ] = tri + 0;
	tri++;
	i += 3;
}
#endregion
 
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = triangles;
 
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox