Jump to content
Protozoaire

Move vertices position with script

Recommended Posts

To help those who are searching to do the same, below is a C# script to move the vertices of a pb object :

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

public class MoveVertex : MonoBehaviour
{
	pb_Object pb; //ProBuilder Object
	pb_Face face; //Object face
	Vector3 VertCoo; //Vertex Coordinates
	int[] indices; //List of vertices in the choosen face
	
	void Start()
	{
		pb = GetComponent<pb_Object>(); //Get the component on the object
		pb_Face face = pb.faces[0]; //Get the choosen face
		
		indices = face.distinctIndices; //Only grab the unique indices - so (0,1,2,1,3,2) becomes (0,1,2,3)

		VertCoo = new Vector3(0.5f,0.5f,0); //New coordinates

		StartCoroutine(WaitAndChange());
	}

	IEnumerator WaitAndChange()
	{
		yield return new WaitForSeconds(2); //After 2 seconds
		
		//We change the vertices' position
		pb.SetSharedVertexPosition(indices[0], VertCoo);
		pb.SetSharedVertexPosition(indices[1], VertCoo);
		pb.SetSharedVertexPosition(indices[2], VertCoo);
		pb.SetSharedVertexPosition(indices[3], VertCoo);
	}
}

To see what it does :

 

903879BoxToTriangle.gif

Share this post


Link to post
Share on other sites

I was searching the forums for a way to animate the vertices on a pb object. I wonder if you can help. My object is a bow, If i pull the vertices in the center of the bow string it looks like it is drawn back. Unity animate doesn't detect this movement to use it to animate. So I wanted to animate in script. But my next issue was how do I inform the script which vertices I want to move? using this script if I ignore pb_face and replace with (?) pb_vert  How would I select the vertices I want moving? How do I know what to manipulate via script, there is no documentation on pb types. 

Thanks

Share this post


Link to post
Share on other sites

While most of the API is accessible, an embarrassingly small amount of it is actually documented.  I run doxygen occasionally so that at least stubs are available; most recently a few weeks ago.

http://parabox.co/probuilder/probuilder-api/html/annotated.html

 

However I don't think animating a bow draw via script would be the best way to approach it.  I think it would be easier to write a utility that creates blend shapes from mesh snapshots, which you could then plug in to ProBuilder.  I'll try to do a quick proof of concept for that either tomorrow or next week to get you started.

Share this post


Link to post
Share on other sites

I'm not sure if it would be the best approach either. The mesh snapshot is a good idea.  So was taking this approach first, I've modified this script. But my other idea was to create a 2nd object and weld the verts to that object. Make it transparent or super tiny and use unity animator and move object 2 back and forth. 

When you build your project dont pb objects get stripped to normal meshes? So is that done last so all scripts know what was previously a pb object?

Share this post


Link to post
Share on other sites
Quote

When you build your project dont pb objects get stripped to normal meshes? So is that done last so all scripts know what was previously a pb object?

 

Yes, though even before that they are still normal meshes.  The stripping process just removes the (usually) redundant pb_Object and pb_Entity components.

I'm not sure what you mean by previously a pb_Object - once in the build there would be no reference to their past lives as ProBuilder objects.

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

×