本页内容

ShaderProgram QML Type

封装了一个着色器程序。更多...

Import Statement: import Qt3D.Render 2.12
In C++: QShaderProgram
Status: Deprecated

此类型已弃用。我们强烈建议不要在新代码中使用它。

属性

方法

详细说明

ShaderProgram 类封装了一个着色器程序。一个着色器程序由多个不同的着色器组成,例如顶点着色器和片元着色器。

如果在着色器内省阶段遇到一组默认统一变量,Qt3D 会自动为其赋值。

默认统一变量关联的 Qt3D 参数名称GLSL 声明
ModelMatrixmodelMatrixuniform mat4 modelMatrix;
视图矩阵viewMatrixuniform mat4 视图矩阵;
投影矩阵投影矩阵uniform mat4 投影矩阵;
模型视图矩阵modelViewuniform mat4 模型视图矩阵;
视投影矩阵视图投影矩阵uniform mat4 视投影矩阵;
模型视图投影矩阵modelViewProjection
mvp
uniform mat4 模型视图投影;
uniform mat4 mvp;
模型矩阵逆逆模型矩阵uniform mat4 逆模型矩阵;
视图矩阵的逆矩阵视图矩阵逆矩阵uniform mat4 逆视图矩阵;
投影矩阵逆逆投影矩阵uniform mat4 逆投影矩阵;
模型视图矩阵逆矩阵逆模型视图矩阵uniform mat4 逆模型视图矩阵;
视图投影矩阵逆矩阵逆视图投影矩阵uniform mat4 视图投影逆矩阵;
模型视图投影逆矩阵逆模型视图投影矩阵uniform mat4 逆模型视图投影矩阵;
模型法线矩阵模型法线矩阵uniform mat3 模型法线矩阵;
模型视图法线矩阵模型视图法线uniform mat3 modelViewNormal;
视口矩阵视口矩阵uniform mat4 视口矩阵;
视口矩阵逆矩阵逆视口矩阵uniform mat4 视口矩阵逆;
AspectRatio
(曲面宽度 / 曲面高度)
aspectRatioxml-ph-0000@deepl.internal(曲面宽度 / 曲面高度)uniform float aspectRatio;
曝光曝光uniform float 曝光量;
伽马伽马均匀浮点伽马;
时间
(单位:纳秒)
timeuniform float time;
EyePositioneyePositionuniform vec3 视点位置;
SkinningPaletteskinningPalette[0]const int maxJoints = 100;
uniform mat4 skinningPalette[maxJoints];

RHI 支持

在编写用于Qt 3D 的 RHI 后端的 GLSL 450 着色器代码时,默认的 uniform 将作为 2 个 uniform 缓冲区对象提供。

这些变量的绑定位置设置为:RenderView 统一变量绑定在 0,Command 统一变量绑定在 1。

#version 450 core

layout(location = 0) in vec3 vertexPosition;

layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
  mat4 viewMatrix;
  mat4 projectionMatrix;
  mat4 uncorrectedProjectionMatrix;
  mat4 clipCorrectionMatrix;
  mat4 viewProjectionMatrix;
  mat4 inverseViewMatrix;
  mat4 inverseProjectionMatrix;
  mat4 inverseViewProjectionMatrix;
  mat4 viewportMatrix;
  mat4 inverseViewportMatrix;
  vec4 textureTransformMatrix;
  vec3 eyePosition;
  float aspectRatio;
  float gamma;
  float exposure;
  float time;
  float yUpInNDC;
  float yUpInFBO;
};

layout(std140, binding = 1) uniform qt3d_command_uniforms {
  mat4 modelMatrix;
  mat4 inverseModelMatrix;
  mat4 modelViewMatrix;
  mat3 modelNormalMatrix;
  mat4 inverseModelViewMatrix;
  mat4 modelViewProjection;
  mat4 inverseModelViewProjectionMatrix;
};

void main()
{
    gl_Position = (projectionMatrix * viewMatrix * modelMatrix * vertexPosition);
}

对于用户自定义的uniform缓冲区对象,请使用从2开始的绑定,或使用auto让Qt 3D 自动处理绑定。请确保在不同的着色器阶段之间保持一致。

#version 450 core

layout(std140, binding = auto) uniform my_uniforms {
  vec4 myColor;
};

layout(location=0) out vec4 fragColor;

void main()
{
    fragColor = myColor;
}

向统一变量赋值时无需进行任何更改。

对于上述示例,可通过以下方式设置 myColor:

Parameter { name: "myColor"; value: "blue" }

纹理仍需作为独立的uniform进行定义。

#version 450 core

layout(binding=0) uniform sampler2D source;

layout(location=0) out vec4 fragColor;

void main()
{
    fragColor = texture(source, vec2(0.5, 0.5));
}

属性文档

computeShaderCode : string

存储该着色器程序所使用的计算着色器代码。

computeShaderGraph : string

保存此着色器程序生成器所使用的计算着色器图的 URL。

format : enumeration

采用ShaderProgram 上提供的代码格式。默认值为ShaderProgram.GLSL

fragmentShaderCode : string

存储该着色器程序所使用的片段着色器代码。

fragmentShaderGraph : string

保存此着色器程序生成器所使用的片段着色器图的 URL。

geometryShaderCode : string

存储该着色器程序所使用的几何着色器代码。

geometryShaderGraph : string

保存此着色器程序生成器所使用的几何着色器图的 URL。

log : string [read-only]

保存当前着色器程序的日志。这有助于诊断着色器程序的编译失败问题。

status : enumeration [read-only]

保存当前着色器程序的状态。

tessellationControlShaderCode : string

包含该着色器程序所使用的镶嵌控制着色器代码。

tessellationControlShaderGraph : string

保存此着色器程序生成器所使用的镶嵌控制着色器图的 URL。

tessellationEvaluationShaderCode : string

包含该着色器程序所使用的镶嵌评估着色器代码。

tessellationEvaluationShaderGraph : string

保存此着色器程序生成器所使用的镶嵌评估着色器图的 URL。

vertexShaderCode : string

包含该着色器程序所使用的顶点着色器代码。

vertexShaderGraph : string

保存此着色器程序生成器所使用的顶点着色器图的 URL。

方法文档

string loadSource(url sourceUrl)

返回从sourceUrl 加载的着色器代码。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.