ProceduralPrimitives

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
(Added plane.)
Line 3: Line 3:
 
== Description ==
 
== Description ==
  
Use those scripts to procedurally create primitive meshes, with custom parameters. More to come later.
+
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 ==
 
== Usage ==
  
 
The entry point of those script must be a reference to a meshfilter's mesh. The return value is that mesh filled up.
 
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 ==
 +
 +
<syntaxhighlight lang="csharp">
 +
// 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 Normals
 +
Vector3[] normals = new Vector3[ vertices.Length ];
 +
{
 +
for(int i = 0; i < normals.Length; i++)
 +
normals[i] = Vector3.up;
 +
};
 +
#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; n++ )
 +
{
 +
// 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.normals = normals;
 +
mesh.uv = uvs;
 +
mesh.triangles = triangles;
 +
 +
mesh.RecalculateNormals();
 +
mesh.RecalculateBounds();
 +
mesh.Optimize();
 +
</syntaxhighlight>
  
 
== C# - Box ==  
 
== C# - Box ==  
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
// You can change that line to provide another MeshFilter
+
// You can change that line to provide another MeshFilter
MeshFilter filter = gameObject.AddComponent< MeshFilter >();
+
MeshFilter filter = gameObject.AddComponent< MeshFilter >();
Mesh mesh = filter.mesh;
+
Mesh mesh = filter.mesh;
mesh.Clear();
+
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,
 
 
float length = 1f;
+
// Left
float width = 1f;
+
p7, p4, p0, p3,
float height = 1f;
+
 
 
#region Vertices
+
// Front
Vector3 p0 = new Vector3( -length * .5f, -width * .5f, height * .5f );
+
p4, p5, p1, p0,
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 );
+
// Back
Vector3 p5 = new Vector3( length * .5f, width * .5f,  height * .5f );
+
p6, p7, p3, p2,
Vector3 p6 = new Vector3( length * .5f, width * .5f, -height * .5f );
+
Vector3 p7 = new Vector3( -length * .5f, width * .5f, -height * .5f );
+
 
 
Vector3[] vertices = new Vector3[]
+
// Right
{
+
p5, p6, p2, p1,
// 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 Normals
+
// Top
Vector3 up = Vector3.up;
+
p7, p6, p5, p4
Vector3 down = Vector3.down;
+
};
Vector3 front = Vector3.forward;
+
#endregion
Vector3 back = Vector3.back;
+
 
Vector3 left = Vector3.left;
+
#region Normals
Vector3 right = Vector3.right;
+
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,
 
 
Vector3[] normals = new Vector3[]
+
// Left
{
+
left, left, left, left,
// 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
+
// Front
Vector2 _00 = new Vector2( 0f, 0f );
+
front, front, front, front,
Vector2 _10 = new Vector2( 1f, 0f );
+
Vector2 _01 = new Vector2( 0f, 1f );
+
Vector2 _11 = new Vector2( 1f, 1f );
+
 
 
Vector2[] uvs = new Vector2[]
+
// Back
{
+
back, back, back, back,
// 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
+
// Right
int[] triangles = new int[]
+
right, right, right, right,
{
+
// Bottom
+
// Top
3, 1, 0,
+
up, up, up, up
3, 2, 1,
+
};
+
#endregion
// Left
+
 
3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
+
#region UVs
3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
+
Vector2 _00 = new Vector2( 0f, 0f );
+
Vector2 _10 = new Vector2( 1f, 0f );
// Front
+
Vector2 _01 = new Vector2( 0f, 1f );
3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
+
Vector2 _11 = new Vector2( 1f, 1f );
3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
+
 
 +
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,
 
 
// Back
+
// Front
3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
+
3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
+
3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
+
// Right
+
// Back
3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
+
3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
+
3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
+
// Top
+
3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
+
3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
+
+
};
+
#endregion
+
 
 
mesh.vertices = vertices;
+
// Right
mesh.normals = normals;
+
3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
mesh.uv = uvs;
+
3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
mesh.triangles = triangles;
+
 
 
mesh.RecalculateNormals();
+
// Top
mesh.RecalculateBounds();
+
3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
mesh.Optimize();
+
3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
 +
 +
};
 +
#endregion
 +
 
 +
mesh.vertices = vertices;
 +
mesh.normals = normals;
 +
mesh.uv = uvs;
 +
mesh.triangles = triangles;
 +
 
 +
mesh.RecalculateNormals();
 +
mesh.RecalculateBounds();
 +
mesh.Optimize();
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 23:50, 22 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 Normals		
Vector3[] normals = new Vector3[ vertices.Length ];
{
	for(int i = 0; i < normals.Length; i++)
		normals[i] = Vector3.up;
};
#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; n++ )
{
	// 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.normals = normals;
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 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
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.normals = normals;
mesh.uv = uvs;
mesh.triangles = triangles;
 
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox