BillboardTextActor3D 3D字体随镜头旋转

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程------逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①vtk中几种txt的对比,②vtkBillboardTextActor3D的简介

二:代码及注释

python 复制代码
#!/usr/bin/env python3

# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingFreeType
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
# noinspection PyUnresolvedReferences
from vtkmodules.vtkCommonCore import vtkCommand
from vtkmodules.vtkCommonCore import (
    vtkMinimalStandardRandomSequence
)
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkBillboardTextActor3D,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)


def main():
    colors = vtkNamedColors()

    rng = vtkMinimalStandardRandomSequence()
    rng.SetSeed(5127)

    renderer = vtkRenderer()
    renderer.SetBackground(colors.GetColor3d('DarkSlateGray'))

    render_window = vtkRenderWindow()
    render_window.AddRenderer(renderer)
    render_window.SetWindowName('BillboardTextActor3D')

    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(render_window)

    sphere_source = vtkSphereSource()
    sphere_source.SetCenter(0.0, 0.0, 0.0)
    sphere_source.SetRadius(1.0)

    min_r = -10.0
    max_r = 10.0

    for i in range(0, 10):
        if i == 0:
            mapper = vtkPolyDataMapper()
            mapper.SetInputConnection(sphere_source.GetOutputPort())

            actor = vtkActor()
            actor.SetMapper(mapper)
            actor.SetPosition(0, 0, 0)
            actor.GetProperty().SetColor(colors.GetColor3d('Peacock'))

            renderer.AddActor(actor)

        mapper = vtkPolyDataMapper()
        mapper.SetInputConnection(sphere_source.GetOutputPort())


        actor = vtkActor()
        actor.SetMapper(mapper)
        actor.SetPosition(0, 0, 0)
        actor.GetProperty().SetColor(colors.GetColor3d('MistyRose'))

        """
        vtkBillboardTextActor3D  是一种 3D 文字显示类,能够在三维空间中的任意坐标位置显示文字,并自动面向相机
        """
        text_actor = vtkBillboardTextActor3D()
        text_actor.SetPosition(actor.GetPosition())
        text_actor.GetTextProperty().SetFontSize(12)
        text_actor.GetTextProperty().SetColor(colors.GetColor3d('Gold'))
        text_actor.GetTextProperty().SetJustificationToCentered()

        """
        几种text的比对
         类名                               | 类型       | 位置坐标系          | 是否随相机旋转   | 是否受光照影响        | 常见用途 
         vtkTextActor            | 2D 屏幕文字  | 屏幕坐标(Viewport) |  固定在屏幕上  |  否            | UI文字、标题、图例    
         vtkVectorText           | 3D 几何体文字 | 世界坐标(World)    | 会随相机旋转  | 是(真正的 3D 模型) | 立体文字、雕刻文字     
         vtkBillboardTextActor3D | 3D 文字贴图  | 世界坐标(World)    | 自动面向相机 | 否            | 3D 场景标注、标签、说明 

        """

        position = random_position(min_r, max_r, rng)
        actor.SetPosition(position)
        label = f'{position[0]:0.3g}, {position[1]:0.3g}, {position[2]:0.3g}'
        text_actor.SetPosition(position)
        text_actor.SetInput(label)

        renderer.AddActor(actor)
        renderer.AddActor(text_actor)

    render_window.Render()
    render_window.SetWindowName('BillboardTextActor3D')
    iren.Start()





def random_position(min_r, max_r, rng):
    p = list()
    for i in range(0, 3):
        p.append(rng.GetRangeValue(min_r, max_r))
        rng.Next()
    return p


if __name__ == '__main__':
    main()
相关推荐
API快乐传递者7 小时前
电商竞品分析接口实战指南:从数据采集到决策洞察的全链路方案
java·python
2401_868534787 小时前
集中式存储和分布式存储
python·pygame
卷无止境8 小时前
FastAPI 权限管理实战:从 ACL 到 RBAC 的那些门道
后端·python·fastapi
卷无止境8 小时前
软件文档写作中,Agent最常用的十种skill拆解
python·agent·claude
2603_9651481116 小时前
如何解析JSON数据?API返回的商品信息处理教程
开发语言·数据库·python·自动化·json·api
jufeng130718 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 8 篇】
python·ai agent·配置系统
circuitsosk18 小时前
跨境电商智能化实战:AI如何赋能客服自动回复、广告智能投放与供应链预测
大数据·人工智能·python·langchain·智能客服
2601_9563198819 小时前
2026年零基础学量化:从看懂示例到写清条件和动作
人工智能·python
过期的秋刀鱼!19 小时前
LangChain-D1-模型的工作流程
人工智能·python·langchain
萤火工厂目视化设计19 小时前
智能制造与企业文化目视化浪潮下:中小工厂的机遇与挑战
python·制造