我目前只能将内容从QML迁移到Unity。我的想法是使用Spout视频帧共享系统。 Unity部件可以正常工作,但是仅接收无效的纹理。
对于Qt / QML部分,我必须使用MinGW编译器,因此我只能使用本机C Spout .dll(应该可以正常工作,因为Unity会检测到Spout发送者)
对于QML部分:
Item {
id:root
width: 500
height: 500
TextureCapturing{ //This is my own element, which provides the Spout sender
id: capturing
}
ShaderEffectSource { //This is used as the source item
id: textureSource
sourceItem: test
hideSource: false
width: sourceItem ? sourceItem.width : 0
height: sourceItem ? sourceItem.height : 0
}
Timer{ //This prevents code executing until the window is fully build up
interval: 500
running: true
repeat: false
onTriggered: capturing.source = textureSource;
}
Rectangle{ //Some test content
id:test
property bool isClicked: false
anchors.fill: parent
color: isClicked ? "red" : "blue"
MouseArea{
anchors.fill: parent
onClicked: parent.isClicked ^= true
}
}
TextureCapturing组件是我添加的一个自定义类。以下代码可以正常工作,不会产生任何错误,但遗憾的是也不会产生任何结果:
TextureCapturing::TextureCapturing() : QObject()
{
spoutptr = GetSpout(); // Create an instance of the Spout library
spoutptr->SetDX9(false);
}
TextureCapturing::~TextureCapturing()
{
if(spoutptr) {
qDebug()<< "Releasing Spout";
spoutptr->ReleaseSender(); // Release the sender
spoutptr->Release(); // Release the Spout library instance
}
}
void TextureCapturing::setSource(QQuickItem *source)
{
if(source != m_source){
m_source = source;
newWindow = m_source ? m_source->window() : nullptr;
connect(newWindow, &QQuickWindow::afterRendering,
this, &TextureCapturing::update, Qt::DirectConnection);
}
}
QQuickItem *TextureCapturing::source() const
{
return m_source;
}
void TextureCapturing::update()
{
if(!senderExists){
senderExists = spoutptr->CreateSender("QMLSender", 500, 500);
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
format.setMipmap(false);
format.setSamples(0);
format.setTextureTarget(GL_TEXTURE_2D);
format.setInternalTextureFormat(GL_RGBA8);
fbo = new QOpenGLFramebufferObject(500,500,format);
newWindow->setRenderTarget(fbo);
}
qDebug()<< "TextureId: " << fbo->texture();
glBindTexture(GL_TEXTURE_2D, fbo->texture());
spoutptr->SendTexture(fbo->texture(), GL_TEXTURE_2D, 500, 500);
glBindTexture(GL_TEXTURE_2D, 0);
qDebug()<< "Send";
}
我很确定在绑定fbo和发送纹理时出现了错误。问题是,我从未使用过OpenGL,并且仅在结果没有任何变化的情况下尝试了其他方法...
提前谢谢!