- Home /
How can I get/set faces of a procedural created gameobject for usage with ProbuilderMesh.Extrude in script?
Hi, I try to extrude faces ingame. In the actual case from a plane but it should not matter from wich geometry. I want to try it with ProbuilderMesh.Extrude. I found "ExtrudeRandomEdges" (see below 1st code) in the Probuilder samples and want to adapt the face version of the function instead of the edge version in my "ExtrudeFaces" (see below 2nd code) but I'm not able to get the sourceFace of the plane. I would like to specify wich faces to extrude along to use later in case of a cube etc. After that I need to get the direction of the face with the normals I think.
Do you have an idea? I'm a coding noob and thankful for every help :), so sorry if my first post is not accurate.
#if UNITY_EDITOR || UNITY_STANDALONE
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.MeshOperations;
namespace ProBuilder.Examples
{
/// <summary>
/// Do a snake-like thing with a quad and some extrudes.
/// </summary>
class ExtrudeRandomEdges : MonoBehaviour
{
ProBuilderMesh m_Mesh;
Face m_LastExtrudedFace = null;
public float distance = 1f;
/// <summary>
/// Build a starting point (in this case, a quad)
/// </summary>
void Start()
{
m_Mesh = ShapeGenerator.GeneratePlane(PivotLocation.Center, 1, 1, 0, 0, Axis.Up);
m_Mesh.GetComponent<MeshRenderer>().sharedMaterial = BuiltinMaterials.defaultMaterial;
m_LastExtrudedFace = m_Mesh.faces[0];
}
void OnGUI()
{
if (GUILayout.Button("Extrude Random Edge"))
{
ExtrudeEdge();
}
}
void ExtrudeEdge()
{
// fetch a random perimeter edge connected to the last face extruded
List<WingedEdge> wings = WingedEdge.GetWingedEdges(m_Mesh);
IEnumerable<WingedEdge> sourceWings = wings.Where(x => x.face == m_LastExtrudedFace);
List<Edge> nonManifoldEdges = sourceWings.Where(x => x.opposite == null).Select(y => y.edge.local).ToList();
int rand = (int) Random.Range(0, nonManifoldEdges.Count);
Edge sourceEdge = nonManifoldEdges[rand];
// get the direction this edge should extrude in
var edgeCenter = Math.Average(m_Mesh.positions, new[] { sourceEdge.a, sourceEdge.b });
var faceCenter = Math.Average(m_Mesh.positions, m_LastExtrudedFace.distinctIndexes);
Vector3 dir = (edgeCenter - faceCenter).normalized;
// this will be populated with the extruded edge
Edge[] extrudedEdges;
// perform extrusion
extrudedEdges = m_Mesh.Extrude(new Edge[] {sourceEdge}, 0f, false, true);
// get the last extruded face
m_LastExtrudedFace = m_Mesh.faces.Last();
// translate the vertices
m_Mesh.TranslateVertices(extrudedEdges, dir * distance);
// rebuild mesh with new geometry added by extrude
m_Mesh.ToMesh();
// rebuild mesh normals, textures, collisions, etc
m_Mesh.Refresh();
}
}
}
#endif
My Version
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.MeshOperations;
public class ExtrudeFace : MonoBehaviour
{
ProBuilderMesh m_Mesh;
public float distance = 0.1f;
//private Mesh srcMesh;
///<summary>
/// extrude a specific face
/// </summary>
// Start is called before the first frame update
void Start()
{
m_Mesh = ShapeGenerator.GeneratePlane(PivotLocation.Center, 1, 1, 0, 0, Axis.Up);
m_Mesh.GetComponent<MeshRenderer>().sharedMaterial = BuiltinMaterials.defaultMaterial;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.E))
{
ExtrudeFace();
}
}
void ExtrudeFace()
{
//sourceface to Do!!!
Face sourceFace = ??????;
// get the direction this face should extrude in toDo!!!
Vector3 dir = ???????;
// this will be populated with the extruded face
Face[] extrudedFaces;
// perform extrusion
extrudedFaces = m_Mesh.Extrude(new Face[] {sourceFace}, ExtrudeMethod.FaceNormal, 1f);
// translate the vertices
m_Mesh.TranslateVertices(extrudedFaces, dir * distance);
// rebuild mesh with new geometry added by extrude
m_Mesh.ToMesh();
// rebuild mesh normals, textures, collisions, etc
m_Mesh.Refresh();
}
}
Comment