news 2026/3/28 3:44:53

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/3/27 8:59:01

Rainbow读取和渲染 PLOT3D 格式的流体动力学(CFD)仿真数据

一:主要的知识点 1、说明 本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客 2、知识点纪要 本段代码主要涉及的有①vtkStructuredGridGeometryFilter网格到几…

作者头像 李华
网站建设 2026/3/28 3:13:10

Rotations 物体绕轴旋转

一:主要的知识点 1、说明 本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客 2、知识点纪要 本段代码主要涉及的有①物体如何绕轴旋转,②渲染的擦…

作者头像 李华
网站建设 2026/3/25 10:33:19

【C#网络通信协议深度解析】:掌握高性能Socket编程的5大核心技巧

第一章:C#网络通信协议概述在现代软件开发中,C# 作为 .NET 平台的核心语言之一,广泛应用于构建高性能的网络通信应用。其强大的类库支持和异步编程模型,使得开发者能够高效实现基于 TCP、UDP 和 HTTP 等协议的数据传输。核心通信协…

作者头像 李华
网站建设 2026/3/24 13:18:05

ReAct架构深度解析:让智能体“边思考边行动”的实战范式

本文同步更新于公众号:AI开发的后端厨师,本文完整代码开源github:https://github.com/windofbarcelona/all-agentic-architectures-golang/tree/main/03_react 本文同步更新于公众号:AI开发的后端厨师,本文完整代码开源…

作者头像 李华
网站建设 2026/3/24 2:01:01

C# 12拦截器异常全解析,深度解读编译时AOP的致命短板

第一章:C# 12拦截器异常全解析,深度解读编译时AOP的致命短板C# 12 引入的拦截器(Interceptors)特性标志着编译时面向切面编程(AOP)在语言层面的初步尝试。该机制允许开发者在编译阶段将特定方法调用重定向至…

作者头像 李华
网站建设 2026/3/26 21:11:41

从单一残差流,看懂 Prompt 为什么“能工作”

引子:Prompt 真的是“指令”吗?几乎所有人第一次被 Prompt 震到,都是在某个瞬间意识到: 我并没有教模型新知识,它却突然换了一种思考方式。不是模型升级,不是微调,也不是参数变化。 只是多写了几…

作者头像 李华