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 유형의 속성은 셰이더에 유니폼 및 샘플러로 자동 노출됩니다.
두 번째 효과는 더 복잡합니다. 버텍스와 프래그먼트 셰이더를 모두 지정하고 uRed
와 uGreen
, 애니메이션이 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
을 사용합니다. 마지막으로 셰이더는 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; 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; }
© 2025 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.