Jump to content

How to script extrusions

Recommended Posts

Hi everyone.

I'm doing a virtual art gallery for a painter friend of mine. I do all the design with ProBuilder which is great.

Having a lot of paintings to exhibit, and none of them being the same dimensions, I'm trying to write a script to create dynamical framing.

For each painting, I'm using a PB cube set to the painting's dimensions. Then I attach my framing script to it, so that the frame is created at runtime, using several extrusions. Unfortunately my script doesn't work very well. Is there any scripting documentation available ?

Thanx in advance.

Share this post

Link to post
Share on other sites

Hi, I did some progress with my framing script.
It now works, but I have new issues.

Here is how I proceed.
First I create a PB cube, with 3 different materials (left picture).
Then, I attach my script (Encadrement.cs) to it. It generates the frame, by extruding several times the back face (see the result on the right).


Everything's fine until I put a second painting in my scene (the same way, with a new PB cube).
See case 1 below.

It seems that their meshes are combined in a way, as I obtain this error :


Not allowed to access normals on mesh 'Combined Mesh (root: scene)' (isReadable is false; Read/Write must be enabled in import settings)
ProBuilder2.MeshOperations.pbMeshOps:Extrude(pb_Object, pb_Face[], ExtrudeMethod, Single, pb_Face[]&)
ProBuilder2.MeshOperations.pbMeshOps:Extrude(pb_Object, pb_Face[], Single, Boolean, pb_Face[]&)
ProBuilder2.MeshOperations.pbMeshOps:Extrude(pb_Object, pb_Face[], Single)
Encadrement:Start() (at Assets/Framing/Encadrement.cs:24)

This is quite strange, as I have in my scene another PB object which doesn't cause trouble : it is one first painting for which I've created the frame manually (so this object doesn't make use of my script).
Moreover,  this issue occurs even if I deactivate my script on one of my 2 paintings.

The only way I can have it work again is to deactivate the entire game object (case 2).
But, if I reactivate it during runtime, then it works (case 3) ! Both of them get procedurally framed if not activated at the same time.



Last thing I have to mention : Polybrush and Progrids are also intalled in my project.
The odd thing is : whatever the last tool selected (scale tool, rotation tool...), when I run the play mode, a Polybrush tool is automatically activated.
Is it a bug with the beta version of Polybrush ? And could this have anything to do with the first issue I mentioned ?


Thanx in advance for your help.

Share this post

Link to post
Share on other sites

Forgot to put my script, which is quite simple :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using ProBuilder2.Common;
using ProBuilder2.MeshOperations;

public class Encadrement : MonoBehaviour {
	public float largeur = 0.2f;
	public float epaisseur = 0.02f;
	public float profondeur = 0.1f;

	void Start () {
		pb_Object pb = GetComponent<pb_Object> ();

		// 1 - backface extrusion
		pb.Extrude (new pb_Face[] { pb.faces [0] }, 0.025f);
		pb.ToMesh ();
		pb.Refresh ();

		// 2 - borders extrusion
		pb.Extrude (new pb_Face[] { pb.faces [6], pb.faces [7], pb.faces [8], pb.faces [9] }, largeur);
		pb.ToMesh ();
		pb.Refresh ();

		// 3 - borders 2nd extrusion
		pb.Extrude (new pb_Face[] { pb.faces [6], pb.faces [7], pb.faces [8], pb.faces [9] }, epaisseur);
		pb.ToMesh ();
		pb.Refresh ();

		// 4 - sides extrusion towards front
		pb.Extrude (new pb_Face[] { pb.faces [18], pb.faces [20], pb.faces [22], pb.faces [24] }, profondeur - 0.025f);
		pb.ToMesh ();
		pb.Refresh ();





Share this post

Link to post
Share on other sites

First guess is that static batching is enabled. Make sure that any objects that will be modified at runtime are marked as not batching static.

If that isn't it, make sure that the objects are unique ProBuilder meshes. If they're prefabs you'll need to do a `pb.MakeUnique()` call to ensure they are not linked instances.

Share this post

Link to post
Share on other sites

You guessed right @karl, the problem came from static batching. 
I made my cubes not static anymore, and now everything works fine.
It also solves a lighting issue, which makes my procedural frames appear less white than they should.
I always forget that the default state is static when creating a PB object.

Thank you so much !


By the way, 2 questions :

1- To make them not static, I just unmarked the checkbox at the top of the inspector.
Is it OK to do so, or is it better to change the Entity Type to Mover ?

2- About assigning different materials to faces. 
Before PB became part of Unity Technologies, dragging materials directly from the assets to the object's face had bad results, and the Material Editor was the only way (but not so handy). Now it seems to work, is that right ?

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