news 2026/4/17 9:24:19

PBR_Clear_Coat 实现一个具有 金属质感 + 镜面反射 + 天空盒光照 的立方体

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
PBR_Clear_Coat 实现一个具有 金属质感 + 镜面反射 + 天空盒光照 的立方体

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①纹理的伽马校正,②清漆层


二:代码及注释

import json from pathlib import Path from vtkmodules.vtkRenderingCore import vtkPolyDataMapper, vtkActor from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkRenderingOpenGL2 import vtkOpenGLRenderer from vtkmodules.vtkRenderingCore import vtkLight, vtkRenderWindow, vtkRenderWindowInteractor, vtkSkybox, vtkTexture from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera from vtkmodules.vtkIOImage import vtkImageReader2Factory, vtkHDRReader, vtkPNGReader from vtkmodules.vtkImagingCore import vtkImageFlip from vtkmodules.vtkFiltersSources import vtkCubeSource from vtkmodules.vtkFiltersCore import vtkTriangleFilter, vtkPolyDataTangents def get_parameters(fn_path): """ Read the parameters from a JSON file and check that the file paths exist. :param fn_path: The path to the JSON file. :return: True if the paths correspond to files and the parameters. """ with open(fn_path) as data_file: json_data = json.load(data_file) parameters = dict() # Extract the values. keys_no_paths = {'title', 'object', 'objcolor', 'bkgcolor', 'skybox'} keys_with_paths = {'cubemap', 'equirectangular', 'albedo', 'normal', 'material', 'coat', 'anisotropy', 'emissive'} paths_ok = True for k, v in json_data.items(): if k in keys_no_paths: parameters[k] = v continue if k in keys_with_paths: if k == 'cubemap': if ('root' in v) and ('files' in v): root = Path(v['root']) if not root.exists(): print(f'Bad cubemap path: {root}') paths_ok = False elif len(v['files']) != 6: print(f'Expect six cubemap file names.') paths_ok = False else: cm = list(map(lambda p: root / p, v['files'])) for fn in cm: if not fn.is_file(): paths_ok = False print(f'Not a file {fn}') if paths_ok: parameters['cubemap'] = cm else: paths_ok = False print('Missing the key "root" and/or the key "fíles" for the cubemap.') else: fn = Path(v) if not fn.exists(): print(f'Bad {k} path: {fn}') paths_ok = False else: parameters[k] = fn # Set Boy as the default surface. if ('object' in parameters.keys() and not parameters['object']) or 'object' not in parameters.keys(): parameters['object'] = 'Boy' return paths_ok, parameters def read_cubemap(cubemap): cube_map = vtkTexture() cube_map.CubeMapOn() i = 0 for fn in cubemap: reader_factory = vtkImageReader2Factory() img_reader = reader_factory.CreateImageReader2(str(fn)) img_reader.SetFileName(str(fn)) flip = vtkImageFlip() flip.SetInputConnection(img_reader.GetOutputPort()) flip.SetFilteredAxis(1) cube_map.SetInputConnection(i, flip.GetOutputPort()) cube_map.MipmapOn() cube_map.InterpolateOn() return cube_map def read_equirectangular_file(fn_path): texture = vtkTexture() suffix = fn_path.suffix.lower() if suffix in ['.jpeg', '.jpg', '.png']: read_factory = vtkImageReader2Factory() reader = read_factory.CreateImageReader2(str(fn_path)) reader.SetFileName(str(fn_path)) texture.SetInputConnection(reader.GetOutputPort()) else: reader = vtkHDRReader() reader.SetFileName(str(fn_path)) texture.SetColorModeToDirectScalars() texture.SetInputConnection(reader.GetOutputPort()) texture.MipmapOn() texture.InterpolateOn() return texture def main(): fn = "Data/PBR_Parameters.json" use_cubemap = False paths_ok, parameters = get_parameters(fn) colors = vtkNamedColors() ren = vtkOpenGLRenderer() ren.SetBackground(colors.GetColor3d("Black")) """ AutomaticLightCreationOff 禁用渲染器的自动光照创建机制 在 VTK 中,为了让用户即使不手动添加光源也能看到3D模型,会先检查是否有光源,没有就会自动创建一个,防止场景一片漆黑 """ ren.AutomaticLightCreationOff() light = vtkLight() light.SetPosition(2.0, 0.0, 2.0) light.SetFocalPoint(0.0, 0.0, 0.0) ren.AddLight(light) ren_win = vtkRenderWindow() ren_win.SetSize(600, 600) ren_win.AddRenderer(ren) iren = vtkRenderWindowInteractor() iren.SetRenderWindow(ren_win) style = vtkInteractorStyleTrackballCamera() iren.SetInteractorStyle(style) irradiance = ren.GetEnvMapIrradiance() irradiance.SetIrradianceStep(0.3) skybox = vtkSkybox() is_hdr = False has_skybox = False gamma_correct = False if use_cubemap and 'cubemap' in parameters.keys(): env_texture = read_cubemap(parameters['cubemap']) if parameters['skybox']: skybox.SetTexture(env_texture) elif 'equirectangular' in parameters.keys(): env_texture = read_equirectangular_file(parameters['equirectangular']) if parameters['equirectangular'].suffix.lower() in '.hdr .pic': gamma_correct = True is_hdr = True if parameters['skybox']: skybox.SetFloorRight(0, 0, 1) skybox.SetProjection(vtkSkybox.Sphere) skybox.SetTexture(env_texture) has_skybox = True else: return ren.UseImageBasedLightingOn() if is_hdr: ren.UseSphericalHarmonicsOn() ren.SetEnvironmentTexture(env_texture, False) else: ren.UseSphericalHarmonicsOff() ren.SetEnvironmentTexture(env_texture, True) cube = vtkCubeSource() triangulation = vtkTriangleFilter() triangulation.SetInputConnection(cube.GetOutputPort()) tangents = vtkPolyDataTangents() tangents.SetInputConnection(triangulation.GetOutputPort()) mapper = vtkPolyDataMapper() mapper.SetInputConnection(tangents.GetOutputPort()) # 读取cube的表面的材质的照片 material_reader = vtkPNGReader() material_reader.SetFileName(parameters['material']) material = vtkTexture() material.InterpolateOn() material.SetInputConnection(material_reader.GetOutputPort()) # 颜色贴图的读取 albedo_reader = vtkPNGReader() albedo_reader.SetFileName(parameters['albedo']) albedo = vtkTexture() """ UseSRGBColorSpaceOn 这是 PBR 中最关键的设置之一,等同于对纹理进行输入伽马校正 大多数颜色贴图(包括 Albedo 贴图)都是在非线性的 sRGB 颜色空间中保存的。但是,渲染器内部的光照计算必须在线性空间中进行才能保证物理准确性 这确保了物体的基础颜色在渲染时不会显得过亮或过暗 """ albedo.UseSRGBColorSpaceOn() albedo.InterpolateOn() albedo.SetInputConnection(albedo_reader.GetOutputPort()) # 法相贴图的读取 normal_reader = vtkPNGReader() normal_reader.SetFileName(parameters['normal']) normal = vtkTexture() normal.InterpolateOn() normal.SetInputConnection(normal_reader.GetOutputPort()) # 作用于清漆层(Clear Coat)的法线贴图 """ 清漆层(Coat Layer)是指覆盖在主体材质(Base Material)之上的一层薄而透明的保护层 现实世界示例: 汽车漆(最外层的透明保护漆)、家具上的清漆/光油、带有釉面的陶瓷。 物理效果: 清漆层有自己的反射和粗糙度属性,它会独立地产生一次反射(高光),并且这层反射是半透明的, 因此光线会穿透它,击中下方的基础材质,再产生第二次反射(漫反射和基础反射)。 coat_normal就是一张法线贴图,但它只应用于最外面的清漆层 """ coat_normal = vtkTexture() coat_normal.InterpolateOn() coat_normal.SetInputConnection(normal_reader.GetOutputPort()) actor = vtkActor() actor.SetOrientation(0.0, 25.0, 0.0) actor.SetMapper(mapper) actor.GetProperty().SetInterpolationToPBR() actor.GetProperty().SetMetallic(1.0) actor.GetProperty().SetRoughness(1.0) """ SetCoatStrength 启用并设置清漆层的强度为 1.0(最大强度)。这模拟了物体表面覆盖着一层透明的保护漆或釉面。 """ actor.GetProperty().SetCoatStrength(1.0) """ SetCoatColor 定义清漆层的高光颜色。在这里设置为 'Red',意味着清漆层反射的光线会带有红色调,模拟一些特殊的着色清漆。 """ actor.GetProperty().SetCoatColor(colors.GetColor3d('Red')) """ SetBaseColorTexture 用于定义物体表面的基础颜色。这是材质的“底色”。 """ actor.GetProperty().SetBaseColorTexture(albedo) """ SetORMTexture 设置材质属性,通常一个3D模型的材质属性会存储在三原色通道中: - R 通道: Occlusion (环境光遮蔽) - G 通道: Roughness (粗糙度) - B 通道: Metallic (金属性) """ actor.GetProperty().SetORMTexture(material) """ SetNormalTexture 添加法线贴图 """ actor.GetProperty().SetNormalTexture(normal) """ SetCoatNormalTexture 清漆层法线贴图 """ actor.GetProperty().SetCoatNormalTexture(coat_normal) ren.AddActor(actor) if has_skybox: if gamma_correct: skybox.GammaCorrectOn() else: skybox.GammaCorrectOff() ren.AddActor(skybox) ren_win.SetWindowName('PBR_Clear_Coat') ren_win.Render() ren.GetActiveCamera().Zoom(1.5) iren.Start() if __name__ == '__main__': main()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/15 13:29:15

Ollama 下载安装教程(2025 最新版):本地运行大模型的快速上手指南

一、前言 随着人工智能大模型技术的持续演进,大多数用户已经不再满足于仅通过在线服务或API来体验AI能力。越来越多的人希望能在自己的电脑上直接运行ChatGPT、LLaMA、Mistral等主流AI模型,从而获得更高的隐私性、更快的响应速度和更多个性化的控制空间…

作者头像 李华
网站建设 2026/4/17 15:16:40

JBL便携音箱播放HeyGem视频用于公共展示

JBL便携音箱播放HeyGem视频用于公共展示 在商场中庭,一台显示器正播放着一位虚拟讲解员的影像,她面带微笑、口型精准地介绍着当季促销活动——而她的声音并非来自设备内置扬声器,而是由角落里一台小巧的JBL音箱传出。画面与音频同步自然&…

作者头像 李华
网站建设 2026/4/16 14:19:11

企业微信审批通知语音化?HeyGem制作引导视频

企业微信审批通知还能这样玩?用HeyGem一键生成主管“亲口讲解”视频 在企业日常运营中,最让人头疼的不是技术难题,而是“沟通损耗”——明明发了通知,员工却视而不见;反复解释流程,还是有人搞错步骤。尤其…

作者头像 李华
网站建设 2026/4/15 13:32:34

4个月烧掉30亿Token,菜鸟程序员如何打造50多个AI产品?

长久以来,代码世界的大门似乎只对少数掌握秘术的人敞开。我们被告知:你必须先理解内存、掌握语法、忍受枯燥的文档,才配谈论创造。 现在,随着大模型的发展,编程不再是一场苦修,而是一场大型即时策略游戏。…

作者头像 李华
网站建设 2026/4/16 17:54:01

当硕士论文不再只是“熬时间”:一位研二学生如何用AI工具把模糊问题转化为可执行研究路径——书匠策AI的深度陪伴式写作实践

硕士阶段的论文写作,早已不是“写一篇长文章”那么简单。 它是一场高强度的思维训练:你得从海量文献中识别前沿缺口,设计严谨方法,处理复杂数据,还要在学术规范与创新表达之间走钢丝。更难的是,导师往往只…

作者头像 李华
网站建设 2026/4/15 13:30:07

三大变动归于一处,吉利瞄准“智能化”

文|刘俊宏编|王一粟2025年末,吉利已经在酝酿下一轮战略。就在短短一个月内,吉利连续经历了三次变动。在公司架构层面,吉利汽车控股有限公司宣布已完成对极氪智能科技的私有化及合并交易,极氪和领克再度回归…

作者头像 李华