一:主要的知识点
1、说明
本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程------逐行解析官网所有Python示例-CSDN博客
2、知识点纪要
本段代码主要涉及的有①vtkRenderStepsPass的使用
二:代码及注释
python
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.vtkInteractionStyle
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkRenderingCore import vtkRenderer, vtkRenderWindow, vtkRenderWindowInteractor, vtkPolyDataMapper, vtkActor
from vtkmodules.vtkIOPLY import vtkPLYReader
from vtkmodules.vtkRenderingOpenGL2 import vtkRenderStepsPass, vtkSimpleMotionBlurPass
def main():
fileName = "Data/Armadillo.ply"
colors = vtkNamedColors()
colors.SetColor('A1Diff', [255, 204, 77, 255])
colors.SetColor('A2Amb', [51, 51, 255, 255])
colors.SetColor('A2Diff', [51, 255, 204, 255])
colors.SetColor('A3Amb', [128, 166, 255, 255])
colors.SetColor('Bkg', [77, 102, 153, 255])
renderer = vtkRenderer()
renderer.SetBackground(colors.GetColor3d("Bkg"))
renderWindow = vtkRenderWindow()
renderWindow.SetSize(500, 500)
renderWindow.SetWindowName('MotionBlur')
renderWindow.AddRenderer(renderer)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renderWindow)
reader = vtkPLYReader()
reader.SetFileName(fileName)
reader.Update()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(reader.GetOutputPort())
# 创建3个模型
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetAmbientColor(colors.GetColor3d('Red'))
actor.GetProperty().SetDiffuseColor(colors.GetColor3d('A1Diff'))
actor.GetProperty().SetSpecular(0.0)
actor.GetProperty().SetDiffuse(0.5)
actor.GetProperty().SetAmbient(0.3)
actor.SetPosition(-0.1, 0.0, -0.1)
renderer.AddActor(actor)
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetAmbientColor(colors.GetColor3d('A2Amb'))
actor.GetProperty().SetDiffuseColor(colors.GetColor3d('A2Diff'))
actor.GetProperty().SetSpecularColor(colors.GetColor3d('Black'))
actor.GetProperty().SetSpecular(0.2)
actor.GetProperty().SetDiffuse(0.9)
actor.GetProperty().SetAmbient(0.1)
actor.GetProperty().SetSpecularPower(10.0)
renderer.AddActor(actor)
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetDiffuseColor(colors.GetColor3d('A3Amb'))
actor.GetProperty().SetSpecularColor(colors.GetColor3d('White'))
actor.GetProperty().SetSpecular(0.7)
actor.GetProperty().SetDiffuse(0.4)
actor.GetProperty().SetSpecularPower(60.0)
actor.SetPosition(0.1, 0.0, 0.1)
renderer.AddActor(actor)
"""
SetMultiSamples(0) 禁用多重采样抗锯齿
SetMultiSamples 设置为大于1的值时(例如 4 或 8),
图形卡会为每个像素在多个位置采样颜色和深度信息,然后将这些样本混合起来,
从而产生平滑的边缘。
"""
renderWindow.SetMultiSamples(0)
"""
vtkRenderStepsPass 属于VTK的渲染管线的高级部分,它是一个渲染步骤通道(Render Pass)类,用于调试和分阶段执行渲染过程
在现代VTK中(尤其是8.X以后的版本),渲染过程被模块化为一系列 渲染步骤,比如
绘制几何体(geometry pass)
绘制阴影(shadow pass)
绘制透明度(translucent pass)
绘制体数据(volume pass)
绘制后期处理(post-processing pass)
这些步骤可以由一个"Pass管线"按顺序执行
其主要作用是:封装并依次执行 VTK 的标准渲染步骤,用于调试或自定义渲染流程
vtkRenderStepPass 内部通常会依次执行以下pass
顺序 渲染阶段(Pass) 说明
1 vtkSequencePass 渲染步骤序列管理器(内部容器)
2 vtkLightsPass 设置光照
3 vtkCameraPass 应用相机变换
4 vtkOpaquePass 绘制不透明物体
5 vtkTranslucentPass 绘制透明物体
6 vtkVolumetricPass 绘制体渲染对象(volume)
7 vtkOverlayPass 绘制叠加层(文本、UI)
"""
basicPasses = vtkRenderStepsPass()
"""
vtkSimpleMotionBlurPass 是一个 图像后期处理(post-processing)渲染 pass,用于在渲染结果中实现运动模糊(Motion Blur)效果
它在渲染完成后,对场景的多帧进行时间平均,从而模拟 相机快门导致的运动模糊效果
工作原理:"渲染多帧场景 → 累积图像 → 平均 → 产生模糊感"
渲染结构的关系:
vtkSimpleMotionBlurPass并不自己绘制几何体,它是一个"包裹"pass,必须包裹一个基础渲染pass。
例如:
vtkCameraPass
└── vtkSimpleMotionBlurPass
└── vtkRenderStepsPass
整个链条会按层次执行:
vtkRenderStepsPass → 绘制正常场景
vtkSimpleMotionBlurPass → 对渲染结果做累积与平均
vtkCameraPass → 管理相机和最终合成
"""
motion = vtkSimpleMotionBlurPass()
"""
SetDelegatePass 指定内部真正负责绘制场景的渲染pass
vtkSimpleMotionBlurPass的其余方法:
SetNumberOfFrames(number):设置渲染并累积的帧数(数值越大,模糊越平滑,但性能越低)
GetNumberOfFrames():获取帧数
Render():执行渲染与混合操作
ReleaseGraphicsResources():释放显存和缓冲区资源
"""
motion.SetDelegatePass(basicPasses)
"""
SetPass:
vtk中,渲染器(vtkRenderer)有两种工作模式:
1、默认渲染模式, 使用VTK内部默认流程,绘制几何体、光照、透明、叠加层等
2、自定义Render Pass模式 通过 renderer.SetPass() 手动设置一条自定义渲染管线,由你控制渲染的每个步骤
当调用 renderer.SetPass(some_pass) 时, 就告诉 VTK:"别用你默认的渲染方式,用我指定的渲染 pass 管线来绘制场景。"
"""
renderer.SetPass(motion)
numRenders = 30
renderer.GetActiveCamera().SetPosition(0, 0, -1)
renderer.GetActiveCamera().SetFocalPoint(0, 0, 0)
renderer.GetActiveCamera().SetViewUp(0, 1, 0)
renderer.ResetCamera()
renderer.GetActiveCamera().Azimuth(15.0)
renderer.GetActiveCamera().Zoom(1.2)
renderWindow.Render()
for i in range(0, numRenders):
renderer.GetActiveCamera().Azimuth(10.0 / numRenders)
renderer.GetActiveCamera().Elevation(10.0 / numRenders)
renderWindow.Render()
iren.Start()
if __name__ == '__main__':
main()