- Home /
Flat shading
Is it possible to obtain a flat shader in Unity to gain some results like these:
Note: I would like to use this on my terrain and making it in a 3D program isn't an option.
I don't pretend to know how to write shaders, but I figure you could download the source for the built-in shaders here, take a shader you like the look of, and use face normals ins$$anonymous$$d of vertex normals.
As an educated guess, I think you'll have to create your mesh without shared vertices to get what you want.
I'm not sure that's entirely true Robertbu. The shader could completely disregard the vertex normals and just work off the face direction.
@VesuvianPrime - I've spent very little time with shaders, but I don't see how you can get the normal of the face. Can the shader get the three vertices of the current triangle being processed?
Answer by robertbu · Sep 27, 2014 at 07:27 PM
Here is an editor script that takes any game objects and produces a clone game object where the mesh does not share vertices. Create a C# script in the Assets/Editor folder named NoSharedVertices. Insert the following script. Then select the object you want to process and select 'No Shared Vertices' from the Window menu:
using UnityEngine;
using UnityEditor;
public class NoSharedVertices : EditorWindow {
private string error = "";
[MenuItem("Window/No Shared Vertices")]
public static void ShowWindow() {
EditorWindow.GetWindow(typeof(NoSharedVertices));
}
void OnGUI() {
//Transform curr = Selection.activeTransform;
GUILayout.Label ("Creates a clone of the game object where the triangles\n" +
"do not share vertices");
GUILayout.Space(20);
if (GUILayout.Button ("Process")) {
error = "";
NoShared();
}
GUILayout.Space(20);
GUILayout.Label(error);
}
void NoShared() {
Transform curr = Selection.activeTransform;
if (curr == null) {
error = "No appropriate object selected.";
Debug.Log (error);
return;
}
MeshFilter mf;
mf = curr.GetComponent<MeshFilter>();
if (mf == null || mf.sharedMesh == null) {
error = "No mesh on the selected object";
Debug.Log (error);
return;
}
// Create the duplicate game object
GameObject go = Instantiate (curr.gameObject) as GameObject;
mf = go.GetComponent<MeshFilter>();
Mesh mesh = Instantiate (mf.sharedMesh) as Mesh;
mf.sharedMesh = mesh;
Selection.activeObject = go.transform;
//Process the triangles
Vector3[] oldVerts = mesh.vertices;
int[] triangles = mesh.triangles;
Vector3[] vertices = new Vector3[triangles.Length];
for (int i = 0; i < triangles.Length; i++) {
vertices[i] = oldVerts[triangles[i]];
triangles[i] = i;
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
// Save a copy to disk
string name = "Assets/Editor/"+go.name+Random.Range (0, int.MaxValue).ToString()+".asset";
AssetDatabase.CreateAsset(mf.sharedMesh, name);
AssetDatabase.SaveAssets();
}
}
Before and after using the built-in sphere and a directional light:
Is there anyway to use this script on unity's terrain meshes? When I try it tells me that there is no mesh found, so I guess my real question is how to turn a terrain made with unity's terrain tools in a useable mesh?
after two days of trying shaders and flipping normals I finally tried this and it works on my dynamically generated mesh. and its LOO$$anonymous$$S FANTASTIC! thank you
Answer by wrstscrnnm6 · Sep 27, 2014 at 04:29 PM
The look you are talking about is not just a shader trick. You actually have to create/process the mesh properly to get that look.
I would check out Polyworld: http://forum.unity3d.com/threads/polyworld-for-unity.239365/ It has a built in mesh processing tool to get that sort of look out of your exiting meshes and you can process terrain with it also.
Answer by MakakWasTaken · Jul 10, 2016 at 09:09 PM
First of all I know this post is old, but I might have found a better solution.
It is possible to do this with a shader, which will give (usually) better framerates and will also make it possible to create bigger models (Vertex limit: 65000)
Big credits to whomever wrote this: http://www.battlemaze.com/?p=153 as it is just a simplified version without the movement.
Shader: http://pastebin.com/zyd5QMzZ
Because the shader in your link uses a geometry shader, this makes it less-universally supported than the accepted answer. If geometry shaders aren't supported by the hardware, then the C# script would be necessary ins$$anonymous$$d.
Actually there is a way to get flatshading without a geometry shader and without modifying the mesh in a script. You can use the "partial derivative functions" inside the fragment shader. It allows you to get two tangent vectors of the surface from which you can calculate a normal vector. I explained that over here.
I can't quite get this to work, this is my current result. Which just makes it look like a 2d model at best: http://pastebin.com/raw/CLkCtC3N
Answer by Georodin · Jul 04, 2021 at 01:18 PM
Hey guys this is a old post but here is a great blog posting that I found. Im using for my current project the HDRP and this little shader works like a charm. https://hextantstudios.com/unity-flat-low-poly-shader/
Your answer
Follow this Question
Related Questions
Flat Shader 1 Answer
Stutter/Lag while animating 2 Answers
Procedural "Faceted" Terrain 1 Answer
low poly technical 1 Answer
Procedural Low Poly Terrain 1 Answer