Skip to content
Snippets Groups Projects
Commit 5f8e3276 authored by Yuya Nishihara's avatar Yuya Nishihara
Browse files

implement weird hack to capture View3D output as texture

parent f3828a5b
No related branches found
No related tags found
No related merge requests found
...@@ -13,55 +13,192 @@ Window { ...@@ -13,55 +13,192 @@ Window {
visible: true visible: true
title: qsTr("Hello World") title: qsTr("Hello World")
View3D { RowLayout {
anchors.fill: parent anchors.fill: parent
OrthographicCamera { View3D {
position: Qt.vector3d(0, 0, 100) id: mainView
} Layout.fillWidth: true
Layout.fillHeight: true
DirectionalLight { OrthographicCamera {
position: Qt.vector3d(0, 0, 100) position: Qt.vector3d(0, 0, 100)
brightness: 100 }
}
Model { Model {
position: Qt.vector3d(0, 0, 0) position: Qt.vector3d(vxSpinBox.value, vySpinBox.value, -1)
source: "#Rectangle" scale: Qt.vector3d(5, 5, 1)
materials: myTextureMaterial source: "#Rectangle"
materials: DefaultMaterial {
diffuseColor: "gray"
lighting: DefaultMaterial.NoLighting
}
}
Model {
position: Qt.vector3d(vxSpinBox.value, vySpinBox.value, 0)
scale: Qt.vector3d(5, 5, 1)
source: "#Rectangle"
materials: CustomMaterial {
readonly property TextureInput myTexture: TextureInput {
texture: Texture {
// As of Qt 5.15.2, TextureInput doesn't upload texture
// unless source URL is set. So this TextureInput does
// effectively nothing in redering path. Instead, we do
// capture the intermediate output by Buffer and feed
// it via BufferInput.
//
// However, we still need this TextureInput to describe
// dependency on the textureView item. ShaderEffectSource
// is a workaround for QTBUG-93097.
//
// See also QSSGMaterialSystem::doApplyInstanceValue().
sourceItem: ShaderEffectSource {
width: textureView.width
height: textureView.height
sourceItem: textureView
}
}
}
hasTransparency: true
shaderInfo: ShaderInfo {
version: "330"
type: "GLSL"
}
passes: [
Pass {
commands: [
// See the comment above for why.
BufferInput { buffer: textureBuffer; param: "myTexture" }
]
shaders: [myFragShader]
}
]
Shader {
id: myFragShader
stage: Shader.Fragment
shader: "
out vec4 fragColor;
uniform float objectOpacity;
void main() {
fragColor = texture2D(myTexture, varTexCoord0.xy) * objectOpacity;
}
"
}
}
}
} }
}
CustomMaterial { Pane {
id: myTextureMaterial Layout.fillHeight: true
palette.window: "#eee"
GridLayout {
columns: 2
Label { text: "vx" }
SpinBox {
id: vxSpinBox
from: -1000
to: 1000
stepSize: 10
editable: true
}
Label { text: "vy" }
SpinBox {
id: vySpinBox
from: -1000
to: 1000
stepSize: 10
editable: true
}
Label { text: "tx" }
SpinBox {
id: txSpinBox
from: -150
to: 150
stepSize: 10
editable: true
}
readonly property TextureInput myTexture: TextureInput { Label { text: "ty" }
texture: Texture { SpinBox {
source: "assets/quick-fox-grayscale.png" id: tySpinBox
from: -150
to: 150
stepSize: 10
editable: true
}
} }
} }
}
Label {
text: "Red rectangle is rendered separately in hidden View3D."
}
View3D {
id: textureView
// Size of this view must be identical to the main view. Otherwise
// the shared Buffer {} wouldn't work.
width: mainView.width
height: mainView.height
visible: false
shaderInfo: ShaderInfo { OrthographicCamera {
version: "330" position: Qt.vector3d(0, 0, 0.5)
type: "GLSL" scale: Qt.vector3d(200 / textureView.width, 200 / textureView.height, 1)
clipNear: 0
clipFar: 1
} }
passes: [ Model {
Pass { position: Qt.vector3d(txSpinBox.value, tySpinBox.value, 0)
shaders: [myFragShader] source: "#Rectangle"
} materials: CustomMaterial {
] shaderInfo: ShaderInfo {
version: "330"
Shader { type: "GLSL"
id: myFragShader }
stage: Shader.Fragment
shader: " passes: [
out vec4 fragColor; Pass {
uniform float objectOpacity; shaders: [fillColorFragShader]
void main() { output: textureBuffer
fragColor = texture2D(myTexture, varTexCoord0.xy) * objectOpacity; }
]
// Captures the output so that it can be fed to a Material
// in the main scene.
Buffer {
id: textureBuffer
name: "textureBuffer"
format: Buffer.Unknown
textureCoordOperation: Buffer.ClampToEdge
textureFilterOperation: Buffer.Linear
// Since this buffer is referred by the main view, we can't
// control the lifetime. Let it be alive forever.
bufferFlags: Buffer.SceneLifetime
} }
"
Shader {
id: fillColorFragShader
stage: Shader.Fragment
shader: "
out vec4 fragColor;
uniform float objectOpacity;
void main() {
fragColor = vec4(1, 0, 0, 1) * objectOpacity;
}
"
}
}
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment