Shader to bevel the edges of a cube?

10,142

Solution 1

I recently finished creating such shader. The only way it can work is by providing 4 normal vectors instead of one for each vertex (smooth, sharp and one for each edge of the triangle for the given vertex). You will also need one float3 to detect edges.

To add such data in a mesh I made a custom mesh editor, comes with Playtime Painter Asset from Unity Asset Store. Will post the shader with the next update. Also will post to public GitHub.

Sharp Edge Smooth Edge

You can see some dark lines, it's because it starts to interpolate to a normal vector which facing away from light source, but since there are no additional triangles, the result is visible on a triangle which is facing the camera.

Update (2/12/2018) Realised that by clipping pixels that end up having a normal facing away from the camera, it is possible to smooth the outline shape. It wasn't tested for all possible scenarios but works great for simple shapes: Clipped edges

As per request added a comparison cube: Cubes with Bump Mapping

Currently, Playtime Painter has a simplified version of that shader, which interpolates between 2 normal vectors and gives ok results on some edges.

Wrote an article.

Solution 2

In general the Relief Mapping is able to modify the object silhouette like on this picture. You'd need to prepare a heightmap that lowers at the borders and that's it. However I think that using such shader might be an overkill for such a simple effect so maybe it's better to just make it in your geometry.

Share:
10,142
Fattie
Author by

Fattie

Updated on June 04, 2022

Comments

  • Fattie
    Fattie almost 2 years

    This question relates to using shaders (probably in the Unity3D milieu, but Metal or OpenGL is fine), to achieve rounded edges on a mesh-minimal cube.

    enter image description here

    • I wish to use only 12-triangle minimalist mesh cubes,

    • and then via the shader,

    • Achieve the edges (/corners) of each block being slightly bevelled.

    In fact, can this be done with a shader?

  • Fattie
    Fattie almost 10 years
    Hi Kolenda, I appreciate it could possibly be done using a bump map. (On unity3d, docs.unity3d.com/Manual/HOWTO-bumpmap.html ) I was more curious if this can be done by a shader. Cheers
  • kolenda
    kolenda almost 10 years
    Please note that Bumpmapping doesn't change the object shape, just the light intensity, so you can't make the edges to be really rounded this way.
  • Fattie
    Fattie almost 10 years
    HI Kolenda. Quite right, indeed you've helped me realise there are a couple if relief shaders on Unity (for example assetstore.unity3d.com/en/#!/content/5705) (However, I"m not sure if there are any that will run on mobile but I'll check it out.) I guess you're quite right Kolenda, I guess the answer to my question is "some sort of relief shader". Good one.
  • kolenda
    kolenda almost 10 years
    I'm glad I could help.
  • Iurii Selinnyi
    Iurii Selinnyi over 6 years
    Added the cubes screenshot.
  • Iurii Selinnyi
    Iurii Selinnyi over 6 years
    True, I am new. Used SO a bit long time ago. Also, no idea what is that Bounty)
  • Iurii Selinnyi
    Iurii Selinnyi over 6 years
    Ou, that is very nice)
  • Iurii Selinnyi
    Iurii Selinnyi over 6 years
    @Fattie Added Bump Mapping and added that image you requested with 2 cubes.