- Home /
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
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
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).
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
![](https://koobas.hobune.stream/wayback/20220612124154im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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