Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ExtremePowers · Sep 27, 2014 at 09:50 AM · low poly

Flat shading

Is it possible to obtain a flat shader in Unity to gain some results like these: alt text

Note: I would like to use this on my terrain and making it in a 3D program isn't an option.

Comment
Add comment · Show 5
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 VesuvianPrime · Sep 27, 2014 at 12:24 PM 0
Share

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.

avatar image robertbu · Sep 27, 2014 at 04:00 PM 1
Share

As an educated guess, I think you'll have to create your mesh without shared vertices to get what you want.

avatar image VesuvianPrime · Sep 27, 2014 at 04:48 PM 0
Share

I'm not sure that's entirely true Robertbu. The shader could completely disregard the vertex normals and just work off the face direction.

avatar image robertbu · Sep 27, 2014 at 07:31 PM 0
Share

@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?

avatar image tanoshimi · Sep 27, 2014 at 08:11 PM 0
Share

@robertbu No, it can't. Your answer is correct ;)

4 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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:

alt text


noshared.png (36.5 kB)
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 Sellars · Jun 01, 2016 at 05:14 PM 0
Share

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?

avatar image jaxx0rr · May 17, 2017 at 02:13 PM 0
Share

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

avatar image
2

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.

Comment
Add comment · 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
0

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

Comment
Add comment · Show 4 · 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 Eno-Khaon · Jul 10, 2016 at 09:20 PM 0
Share

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.

avatar image Bunny83 Eno-Khaon · Jul 11, 2016 at 01:01 AM 0
Share

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.

avatar image MakakWasTaken Bunny83 · Jul 11, 2016 at 08:47 AM 0
Share

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

Show more comments
avatar image
0

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/

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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