新建一个LightTest.qml来测试环境,测试效果如下:
qmlscene 2023-08-17 14-51-59
import QtQuick 2.15
import QtGraphicalEffects 1.15
Rectangle{
id:lightTest
width:1920
height:1080
color:"#575757"
property color lightColor2:"#ffffff" //白色
property real brightnessMultiplier:1.0 //亮度倍增器
property real additionalGlassBloomLuminance:1.0
property real additionalGlassBlurRadius:30.0
LightTestView3D{
id:renderedScene
visible: true
anchors.fill: parent
renderScene: true
}
LightTestView3D{
id:renderedLights
visible:true
renderScene: false
anchors.fill: parent
}
//获取控件快照。是在GPU中进行的
ShaderEffectSource{
id:renderedSceneBuffer
width:960
height: 540
visible: true
hideSource: false //不会阻止鼠标到达sourceItem
sourceItem: renderedScene //将sourceItem渲染为纹理并在场景中显示
}
ShaderEffectSource{
id:renderedLightsBuffer
x:960
y:0
width:960
height:540
visible: true
hideSource: true
sourceItem: renderedLights
}
/*
//将自定义的着色器用于矩形
ShaderEffect{
id:colorizedLights
width:960
height:540
x:0
y:540
property color lightCol:lightTest.lightColor2
property variant renderedLightStencil:renderedLightsBuffer
fragmentShader: "
#version 410
in highp vec2 qt_TexCoord0;
out vec4 Color;
uniform lowp float qt_Opacity;
uniform highp vec4 lightCol;
uniform sampler2D renderedLightStencil;
void main() {
lowp vec4 emission = texture(renderedLightStencil, qt_TexCoord0);
emission.xyz *= lightCol.xyz;
Color = vec4(emission.xyz, 1.0);
}
"
}
//对一个或多个源项目应用快速模糊效果
FastBlur{
id:blurredGlass
source:colorizedLights
width:960
height:540
visible: true
x:0
y:1080
radius:additionalGlassBlurRadius
}
ShaderEffectSource{
id:blurredGlassEffectSource
width:960
height:540
x:960
y:540
hideSource: false
sourceItem: blurredGlass
}
*/
}
上述就是进行一个灯光场景的测试。下一节介绍各个3D模块的加载和显示。