Jump to content
bzkarcade

Create a custom shape via script

Recommended Posts

Hello, it's my first question.

I need to create shapes based on vertices as in the editor (capture attached)

I try to find something to create meshes procedurally, currently have this code, but it produces an exception

 

            pb = new pb_Object();
            List<Vector3> points = new List<Vector3>();
            points.Add(new Vector3(0, 0, 0));
            points.Add(new Vector3(1, 0, 0));
            points.Add(new Vector3(0, 1, 0));
            points.Add(new Vector3(1, 1, 0)); //same as in editor
            pb_ActionResult ar = pb.CreateShapeFromPolygon(points, 1, false);
            pb.ToMesh();
            pb.Refresh();

 

Exception:

 

NullReferenceException
UnityEngine.Component.GetComponent[MeshFilter] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:48)
pb_Object.get_msh ()
pb_Object.ToMesh (MeshTopology preferredTopology)
pb_Object.ToMesh ()

 

how can I do it?, it would help me a lot

cap-probuilder.png

Share this post


Link to post
Share on other sites

Thanks, in that moment, I could already make it work with some basic figures, but in some others with such vertices I have problems.

For example

160,76,0
69,165,0
67,354,0
169,451,0
343,451,0
445,354,0
443,165,0
352,76,0

 

Produce ghost mesh in cap_002.PNG 

 

when what I'm looking for is cap_003.PNG

 

I think it's the order of the vertices, but I'm not sure

 

cap_002.PNG

cap_003.PNG

Share this post


Link to post
Share on other sites

The points need to be ordered, yes. There's actually an overload that does this for convex shapes:

		public static pb_ActionResult CreatePolygon(this pb_Object pb, IList<int> indices, bool unordered, out pb_Face face)

 

Share this post


Link to post
Share on other sites

Thanks for your fast reply, in fact works, but, it's like I add a new face and the two previous ones stay. In Back view the figure is seen, with the overlap of one of the previous faces 004, but in front view nope.

If I deleted the original faces or detach the new face, I ccan fix it

So in the code I delete the previous faces, although rare
 

This is my complete code:

 

           
            pb = new pb_Object();
            List<Vector3> points = new List<Vector3>();
            points.Add(new Vector3(160, 76, 0));
            points.Add(new Vector3(69, 165, 0));
            points.Add(new Vector3(67, 354, 0));
            points.Add(new Vector3(169, 451, 0));
            points.Add(new Vector3(343, 451, 0));
            points.Add(new Vector3(445, 354, 0));
            points.Add(new Vector3(443, 165, 0));
            points.Add(new Vector3(352, 76, 0));
            pb = pb_Object.CreateInstanceWithPoints(points.ToArray());
            List<int> indices = new List<int>();
            for (int i = 0; i < points.Count; i++)
            {
                indices.Add(i);
            }           
            List<pb_Face> trashFaces = pb.faces.ToList();
            pb_Face baseFace = new pb_Face();
            pb.CreatePolygon(indices, false, out baseFace);
           
            pb.DeleteFaces(trashFaces);
           
            pb.ToMesh();
            pb.Refresh();
 
 

In cap_006  finally have what I want :D

 

PD: at this moment I do not need to have both faces of the figure, but if later I want my figure to have the back face,  now I do it with the fillhole option in editor. Would there be an easy way to do it?

cap_004.PNG

cap_005.PNG

cap_006.PNG

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

×