1

PyVista 很棒,让我非常方便地做很多很酷的事情。我想将 3D 对象的阴影投射到带有网格线的平面上。这就是我想要的,减去阴影shadows=False,,gridlines=True

在此处输入图像描述

我可以在没有网格线的情况下启用阴影:gridlines=Falseshadows=True

在此处输入图像描述

但是当我尝试两者都做时gridlines=Trueshadows=True我得到了一个巨大的错误跟踪,如下所示,结果就像上面一样,有阴影但没有网格线。

问题:我想要做的是什么?我是否误解了网格和阴影,是否编码错误,或者这是一个错误。我更大的程序,有更多的图形实体会出现段错误。我在 MacOS 10.15.7 上运行 PyVista 0.29.0、Python 3.8.5。如果我尝试将阴影投射到StructuredGrid.

下面是我的最小代码示例

import pyvista as pv
import vtk
import numpy as np

# change these values
gridlines = True
shadows = False

plotter = pv.Plotter(polygon_smoothing=True, window_size=(2000,2000))
plotter.disable_parallel_projection()

sphere = pv.Sphere(radius=0.3, center=(0,0,1))
plotter.add_mesh(sphere, ambient=0.2, diffuse=0.5, specular=0.8, specular_power=30,
            smooth_shading=True, color='dodgerblue')

# add the grid
grid = pv.Plane(i_size=5, j_size=5)
plotter.add_mesh(grid, show_edges=gridlines, ambient=0, diffuse=0.5, specular=0.8, color='red', edge_color='white')

if shadows:
    # do the shadows
    shadows = vtk.vtkShadowMapPass()
    seq = vtk.vtkSequencePass()

    passes = vtk.vtkRenderPassCollection()
    passes.AddItem(shadows.GetShadowMapBakerPass())
    passes.AddItem(shadows)
    seq.SetPasses(passes)

    # Tell the renderer to use our render pass pipeline
    cameraP = vtk.vtkCameraPass()
    cameraP.SetDelegatePass(seq)
    plotter.renderer.SetPass(cameraP)

plotter.set_background('white')
plotter.show()

错误跟踪:

(dev) [233 ~...code/RVC3-python/tools]  % /Users/corkep/opt/miniconda3/envs/dev/bin/python /Users/corkep/Dropbox/code/RVC3-python/chapter8/pvbug.py
2021-03-11 19:57:38.908 (   0.621s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b5f90): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.908 (   0.622s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b6840): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.908 (   0.622s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b7120): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.908 (   0.622s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b7a00): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.908 (   0.622s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b82e0): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:38.990 (   0.704s) [          84DF2C]   vtkShaderProgram.cxx:452    ERR| vtkShaderProgram (0x7fee29ba49b0): 1: #version 150
2: #ifdef GL_ES
3: #ifdef GL_FRAGMENT_PRECISION_HIGH
4: precision highp float;
5: precision highp sampler2D;
6: precision highp sampler3D;
7: #else
8: precision mediump float;
9: precision mediump sampler2D;
10: precision mediump sampler3D;
11: #endif
12: #define texelFetchBuffer texelFetch
13: #define texture1D texture
14: #define texture2D texture
15: #define texture3D texture
16: #else // GL_ES
17: #define highp
18: #define mediump
19: #define lowp
20: #if __VERSION__ == 150
21: #define texelFetchBuffer texelFetch
22: #define texture1D texture
23: #define texture2D texture
24: #define texture3D texture
25: #endif
26: #endif // GL_ES
27: #define varying in
28: 
29: 
30: /*=========================================================================
31: 
32:   Program:   Visualization Toolkit
33:   Module:    vtkPolyDataFS.glsl
34: 
35:   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
36:   All rights reserved.
37:   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
38: 
39:      This software is distributed WITHOUT ANY WARRANTY; without even
40:      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
41:      PURPOSE.  See the above copyright notice for more information.
42: 
43: =========================================================================*/
44: // Template for the polydata mappers fragment shader
45: 
46: uniform int PrimitiveIDOffset;
47: 
48: 
49: 
50: // VC position of this fragment
51: //VTK::PositionVC::Dec
52: 
53: // Camera prop
54: uniform int cameraParallel;
55: 
56: 
57: // optional color passed in from the vertex shader, vertexColor
58: uniform float ambientIntensity; // the material ambient
59: uniform float diffuseIntensity; // the material diffuse
60: uniform float opacityUniform; // the fragment opacity
61: uniform vec3 ambientColorUniform; // ambient color
62: uniform vec3 diffuseColorUniform; // diffuse color
63: 
64: 
65: // optional surface normal declaration
66: //VTK::Normal::Dec
67: 
68: // extra lighting parameters
69: uniform vec3 lightColor0;
70:   uniform vec3 lightDirectionVC0; // normalized
71: uniform vec3 lightColor1;
72:   uniform vec3 lightDirectionVC1; // normalized
73: uniform vec3 lightColor2;
74:   uniform vec3 lightDirectionVC2; // normalized
75: uniform vec3 lightColor3;
76:   uniform vec3 lightDirectionVC3; // normalized
77: uniform vec3 lightColor4;
78:   uniform vec3 lightDirectionVC4; // normalized
79: 
80: uniform float depthC;
81: vec2 calcShadow(in vec4 vert,
82:                   in sampler2D shadowMap,
83:                   in mat4 shadowTransform,
84:                   in float attenuation,
85:                   in int shadowParallel,
86:                   in float sNearZ, in float sFarZ)
87: {
88:   vec4 shadowCoord = shadowTransform*vert;
89:   float expFactor = 8.0;
90:   float thickness = 0.0;
91:   if(shadowCoord.w > 0.0)
92:     {
93:     vec2 projected = shadowCoord.xy/shadowCoord.w;
94:     if(projected.x >= 0.0 && projected.x <= 1.0
95:        && projected.y >= 0.0 && projected.y <= 1.0)
96:       {
97:       float ldepth = shadowCoord.z;
98:       if (shadowParallel == 0) { ldepth =  (shadowCoord.w - sNearZ)/(sFarZ - sNearZ); }
99:       float depthCExpActual = exp(- depthC*ldepth);
100:       float depthCExpBlured = texture2D(shadowMap,projected).r;
101:       expFactor = depthCExpBlured * depthCExpActual;
102:       float depth = log(depthCExpBlured)/depthC;
103:       thickness = clamp(ldepth - depth, 0.0, 1.0)*(sFarZ - sNearZ);
104:       if (expFactor > 1.0) { expFactor = 1.0; }
105:       }
106:     }
107:   return vec2(1.0 - attenuation + attenuation*expFactor, thickness);
108: }
109: uniform int shadowParallel0;
110: uniform float shadowNearZ0;
111: uniform float shadowFarZ0;
112: uniform float shadowAttenuation0;
113: uniform sampler2D shadowMap0;
114: uniform mat4 shadowTransform0;
115: uniform int shadowParallel1;
116: uniform float shadowNearZ1;
117: uniform float shadowFarZ1;
118: uniform float shadowAttenuation1;
119: uniform sampler2D shadowMap1;
120: uniform mat4 shadowTransform1;
121: uniform int shadowParallel2;
122: uniform float shadowNearZ2;
123: uniform float shadowFarZ2;
124: uniform float shadowAttenuation2;
125: uniform sampler2D shadowMap2;
126: uniform mat4 shadowTransform2;
127: uniform int shadowParallel3;
128: uniform float shadowNearZ3;
129: uniform float shadowFarZ3;
130: uniform float shadowAttenuation3;
131: uniform sampler2D shadowMap3;
132: uniform mat4 shadowTransform3;
133: 
134: 
135: // Texture maps
136: //VTK::TMap::Dec
137: 
138: // Texture coordinates
139: //VTK::TCoord::Dec
140: 
141: // picking support
142: //VTK::Picking::Dec
143: 
144: // Depth Peeling Support
145: //VTK::DepthPeeling::Dec
146: 
147: // clipping plane vars
148: //VTK::Clip::Dec
149: 
150: // the output of this shader
151: out vec4 fragOutput0;
152: 
153: 
154: // Apple Bug
155: //VTK::PrimID::Dec
156: 
157: // handle coincident offsets
158: uniform float cCValue;
159: uniform float cSValue;
160: uniform float cDValue;
161: 
162: // Value raster
163: //VTK::ValuePass::Dec
164: 
165: void main()
166: {
167:   // VC position of this fragment. This should not branch/return/discard.
168:   //VTK::PositionVC::Impl
169: 
170:   // Place any calls that require uniform flow (e.g. dFdx) here.
171:   //VTK::UniformFlow::Impl
172: 
173:   // Set gl_FragDepth here (gl_FragCoord.z by default)
174:   float Zdc = gl_FragCoord.z*2.0 - 1.0;
175:   float Z2 = -1.0*cDValue/(Zdc + cCValue) + cSValue;
176:   float Zdc2 = -1.0*cCValue - cDValue/Z2;
177:   gl_FragDepth = Zdc2*0.5 + 0.5;
178: 
179: 
180:   // Early depth peeling abort:
181:   //VTK::DepthPeeling::PreColor
182: 
183:   // Apple Bug
184:   //VTK::PrimID::Impl
185: 
186:   //VTK::Clip::Impl
187: 
188:   //VTK::ValuePass::Impl
189: 
190:     vec3 ambientColor = ambientIntensity * ambientColorUniform;
191:   vec3 diffuseColor = diffuseIntensity * diffuseColorUniform;
192:   float opacity = opacityUniform;
193: 
194: 
195:   // Generate the normal if we are not passed in one
196:   //VTK::Normal::Impl
197: 
198:   vec2 factor0 = vec2(1.0);
199: vec2 factor1 = calcShadow(vertexVC, shadowMap0, shadowTransform0, shadowAttenuation0, shadowParallel0, shadowNearZ0, shadowFarZ0);
200: vec2 factor2 = calcShadow(vertexVC, shadowMap1, shadowTransform1, shadowAttenuation1, shadowParallel1, shadowNearZ1, shadowFarZ1);
201: vec2 factor3 = calcShadow(vertexVC, shadowMap2, shadowTransform2, shadowAttenuation2, shadowParallel2, shadowNearZ2, shadowFarZ2);
202: vec2 factor4 = calcShadow(vertexVC, shadowMap3, shadowTransform3, shadowAttenuation3, shadowParallel3, shadowNearZ3, shadowFarZ3);
203:   fragOutput0 = vec4(ambientColor + diffuseColor, opacity);
204:   //VTK::Light::Impl
205: 
206: 
207: 
208:   //VTK::TCoord::Impl
209: 
210:   if (fragOutput0.a <= 0.0)
211:     {
212:     discard;
213:     }
214: 
215:   //VTK::DepthPeeling::Impl
216: 
217:   //VTK::Picking::Impl
218: 
219:   // handle coincident offsets
220:   //VTK::Coincident::Impl
221: }

ERROR:root:1: #version 150
2021-03-11 19:57:38.991 (   0.705s) [          84DF2C]   vtkShaderProgram.cxx:453    ERR| vtkShaderProgram (0x7fee29ba49b0): ERROR: 0:199: Use of undeclared identifier 'vertexVC'
ERROR: 0:200: Use of undeclared identifier 'vertexVC'
ERROR: 0:201: Use of undeclared identifier 'vertexVC'
ERROR: 0:202: Use of undeclared identifier 'vertexVC'

ERROR:root:ERROR: 0:199: Use of undeclared identifier 'vertexVC'
2021-03-11 19:57:39.023 (   0.737s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b5f90): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.023 (   0.737s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b6840): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.023 (   0.737s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b7120): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.023 (   0.737s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b7a00): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.023 (   0.737s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b82e0): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.024 (   0.738s) [          84DF2C]   vtkShaderProgram.cxx:437    ERR| vtkShaderProgram (0x7fee29ba49b0): 1: #version 150
2: #ifndef GL_ES
3: #define highp
4: #define mediump
5: #define lowp
6: #endif // GL_ES
7: #define attribute in
8: #define varying out
9: 
10: 
11: /*=========================================================================
12: 
13:   Program:   Visualization Toolkit
14:   Module:    vtkPolyDataVS.glsl
15: 
16:   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
17:   All rights reserved.
18:   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
19: 
20:      This software is distributed WITHOUT ANY WARRANTY; without even
21:      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22:      PURPOSE.  See the above copyright notice for more information.
23: 
24: =========================================================================*/
25: 
26: in vec4 vertexMC;
27: 
28: 
29: 
30: // frag position in VC
31: //VTK::PositionVC::Dec
32: 
33: // optional normal declaration
34: //VTK::Normal::Dec
35: 
36: // extra lighting parameters
37: //VTK::Light::Dec
38: 
39: // Texture coordinates
40: //VTK::TCoord::Dec
41: 
42: // material property values
43: //VTK::Color::Dec
44: 
45: // clipping plane vars
46: //VTK::Clip::Dec
47: 
48: // camera and actor matrix values
49: uniform mat4 MCDCMatrix;
50: 
51: // Apple Bug
52: //VTK::PrimID::Dec
53: 
54: // Value raster
55: //VTK::ValuePass::Dec
56: 
57: // picking support
58: //VTK::Picking::Dec
59: 
60: void main()
61: {
62:   //VTK::Color::Impl
63: 
64:   //VTK::Normal::Impl
65: 
66:   //VTK::TCoord::Impl
67: 
68:   //VTK::Clip::Impl
69: 
70:   //VTK::PrimID::Impl
71: 
72:     gl_Position = MCDCMatrix * vertexMC;
73: 
74: 
75:   //VTK::ValuePass::Impl
76: 
77:   //VTK::Light::Impl
78: 
79:   //VTK::Picking::Impl
80: }

ERROR:root:1: #version 150
2021-03-11 19:57:39.025 (   0.739s) [          84DF2C]   vtkShaderProgram.cxx:438    ERR| vtkShaderProgram (0x7fee29ba49b0): 
WARNING:root:ERROR: In /Users/tjcorona/Development/vtk/source/Rendering/OpenGL2/vtkShaderProgram.cxx, line 438
vtkShaderProgram (0x7fee29ba49b0): 


2021-03-11 19:57:39.292 (   1.006s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b5f90): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.292 (   1.006s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b6840): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.292 (   1.006s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b7120): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.292 (   1.006s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b7a00): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.292 (   1.006s) [          84DF2C]      vtkLightActor.cxx:285    ERR| vtkLightActor (0x7fee299b82e0): not a spotlight.
ERROR:root:not a spotlight.
2021-03-11 19:57:39.293 (   1.007s) [          84DF2C]   vtkShaderProgram.cxx:437    ERR| vtkShaderProgram (0x7fee29ba49b0): 1: #version 150
2: #ifndef GL_ES
3: #define highp
4: #define mediump
5: #define lowp
6: #endif // GL_ES
7: #define attribute in
8: #define varying out
9: 
10: 
11: /*=========================================================================
12: 
13:   Program:   Visualization Toolkit
14:   Module:    vtkPolyDataVS.glsl
15: 
16:   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
17:   All rights reserved.
18:   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
19: 
20:      This software is distributed WITHOUT ANY WARRANTY; without even
21:      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22:      PURPOSE.  See the above copyright notice for more information.
23: 
24: =========================================================================*/
25: 
26: in vec4 vertexMC;
27: 
28: 
29: 
30: // frag position in VC
31: //VTK::PositionVC::Dec
32: 
33: // optional normal declaration
34: //VTK::Normal::Dec
35: 
36: // extra lighting parameters
37: //VTK::Light::Dec
38: 
39: // Texture coordinates
40: //VTK::TCoord::Dec
41: 
42: // material property values
43: //VTK::Color::Dec
44: 
45: // clipping plane vars
46: //VTK::Clip::Dec
47: 
48: // camera and actor matrix values
49: uniform mat4 MCDCMatrix;
50: 
51: // Apple Bug
52: //VTK::PrimID::Dec
53: 
54: // Value raster
55: //VTK::ValuePass::Dec
56: 
57: // picking support
58: //VTK::Picking::Dec
59: 
60: void main()
61: {
62:   //VTK::Color::Impl
63: 
64:   //VTK::Normal::Impl
65: 
66:   //VTK::TCoord::Impl
67: 
68:   //VTK::Clip::Impl
69: 
70:   //VTK::PrimID::Impl
71: 
72:     gl_Position = MCDCMatrix * vertexMC;
73: 
74: 
75:   //VTK::ValuePass::Impl
76: 
77:   //VTK::Light::Impl
78: 
79:   //VTK::Picking::Impl
80: }

ERROR:root:1: #version 150
2021-03-11 19:57:39.294 (   1.008s) [          84DF2C]   vtkShaderProgram.cxx:438    ERR| vtkShaderProgram (0x7fee29ba49b0): 
WARNING:root:ERROR: In /Users/tjcorona/Development/vtk/source/Rendering/OpenGL2/vtkShaderProgram.cxx, line 438
vtkShaderProgram (0x7fee29ba49b0): 

4

1 回答 1

2

阴影目前在 PyVista 中没有实现,我不知道它在 vtk 中有多可靠(或者如何让它可靠地工作)。

引用此评论

至于阴影,它在 60% 的时间里 100% 的时间起作用。我们应该添加一个选项,然后用户可以使用它。我确信在某些时候实时光线追踪将成为一项硬件功能,甚至包括集成 GPU,因此成为一个经过良好测试/支持的功能只是时间问题。

例如,在我的带有集成 GPU 阴影渲染的笔记本电脑上是一团糟。

使用窗口大小(1000、1000): 合理的带阴影的轨道视频

窗口大小(2000、1000): 带有扭曲阴影的越野车、伪影环绕的轨道视频


无论如何,我可以或多或少地重现您看到的错误。第一个错误部分来自 PyVista:

vtkLightActor.cxx:285 错误| vtkLightActor (0x7fee299b5f90):不是聚光灯。错误:根:不是聚光灯。

这是因为 PyVistaLight对象有一个vtkLightActor附加到它们,但这些应该被隐藏(即从不渲染),除非光是聚光灯(唯一vtkLightActor有意义的情况)。正如我在上述问题上指出的那样,不应该出现错误,并且阴影影响这一点毫无意义。如果这最终成为一个问题,我们可以尝试在 PyVista 中缓解它,但对于可疑的收益来说,这将是一个很大的复杂性。

着色器的第二个错误非常类似于 VTK,我不知道它到底是什么。考虑到这不是 PyVista 的受支持功能,而您正在vtk直接使用,使用vtk. (虽然我不知道我们是否可以在 PyVista 端添加一些东西以使其更可靠地工作。)

对于它的价值,你也许可以if shadows用这个来交换你的块:

plotter.renderer.SetUseShadows(shadows)

如果我使用它,至少我会看到相同的行为。因此,如果您的代码适合您,它可能是一个更简单的选择。

于 2021-03-11T22:55:21.547 回答