Shader to bevel the edges of a cube?

╄→гoц情女王★ 提交于 2019-12-03 12:13:07

问题


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.

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

Thanks, shader gurus!


回答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.

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:

As per request added a comparison cube:

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.




回答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.



来源:https://stackoverflow.com/questions/25105571/shader-to-bevel-the-edges-of-a-cube

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!