Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

c# - Unsmooth generated mesh in unity

I'm trying to make a low poly terrain in unity, but for some weird reason it keeps smoothing it out. I tried removing Recalculate Normals, but that did not seem to help (also it wrecked the lighting). Please don't post any links to assets on the assets store.

Also using unity 2020.2.1f1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The idea behind low poly mesh is to duplicate vertices to achieve sharp edges. When we normally create a mesh, it has N vertices. And when you recalculate normals, Unity generates exactly N normals. That means that for each angle in your mesh there is only one normal vector, which 3D engines try to smooth. So supposing a simplificated 2D example, it looks something like this:

Smooth edge

Your goal instead is to duplicate vertex for each triangle, that this vertex belongs, so that each edge would be disjoint, and that would cause Unity to generate separate normals for each edge and sharp the angle. Like this:

Sharp edge

Unfortunately, there is no way to create multiple normals for one vertex, that's because you'd have to duplicate them. So for example you want to create a mesh with two triangles with a sharp edge. In code it would be something similar to this:

var mesh = new Mesh();

// Basically amount of your vertices is equal to amount of indices.
var vertices = new Vector3[6];
var triangles = new int[] { 0, 1, 2, 3, 4, 5 };

// Define first triangle.
vertices[0] = new Vector3(1, 0, 0);
vertices[1] = new Vector3(0, -1, 0);
vertices[2] = new Vector3(0, 0, 0);

// Define second triangle
vertices[3] = vertices[2];    // Duplicating vertex #2.
vertices[4] = vertices[1];    // Duplicating vertex #1.
vertices[5] = new Vector3(-1, 0, 1);

mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();

Also check this article, it describes nicely behaviour of shading in 3D engines depending on shared and separated vertices: Tutorial 9: VBO Indexing

Update: As mentioned in the comments you can also try to use Flat shading model. But as far as I know, it might be not supported on some platforms.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...