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 JimmyCushnie · Nov 07, 2017 at 08:55 PM · meshmeshfilter

How come my code-created cuboids visually have no distinct edges?

Sorry for the bad title, the issue is hard to describe with words. Here are some pictures. The cuboid I created with code is the one with the grid on it. The white one next to it is a standard Unity primitive cube set to the same scale for comparison.

alt text

alt text

And here's the code I'm using to generate the custom mesh:

     public int x;
     public int z;
 
     public BoxCollider ThisCollider;
     public Renderer ThisRenderer;
 
     public void CreateCuboid()
     {
         Vector3[] vertices = {
             new Vector3 (0, -0.5f, 0) * 0.15f, // each element is *0.3 because that's what I've decided the global scale shall be, and then by 0.5 because idk it just works
             new Vector3 (2 * x, -0.5f, 0) * 0.15f,
             new Vector3 (2 * x, 0.5f, 0) * 0.15f,
             new Vector3 (0, 0.5f, 0) * 0.15f,
             new Vector3 (0, 0.5f, 2 * z) * 0.15f,
             new Vector3 (2 * x, 0.5f, 2 * z) * 0.15f,
             new Vector3 (2 * x, -0.5f, 2 * z) * 0.15f,
             new Vector3 (0, -0.5f, 2 * z) * 0.15f,
         };
 
         int[] triangles = {
             0, 2, 1, //face front
             0, 3, 2,
             2, 3, 4, //face top
             2, 4, 5,
             1, 2, 5, //face right
             1, 5, 6,
             0, 7, 4, //face left
             0, 4, 3,
             5, 4, 7, //face back
             5, 7, 6,
             0, 6, 7, //face bottom
             0, 1, 6
         };
 
         Mesh mesh = GetComponent<MeshFilter>().mesh;
         mesh.Clear();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
 
         // I suspect this section is why the shading is borked. I don't actually know what UVs are or how to use them, this code is straight up copied from the Unity manual
         Vector2[] uvs = new Vector2[vertices.Length];
         for (int i = 0; i < uvs.Length; i++)
         {
             uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
         }
         mesh.uv = uvs;
 
         mesh.RecalculateNormals();
     }


Can someone help me make my cuboids look normal?

screen-1920x1080-2017-11-07-15-38-13.jpg (478.3 kB)
screen-1920x1080-2017-11-07-15-38-56.jpg (455.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ElijahShadbolt · Nov 07, 2017 at 11:00 PM

The problem is with Normals. Unity's primitive cube has 24 vertices: 4 vertices for each of the 6 faces of a cube (3 vertices for each cube corner). The two triangles of each face use their own face's vertices. The normals for each face (one normal per vertex) are set to the same direction: perpendicular to the face.

What you've done is create a box with 8 vertices: one for each of the 8 corners of a box. Then you re-used those vertices for the triangles on different faces of the box. Then when you call mesh.RecalculateNormals() it thinks each vertex is on a smooth face, so each normal points away from the centre of the box, not perpendicular to any one face.

Note: You can manually set vertex normals by creating a mesh.normals array after creating the mesh.vertices array.

Comment
Add comment · Show 2 · 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
avatar image Bunny83 · Nov 07, 2017 at 11:29 PM 1
Share

Right +1.

Though it's not only the normals but also the UV coordinates for each face. All vertex attributes are specitied per vertex. If any of those attributes should be different for two or more faces at the same vertex you have to split the vertex into seperate vertices. So they have the same "position" attribute but may have different normals / tangents, UV coordinates, vertex colors.


$$anonymous$$any 3d artists don't understand this as they only work with "logical vertices" in modelling tools. However this is how the GPU actually processes the mesh data. $$anonymous$$odelling tools are ment to simplify the editing. It would be a pain to drag 3 seperate vertices around

edit


Over here i have some compact way to create a "cuboid". Well i actually created a mesh for the camera frustum. You can just ignore the first for loop which does the projection in camera space. I also specify 8 vertices which are then split into 24 by code.

avatar image JimmyCushnie · Nov 08, 2017 at 02:57 AM 1
Share

Thank you so much, it works!!

alt text

I fixed it by replacing the vertexes and triangles arrays with the following:

         Vector3[] vertices = {
             // set 1
             new Vector3 (0, -0.5f, 0) * 0.15f, // each element is *0.3 because that's what I've decided the global scale shall be, and then by 0.5 because idk it just works
             new Vector3 (2 * x, -0.5f, 0) * 0.15f,
             new Vector3 (2 * x, 0.5f, 0) * 0.15f,
             new Vector3 (0, 0.5f, 0) * 0.15f,
             new Vector3 (0, 0.5f, 2 * z) * 0.15f,
             new Vector3 (2 * x, 0.5f, 2 * z) * 0.15f,
             new Vector3 (2 * x, -0.5f, 2 * z) * 0.15f,
             new Vector3 (0, -0.5f, 2 * z) * 0.15f,
 
             // set 2
             new Vector3 (0, -0.5f, 0) * 0.15f,
             new Vector3 (2 * x, -0.5f, 0) * 0.15f,
             new Vector3 (2 * x, 0.5f, 0) * 0.15f,
             new Vector3 (0, 0.5f, 0) * 0.15f,
             new Vector3 (0, 0.5f, 2 * z) * 0.15f,
             new Vector3 (2 * x, 0.5f, 2 * z) * 0.15f,
             new Vector3 (2 * x, -0.5f, 2 * z) * 0.15f,
             new Vector3 (0, -0.5f, 2 * z) * 0.15f,
 
             // set 3
             new Vector3 (0, -0.5f, 0) * 0.15f,
             new Vector3 (2 * x, -0.5f, 0) * 0.15f,
             new Vector3 (2 * x, 0.5f, 0) * 0.15f,
             new Vector3 (0, 0.5f, 0) * 0.15f,
             new Vector3 (0, 0.5f, 2 * z) * 0.15f,
             new Vector3 (2 * x, 0.5f, 2 * z) * 0.15f,
             new Vector3 (2 * x, -0.5f, 2 * z) * 0.15f,
             new Vector3 (0, -0.5f, 2 * z) * 0.15f,
         };
 
         int[] triangles = {
             // using set 1
             0, 2, 1, //face front
             0, 3, 2,
             5, 4, 7, //face back
             5, 7, 6,
 
             // using set 2
             10, 11, 12, //face top
             10, 12, 13,
             8, 14, 15, //face bottom
             8, 9, 14,
 
             // using set 3
             17, 18, 21, //face right
             17, 21, 22,
             16, 23, 20, //face left
             16, 20, 19,
         };

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

81 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

Related Questions

Replace MeshFilter mesh by a other mesh in Editor 1 Answer

Multiple MeshFilters for a GameObject 0 Answers

Unity5 Procedural meshing slower than in Unity4? 1 Answer

Mirror vertices procedurally 2 Answers

Combine meshes? 1 Answer


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