Archive for May, 2013

ProPrimitives – Parametric primitives in Unity3D

May 5 | 13

I’ve been working for the last few months (on and off) on a utility package that I think is missing from the Unity3D engine and its a way to create flexible primitives in the means of being able to create them with more or less subdivisions depending on the needs.

Of course one would say “Just open maya and create a basic primitive with the specific needs and import it back into Unity”, but there are problems in this approach:

1. What happens for the ones like (me) that don’t use modeling software at all? it will take us a good amount of time to create a simple cube or a sphere plus then I will have to start to fight with the exporting issues and what not to get everything correct into Unity.

2. What happens if I need to create primitives on runtime?, the 3rd party 3d modeling option dies here as there is a need to generate the primitives on the go.

For this reason and also because I like the idea of generating by code 3D shapes I created ProPrimitives for Unity which is basically a Unity editor window that lets you create Planes, Spheres, Cubes, Capsules and Cylinders parametrically depending on the needs.

Also I created a simple API that everyone could use for creating their own primitives via scripting either in an other Editor Window or for in-game content.

Here are some screenshots of what the ProPrimitives Package can do for you:

 

Finally here is the Example.cs code that lets you create primitives via code:

//Create a Plane:
// 1st Param:Width of the plane:
// 2nd Param:Height of the plane
// 3rd Param:(opt) Width subdivisions of the plane (Default: 0)
// 4th Param:(opt) Height subdivisions of the plane (Default: 0)
// 5th Param:(opt) Space where the plane is going to be drawn (Default: PrimitiveSpace.XZ)
//
// to create a billboard just set subdivisions to 0 (3rd and 4th param)
GameObject examplePlane = ProPrimitive.CreatePlane(1, 1, 1, 1, PrimitiveSpace.XY);
examplePlane.transform.position = new Vector3(-5, 0, 0);
//
//Create a Cube:
// 1st Param:Width of the cube (along the X axis)
// 2nd Param:Height of the cube (along the Y axis)
// 3rd Param:Depth of the cube (along the Z axis)
// 4th Param:(opt) width subdivisions (Default: 1)
// 5th Param:(opt) height subdivisions (Default: 1)
// 6th Param:(opt) depth subdivisions (Default: 1)
GameObject exampleCube = ProPrimitive.CreateCube(1, 1, 1, 2, 10, 6);
exampleCube.transform.position = new Vector3(-3,0,0);
//
//Create a Cylinder:
// 1st Param:Radius of the cylinder
// 2nd Param:Height of the cylinder
// 3rd Param:(opt) Number of subdivisions the circumference of the cylinder will have (Default: 15)
// 4th Param:(opt) Number of subdivisions the height of the cylinder will have (Default: 0)
// 5th Param:(opt) Space where the cylinder is going to be drawn (Default: PrimitiveSpace.XZ)
GameObject exampleCylinder = ProPrimitive.CreateCylinder(0.8f, 2, 10, 2, PrimitiveSpace.XZ);
exampleCylinder.transform.position = new Vector3(0,0,0);
//
//Create a Sphere:
// 1st Param:Radius of the Sphere
// 2nd Param:(Opt) Subdivisions the sphere will have (Default: 15)
GameObject exampleSphere = ProPrimitive.CreateSphere(0.7f, 25);
exampleSphere.transform.position = new Vector3(2.5f,0,0);
//
//Create a Capsule:
// 1st Param:Radius if the capsule
// 2nd Param:Height of the capsule
// 3rd Param:(opt) Number of subdivisions the circumference of the capsule will have (Default: 15)
// 4th Param:(opt) Number of subdivisions the height of the capsule will have (Default: 0)
// 5th Param:(opt) Space where the capsule is going to be drawn (Default: PrimitiveSpace.XZ)
GameObject exampleCapsule = ProPrimitive.CreateCapsule(0.8f, 2, 10, 2, PrimitiveSpace.XY);
exampleCapsule.transform.position = new Vector3(5, 0, 0);

So if you want to try the package just get it in, its in the asset store!

Also if you just want to check how it looks like just go to: http://www.youtube.com/watch?v=qC5j0OQ25qo to check a small video I created