- Home /
Creating a plane mesh directly from code
Hi
As in topic-I'd like to create a small Plane fully programatically.How to do that? I think I have to create a new GameObject then add MeshFilter and MeshRenderer components to it.But what's next?
Answer by zmar0519 · Jul 06, 2011 at 12:01 PM
you could use gameObject.CreatePrimitive(PrimitiveType.Plane);
, but that will give you the ten by ten vertice plane we get from the gameObject Menu. I reccomend trying the below code. It will give you a four vertice and two tri plane.
var size : float;
function Awake()
{
var m : Mesh = new Mesh();
m.name = "Scripted_Plane_New_Mesh";
m.vertices = [Vector3(-size, -size, 0.01), Vector3(size, -size, 0.01), Vector3(size, size, 0.01), Vector3(-size, size, 0.01) ];
m.uv = [Vector2 (0, 0), Vector2 (0, 1), Vector2(1, 1), Vector2 (1, 0)];
m.triangles = [0, 1, 2, 0, 2, 3];
m.RecalculateNormals();
var obj : GameObject = new GameObject("New_Plane_Fom_Script", MeshRenderer, MeshFilter, MeshCollider);
obj.GetComponent(MeshFilter).mesh = m;
}
I've created something similliar,but there is a problem with visibility :/
Take a look: http://answers.unity3d.com/questions/140070/mesh-created-in-code-is-not-visible.html
Thanks for the code! I created a C# function for $$anonymous$$esh creation (based on the code above), modified to take width and height parameters.
$$anonymous$$esh Create$$anonymous$$esh(float width, float height)
{
$$anonymous$$esh m = new $$anonymous$$esh();
m.name = "Scripted$$anonymous$$esh";
m.vertices = new Vector3[] {
new Vector3(-width, -height, 0.01f),
new Vector3(width, -height, 0.01f),
new Vector3(width, height, 0.01f),
new Vector3(-width, height, 0.01f)
};
m.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2(1, 1),
new Vector2 (1, 0)
};
m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
m.RecalculateNormals();
return m;
}
I call that function from the Awake() function, creating an object to apply the mesh to and setting up the shader and material (to apply a colour):
void Awake() {
GameObject plane = new GameObject("Plane");
$$anonymous$$eshFilter meshFilter = ($$anonymous$$eshFilter)plane.AddComponent(typeof($$anonymous$$eshFilter));
meshFilter.mesh = Create$$anonymous$$esh(1, 0.2f);
$$anonymous$$eshRenderer renderer = plane.AddComponent(typeof($$anonymous$$eshRenderer)) as $$anonymous$$eshRenderer;
renderer.material.shader = Shader.Find ("Particles/Additive");
Texture2D tex = new Texture2D(1, 1);
tex.SetPixel(0, 0, Color.green);
tex.Apply();
renderer.material.mainTexture = tex;
renderer.material.color = Color.green;
}
Answer by Eric5h5 · Jul 05, 2011 at 06:33 PM
See the CreatePlane script on the wiki.
Hmmmm and what about struct Plane I've found in code?Have I to make the mesh by assigning those verticles manually,or I just can use that struct?
@$$anonymous$$asterBLB: that's a mathematical plane. Nothing to do with visual planes.
There doesn't seem to be a website at that address. Has this moved?
@sosh: Yes it has moved as Unity now hosts the wiki on it's own domain. For a long time the old domain was redirected to the new one, but now the domain seems to be outdated. I've fixed the link.
Answer by Chris D · Jul 05, 2011 at 06:28 PM
The documentation on the Mesh functions is pretty thorough. Point 1 has a code example to build a mesh from scratch:
1. Building a mesh from scratch: should always be done in the following order: 1) assign vertices 2) assign triangles
var newVertices : Vector3[];
var newUV : Vector2[];
var newTriangles : int[];
function Start () {
var mesh : Mesh = new Mesh ();
GetComponent(MeshFilter).mesh = mesh;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
Answer by sysmaya · Jan 30, 2017 at 10:37 AM
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
// Position and rotation plane
plane.transform.position = new Vector3(1f, 2f, 3f);