Qt Quick 3D - カスタムエフェクトの例
カスタムポストプロセッシングエフェクトの作成方法を示します。

このサンプルでは、独自のカスタムポストプロセッシングエフェクトを実装し、SceneEnvironment::effects を通じてシーンに適用しています。フラグメントシェーダーのみを使用する最も単純なタイプのエフェクトと、頂点シェーダーとフラグメントシェーダーの両方が存在し、両者の間でデータがやり取りされるより高度なケースの両方を示しています。
単純なエフェクトでは、単一のフラグメントシェーダーのみを使用し、画像ファイルからのテクスチャ入力を追加しています:
Effect {
id: eff1
property TextureInput tex: TextureInput {
id: qtLogo
texture: Texture { source: "qt_logo_rect.png" }
}
passes: Pass {
shaders: Shader {
id: fs1
stage: Shader.Fragment
shader: "effect.frag"
}
}
}このエフェクトでは、非常にシンプルなフラグメントシェーダーを使用しており、シーンを含む入力テクスチャを受け取り、それを画像テクスチャと乗算するだけです:
void MAIN()
{
vec4 c = texture(tex, TEXTURE_UV);
FRAGCOLOR = c * texture(INPUT, INPUT_UV);
}.vert および.frag ファイル内のシェーダースニペットは、Effect のドキュメントに記載されている組み込みキーワードを使用して記述されています。基本型を持つカスタムプロパティや、TextureInput 型のプロパティは、ユニフォームやサンプラーとしてシェーダーに自動的に公開されます。
2つ目のエフェクトはより複雑です。このエフェクトでは、頂点シェーダーとフラグメントシェーダーの両方が指定されており、さらにuRed とuGreen という2つのプロパティが定義され、uRed に対してアニメーションが適用されています:
Effect {
id: eff2
property real uRed: 0.0
SequentialAnimation {
running: radioEff2.checked || radioEff3.checked
loops: Animation.Infinite
NumberAnimation { target: eff2; property: "uRed"; from: 0; to: 1; duration: 2000 }
NumberAnimation { target: eff2; property: "uRed"; from: 1; to: 0; duration: 2000 }
}
property real uGreen: 1.0
Shader {
id: vs2
stage: Shader.Vertex
shader: "effect2.vert"
}
Shader {
id: fs2
stage: Shader.Fragment
shader: "effect2.frag"
}
passes: Pass {
shaders: [ vs2, fs2 ]
}
}このエフェクトのフラグメントシェーダーは、サンプリング座標を変更することで歪みを生み出します。この計算では、center_vec を使用します。これは、頂点シェーダーによって設定される VARYING プロパティであり、画面の中心から現在のフラグメントまでのアスペクト比補正済みベクトルを保持しています。 最後に、シェーダーはuRed およびuGreen というユニフォームを使用して色を調整します。なお、これらのユニフォームはシェーダー内で宣言する必要はありません:
VARYING vec2 center_vec;
void MAIN()
{
float radius = 0.25;
float dist_to_center = length(center_vec) / radius;
vec2 texcoord = INPUT_UV;
if (dist_to_center <= 1.0) {
float rotation_amount = (1.0 - dist_to_center) * (1.0 - dist_to_center);
float r = radians(360.0) * rotation_amount / 4.0 * FRAMEBUFFER_Y_UP;
float cos_r = cos(r);
float sin_r = sin(r);
mat2 rotation = mat2(cos_r, sin_r, -sin_r, cos_r);
texcoord = vec2(0.5, 0.5) + rotation * (INPUT_UV - vec2(0.5, 0.5));
}
vec4 c = texture(INPUT, texcoord);
c.r *= uRed;
c.g *= uGreen;
FRAGCOLOR = c;
}© 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.