Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

PointsPolygon mixing smooth and flat shading

Help
2009-04-27
2013-04-25
  • Richard Layman
    Richard Layman
    2009-04-27

    I'm adding support for smooth and flat shading in my blender plugin but have hit a snag. Basically, I want to be able to mix smooth and flat shading on a single polygon. Let's say that I have a cube and all the faces are smooth shaded except for one. I would like to do this as a single mesh and I know there must be a way, but I can't figure it out. I got it working, kind of, by making a copy of the mesh and making the faces transparent where there is flat shading... something like this:

    AttributeBegin
    Surface "plastic"
    # smooth shaded cube
    PointsPolygon [ 4 4 4 4 4 4] [ 0 1 2 3 4 7 6 5 0 4 5 1 1 5 6 2 2 6 7 3 4 0 3 7] "P"[ 1.0 1.0 -0.999999940395 1.0 -0.999999940395 -1.0 -1.00000011921 -0.999999761581 -1.0 -0.999999642372 1.00000035763 -0.999999940395 1.00000047684 0.999999403954 1.0 0.999999344349 -1.00000059605 0.999999940395 -1.00000035763 -0.999999701977 0.999999940395 -0.999999940395 0.999999940395 1.0] "N" [ 0.577349185944 0.577349185944 -0.577349185944 0.577349185944 -0.577349185944 -0.577349185944 -0.577349185944 -0.577349185944 -0.577349185944 -0.577349185944 0.577349185944 -0.577349185944 0.577349185944 0.577349185944 0.577349185944 0.577349185944 -0.577349185944 0.577349185944 -0.577349185944 -0.577349185944 0.577349185944 -0.577349185944 0.577349185944 0.577349185944] "uniform normal Os"[ 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1]

    # flat shaded cube
    PointsPolygon [ 4 4 4 4 4 4] [ 0 1 2 3 4 7 6 5 0 4 5 1 1 5 6 2 2 6 7 3 4 0 3 7] "P"[ 1.0 1.0 -0.999999940395 1.0 -0.999999940395 -1.0 -1.00000011921 -0.999999761581 -1.0 -0.999999642372 1.00000035763 -0.999999940395 1.00000047684 0.999999403954 1.0 0.999999344349 -1.00000059605 0.999999940395 -1.00000035763 -0.999999701977 0.999999940395 -0.999999940395 0.999999940395 1.0] "uniform normal N" [ 0.000000 0.000000 -1.000000 -0.000000 -0.000000 1.000000 1.000000 -0.000000 0.000000 -0.000000 -1.000000 -0.000000 -1.000000 0.000000 -0.000000 0.000000 1.000000 0.000000] "uniform normal Os"[ 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0]
    AttributeEnd

    This will create a smooth shaded cube with one flat side.

    The problem with this method is that it seems to rain havoc on shaders and it's just doesn't seem like the right way of doing this.

    Any idea?

     
    • Moritz Moeller
      Moritz Moeller
      2009-04-27

      You need to switch "N" to the "facevarying" class. The default is "vertex normal N" (one normal per vertex).

      But in your case you want "facevarying normal N" (one normal per vertex per face).
      If a vertex was e.g. shared by 3 polygons, you'd need to specify the normal thrice for that vertex. And it could point in different directions, one for each face.

      Check the RI Spec for more on the "facevarying" class and how it applied to RiPointsPolygons().

      Beers,

      Moritz

       
    • Richard Layman
      Richard Layman
      2009-04-27

      Thanks for explaining that to me Moritz.  When I was looking at the spec they mentioned constant, uniform, varying, and vertex, but I didn't see a facevarying option. I'll give it a shot and see what happens.

       
    • Richard Layman
      Richard Layman
      2009-04-27

      Yep, that fixed it. I also used it to split up the uv's to separate faces and it helps that I don't have to always use creases on a SubdivisionMesh to get a flat shade on certain parts fo the mesh.

      Thanks a lot!