Jump to content
willychyr

ProBuilder Scripting API for procedural generation?

Recommended Posts

Hi All,

I'm looking into using procedural generation to make levels and objects, and wanted to know if there's a ProBuilder scripting API for this?

 

My specific goal is to have a script spawn and place boxes of different sizes around the scene.

 

I know how to do this with the default Unity cubes, by just using

GameObject.CreatePrimitive(PrimitiveType.Cube);

And then changing its position and scale.

 

However, I'd like to do it with ProBuilder cubes, so that I can edit them by hand.

 

Also, I'd like to not use scale, but use the extrude face function, for having cubes of different sizes.

 

Any help would be greatly appreciated.

Thank you!

- Willy

Share this post


Link to post
Share on other sites

Here's a quick script to show how you would instantiate a ProBuilder cube and modify the size using either extrusion or translation.

using UnityEngine;
using System.Collections;
using ProBuilder2.Common;
using ProBuilder2.Math;
using ProBuilder2.MeshOperations;

public class SpawnCube : MonoBehaviour
{
	pb_Object pb;

	public void Start()
	{
		pb = ProBuilder.CreatePrimitive(Shape.Cube);
		
	}

	void OnGUI()
	{
		if(GUILayout.Button("Grow"))
		{
			foreach(pb_Face face in pb.faces)
			{
				// move all faces .5 meters
				pb.TranslateVertices(face.distinctIndices, pb_Math.Normal(pb.GetVertices(face.distinctIndices)) * .5f);
			}
			
			pb.Refresh();
		}

		if(GUILayout.Button("Extrude"))
		{
			// extrude all faces .5 meters
			foreach(pb_Face face in pb.faces)
				pb.Extrude(new pb_Face[] {face}, .5f);

			pb.Refresh();
		}
	}
}

Share this post


Link to post
Share on other sites

Hi Karl,  

 

Thanks for the response and for the example script. 

I tried running it, but I'm getting the error that "The name `Shape' does not exist in the current context"'

 

for the line

pb = ProBuilder.CreatePrimitive(Shape.Cube);

And "`ProBuilder2.Math.pb_Math' does not contain a definition for `Normal'"

 

for the line: 

pb.TranslateVertices(face.distinctIndices, pb_Math.Normal(pb.GetVertices(face.distinctIndices)) * .5f);

Going through the ProBuilder folder, I found the API Projects, and tried to run the scene 'pbObjectInstantiate', but actually got the same errors. 

Share this post


Link to post
Share on other sites

Ok, so it turns out I actually had to use this 'Probuilder.Shape', instead of just 'shape'. 

pb = ProBuilder.CreatePrimitive(ProBuilder.Shape.Cube);

and 

 

'PlaneNormal' instead of 'Normal'

pb.TranslateVertices(face.distinctIndices, pb_Math.PlaneNormal(pb.GetVertices(face.distinctIndices)) * .5f);

After this, I realized I may not have the same version, I'm using 1536 instead of r1536. Will update now.

Share this post


Link to post
Share on other sites

Sounds like you've figured out the errors on your own, but just thought I'd chime in to say the latest version is rv2321 (2.2.5f6).  There have been a lot of API changes since 1536, most of which are an attempt to standardize the style of calls to ProBuilder methods.

Share this post


Link to post
Share on other sites

Oh myyy. r1536 is ancient! Send me an email if you need the latest build. I'd recommend jumping into the Beta, honestly- it's rockin' good. :D

 

I second that. There aren't enough expletives to describe how good it is. :)

Share this post


Link to post
Share on other sites

Hi, 

 

I was trying to extrude some faces on my mesh using the pb.Extrude call shown in the example above but this is not a valid api call anymore. Could someone tell me what the correct way to extrude faces is now in the latest build of ProBuilder?

Share this post


Link to post
Share on other sites

I would take a look through this brand-new tutorial just sent out by Karl:

 

http://paraboxstudios.com/blog/blog_entry.html?2014-09-19=PipeDreams.html

 

Just had a look at this tutorial but it still references pb.Extrude(). I don't seem to have that method when referencing my pb object.  :(

 

pb_Object pb = GetComponent<pb_Object>();

// Call the Extrude extension method by passing an array of faces to extrude, and a distance.

// You may even omit the distance paramter and ProBuilder will extrude using a default value.

pb.Extrude( new pb_Face[] { pb.faces[0] }, 1f);

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×