Curvature shader

Maya API: Curvature shader

Are you one of the people who’d kill for a curvature shader for Maya? Good news, you don’t have to!


Although polygons are no match for mathematically defined NURBS surfaces when it comes to serious design surfacing, in sketch modelling and concept development they’re killing it. To get the best speed to surface quality ratio when dealing with polygons I created this curvature shader for Maya. It is an ultimate tool for highlighting lumps and imperfections in your model as well as achieving better control over surface transitions and radii.

If you found this article while searching for “Maya curvature shader”, there’s quite a big chance you already found HW shader by Byron or Mentalray shader by Tom Cowland. Originally I though I’d do just fine with Byron’s shader, but I ended up with completely new plugin when trying to improve performance and display quality.

Computing vertex curvature

The both previously mentioned shaders are using very common method of calculating mesh curvature by measuring angle between face normal and vertex normal. The angle is then divided by perimeter of the face which results in higher curvature values in areas with increased density such as radii. Also thanks to this step the displayed curvature stays the same regardless of subdivision level of the mesh.

Vertex normal to face normal method
Vertex-to-face normal method

Since this method measures curvature per-vertex-per-face, with the same vertex the value differs for each adjacent face. This causes somewhat jagged appearance, which doesn’t improve even with increased subdivision levels. The easiest way to fix this problem is to average values of each vertex.

Here’s another problem. The angle between face normal and vertex normal ranges from 0° to 90° without any indication whether we’re dealing with a positive or a negative value. Luckily we can get this information by comparing orientation of angle plane normal and a reference plane normal.

Curvature shader based on vertex-to-face normal angle has one major weakness which shows in areas where for example a tight positive radius crosses a larger negative radius. This issue was the main reason I came up with algorithm which uses adjacent edge instead of face normal.

Vertex normal to edge method
Vertex-to-edge method

This method beats the previous one in several ways. The angle between vertex normal and adjacent edge falls between 0° and 180°, where value < 90° indicates negative curvature while value > 90° is a sign of positive curvature. It also handles better the previously mentioned scenario of two radii with different size and orientation since the average curvature is weighted by individual edge lengths.

Comparison of different vertex curvature methods
left: vertex-to-face method, middle: vertex-to-face average, right: vertex-to-edge average

Preparation

Although MPxHwShaderNode::geometry() method provides points and vectors of the rendered mesh in function parameters, it doesn’t give us any information about connections between vertices, which is crucial for this plugin. Fortunately we can get DAG path of the rendered mesh with MPxHwShaderNode::currentPath() which we’re going to call inside MPxHwShaderNode::bind().

Before calculating the surface curvature we have to generate a smooth mesh according to its smooth display options.

Implementation

Once we have the smooth mesh, the easiest way to calculate curvature values is using MItMeshVertex to query vertex position, normal, and connected points to define adjacent edges.

The rest is simple vector math. Using current vertex position and a connected point we construct a vector for each adjacent edge and calculate an angle to vertex normal. Since the angle is in 0-π range we need to offset it so that π/2 (zero curvature) becomes 0 (to achieve symmetric scaling for positive and negative values). After applying edge length and custom scale we calculate an average vertex curvature which needs to be offset back to get correct values from MRampAttribute.

Rendering

With previous code we got colors, positions and normals of all mesh vertices which we can now use to draw triangulated mesh with OpenGL

Conclusion

This article covers the most important part of making a mesh curvature shader such as quering mesh properties, calculating vertex curvature and OpenGL rendering. Since this plugin is computing values for thousands of vertices I used some additional steps to improve performance, which you can check in source files.

The shader only works with Legacy viewport with texturing enabled.

Download

4 Comments

  1. Malom

    How to install this tool?

    • Stepan Jirka (Author)

      Hi Malom,

      to be completely honest this version is more for developers as it is not quite complete. I’m planning a release of a proper version soon. Stay tuned.

  2. Andrew Bradbury

    thanks so much saved alot of head scratching and will hopefully save me time exporting to another software for minimum radius checking.