vtk shader的默认模板

vtk为了达到灵活的渲染效果,提供了为vtkActor提供自定义shader的两种方式(vtk9.x版本):

1、部分替换shader代码,vtk的渲染过程有一个默认的shader模板:

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: in vec2 tcoord;
41: out vec2 tcoordVCVSOutput;
42:
43:
44: // material property values
45: //VTK::Color::Dec
46:
47: // clipping plane vars
48: //VTK::Clip::Dec
49:
50: // camera and actor matrix values
51: uniform mat4 MCDCMatrix;
52:
53: // Apple Bug
54: //VTK::PrimID::Dec
55:
56: // Value raster
57: //VTK::ValuePass::Dec
58:
59: // picking support
60: //VTK::Picking::Dec
61:
62: // Surface with edges on GLES 3.0
63: //VTK::EdgesGLES30::Dec
64:
65: // PointSize on GLES 3.0
66: //VTK::PointSizeGLES30::Dec
67:
68: // LineWidth on GLES 3.0
69: //VTK::LineWidthGLES30::Dec
70:
71: void main()
72: {
73: //VTK::PointSizeGLES30::Impl
74:
75: //VTK::CustomBegin::Impl
76:
77: //VTK::Color::Impl
78:
79: //VTK::Normal::Impl
80:
81: tcoordVCVSOutput = tcoord;
82:
83:
84: //VTK::Clip::Impl
85:
86: //VTK::PrimID::Impl
87:
88: gl_Position = MCDCMatrix * vertexMC;
89:
90:
91: //VTK::LineWidthGLES30::Impl
92:
93: //VTK::ValuePass::Impl
94:
95: //VTK::Light::Impl
96:
97: //VTK::Picking::Impl
98:
99: //VTK::CustomEnd::Impl
100:
101: //VTK::EdgesGLES30::Impl
102: }

 

替换的写法如下:

  var actor = vtkActor.New();

  var sp = actor.GetShaderProperty();

  sp.ClearAllShaderReplacements();  

  sp.AddFragmentShaderReplacement("//VTK::Color::Impl", true, GetDoseShader(), true);
  sp.AddFragmentShaderReplacement("//VTK::Light::Impl", true, "// Light disabled", true);

 

有一个细节,替换shader时,如果替换点不是最后一个,为了避免替换的效果被后续vtk自动生成的

shader代码影响逻辑,最好加上return,避免后续的逻辑影响,下边的shader替换后的代码可以验证除了

程序替换的代码之外,vtk自动生成的shader代码会影响逻辑:

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: uniform float uAlpha;
49: uniform float uColors[24];
50: uniform int uCount;
51: uniform int uDiscrete;
52: uniform float uLevels[8];
53: uniform float uLineWidth;
54: uniform int uPixelated;
55: uniform int uShowIso;
56: uniform vec2 uTexSize;
57:
58:
59: // VC position of this fragment
60: //VTK::PositionVC::Dec
61:
62: // Camera prop
63: uniform int cameraParallel;
64:
65:
66: // optional color passed in from the vertex shader, vertexColor
67: uniform float ambientIntensity; // the material ambient
68: uniform float diffuseIntensity; // the material diffuse
69: uniform float opacityUniform; // the fragment opacity
70: uniform vec3 ambientColorUniform; // ambient color
71: uniform vec3 diffuseColorUniform; // diffuse color
72:
73:
74: // optional surface normal declaration
75: //VTK::Normal::Dec
76:
77: // extra lighting parameters
78: uniform vec3 lightColor0;
79:
80:
81: // Texture maps
82: uniform bool showTexturesOnBackface;uniform sampler2D actortexture;
83:
84:
85: // Texture coordinates
86: in vec2 tcoordVCVSOutput;
87:
88:
89: // picking support
90: //VTK::Picking::Dec
91:
92: // Depth Peeling Support
93: //VTK::DepthPeeling::Dec
94:
95: // clipping plane vars
96: //VTK::Clip::Dec
97:
98: // the output of this shader
99: out vec4 fragOutput0;
100: out vec4 fragOutput1;
101:
102:
103: // Apple Bug
104: //VTK::PrimID::Dec
105:
106: // handle coincident offsets
107: //VTK::Coincident::Dec
108:
109: // Value raster
110: //VTK::ValuePass::Dec
111:
112: // surface with edges
113: //VTK::Edges::Dec
114:
115: void main()
116: {
117: // VC position of this fragment. This should not branch/return/discard.
118: //VTK::PositionVC::Impl
119:
120: // Place any calls that require uniform flow (e.g. dFdx) here.
121: //VTK::UniformFlow::Impl
122:
123: // Set gl_FragDepth here (gl_FragCoord.z by default)
124: //VTK::Depth::Impl
125:
126: // Early depth peeling abort:
127: //VTK::DepthPeeling::PreColor
128:
129: // Apple Bug
130: //VTK::PrimID::Impl
131:
132: //VTK::Clip::Impl
133:
134: //VTK::ValuePass::Impl
135:
136:

137: // --- ??????? ---

138: vec2 texPos = tcoordVCVSOutput * uTexSize;

139: vec2 center = floor(texPos - 0.5) + 0.5;

140: vec2 f = texPos - center;

141: vec2 invSize = 1.0 / uTexSize;

142:

143: float val = 0.0;

144: vec2 grad = vec2(0.0);

145:

146: if (uPixelated != 0) {

147: val = texture(actortexture, tcoordVCVSOutput).r;

148: float vL = texture(actortexture, tcoordVCVSOutput + vec2(-invSize.x, 0)).r;

149: float vR = texture(actortexture, tcoordVCVSOutput + vec2(invSize.x, 0)).r;

150: float vT = texture(actortexture, tcoordVCVSOutput + vec2(0, -invSize.y)).r;

151: float vB = texture(actortexture, tcoordVCVSOutput + vec2(0, invSize.y)).r;

152: grad = vec2(vR - vL, vB - vT) * 0.5;

153: } else {

154: float t = f.x;

155: float t2 = t * t; float t3 = t2 * t;

156: vec4 wx = vec4(-0.5*t3 + t2 - 0.5*t, 1.5*t3 - 2.5*t2 + 1.0, -1.5*t3 + 2.0*t2 + 0.5*t, 0.5*t3 - 0.5*t2);

157: vec4 wx_d = vec4(-1.5*t2 + 2.0*t - 0.5, 4.5*t2 - 5.0*t, -4.5*t2 + 4.0*t + 0.5, 1.5*t2 - t);

158:

159: t = f.y;

160: t2 = t * t; t3 = t2 * t;

161: vec4 wy = vec4(-0.5*t3 + t2 - 0.5*t, 1.5*t3 - 2.5*t2 + 1.0, -1.5*t3 + 2.0*t2 + 0.5*t, 0.5*t3 - 0.5*t2);

162: vec4 wy_d = vec4(-1.5*t2 + 2.0*t - 0.5, 4.5*t2 - 5.0*t, -4.5*t2 + 4.0*t + 0.5, 1.5*t2 - t);

163:

164: vec2 r0 = (center + vec2(-1.0, -1.0)) * invSize;

165: vec4 vals0 = vec4(texture(actortexture, r0).r, texture(actortexture, r0 + vec2(1.0, 0.0)*invSize).r, texture(actortexture, r0 + vec2(2.0, 0.0)*invSize).r, texture(actortexture, r0 + vec2(3.0, 0.0)*invSize).r);

166: vec2 r1 = (center + vec2(-1.0, 0.0)) * invSize;

167: vec4 vals1 = vec4(texture(actortexture, r1).r, texture(actortexture, r1 + vec2(1.0, 0.0)*invSize).r, texture(actortexture, r1 + vec2(2.0, 0.0)*invSize).r, texture(actortexture, r1 + vec2(3.0, 0.0)*invSize).r);

168: vec2 r2 = (center + vec2(-1.0, 1.0)) * invSize;

169: vec4 vals2 = vec4(texture(actortexture, r2).r, texture(actortexture, r2 + vec2(1.0, 0.0)*invSize).r, texture(actortexture, r2 + vec2(2.0, 0.0)*invSize).r, texture(actortexture, r2 + vec2(3.0, 0.0)*invSize).r);

170: vec2 r3 = (center + vec2(-1.0, 2.0)) * invSize;

171: vec4 vals3 = vec4(texture(actortexture, r3).r, texture(actortexture, r3 + vec2(1.0, 0.0)*invSize).r, texture(actortexture, r3 + vec2(2.0, 0.0)*invSize).r, texture(actortexture, r3 + vec2(3.0, 0.0)*invSize).r);

172:

173: vec4 colSums = vec4(dot(vals0, wx), dot(vals1, wx), dot(vals2, wx), dot(vals3, wx));

174: val = dot(colSums, wy);

175:

176: vec4 colSums_dx = vec4(dot(vals0, wx_d), dot(vals1, wx_d), dot(vals2, wx_d), dot(vals3, wx_d));

177: grad.x = dot(colSums_dx, wy);

178: grad.y = dot(colSums, wy_d);

179: }

180:

181: // --- Color Wash ---

182: int idx = -1;

183: if (val >= uLevels[0]) idx = 0;

184: if (idx == 0 && val >= uLevels[1]) idx = 1;

185: if (idx == 1 && val >= uLevels[2]) idx = 2;

186: if (idx == 2 && val >= uLevels[3]) idx = 3;

187: if (idx == 3 && val >= uLevels[4]) idx = 4;

188: if (idx == 4 && val >= uLevels[5]) idx = 5;

189: if (idx == 5 && val >= uLevels[6]) idx = 6;

190: if (idx == 6 && val >= uLevels[7]) idx = 7;

191: if (idx >= uCount - 1) idx = uCount - 1;

192:

193: vec3 washColor = vec3(0.0);

194: float washAlpha = 0.0;

195:

196: if (idx >= 0) {

197: washAlpha = uAlpha;

198: vec3 col1 = vec3(0.0);

199: if (idx == 0) col1 = vec3(uColors[0], uColors[1], uColors[2]);

200: else if (idx == 1) col1 = vec3(uColors[3], uColors[4], uColors[5]);

201: else if (idx == 2) col1 = vec3(uColors[6], uColors[7], uColors[8]);

202: else if (idx == 3) col1 = vec3(uColors[9], uColors[10], uColors[11]);

203: else if (idx == 4) col1 = vec3(uColors[12], uColors[13], uColors[14]);

204: else if (idx == 5) col1 = vec3(uColors[15], uColors[16], uColors[17]);

205: else if (idx == 6) col1 = vec3(uColors[18], uColors[19], uColors[20]);

206: else if (idx == 7) col1 = vec3(uColors[21], uColors[22], uColors[23]);

207:

208: if (uDiscrete != 0) {

209: washColor = col1;

210: } else {

211: int nextIdx = min(idx + 1, uCount - 1);

212: vec3 col2 = vec3(0.0);

213: if (nextIdx == 0) col2 = vec3(uColors[0], uColors[1], uColors[2]);

214: else if (nextIdx == 1) col2 = vec3(uColors[3], uColors[4], uColors[5]);

215: else if (nextIdx == 2) col2 = vec3(uColors[6], uColors[7], uColors[8]);

216: else if (nextIdx == 3) col2 = vec3(uColors[9], uColors[10], uColors[11]);

217: else if (nextIdx == 4) col2 = vec3(uColors[12], uColors[13], uColors[14]);

218: else if (nextIdx == 5) col2 = vec3(uColors[15], uColors[16], uColors[17]);

219: else if (nextIdx == 6) col2 = vec3(uColors[18], uColors[19], uColors[20]);

220: else if (nextIdx == 7) col2 = vec3(uColors[21], uColors[22], uColors[23]);

221:

222: float v1 = 0.0; float v2 = 1.0;

223: if (idx == 0) v1 = uLevels[0]; else if (idx == 1) v1 = uLevels[1]; else if (idx == 2) v1 = uLevels[2]; else if (idx == 3) v1 = uLevels[3]; else if (idx == 4) v1 = uLevels[4]; else if (idx == 5) v1 = uLevels[5]; else if (idx == 6) v1 = uLevels[6]; else if (idx == 7) v1 = uLevels[7];

224: if (nextIdx == 0) v2 = uLevels[0]; else if (nextIdx == 1) v2 = uLevels[1]; else if (nextIdx == 2) v2 = uLevels[2]; else if (nextIdx == 3) v2 = uLevels[3]; else if (nextIdx == 4) v2 = uLevels[4]; else if (nextIdx == 5) v2 = uLevels[5]; else if (nextIdx == 6) v2 = uLevels[6]; else if (nextIdx == 7) v2 = uLevels[7];

225:

226: float t = 0.0;

227: if (abs(v2 - v1) > 1e-5) t = (val - v1) / (v2 - v1);

228: washColor = mix(col1, col2, t);

229: }

230: }

231:

232: // --- Iso Lines ---

233: float lineMask = 0.0;

234: vec3 isoRGB = vec3(0.0);

235:

236: if (uShowIso != 0) {

237: float screenGradLen = length(grad * fwidth(tcoordVCVSOutput) * uTexSize);

238: float width = uLineWidth * 0.5;

239: float feather = 0.5;

240: float dist = 0.0; float la = 0.0;

241:

242: if (0 < uCount) { dist = abs(val - uLevels[0]) / max(screenGradLen, 1e-6); la = 1.0 - smoothstep(width - feather, width + feather, dist); if (la > lineMask) { lineMask = la; isoRGB = vec3(uColors[0], uColors[1], uColors[2]); }}

243: if (1 < uCount) { dist = abs(val - uLevels[1]) / max(screenGradLen, 1e-6); la = 1.0 - smoothstep(width - feather, width + feather, dist); if (la > lineMask) { lineMask = la; isoRGB = vec3(uColors[3], uColors[4], uColors[5]); }}

244: if (2 < uCount) { dist = abs(val - uLevels[2]) / max(screenGradLen, 1e-6); la = 1.0 - smoothstep(width - feather, width + feather, dist); if (la > lineMask) { lineMask = la; isoRGB = vec3(uColors[6], uColors[7], uColors[8]); }}

245: if (3 < uCount) { dist = abs(val - uLevels[3]) / max(screenGradLen, 1e-6); la = 1.0 - smoothstep(width - feather, width + feather, dist); if (la > lineMask) { lineMask = la; isoRGB = vec3(uColors[9], uColors[10], uColors[11]); }}

246: if (4 < uCount) { dist = abs(val - uLevels[4]) / max(screenGradLen, 1e-6); la = 1.0 - smoothstep(width - feather, width + feather, dist); if (la > lineMask) { lineMask = la; isoRGB = vec3(uColors[12], uColors[13], uColors[14]); }}

247: if (5 < uCount) { dist = abs(val - uLevels[5]) / max(screenGradLen, 1e-6); la = 1.0 - smoothstep(width - feather, width + feather, dist); if (la > lineMask) { lineMask = la; isoRGB = vec3(uColors[15], uColors[16], uColors[17]); }}

248: if (6 < uCount) { dist = abs(val - uLevels[6]) / max(screenGradLen, 1e-6); la = 1.0 - smoothstep(width - feather, width + feather, dist); if (la > lineMask) { lineMask = la; isoRGB = vec3(uColors[18], uColors[19], uColors[20]); }}

249: }

250:

251: // --- Output ---

252: vec3 finalRGB = mix(washColor, isoRGB, lineMask);

253: float finalA = mix(washAlpha, 1.0, lineMask);

254: finalRGB = pow(finalRGB, vec3(0.75)); // Gamma

255:

256: if (finalA <= 0.0) discard;

257: fragOutput0 = vec4(finalRGB, finalA);

258: return

259:
260:
261: //VTK::Edges::Impl
262:
263: // Generate the normal if we are not passed in one
264: //VTK::Normal::Impl
265:
266: // Light disabled
267:
268: vec4 tcolor_0 = texture(actortexture, tcoordVCVSOutput); // Read texture color
269: ; // Update color based on texture nbr of components
270: vec4 tcolor = tcolor_0; // BLENDING: None (first texture)
271:
272: if (gl_FrontFacing == true || showTexturesOnBackface) {fragOutput0 = fragOutput0 * tcolor; }
273:
274: if (fragOutput0.a <= 0.0)
275: {
276: discard;
277: }
278:
279: fragOutput0 = vec4(fragOutput0.rgb*fragOutput0.a, fragOutput0.a);
280: fragOutput1.r = fragOutput0.a;
281:
282:
283: //VTK::Picking::Impl
284:
285: // handle coincident offsets
286: //VTK::Coincident::Impl
287: }

我替换的代码在第258行结束,如果没有return,对于fragOutput0的修改,会被后续自动生成的代码逻辑覆盖掉,

导致的结果是替换的shader没有生效。

 2、直接替换所有的shader代码:

var sp = actor.GetShaderProperty();
sp.ClearAllShaderReplacements();
sp.SetFragmentShaderCode(someShaderCode);

这样会把vtk默认的shader都去掉,所有的渲染都需要自己复杂,慎用。

posted @ 2025-12-18 09:29  luqc  阅读(0)  评论(0)    收藏  举报