def make_scene(self): """ Overriddes Animation.make_scene """ # create a cube object cube_entity = Qt3DCore.QEntity(self.scene) cube_mesh = Qt3DExtras.QCuboidMesh() cube_mesh.setXExtent(1.0) cube_mesh.setYExtent(1.0) cube_mesh.setZExtent(1.0) cube_entity.addComponent(cube_mesh) # transform controls the cube's position, rotation, scale self.cube_transform = Qt3DCore.QTransform() cube_entity.addComponent(self.cube_transform) # material controls the cube's appearence self.cube_material = Qt3DExtras.QPhongMaterial(self.scene) self.cube_material.setSpecular(util.hsl(0, 0, 100)) self.cube_material.setShininess(0.0) cube_entity.addComponent(self.cube_material) # add some lights self.add_light(QtGui.QVector3D(-20.0, 20.0, -20.0), 1.0) # upper right key light self.add_light(QtGui.QVector3D(20.0, 10.0, -20.0), 0.5) # upper left fill light
def add_light(self, position, intensity=1.0, color=util.hsl(0, 0, 100)): """ Helper method to add a simple point light to the scene. Arguments: position: QVector3D, the position of the light in the scene intensity: float, the intensity of the light color: QColor, the color of the light """ light_entity = Qt3DCore.QEntity(self.scene) light = Qt3DRender.QPointLight(light_entity) light.setColor(color) light.setIntensity(intensity) light_transform = Qt3DCore.QTransform() light_transform.setTranslation(position) light_entity.addComponent(light) light_entity.addComponent(light_transform)
def __init__(self, height, rad): Qt3DCore.QEntity.__init__(self) self.cylMesh = Qt3DExtras.QCylinderMesh() self.cylMesh.setRadius(rad) self.cylMesh.setLength(height) # self.cylMesh.setRings(100) # self.cylMesh.setSlices(20) self.cylTransform = Qt3DCore.QTransform() self.cylTransform.setScale(1) self.cylTransform.setRotation( QtGui.QQuaternion.fromAxisAndAngle(QtGui.QVector3D(1, 0, 0), 45)) self.cylTransform.setTranslation(QtGui.QVector3D(0, 0, 0)) self.material = Qt3DExtras.QDiffuseSpecularMaterial() self.material.setSpecular(QtGui.QColor(0x928327)) self.material.setShininess(100) self.addComponent(self.cylMesh) self.addComponent(self.material) self.addComponent(self.cylTransform)
def setup_scene(self, background_color, camera_position, camera_lookat): """ Sets up the scene. Should be called before running the animation. Arguments: background_color: QColor, background color of the scene camera_position: QVector3D, the position of the camera in the scene camera_lookat: QVector3D, the point that the camera should be pointing at in the scene """ # clear color is the background color self.view.defaultFrameGraph().setClearColor(background_color) self.scene = Qt3DCore.QEntity() # let subclass populate scene self.make_scene() # set up camera camera = self.view.camera() camera.setPosition(camera_position) camera.setViewCenter(camera_lookat) self.view.setRootEntity(self.scene)
view.defaultFrameGraph().setClearColor(QColor('#4d4d4f')) container = QtWidgets.QWidget.createWindowContainer(view) screenSize = view.screen().size() container.setMinimumSize(QSize(200, 100)) container.setMaximumSize(screenSize) widget = QtWidgets.QWidget() hLayout = QtWidgets.QHBoxLayout(widget) vLayout = QtWidgets.QVBoxLayout() vLayout.setAlignment(Qt.AlignTop) hLayout.addWidget(container, 1) hLayout.addLayout(vLayout) widget.setWindowTitle('Basic shapes') # Root entity rootEntity = Qt3DCore.QEntity() # Camera cameraEntity = view.camera() cameraEntity.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0) cameraEntity.setPosition(QVector3D(0, 0, 20.0)) cameraEntity.setUpVector(QVector3D(0, 1, 0)) cameraEntity.setViewCenter(QVector3D(0, 0, 0)) lightEntity = Qt3DCore.QEntity(rootEntity) light = Qt3DRender.QPointLight(lightEntity) light.setColor(QColor("white")) light.setIntensity(1) lightEntity.addComponent(light) lightTransform = Qt3DCore.QTransform(lightEntity)
def __init__(self, root_entity=None): super().__init__() self.m_rootEntity = root_entity self.m_torus = Qt3DExtras.QTorusMesh(radius=1.0, minorRadius=0.4, rings=100, slices=20) self.torusTransform = Qt3DCore.QTransform( scale=2.0, rotation=QtGui.QQuaternion.fromAxisAndAngle( QtGui.QVector3D(0.0, 1.0, 0.0), 25.0), translation=QtGui.QVector3D(5.0, 4.0, 0.0), ) self.torusMaterial = Qt3DExtras.QPhongMaterial( diffuse=QtGui.QColor("#beb32b")) self.m_torusEntity = Qt3DCore.QEntity(self.m_rootEntity) self.m_torusEntity.addComponent(self.m_torus) self.m_torusEntity.addComponent(self.torusMaterial) self.m_torusEntity.addComponent(self.torusTransform) self.cone = Qt3DExtras.QConeMesh(topRadius=0.5, bottomRadius=1, length=3, rings=50, slices=20) self.coneTransform = Qt3DCore.QTransform( scale=1.5, rotation=QtGui.QQuaternion.fromAxisAndAngle( QtGui.QVector3D(1.0, 4.0, -1.5), 45.0), translation=QtGui.QVector3D(0.0, 4.0, -1.5), ) self.coneMaterial = Qt3DExtras.QPhongMaterial( diffuse=QtGui.QColor("#928327")) self.m_coneEntity = Qt3DCore.QEntity(self.m_rootEntity) self.m_coneEntity.addComponent(self.cone) self.m_coneEntity.addComponent(self.coneMaterial) self.m_coneEntity.addComponent(self.coneTransform) self.cylinder = Qt3DExtras.QCylinderMesh(radius=1, length=3, rings=100, slices=20) self.cylinderTransform = Qt3DCore.QTransform( scale=1.5, rotation=QtGui.QQuaternion.fromAxisAndAngle( QtGui.QVector3D(1.0, 0.0, 0.0), 45.0), translation=QtGui.QVector3D(-5.0, 4.0, -1.5), ) self.cylinderMaterial = Qt3DExtras.QPhongMaterial( diffuse=QtGui.QColor("#928327")) self.m_cylinderEntity = Qt3DCore.QEntity(self.m_rootEntity) self.m_cylinderEntity.addComponent(self.cylinder) self.m_cylinderEntity.addComponent(self.cylinderMaterial) self.m_cylinderEntity.addComponent(self.cylinderTransform) self.cuboid = Qt3DExtras.QCuboidMesh() self.cuboidTransform = Qt3DCore.QTransform( scale=4.0, translation=QtGui.QVector3D(5.0, -4.0, 0.0), ) self.cuboidMaterial = Qt3DExtras.QPhongMaterial( diffuse=QtGui.QColor("#665423")) self.m_cuboidEntity = Qt3DCore.QEntity(self.m_rootEntity) self.m_cuboidEntity.addComponent(self.cuboid) self.m_cuboidEntity.addComponent(self.cuboidMaterial) self.m_cuboidEntity.addComponent(self.cuboidTransform) self.planeMesh = Qt3DExtras.QPlaneMesh(width=2, height=2) self.planeTransform = Qt3DCore.QTransform( scale=1.3, rotation=QtGui.QQuaternion.fromAxisAndAngle( QtGui.QVector3D(1.0, 0.0, 0.0), 45.0), translation=QtGui.QVector3D(0.0, -4.0, 0.0), ) self.planeMaterial = Qt3DExtras.QPhongMaterial( diffuse=QtGui.QColor("#a69929")) self.m_planeEntity = Qt3DCore.QEntity(self.m_rootEntity) self.m_planeEntity.addComponent(self.planeMesh) self.m_planeEntity.addComponent(self.planeMaterial) self.m_planeEntity.addComponent(self.planeTransform) self.sphereMesh = Qt3DExtras.QSphereMesh(rings=20, slices=20, radius=2) self.sphereTransform = Qt3DCore.QTransform( scale=1.3, translation=QtGui.QVector3D(-5.0, -4.0, 0.0), ) self.sphereMaterial = Qt3DExtras.QPhongMaterial( diffuse=QtGui.QColor("#a69929")) self.m_sphereEntity = Qt3DCore.QEntity(self.m_rootEntity) self.m_sphereEntity.addComponent(self.sphereMesh) self.m_sphereEntity.addComponent(self.sphereMaterial) self.m_sphereEntity.addComponent(self.sphereTransform)
def __init__(self, rootEntity): super(SceneModifier, self).__init__(rootEntity) # Torus shape data self.m_torus = Qt3DExtras.QTorusMesh() self.m_torus.setRadius(1.0) self.m_torus.setMinorRadius(0.4) self.m_torus.setRings(100) self.m_torus.setSlices(20) # TorusMesh Transform torusTransform = Qt3DCore.QTransform() torusTransform.setScale(2.0) torusTransform.setRotation(QQuaternion.fromAxisAndAngle(QVector3D(0.0, 1.0, 0.0), 25.0)) torusTransform.setTranslation(QVector3D(5.0, 4.0, 0.0)) torusMaterial = Qt3DExtras.QPhongMaterial() torusMaterial.setDiffuse(QColor('#beb32b')) # Torus self.m_torusEntity = Qt3DCore.QEntity(rootEntity) self.m_torusEntity.addComponent(self.m_torus) self.m_torusEntity.addComponent(torusMaterial) self.m_torusEntity.addComponent(torusTransform) # Cone shape data cone = Qt3DExtras.QConeMesh() cone.setTopRadius(0.5) cone.setBottomRadius(1) cone.setLength(3) cone.setRings(50) cone.setSlices(20) # ConeMesh Transform coneTransform = Qt3DCore.QTransform() coneTransform.setScale(1.5) coneTransform.setRotation(QQuaternion.fromAxisAndAngle(QVector3D(1.0, 0.0, 0.0), 45.0)) coneTransform.setTranslation(QVector3D(0.0, 4.0, -1.5)) coneMaterial = Qt3DExtras.QPhongMaterial() coneMaterial.setDiffuse(QColor('#928327')) # Cone self.m_coneEntity = Qt3DCore.QEntity(rootEntity) self.m_coneEntity.addComponent(cone) self.m_coneEntity.addComponent(coneMaterial) self.m_coneEntity.addComponent(coneTransform) # Cylinder shape data cylinder = Qt3DExtras.QCylinderMesh() cylinder.setRadius(1) cylinder.setLength(3) cylinder.setRings(100) cylinder.setSlices(20) # CylinderMesh Transform cylinderTransform = Qt3DCore.QTransform() cylinderTransform.setScale(1.5) cylinderTransform.setRotation(QQuaternion.fromAxisAndAngle(QVector3D(1.0, 0.0, 0.0), 45.0)) cylinderTransform.setTranslation(QVector3D(-5.0, 4.0, -1.5)) cylinderMaterial = Qt3DExtras.QPhongMaterial() cylinderMaterial.setDiffuse(QColor('#928327')) # Cylinder self.m_cylinderEntity = Qt3DCore.QEntity(rootEntity) self.m_cylinderEntity.addComponent(cylinder) self.m_cylinderEntity.addComponent(cylinderMaterial) self.m_cylinderEntity.addComponent(cylinderTransform) # Cuboid shape data cuboid = Qt3DExtras.QCuboidMesh() # CuboidMesh Transform cuboidTransform = Qt3DCore.QTransform() cuboidTransform.setScale(4.0) cuboidTransform.setTranslation(QVector3D(5.0, -4.0, 0.0)) cuboidMaterial = Qt3DExtras.QPhongMaterial() cuboidMaterial.setDiffuse(QColor('#665423')) # Cuboid self.m_cuboidEntity = Qt3DCore.QEntity(rootEntity) self.m_cuboidEntity.addComponent(cuboid) self.m_cuboidEntity.addComponent(cuboidMaterial) self.m_cuboidEntity.addComponent(cuboidTransform) # Plane shape data planeMesh = Qt3DExtras.QPlaneMesh() planeMesh.setWidth(2) planeMesh.setHeight(2) # Plane mesh transform planeTransform = Qt3DCore.QTransform() planeTransform.setScale(1.3) planeTransform.setRotation(QQuaternion.fromAxisAndAngle(QVector3D(1.0, 0.0, 0.0), 45.0)) planeTransform.setTranslation(QVector3D(0.0, -4.0, 0.0)) planeMaterial = Qt3DExtras.QPhongMaterial() planeMaterial.setDiffuse(QColor('#a69929')) # Plane self.m_planeEntity = Qt3DCore.QEntity(rootEntity) self.m_planeEntity.addComponent(planeMesh) self.m_planeEntity.addComponent(planeMaterial) self.m_planeEntity.addComponent(planeTransform) # Sphere shape data sphereMesh = Qt3DExtras.QSphereMesh() sphereMesh.setRings(20) sphereMesh.setSlices(20) sphereMesh.setRadius(2) # Sphere mesh transform sphereTransform = Qt3DCore.QTransform() sphereTransform.setScale(1.3) sphereTransform.setTranslation(QVector3D(-5.0, -4.0, 0.0)) sphereMaterial = Qt3DExtras.QPhongMaterial() sphereMaterial.setDiffuse(QColor('#a69929')) # Sphere m_sphereEntity = Qt3DCore.QEntity(rootEntity) m_sphereEntity.addComponent(sphereMesh) m_sphereEntity.addComponent(sphereMaterial) m_sphereEntity.addComponent(sphereTransform)