Jump to content
Sign in to follow this  

Programmatically rotate face

Recommended Posts

I'm trying to conduct some procedural generation, I'm trying to use Probuilder to simplify the process of working with meshes in code and I think it will work pretty well.


However I'm at a wall at the very beginning because I'm not really sure how I would rotate a face using the probuilder api.


I'd assume that I need to get the face, which I have done, and then I can get the vertices from the face, apply a translation to each vertex to apply the rotation.


However how do I update the mesh?   The Vector3s are just structs so I know they don't automatically update the mesh.


Would I use pb.SetVertices?  If I do so, does that mean I need to get all the vertices of the mesh, modify the vertices at the correct index and then pass in the entire array to update the mesh?


At that point, is there any reason for me to explicitly get the pb_Face object?


Any answers or guidance you could give would be helpful!  Thank you for a great product!

Share this post

Link to post
Share on other sites

Here's a quick script that rotates a face in local space.  pb_Editor has a method called VertexRotateTool that can also demonstrate how to move vertices in planar and world space.



using UnityEngine;
using System.Collections;
using ProBuilder2.Common;	/// for pb_Object and pb_Face
using ProBuilder2.Math;

public class RotateFace : MonoBehaviour
	pb_Object pb;
	pb_Face face;

	int[] indices;
	Vector3[] vertexOrigins;
	Vector3 vertexCenter;
	Vector3 nrm;

	void Start()
		pb = GetComponent<pb_Object>();
		pb_Face face = pb.faces[0];

		nrm = pb_Math.Normal(pb, face);

		// only grab the unique indices - so (0,1,2,1,3,2) becomes (0,1,2,3)
		indices = face.distinctIndices;

		// store the origin points of each vertex we'll be moving
		vertexOrigins = pbUtil.ValuesWithIndices(pb.vertices, indices);
		vertexCenter = pb_Math.BoundsCenter(vertexOrigins);

	float rotation = 0f;

	void Update()
		rotation = Mathf.Sin(Time.time) * 90f;

		Quaternion faceRotation = Quaternion.Euler( nrm * rotation );

		for(int i = 0; i < indices.Length; i++)
			Vector3 v = vertexOrigins[i] - vertexCenter;

			v = faceRotation * v;

			v += vertexCenter;

			// Using SetSharedVertexPosition guarantees that all vertices
			// that are shared among the indices are also rotated.  It 
			// also applies the pb.vertices array to the mesh.
			pb.SetSharedVertexPosition(indices[i], v);

Share this post

Link to post
Share on other sites

Above and Beyond Karl!

You guys consistently make me feel like ProBuilder was the best 100$ I ever spent.
On a side note, after I posted this I found the pipe-dreams tutorial which gives a nice bit of information on using the API in script.


The core documentation lacks any reference to interacting with ProBuilder at an API level, I think you guys should include atleast some references to the Pipe dream example and any other script examples demonstrating the API.

Perhaps even little gems in the forums like this!

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
Sign in to follow this