Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mad_Memo · Sep 23, 2020 at 07:05 PM · facesextrude

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Im trying to extrude faces of a GameObject using ProBuiler in runtime using the following code , but its not working, is there any other solution for this? 0 Answers

How to add vertices and faces onto a mesh through c# script 1 Answer

Does Vertices count affect FPS when making buildings? 1 Answer

Ways to optimize voxel generation like Minecraft 0 Answers

Faces vs. Materials 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges