Phong shading for brilliant 3D graphics Python

I am trying to create aesthetically pleasing 3D graphics in Python with a mirror shade, and have so far tried using Matplotlib with 3D axes and surface graphics from Mayavi, for example, from the Mayavi web surfing sample webpage:

enter image description here

The results look good, and in Mayavi it ​​seems that there is reasonable control over the lighting, although I seem to be unable to achieve a “brilliant” appearance.

In Matlab, this can be achieved using the "Phong" lighting:

enter image description here

see http://www.mathworks.com/matlabcentral/fileexchange/35240-matlab-plot-gallery-change-lighting-to-phong/content/html/Lighting_Phong.html

So my question is: how can I achieve this brilliant Phong-style shading in Python-based 3D graphics?

+6
source share
2 answers

As @ben mentioned, you can use Mayavi and then interactively change the lighting. A good idea is to click on the script button, then you can use these lines of code in your scripts (this is how I did for the Mayavi part here).

Another option is to use Matplotlib . Based on the example of shading , I managed to create a surface with a backlight.

See code below.

import numpy as np from mayavi import mlab import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib.colors import LightSource ## Test data: Matlab `peaks()` x, y = np.mgrid[-3:3:150j,-3:3:150j] z = 3*(1 - x)**2 * np.exp(-x**2 - (y + 1)**2) \ - 10*(x/5 - x**3 - y**5)*np.exp(-x**2 - y**2) \ - 1./3*np.exp(-(x + 1)**2 - y**2) ## Mayavi surf = mlab.surf(x, y, z, colormap='RdYlBu', warp_scale='auto') # Change the visualization parameters. surf.actor.property.interpolation = 'phong' surf.actor.property.specular = 0.1 surf.actor.property.specular_power = 5 ## Matplotlib fig = plt.figure() ax = fig.gca(projection='3d') # Create light source object. ls = LightSource(azdeg=0, altdeg=65) # Shade data, creating an rgb array. rgb = ls.shade(z, plt.cm.RdYlBu) surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, linewidth=0, antialiased=False, facecolors=rgb) plt.show() mlab.show() 

This gives as results:

  • MayaVi: enter image description here
  • Matplotlib: enter image description here
+7
source

Yes, you can do it in Mayavi. In the Mayavi window, click the small Mayavi icon in the upper left corner to display the advanced menu. Click on the surface in the scene that matches your surface, then click on the "Actor" tab in the menu on the right, scroll down to the field labeled "Property" and click "Advanced Options". You can set the hue to dark shading in the Interpolation field.

+2
source

Source: https://habr.com/ru/post/981751/


All Articles