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 YoungGriff · Jun 14, 2017 at 04:11 PM · c#lightingshadowpolygonshading

How do I create a polygon that reflects light like the built in 3D objects? (e.g like the cube)

In the following link:

https://docs.unity3d.com/Manual/StandardShaderMaterialParameterNormalMap.html

there is an image partway down the the page that shows two 12-sided cylinders, one with smooth shading, one with flat shading. I have a script that procedurally generates a cylinder, and I want it to have flat shading, but as it stands, the cylinders shading is currently well off.

I have an attached an image, and I would like the way the prism reflects the light to the same as the way the cube reflects the light; specifically I would like to know: how to make a single side reflect the light in the same way throughout that side, and why (on the right hand side of the prism) are the light/shadows so irregular?

The code I used to generate the prism is the following, and the material I attached is the standard material that is also attached to the cube.

 public class TubeCreator : MonoBehaviour {

 public float tubeHeight;
 public int numberOfSides;

 // Use this for initialization
 void Start()
 {
     CreateObelisk();
 }

 public void CreateObelisk()
 {

     // Total vertices
     int totalVertices = numberOfSides * 2;

     int totalTriangles =  2 * numberOfSides;

     // Total indices
     int totalIndices = totalTriangles * 3;

     // Init vertexList and indexList
     ArrayList verticesList = new ArrayList();
     ArrayList indicesList = new ArrayList();

     // Create floats for our xyz coordinates
     float x = 0;
     float y = 0;
     float z = 0;

     float anglePerSide = 2 * Mathf.PI / numberOfSides;

     // Create column vertices

     for (int k = 0; k <= 1; k++)
     {
         for (int i = 0; i < numberOfSides; i++)
         {
             // Calculate X, Y, Z coordinates for startRadius.
             x = Mathf.Cos(i * anglePerSide);
             y = Mathf.Sin(i * anglePerSide);
             z = tubeHeight * k;

             verticesList.Add(new Vector3(x, z, y));
         }
     }

     // Loop through the segments
     for (int i = 0; i < numberOfSides - 1; i++)
     {
         // Draw the first triangle for column
         indicesList.Add(numberOfSides + i + 1);
         indicesList.Add(i + 1);
         indicesList.Add(i);

         // Finish the quad for column
         indicesList.Add(i);
         indicesList.Add(numberOfSides + i);
         indicesList.Add(numberOfSides + i + 1);
     }

     // Do final case independently
     // Draw the first triangle for column
     indicesList.Add(numberOfSides);
     indicesList.Add(0);
     indicesList.Add(numberOfSides - 1);

     // Finish the quad for column
     indicesList.Add(numberOfSides - 1);
     indicesList.Add(2 * numberOfSides - 1);
     indicesList.Add(numberOfSides);

     /*
     // Repeat for interior

     // Loop through the segments
     for (int i = 0; i < numberOfSides - 1; i++)
     {
         // Draw the first triangle for column
         indicesList.Add(numberOfSides + i + 1);
         indicesList.Add(i);
         indicesList.Add(i + 1);

         // Finish the quad for column
         indicesList.Add(i);
         indicesList.Add(numberOfSides + i + 1);
         indicesList.Add(numberOfSides + i);
     }

     // Do final case independently
     // Draw the first triangle for column
     indicesList.Add(numberOfSides);
     indicesList.Add(numberOfSides - 1);
     indicesList.Add(0);

     // Finish the quad for column
     indicesList.Add(numberOfSides - 1);
     indicesList.Add(numberOfSides);
     indicesList.Add(2 * numberOfSides - 1);
     */

     Mesh mesh = new Mesh();

     Vector3[] vertices = new Vector3[totalVertices];
     verticesList.CopyTo(vertices);
     int[] triangles = new int[totalIndices];
     indicesList.CopyTo(triangles);
     
     mesh.vertices = vertices;
     mesh.triangles = triangles;
     mesh.RecalculateNormals();
     mesh.RecalculateBounds();

     MeshFilter mFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;
     mFilter.mesh = mesh;
 }
 }





[1]: /storage/temp/95927-2017-06-14-15h50-58.png

2017-06-14-15h50-58.png (170.7 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
1
Best Answer

Answer by Bunny83 · Jun 14, 2017 at 05:09 PM

RecalculateNormals might not always give you the normals that you like / need. You should provide the normals yourself. In your case the normal for each vertex is almost the same as the local vertex position, just without the z value

  for (int k = 0; k <= 1; k++)
  {
      for (int i = 0; i < numberOfSides; i++)
      {
          // Calculate X, Y, Z coordinates for startRadius.
          x = Mathf.Cos(i * anglePerSide);
          y = Mathf.Sin(i * anglePerSide);
          z = tubeHeight * k;
          verticesList.Add(new Vector3(x, z, y));
          normalsList.Add(new Vector3(x, 0, y));
      }
  }

btw: the generic List class has a ToArray method. So there's no reason to create the array manually and copy the values over.

 mesh.vertices = verticesList.ToArray();

Alternatively you can use Mesh.SetVertices which you can directly pass your generic List. Same for SetNormals and SetIndices

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 YoungGriff · Jun 15, 2017 at 06:50 AM 0
Share

Thanks, this gives a great insight. So this gives the normals in the direction of each vertex, why not in the direction of each face? And should the number of normals be equal to the number of vertices or number of triangles? (I know they're the same in this case, but in general).

avatar image Bunny83 YoungGriff · Jun 15, 2017 at 09:35 PM 0
Share

A GPU has only informations per vertex. So it's impossible to define a normal for a "face" / triangle. If you want your object flat-shaded you would need to duplicate the vertices between two "faces" so you can specify seperate normals for each face. That's why a cube has 24 vertices and not 8. However if the surface should be smooth you want the vertex normals to specify a smooth normal at each vertex.

$$anonymous$$eep in $$anonymous$$d that when the GPU rasterizes a triangle it interpolates all vertex attributes (position, normal, color, uv, ...) across the surface based on the 3 corners. That's why when a surface should look flat, all 3 vertex normals have to be equal.

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

326 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to check if an object is lit by a light? 1 Answer

No shadow on terrain when built 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to use a sprite/mesh shader to light up behind it? 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