Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NanoH · Apr 14, 2017 at 10:37 AM · meshproceduralprocedural-generation

Procedural Box: Only the first face is displayed

Hi,

I'm trying to generate a box procedurally. Everything is almost working, except I have only one face showing up, and it's always the first face to be processed. Here is my code:

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
 
     public class BlockPart : MonoBehaviour
     {
         MeshFilter filter;
         Renderer myRenderer;
         public Vector3 dimensions;
         public Texture2D tex;
         public List<Vector3> vertices;
         public List<int> triangles;
         public List<Vector3> normals;
         public List<Vector2> uv;
         List<Vector3> tmpVert = new List<Vector3>();
         List<int> tmpTris = new List<int>();
         List<Vector3> tmpNorm = new List<Vector3>();
         List<Vector2> tmpUvs = new List<Vector2>();
         public float myAngle = 0;
 
         void Awake()
         {
             dimensions = new Vector3(1, 2.2f, 1);
             filter = GetComponent<MeshFilter>();
             myRenderer = GetComponent<Renderer>();
         }
 
         public void reinit()
         {
             filter.mesh = new Mesh();
             vertices = new List<Vector3>();
             triangles = new List<int>();
             normals = new List<Vector3>();
             uv = new List<Vector2>();
         }
 
         public void addPane(Vector3 norm, Vector3 first, Vector3 second, Vector3 third, Vector3 forth)
         {
             tmpVert = new List<Vector3>() { new Vector3(), new Vector3(), new Vector3(), new Vector3() };
             tmpTris = new List<int>() { 0, 0, 0, 0, 0, 0 };
             tmpNorm = new List<Vector3>() { new Vector3(), new Vector3(), new Vector3(), new Vector3() };
             tmpUvs = new List<Vector2>() { new Vector2(), new Vector2(), new Vector2(), new Vector2() };
             tmpVert[0] = first;
             tmpVert[1] = second;
             tmpVert[2] = third;
             tmpVert[3] = forth;
 
             tmpTris[0] = 0;
             tmpTris[1] = 1;
             tmpTris[2] = 2;
 
             tmpTris[3] = 0;
             tmpTris[4] = 2;
             tmpTris[5] = 3;
             
             tmpNorm[0] = norm;
             tmpNorm[1] = norm;
             tmpNorm[2] = norm;
             tmpNorm[3] = norm;
             tmpUvs[0] = new Vector2(0, 0);
             tmpUvs[1] = new Vector2(0, 1);
             tmpUvs[2] = new Vector2(1, 1);
             tmpUvs[3] = new Vector2(1, 0);
             vertices.AddRange(tmpVert);
             triangles.AddRange(tmpTris);
             normals.AddRange(tmpNorm);
             uv.AddRange(tmpUvs);
         
         }
 
         public Vector3 newV(float x, float y, float z)
         {
             return new Vector3(x, y, z);
         }
 
         public void addBox()
         {
 
         }
 
         public Vector3 RotatePointAroundPivot(Vector3 point, float angle)
         {
             return Quaternion.AngleAxis(angle, Vector3.up) * point;
 
         }
 
         void createMesh()
         {
             filter.mesh.vertices = vertices.ToArray();
             filter.mesh.normals = normals.ToArray();
             filter.mesh.uv = uv.ToArray();
             filter.mesh.triangles = triangles.ToArray();
             myRenderer.material.mainTexture = tex;
             filter.mesh.RecalculateBounds();
             filter.mesh.RecalculateNormals();
             filter.mesh.Optimize();
         }
     
         void Update ()
         {
             // Here, no matter what I do the first call on "addPane" will be the only one to be drawn
             myAngle += Time.deltaTime * 5f;
             reinit();
             addPane(-Vector3.forward, 

                     newV(-0.05f, 0, -0.05f),
                     newV(-0.05f, 2.2f, -0.05f),
                     newV(0.05f, 2.2f, -0.05f),
                     newV(0.05f, 0, -0.05f));
             addPane(Vector3.forward,
                     newV(0.05f, 0, 0.05f),
                     newV(0.05f, 2.2f, 0.05f),
                     newV(-0.05f, 2.2f, 0.05f),
                     newV(-0.05f, 0, 0.05f));
             addPane(-Vector3.left,
                     newV(-0.05f, 0, 0.05f),
                     newV(-0.05f, 2.2f, 0.05f),
                     newV(-0.05f, 2.2f, -0.05f),
                     newV(-0.05f, 0, -0.05f));
             addPane(Vector3.right,
                     newV(0.05f, 0, -0.05f),
                     newV(0.05f, 2.2f, -0.05f),
                     newV(0.05f, 2.2f, 0.05f),
                     newV(0.05f, 0, 0.05f));
             createMesh();
         }
     }
 

I've tried several approaches and I am running out of ideas.

edit: Separately, the faces are correct. They are all clockwise, display correctly with the proper UV, normals and triangles.

Thanks for your help.

Comment
Add comment · Show 1
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
avatar image RibsNGibs · Apr 14, 2017 at 01:05 PM 0
Share

What debugging steps have you tried? Debug.Log to make sure your verts/tris/norms lists have the expected number of items? Commenting out the first two panes to see if you can get a single side face to show up?

Btw one of your side panes is wrong - you passed them the same normal (-left and +right are both +right).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Apr 14, 2017 at 04:29 PM

At least you never add numbers beyond 0-3 to your triangles array, so those are the only 4 vertices you use to draw geometry

 ...
 tmpTris[0] = 0;
 tmpTris[1] = 1;
 tmpTris[2] = 2;
  
 tmpTris[3] = 0;
 tmpTris[4] = 2;
 tmpTris[5] = 3;
 ...

For every face you draw, you have to add 4 to the index from which you start the next time.

Comment
Add comment · Share
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

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

77 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

Related Questions

C# Proceducal Mesh terrain 2 Answers

How to generate a grid for procedurally generated platforms 1 Answer

What is wrong with this mesh editing code? 0 Answers

How do I go about texturing a flat-shaded generated mesh? 1 Answer

Trouble with Inaccurate Mesh Collider 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