相关文章推荐
英俊的野马  ·  python-docx表格设置 - 简书·  9 月前    · 
慷慨的汽水  ·  c++ vtable 深入解析 - 知乎·  1 年前    · 
成熟的匕首  ·  js mousedown 右键 - ...·  1 年前    · 
失望的铁链  ·  webpack踩坑 ...·  1 年前    · 

如何在threejs和PyQt应用程序中使背景透明?

0 人关注

我正在使用threejs在我的PyQt应用程序中显示一些三维物体。经过一番搜索,我能够显示一个立方体。我想做的是将背景从场景中移除,也就是说,使其透明,这样我们就可以看到被设置为应用程序背景色的蓝色。我不知道我应该怎么做。请帮助我。

main.py

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineCore import *
from PyQt5.QtWebEngineWidgets import *
import sys
class QtSchemeHandler(QWebEngineUrlSchemeHandler):
    def requestStarted(self, job):
        request_url = job.requestUrl()
        request_path = request_url.path()
        file = QFile('.' + request_path)
        file.setParent(job)
        job.destroyed.connect(file.deleteLater)
        file_info = QFileInfo(file)
        mime_database = QMimeDatabase()
        mime_type = mime_database.mimeTypeForFile(file_info)
        job.reply(mime_type.name().encode(), file)
class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setStyleSheet('background-color: blue;')
        self.verticalLayout = QVBoxLayout()
        self.setLayout(self.verticalLayout)
        self.browser = QWebEngineView()
        self.scheme_handler = QtSchemeHandler()
        self.browser.page().profile().installUrlSchemeHandler(
            b"qt", self.scheme_handler
        url = QUrl("qt://main")
        url.setPath("/index.html")
        self.browser.load(url)
        self.verticalLayout.addWidget(self.browser)
        self.browser.loadFinished.connect(self.show)
if __name__ == "__main__":
    scheme = QWebEngineUrlScheme(b"qt")
    scheme.setFlags(QWebEngineUrlScheme.CorsEnabled)
    QWebEngineUrlScheme.registerScheme(scheme)
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

index.html

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Three js earth</title>
    <style>
        body{
            margin: 0;
            overflow: hidden;
    </style>
</head>
    <canvas class="webgl"></canvas>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js" integrity="sha512-dLxUelApnYxpLt6K2iomGngnHO83iUvZytA3YjDUCjT0HDOHKXnVYdf3hU4JjM8uEhxf9nD1/ey98U3t2vZ0qQ==" crossorigin="anonymous"></script>
    <script type="module">
        const canvas = document.querySelector('.webgl');
        // scene setup
        const scene = new THREE.Scene();
        // camera setup
        const fov = 60;
        const aspect = window.innerWidth / window.innerHeight;
        const near = 0.1;
        const far = 10;
        const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
        camera.position.set(0, 0, 3)
        scene.add(camera);
        // renderer setup
        const renderer = new THREE.WebGLRenderer({canvas: canvas, antialias: true});
        // 3D Object
        const geometry = new THREE.BoxGeometry(1, 1, 1);
        const material = new THREE.MeshPhongMaterial({color: 0xFF0000});
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);
        // point light
        const pointLight = new THREE.PointLight(0xffffff, 1)
        pointLight.position.set(0, 0, 5);
        scene.add(pointLight);
        // handling resizing
        window.addEventListener('resize', () => {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
            render();
        }, false);
        // main loop
        function render(time) {
            cube.rotation.x -= 0.005;
            cube.rotation.y -= 0.005;
            renderer.render(scene, camera);
            requestAnimationFrame(render);
        render();
  </script>
</body>
</html>
    
1 个评论
我想先用css研究一下图层/z-index。 developer.mozilla.org/en-US/docs/Web/CSS/z-index 使html层为0,你的三个js画布层为-1(我想)。
python
three.js
pyqt
pyqt5
Mohammad
Mohammad
发布于 2022-03-30
1 个回答
Phil N.
Phil N.
发布于 2022-03-30
已采纳
0 人赞同

首先,将 QWebEngineView page 设置为透明背景。

In main.py :

self.scheme_handler = QtSchemeHandler() +++ self.browser.page().setBackgroundColor(Qt.GlobalColor.transparent) self.browser.page().profile().installUrlSchemeHandler( b"qt", self.scheme_handler

然后,将HTML页面的背景设置为透明。

In index.html :

<style> body{ margin: 0; overflow: hidden; +++ background: rgba(0, 0, 0, 0); </style>

最后,将Three.js的 WebGLRenderer 设置为使用透明背景。

Still in index.html :

    // renderer setup
    const renderer = new THREE.WebGLRenderer({
      canvas: canvas, 
      antialias: true,