• 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 DeeCeptor · Mar 26, 2020 at 02:08 PM · shadermesh3dshaders

Mesh shader to outline colour changes

Howdy. I've written a shader to change colour on a deformable mesh's Z position. This is to create the effect of topographic/contour maps where the colour changes based on the height of terrain of the mesh.

This is it viewed from the side: alt text

And viewed head-on (as it's meant to be: alt text

What I need is for a black outline between two colour levels. I have done this, but the problem is that the black outline is not pixel perfect (look at the black grouping near the bottom left of the image).

My current shader simply uses if statements to determine which colour level each part should be in, and if it's within a certain threshold BETWEEN two colour layers, make it black. Obviously this is wrong.

Here's my shader code:

 Shader "Unlit/MapEditorShader"
 {
     Properties {
     
     _Level8Color ("Level8Color", Color) = (0.8,0.9,0.9,1)   
     _Level8 ("Level8", Float) = 30
     _Level7Color ("Level7Color", Color) = (0.8,0.9,0.9,1)   
     _Level7 ("Level7", Float) = 30
     _Level6Color ("Level6Color", Color) = (0.8,0.9,0.9,1)   
     _Level6 ("Level6", Float) = 30
     _Level5Color ("Level5Color", Color) = (0.8,0.9,0.9,1)   
     _Level5 ("Level5", Float) = 30
     _Level4Color ("Level4Color", Color) = (0.8,0.9,0.9,1)   
     _Level4 ("Level4", Float) = 30
     _Level3Color ("Level3Color", Color) = (0.75,0.53,0,1)
     _Level3 ("Level3", Float) = 20
     _Level2Color ("Level2Color", Color) = (0.69,0.63,0.31,1)
     _Level2 ("Level2", Float) = 10
     _Level1Color ("Level1Color", Color) = (0.65,0.86,0.63,1)
     _Level1 ("Level1", Float) = 0
     _Level0Color ("Level0Color", Color) = (0.37,0.78,0.92,1)
     _OutlineColor ("OutlineColor", Color) = (0,0,0,1)
     _Slope ("Slope Fader", Range (0,1)) = 0
 
 }
 SubShader {
     Tags { "RenderType" = "Diffuse" }
     CGPROGRAM
     #pragma surface surf SimpleLight
     struct Input {
         float3 customColor;
         float3 worldPos;
     };
 
     half4 LightingSimpleLight (SurfaceOutput s, half3 lightDir, half atten) {
               half4 c;
               c.rgb = s.Albedo * 0.8;
               c.a = s.Alpha;
               return c;
           }
     
     float _Level8;
     float4 _Level8Color;
     float _Level7;
     float4 _Level7Color;
     float _Level6;
     float4 _Level6Color;
     float _Level5;
     float4 _Level5Color;
     float _Level4;
     float4 _Level4Color;
     float _Level3;
     float4 _Level3Color;
     float _Level2;
     float4 _Level2Color;
     float _Level1;
     float4 _Level1Color;
     float _Slope;
     float4 _Level0Color;
     float4 _OutlineColor;
 
     void surf (Input IN, inout SurfaceOutput o) {
     //at breakpoints we want a black line
         if((IN.worldPos.z > _Level8 - _Slope && IN.worldPos.z < _Level8 + _Slope)||
         (IN.worldPos.z > _Level7 - _Slope && IN.worldPos.z < _Level7 + _Slope)||
         (IN.worldPos.z > _Level6 - _Slope && IN.worldPos.z < _Level6 + _Slope)||
         (IN.worldPos.z > _Level5 - _Slope && IN.worldPos.z < _Level5 + _Slope)||
         (IN.worldPos.z > _Level4 - _Slope && IN.worldPos.z < _Level4 + _Slope)||
         (IN.worldPos.z > _Level3 - _Slope && IN.worldPos.z < _Level3 + _Slope)||
         (IN.worldPos.z > _Level2 - _Slope && IN.worldPos.z < _Level2 + _Slope)||
         (IN.worldPos.z > _Level1 - _Slope && IN.worldPos.z < _Level1 + _Slope))
             o.Albedo = _OutlineColor;
         else if (IN.worldPos.z > _Level8)
             o.Albedo = _Level8Color;
         else if (IN.worldPos.z > _Level7)
             o.Albedo = _Level7Color;
         else if (IN.worldPos.z > _Level6)
             o.Albedo = _Level6Color;
         else if (IN.worldPos.z > _Level5)
             o.Albedo = _Level5Color;
         else if (IN.worldPos.z > _Level4)
             o.Albedo = _Level4Color;
         else if (IN.worldPos.z > _Level3)
             o.Albedo = _Level3Color;
         else if (IN.worldPos.z > _Level2)
             o.Albedo = _Level2Color;
         else if (IN.worldPos.z > _Level1)
             o.Albedo = _Level1Color;
         else
             o.Albedo = _Level0Color;
 
     }
     ENDCG
 }
 Fallback "Opaque"
 }
 

Any suggestions would be greatly appreciated.

shader1.png (29.1 kB)
shader2.png (16.3 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 Namey5 · Mar 27, 2020 at 03:16 AM

Because your depth values don't come from a texture, you can scale the slope by its screen-space derivative to get uniform width across the line;

 float slope = fwidth (IN.worldPos.z) * _Slope;
 
 if((IN.worldPos.z > _Level8 - slope && IN.worldPos.z < _Level8 + slope)||
 (IN.worldPos.z > _Level7 - slope && IN.worldPos.z < _Level7 + slope)||
 (IN.worldPos.z > _Level6 - slope && IN.worldPos.z < _Level6 + slope)||
 (IN.worldPos.z > _Level5 - slope && IN.worldPos.z < _Level5 + slope)||
 (IN.worldPos.z > _Level4 - slope && IN.worldPos.z < _Level4 + slope)||
 (IN.worldPos.z > _Level3 - slope && IN.worldPos.z < _Level3 + slope)||
 (IN.worldPos.z > _Level2 - slope && IN.worldPos.z < _Level2 + slope)||
 (IN.worldPos.z > _Level1 - slope && IN.worldPos.z < _Level1 + slope))
     o.Albedo = _OutlineColor;

There are definitely cleaner ways to write what you are attempting, but if it works it works.

Comment
Add comment · Show 1 · 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 DeeCeptor · Mar 27, 2020 at 06:26 AM 0
Share

Wow, that just works. Fantastic! I'm definitely not very good with shaders, and I agree this could be done more efficiently, but hey, it works now.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

242 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

Related Questions

Mask a 3D Object using a 3D Volume 0 Answers

Outline flat mesh 1 Answer

Outlining simple objects without artifacts 0 Answers

Generate a large data structure using a compute shader 1 Answer

Nicely fade transparency with standard shader? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges