-
-
Save cleemesser/0a92d8868dbefb47aac87eb8b7c3307e to your computer and use it in GitHub Desktop.
Physically Based Rendering (PBR) property for vtkPolyDataMapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import vtk | |
def sphere(center, radius=1.0, color='Gray', metallic=0.5, roughness=1.0): | |
colors = vtk.vtkNamedColors() | |
cx, cy, cz = center | |
# Create a sphere | |
sphereSource = vtk.vtkSphereSource() | |
sphereSource.SetCenter(cx, cy, cz) | |
sphereSource.SetRadius(radius) | |
# Make the surface smooth. | |
sphereSource.SetPhiResolution(100) | |
sphereSource.SetThetaResolution(100) | |
mapper = vtk.vtkPolyDataMapper() | |
mapper.SetInputConnection(sphereSource.GetOutputPort()) | |
actor = vtk.vtkActor() | |
actor.SetMapper(mapper) | |
actor.GetProperty().SetInterpolationToPBR() | |
actor.GetProperty().SetMetallic(metallic) | |
actor.GetProperty().SetRoughness(roughness) | |
actor.GetProperty().SetColor(colors.GetColor3d(color)) | |
return actor | |
def main(): | |
renderer = vtk.vtkRenderer() | |
renderWindow = vtk.vtkRenderWindow() | |
renderWindow.SetWindowName("Sphere") | |
renderWindow.AddRenderer(renderer) | |
renderWindowInteractor = vtk.vtkRenderWindowInteractor() | |
renderWindowInteractor.SetRenderWindow(renderWindow) | |
colors = ['Red', 'Blue', 'Green', 'Silver', 'Gold'] | |
for i in range(8): | |
for j, color in zip(range(5), colors): | |
s = sphere( | |
center=[i, j, 0], | |
radius=0.5, | |
color=color, | |
metallic=j/4., | |
roughness=i/7. | |
) | |
renderer.AddActor(s) | |
renderer.SetBackground([0.5, 0.5, 0.5]) | |
renderWindow.Render() | |
renderWindowInteractor.Start() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment