{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Types of Shading {#shading_example}\n\nComparison of default, flat shading vs. smooth shading.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista\nfrom pyvista import examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "PyVista supports two types of shading: flat and smooth shading that uses\nVTK\\'s Phong shading algorithm.\n\nThis is a plot with the default flat shading.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.load_nut()\nmesh.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here\\'s the same sphere with smooth shading.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh.plot(smooth_shading=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note how smooth shading makes edges that should be sharp look odd, it\\'s\nbecause the points of these normals are averaged between two faces that\nhave a sharp angle between them. You can avoid this by enabling\n`split_sharp_edges`.\n\n::: note\n::: title\nNote\n:::\n\nYou can configure the splitting angle with the optional `feature_angle`\nkeyword argument.\n:::\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh.plot(smooth_shading=True, split_sharp_edges=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can even plot the edges that will be split using\n`extract_feature_edges <pyvista.DataSetFilters.extract_feature_edges>`{.interpreted-text\nrole=\"func\"}.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# extract the feature edges exceeding 30 degrees\nedges = mesh.extract_feature_edges(\n boundary_edges=False,\n non_manifold_edges=False,\n feature_angle=30,\n manifold_edges=False,\n)\n\n# plot both the edges and the smoothed mesh\npl = pyvista.Plotter()\npl.enable_anti_aliasing()\npl.add_mesh(mesh, smooth_shading=True, split_sharp_edges=True)\npl.add_mesh(edges, color='k', line_width=5)\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `split_sharp_edges` keyword argument is compatible with physically\nbased rendering as well.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# plot both the edges and the smoothed mesh\n\n\npl = pyvista.Plotter()\npl.enable_anti_aliasing()\npl.add_mesh(mesh, color='w', split_sharp_edges=True, pbr=True, metallic=1.0, roughness=0.5)\npl.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }