• 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
Question by better_walk_away · Dec 18, 2017 at 07:09 AM · lightingproceduralprocedural meshprocedural-generationnormal map

How to apply PBR material to Procedurally generated Mesh?

I created a box using the code below, and I apply a PBR material to it, but it looks flat, which means the normal map doesn't affect its lighting at all. Here is an image for comparison. Left one is the box created using Unity Editor, right one is the procedural box. How do I fix it?

         MeshFilter filter = GetComponent<MeshFilter>();
         Mesh mesh = filter.mesh;
         mesh.Clear();
 
         float length = 1f;
         float width = 1f;
         float height = 1f;
 
         #region Vertices
         Vector3 p0 = new Vector3(-length * .5f, -width * .5f, height * .5f);
         Vector3 p1 = new Vector3(length * .5f, -width * .5f, height * .5f);
         Vector3 p2 = new Vector3(length * .5f, -width * .5f, -height * .5f);
         Vector3 p3 = new Vector3(-length * .5f, -width * .5f, -height * .5f);
 
         Vector3 p4 = new Vector3(-length * .5f, width * .5f, height * .5f);
         Vector3 p5 = new Vector3(length * .5f, width * .5f, height * .5f);
         Vector3 p6 = new Vector3(length * .5f, width * .5f, -height * .5f);
         Vector3 p7 = new Vector3(-length * .5f, width * .5f, -height * .5f);
 
         Vector3[] vertices = new Vector3[]
         {
     // Bottom
     p0, p1, p2, p3,
  
     // Left
     p7, p4, p0, p3,
  
     // Front
     p4, p5, p1, p0,
  
     // Back
     p6, p7, p3, p2,
  
     // Right
     p5, p6, p2, p1,
  
     // Top
     p7, p6, p5, p4
         };
         #endregion
 
         #region Normales
         Vector3 up = Vector3.up;
         Vector3 down = Vector3.down;
         Vector3 front = Vector3.forward;
         Vector3 back = Vector3.back;
         Vector3 left = Vector3.left;
         Vector3 right = Vector3.right;
 
         Vector3[] normales = new Vector3[]
         {
     // Bottom
     down, down, down, down,
  
     // Left
     left, left, left, left,
  
     // Front
     front, front, front, front,
  
     // Back
     back, back, back, back,
  
     // Right
     right, right, right, right,
  
     // Top
     up, up, up, up
         };
         #endregion
 
         #region UVs
         Vector2 _00 = new Vector2(0f, 0f);
         Vector2 _10 = new Vector2(1f, 0f);
         Vector2 _01 = new Vector2(0f, 1f);
         Vector2 _11 = new Vector2(1f, 1f);
 
         Vector2[] uvs = new Vector2[]
         {
     // Bottom
     _11, _01, _00, _10,
  
     // Left
     _11, _01, _00, _10,
  
     // Front
     _11, _01, _00, _10,
  
     // Back
     _11, _01, _00, _10,
  
     // Right
     _11, _01, _00, _10,
  
     // Top
     _11, _01, _00, _10,
         };
         #endregion
 
         #region Triangles
         int[] triangles = new int[]
         {
     // Bottom
     3, 1, 0,
     3, 2, 1,            
  
     // Left
     3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
     3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
  
     // Front
     3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
     3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
  
     // Back
     3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
     3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
  
     // Right
     3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
     3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
  
     // Top
     3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
     3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
 
         };
         #endregion
 
         mesh.vertices = vertices;
         mesh.normals = normales;
         mesh.uv = uvs;
         mesh.triangles = triangles;
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();

alt text

comparison.png (350.2 kB)
Comment

People who like this

0 Show 0
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

Answer by better_walk_away · Dec 22, 2017 at 09:38 AM

Why no one is replying? :( I have figured it out, just use "mesh.RecalculateTangents()" and it fixes it.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

97 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

Related Questions

Procedurally generate 3D mesh from 2D image 1 Answer

UV problem on procedural mesh generation 1 Answer

C# Proceducal Mesh terrain 2 Answers

Procedural Mesh From Random List Of Veritces 2 Answers

Creating caves in voxel procedural mesh 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