• 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
6
Question by viesc123 · Jul 07, 2019 at 01:38 PM · shader

Blender-like cavity shader for Unity?

I just came across the cavity shader from Blender, that works like Ambient Occlusion in that it creates a shadow in convex/narrow parts of the geometry, but also highlights concave geometry/edges. Has anybody come across something similar for Unity? I assume it would have to be done as a Post Effect, or could it be implemented as a shader too?

Here's a link to a screenshot (For some reason, I can't upload directly to this forum; keep getting an error message): https://imgur.com/a/ujQemCA

Thank you!

Comment
Add comment · Show 4
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 aoun111 · Jul 08, 2019 at 08:52 AM 0
Share

For the shadows, use the ambient occlusion post processing effect, but for the convex effect, i think you might need a shader, or in blender open your model and smoothen it.

avatar image create3dgames · Apr 12, 2020 at 10:57 PM 0
Share

Theoretically both convex and concave edges could be shadowed/highlighted using a shader only, no post processing. Not sure how exactly to do this though, I don't know if it's possible in Shader Graph. But if you were to take an edge and calculate the angle between it and it's adjacent faces, you could deter$$anonymous$$e convexity/concavity.

avatar image A_KNUTy · Jun 03, 2020 at 01:37 PM 0
Share

Hi, Does anyone has a solution for that ? I have the same problem... Thanks

avatar image Adam_Benko · Jun 05, 2020 at 07:19 AM 0
Share

So, have you found any solution ?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by chor64 · Jun 03, 2020 at 10:25 PM

You could use a ambient occlusion image effect, combined with a shader. To make the shader, you should use the built-in unity wireframe shader as a guide: https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/DefaultResourcesExtra/VR/Shaders/SpatialMappingWireframe.shader Or optionally, you could just Google a similar 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
avatar image
1

Answer by BastianUrbach · Jun 07, 2020 at 10:10 AM

In theory, normal derivatives can be used to calculate curvature on smooth meshes. Here is a minimal shader that does this:

 Shader "Unlit/Cavity" {
     Properties {
         _Intensity ("Intensity", Float) = 0.01
     }
 
     SubShader {
         Tags { "RenderType"="Opaque" }
 
         Pass {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             float _Intensity;
 
             struct v2f {
                 float4 vertex : SV_POSITION;
                 float3 normal : NORMAL;
                 float3 worldPos : TEXCOORD1;
             };
 
             v2f vert (float4 vertex : POSITION, float3 normal : NORMAL) {
                 v2f o;
 
                 o.vertex = UnityObjectToClipPos(vertex);
                 o.normal = UnityObjectToWorldNormal(normal);
                 o.worldPos = mul(unity_ObjectToWorld, vertex);
                 
                 return o;
             }
 
             float3 curvature(float3 dNormal, float3 dWorld) {
                 return dot(dNormal, dWorld / dot(dWorld, dWorld)) * _Intensity;
             }
 
             fixed4 frag (v2f i) : SV_Target {
                 float cx = curvature(ddx(i.normal), ddx(i.worldPos));
                 float cy = curvature(ddy(i.normal), ddy(i.worldPos));
 
                 return cx + cy + 0.5;
             }
             ENDCG
         }
     }
 }

Image demonstrating the shader
However, this technique has a few problems, most importantly it always gives the same value for an entire face (like flat shading). This could be solved with "better than linearly interpolated" normals if you can somehow produce them.


unitycavity.png (133.0 kB)
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 sefaenes · Oct 04, 2021 at 04:13 PM 0
Share

@BastianUrbach I'm pretty bad at shader program$$anonymous$$g, can you help me program a simple shader? I want to make my lowpoly-cartoon models more filled using this cavity shader but I don't know how to add the texture property. also, do these shaders work as optimized as Mobile/Diffuse? (static-dynamic batching etc)

I wonder what it would be like to combine a cavity shader with a matcap shader?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

182 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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Fading out a square 1 Answer

Access Terrain Base shader properties 0 Answers

Shader compilation fails for Wii-U 2 Answers

Unity 3 Problem: Legacy Lightmap Shaders don't seem to work. 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