def add_rgb_cube(self, w, h, d): """ Helper method to add an RGB-textured cube to the scene. Arguments: w: float, the width of the cube h: float, the height of the cube d: float, the depth of the cube Returns: the QTransform of the cube """ cube_entity = QEntity(self.scene) cube_mesh = QCuboidMesh() cube_mesh.setXExtent(w) cube_mesh.setYExtent(h) cube_mesh.setZExtent(d) cube_entity.addComponent(cube_mesh) cube_transform = QTransform(self.scene) cube_entity.addComponent(cube_transform) if not hasattr(self, 'rgb_cube_material'): # load material definition from QML # this was the easiest way I could find to create a custom shader in Qt3D... self.rgb_cube_material = self.load_qml( os.path.join(os.path.dirname(__file__), 'RGBCubeMaterial.qml'), self.scene) cube_entity.addComponent(self.rgb_cube_material) return cube_transform
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 = QEntity() # let subclass populate scene self.make_scene() # set up camera camera = self.view.camera() camera.setPosition(camera_position) camera.setViewCenter(camera_lookat) look_vector = camera_position - camera_lookat if look_vector.x() == 0.0 and look_vector.z() == 0.0: camera.setUpVector(QVector3D(0, 0, 1)) else: camera.setUpVector(QVector3D(0, 1, 0)) self.view.setRootEntity(self.scene)
def clear_scene(self): if self._scene: for component in self._scene.components(): self._scene.removeComponent(component) self._scene = QEntity() self.setRootEntity(self._scene) self._camera_control = self._init_camera_control()
def __init__(self): super().__init__() lay = QVBoxLayout(self) self.view = Qt3DWindow() container = QWidget.createWindowContainer(self.view) lay.addWidget(container) self.rootEntity = QEntity() cameraEntity = self.view.camera() camController = QFirstPersonCameraController(self.rootEntity) camController.setCamera(cameraEntity) self.view.setRootEntity(self.rootEntity)
def createScene(self): root = QEntity() # Material material = QPhongMaterial(root) material.setAmbient(QColor(100, 100, 0)) material.setDiffuse(QColor(100, 0, 100)) cube_E = QEntity(root) cube_mesh = QCuboidMesh() cube_mesh.setXExtent(2) cube_mesh.setYExtent(2) cube_mesh.setZExtent(2) cube_tr = QTransform() cube_tr.setScale3D(QVector3D(2, 2, 2)) cube_E.addComponent(cube_mesh) cube_E.addComponent(cube_tr) cube_E.addComponent(material) return root
def create_atom(self, atom: Atom, root_entity, color: QColor): if atom.element in self._materials_dict: material = self._materials_dict[atom.element] else: material = QDiffuseSpecularMaterial(root_entity) material.setDiffuse(color) material.setAmbient(color.darker(200)) self._materials_dict[atom.element] = material sphere_entity = QEntity(root_entity) sphere_mesh = QSphereMesh() sphere_mesh.setRadius(0.5) sphere_transform = QTransform() sphere_transform.setTranslation(QVector3D(*atom.coords_cartesian)) sphere_entity.addComponent(sphere_mesh) sphere_entity.addComponent(sphere_transform) sphere_entity.addComponent(material)
def create_plane(root_entity, vector_1: np.ndarray, vector_2: np.ndarray): material = QDiffuseSpecularMaterial(root_entity) material.setDiffuse(QColor(200, 200, 200)) material.setAmbient(QColor(200, 200, 200)) plane_entity = QEntity(root_entity) plane_mesh = QPlaneMesh() plane_transform = QTransform() plane_mesh.setWidth(np.linalg.norm(vector_1)) plane_mesh.setHeight(np.linalg.norm(vector_2)) plane_mesh.setMirrored(False) translation_vector = (vector_1 + vector_2) / 2 plane_transform.setRotation(_get_quaternion(np.cross(vector_1, vector_2))) plane_transform.setTranslation(QVector3D(*translation_vector)) plane_entity.addComponent(plane_mesh) plane_entity.addComponent(plane_transform) plane_entity.addComponent(material)
def createScene(self): # root rootEntity = QSkyboxEntity() material = QPhongMaterial(rootEntity) #skybox = QSkyboxEntity(rootEntity) # torus cubeEntity = QEntity(rootEntity) cubeMesh = QCuboidMesh() cubeTransform = QTransform() #cubeMaterial = getRgbCubeMaterial(cubeEntity) cubeMaterial = QPhongMaterial(cubeEntity) cubeTransform.setTranslation(QVector3D(5.0, -4.0, 0.0)) cubeTransform.setScale(4.0) cubeTransform.setRotation(QQuaternion.fromAxisAndAngle(QVector3D(1, 0, 0), 45)) cubeEntity.addComponent(cubeMesh) cubeEntity.addComponent(cubeTransform) cubeEntity.addComponent(cubeMaterial) return rootEntity
def createScene(self): # Root entity rootEntity = QEntity() light_entity = QEntity(rootEntity) light = QPointLight(light_entity) light.setColor(QColor(255, 255, 255)) light.setIntensity(0.6) trans = QTransform() trans.setTranslation(QVector3D(0, 0, 2)) light_entity.addComponent(trans) light_entity.addComponent(light) # Material material = QPhongMaterial(rootEntity) material.setAmbient(QColor(100, 100, 100)) # material.setShininess(0) self.robot_entity = QEntity(rootEntity) f1_mesh = QMesh() f1_mesh.setSource(QUrl('qrc:/assets/' + self.robot_type + '.obj')) self.robot_entity.addComponent(f1_mesh) self.robot_entity.addComponent(material) sphereTransform = QTransform() controller = OrbitTransformController(sphereTransform) controller.setTarget(sphereTransform) controller.setRadius(0.0) self.sphereRotateTransformAnimation = QPropertyAnimation( sphereTransform) self.sphereRotateTransformAnimation.setTargetObject(controller) self.sphereRotateTransformAnimation.setPropertyName(b"angle") self.sphereRotateTransformAnimation.setStartValue(0) self.sphereRotateTransformAnimation.setEndValue(360) self.sphereRotateTransformAnimation.setDuration(10000) self.sphereRotateTransformAnimation.setLoopCount(-1) self.robot_entity.addComponent(sphereTransform) self.start_animation() return rootEntity
def add_sphere(self, r): """ Helper method to add a sphere to the scene. Arguments: r: float, the radius of the sphere Returns: the QTransform of the sphere """ sphere_entity = QEntity(self.scene) sphere_mesh = QSphereMesh() sphere_mesh.setRadius(r) sphere_entity.addComponent(sphere_mesh) sphere_transform = QTransform(self.scene) sphere_entity.addComponent(sphere_transform) if not hasattr(self, 'sphere_material'): self.sphere_material = QPhongMaterial(self.scene) self.sphere_material.setAmbient(util.hsl(0, 0, 50)) sphere_entity.addComponent(self.sphere_material) return sphere_transform
def add_path(self, *pts, color=util.hsl(0, 0, 50)): """ Helper method to add a path to the scene. Arguments: pts: list of QVector3D's, the points in the path Returns: a list of the entities added to the scene """ path_material = QPhongMaterial(self.scene) path_material.setDiffuse(color) # make a bunch of cylinder objects aligned along the path entities = [] prev_pt = None for pt in pts: if prev_pt is not None: if prev_pt != pt: # for each adjacent pair of points that are different # make a cylinder path_entity = QEntity(self.scene) path_mesh = QCylinderMesh() path_mesh.setRadius(0.05) # very thin path_mesh.setLength((prev_pt - pt).length()) # length is the distance between the points path_entity.addComponent(path_mesh) path_transform = QTransform(self.scene) path_transform.setRotation(QQuaternion.fromDirection(QVector3D(0, 0, -1), prev_pt - pt)) # rotate to point along path path_transform.setTranslation((pt + prev_pt) / 2) # center between points path_entity.addComponent(path_transform) path_entity.addComponent(path_material) entities.append(path_entity) prev_pt = pt return entities
def create_cylinder(root_entity, center, direction): material = QDiffuseSpecularMaterial(root_entity) material.setDiffuse(QColor(200, 200, 200, 50)) material.setAmbient(QColor(200, 200, 200, 50)) cylinder_entity = QEntity(root_entity) cylinder_mesh = QCylinderMesh() length = np.linalg.norm(direction) translate_vector = center + direction / 2 cylinder_mesh.setLength(length) cylinder_mesh.setRadius(0.02) cylinder_transform = QTransform() cylinder_transform.setRotation(_get_quaternion(direction)) cylinder_transform.setTranslation(QVector3D(*translate_vector)) cylinder_entity.addComponent(cylinder_mesh) cylinder_entity.addComponent(cylinder_transform) cylinder_entity.addComponent(material)
def add_sphere(self, color=util.hsl(0, 0, 50)): """ Helper method to add a sphere to the scene. Arguments: color: QColor, the color of the sphere's material Returns: the QTransform of the sphere """ sphere_entity = QEntity(self.scene) sphere_mesh = QSphereMesh() sphere_entity.addComponent(sphere_mesh) sphere_transform = QTransform(self.scene) sphere_entity.addComponent(sphere_transform) sphere_material = QPhongMaterial(self.scene) sphere_material.setDiffuse(color) sphere_entity.addComponent(sphere_material) return sphere_transform
def add_plane(self, color=util.hsl(0, 0, 50)): """ Helper method to add a plane to the scene. Arguments: color: QColor, the color of the plane's material Returns: the QTransform of the plane """ plane_entity = QEntity(self.scene) plane_mesh = QPlaneMesh() plane_entity.addComponent(plane_mesh) plane_transform = QTransform(self.scene) plane_entity.addComponent(plane_transform) plane_material = QPhongMaterial(self.scene) plane_material.setDiffuse(color) plane_entity.addComponent(plane_material) return plane_transform
def add_cylinder(self, color=util.hsl(0, 0, 50)): """ Helper method to add a cylinder to the scene. Arguments: color: QColor, the color of the cylinder's material Returns: the QTransform of the cylinder """ cylinder_entity = QEntity(self.scene) cylinder_mesh = QCylinderMesh() cylinder_entity.addComponent(cylinder_mesh) cylinder_transform = QTransform(self.scene) cylinder_entity.addComponent(cylinder_transform) cylinder_material = QPhongMaterial(self.scene) cylinder_material.setDiffuse(color) cylinder_entity.addComponent(cylinder_material) return cylinder_transform
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 = QEntity(self.scene) light = QPointLight(light_entity) light.setColor(color) light.setIntensity(intensity) light_transform = QTransform(self.scene) light_transform.setTranslation(position) light_entity.addComponent(light) light_entity.addComponent(light_transform)
def createScene(): # Root entity. rootEntity = QEntity() # Material. material = QPhongMaterial(rootEntity) # Torus. torusEntity = QEntity(rootEntity) torusMesh = QTorusMesh() torusMesh.setRadius(5) torusMesh.setMinorRadius(1) torusMesh.setRings(100) torusMesh.setSlices(20) torusTransform = QTransform() torusTransform.setScale3D(QVector3D(1.5, 1.0, 0.5)) torusTransform.setRotation( QQuaternion.fromAxisAndAngle(QVector3D(1.0, 0.0, 0.0), 45.0)) torusEntity.addComponent(torusMesh) torusEntity.addComponent(torusTransform) torusEntity.addComponent(material) # Sphere. sphereEntity = QEntity(rootEntity) sphereMesh = QSphereMesh() sphereMesh.setRadius(3) sphereTransform = QTransform() controller = OrbitTransformController(sphereTransform) controller.target = sphereTransform controller.radius = 20.0 sphereRotateTransformAnimation = QPropertyAnimation(sphereTransform) sphereRotateTransformAnimation.setTargetObject(controller) sphereRotateTransformAnimation.setPropertyName(b'angle') sphereRotateTransformAnimation.setStartValue(0) sphereRotateTransformAnimation.setEndValue(360) sphereRotateTransformAnimation.setDuration(10000) sphereRotateTransformAnimation.setLoopCount(-1) sphereRotateTransformAnimation.start() sphereEntity.addComponent(sphereMesh) sphereEntity.addComponent(sphereTransform) sphereEntity.addComponent(material) return rootEntity
def createScene(): # Root entity. rootEntity = QEntity() # Material. material = QPhongMaterial(rootEntity) # Torus. torusEntity = QEntity(rootEntity) torusMesh = QTorusMesh() torusMesh.setRadius(5) torusMesh.setMinorRadius(1) torusMesh.setRings(100) torusMesh.setSlices(20) torusTransform = QTransform() torusTransform.setScale3D(QVector3D(1.5, 1.0, 0.5)) torusTransform.setRotation( QQuaternion.fromAxisAndAngle(QVector3D(1.0, 0.0, 0.0), 45.0)) torusEntity.addComponent(torusMesh) torusEntity.addComponent(torusTransform) torusEntity.addComponent(material) # Sphere. sphereEntity = QEntity(rootEntity) sphereMesh = QSphereMesh() sphereMesh.setRadius(3) sphereEntity.addComponent(sphereMesh) sphereEntity.addComponent(material) return rootEntity
self.m_plantNormalImage.setSource( QUrl.fromLocalFile('assets/houseplants/' + HousePlant.plantNames[self.m_plantType] + '_normal.webp')) # Change to the directory containing the example so that the path to the assets # is correct. os.chdir(os.path.dirname(os.path.abspath(__file__))) app = QGuiApplication(sys.argv) view = Qt3DWindow() # Scene root. sceneRoot = QEntity() # Scene camera. basicCamera = view.camera() basicCamera.setProjectionType(QCameraLens.PerspectiveProjection) basicCamera.setAspectRatio(view.width() / view.height()) basicCamera.setUpVector(QVector3D(0.0, 1.0, 0.0)) basicCamera.setViewCenter(QVector3D(0.0, 3.5, 0.0)) basicCamera.setPosition(QVector3D(0.0, 3.5, 25.0)) # Camera controls. camController = QFirstPersonCameraController(sceneRoot) camController.setCamera(basicCamera) # Scene floor. planeEntity = PlaneEntity(sceneRoot)
def create3DWidget(self): view = Qt3DWindow() # view.defaultFramegraph().setClearColor(QColor(0x4d4d4f)) container = QWidget.createWindowContainer(view) screenSize = view.screen().size() container.setMinimumSize(QSize(200, 100)) container.setMaximumSize(screenSize) aspect = QInputAspect() view.registerAspect(aspect) # Root entity. self.rootEntity = QEntity() # Camera. cameraEntity = view.camera() cameraEntity.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0) cameraEntity.setPosition(QVector3D(0.0, -50.0, -0.5)) cameraEntity.setUpVector(QVector3D(0.0, -1.0, 0.0)) cameraEntity.setViewCenter(QVector3D(0.0, 0.0, 0.0)) # Light lightEntity = QEntity(self.rootEntity) light = QPointLight(lightEntity) light.setColor(QColor.fromRgbF(1.0, 1.0, 1.0, 1.0)) light.setIntensity(1) lightEntity.addComponent(light) lightTransform = QTransform(lightEntity) lightTransform.setTranslation(QVector3D(10.0, -40.0, 0.0)) lightEntity.addComponent(lightTransform) # For camera controls. camController = QFirstPersonCameraController(self.rootEntity) camController.setCamera(cameraEntity) # Set root object of the scene. view.setRootEntity(self.rootEntity) self.modifier = SceneModifier(self.rootEntity) moveLeft = QPushButton(text="Left") moveLeft.clicked.connect(self.modifier.transformLeft) moveLeft.setAutoRepeat(True) moveRight = QPushButton(text="Right") moveRight.clicked.connect(self.modifier.transformRight) moveRight.setAutoRepeat(True) moveUp = QPushButton(text="Up") moveUp.clicked.connect(self.modifier.transformUp) moveUp.setAutoRepeat(True) moveDown = QPushButton(text="Down") moveDown.clicked.connect(self.modifier.transformDown) moveDown.setAutoRepeat(True) moveX = QPushButton(text="RotateX") moveX.clicked.connect(self.modifier.rotateX) moveX.setAutoRepeat(True) moveY = QPushButton(text="RotateY") moveY.clicked.connect(self.modifier.rotateY) moveY.setAutoRepeat(True) moveZ = QPushButton(text="RotateZ") moveZ.clicked.connect(self.modifier.rotateZ) moveZ.setAutoRepeat(True) scaleDown = QPushButton(text="Scale Down") scaleDown.clicked.connect(self.modifier.scaleDown) scaleDown.setAutoRepeat(True) scaleUp = QPushButton(text="Scale Up") scaleUp.clicked.connect(self.modifier.scaleUp) scaleUp.setAutoRepeat(True) switchModel = QPushButton(text="Switch Model") switchModel.clicked.connect(self.modifier.handlePickerPress) loadModel = QPushButton(text="Load Model") loadModel.clicked.connect(self.modifier.loadscene) self.vLayout.addWidget(moveLeft) self.vLayout.addWidget(moveRight) self.vLayout.addWidget(moveUp) self.vLayout.addWidget(moveDown) self.vLayout.addWidget(moveX) self.vLayout.addWidget(moveY) self.vLayout.addWidget(moveZ) self.vLayout.addWidget(scaleUp) self.vLayout.addWidget(scaleDown) self.vLayout.addWidget(switchModel) self.vLayout.addWidget(loadModel) self.scribbleArea.modifier = self.modifier return container
def __init__(self, app): view = Qt3DWindow() # view.defaultFramegraph().setClearColor(QColor(0x4d4d4f)) container = QWidget.createWindowContainer(view) screenSize = view.screen().size() container.setMinimumSize(QSize(200, 100)) container.setMaximumSize(screenSize) widget = QWidget() self.hLayout = QHBoxLayout(widget) self.vLayout = QVBoxLayout() self.vLayout.setAlignment(Qt.AlignTop) self.hLayout.addWidget(container, 1) self.hLayout.addLayout(self.vLayout) widget.setWindowTitle("3D Viewer") aspect = QInputAspect() view.registerAspect(aspect) # Root entity. self.rootEntity = QEntity() # Camera. cameraEntity = view.camera() cameraEntity.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0) cameraEntity.setPosition(QVector3D(0.0, -50.0, -0.5)) cameraEntity.setUpVector(QVector3D(0.0, 1.0, 0.0)) cameraEntity.setViewCenter(QVector3D(0.0, 0.0, 0.0)) # Light lightEntity = QEntity(self.rootEntity) light = QPointLight(lightEntity) light.setColor(QColor.fromRgbF(1.0, 1.0, 1.0, 1.0)) light.setIntensity(1) lightEntity.addComponent(light) lightTransform = QTransform(lightEntity) lightTransform.setTranslation(QVector3D(10.0, 40.0, 0.0)) lightEntity.addComponent(lightTransform) # For camera controls. camController = QFirstPersonCameraController(self.rootEntity) camController.setCamera(cameraEntity) # Set root object of the scene. view.setRootEntity(self.rootEntity) modifier = SceneModifier(self.rootEntity) moveLeft = QPushButton(text="Left") moveLeft.clicked.connect(modifier.transformLeft) moveLeft.setAutoRepeat(True) moveRight = QPushButton(text="Right") moveRight.clicked.connect(modifier.transformRight) moveRight.setAutoRepeat(True) moveUp = QPushButton(text="Up") moveUp.clicked.connect(modifier.transformUp) moveUp.setAutoRepeat(True) moveDown = QPushButton(text="Down") moveDown.clicked.connect(modifier.transformDown) moveDown.setAutoRepeat(True) scaleDown = QPushButton(text="Scale Down") scaleDown.clicked.connect(modifier.scaleDown) scaleDown.setAutoRepeat(True) scaleUp = QPushButton(text="Scale Up") scaleUp.clicked.connect(modifier.scaleUp) scaleUp.setAutoRepeat(True) switchModel = QPushButton(text="Switch Model") switchModel.clicked.connect(self.modifier.handlePickerPress) loadModel = QPushButton(text="Load Model") loadModel.clicked.connect(self.modifier.loadscene) self.vLayout.addWidget(moveLeft) self.vLayout.addWidget(moveRight) self.vLayout.addWidget(moveUp) self.vLayout.addWidget(moveDown) self.vLayout.addWidget(scaleUp) self.vLayout.addWidget(scaleDown) self.vLayout.addWidget(switchModel) self.vLayout.addWidget(loadModel) # Show the window. widget.show() widget.resize(1200, 800) sys.exit(app.exec_())
def __init__(self, rootEntity): super(SceneModifier, self).__init__() self.m_rootEntity = rootEntity # Torus shape data. self.m_torus = 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 = 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 = QPhongMaterial() torusMaterial.setDiffuse(QColor(0xbeb32b)) # Torus. self.m_torusEntity = QEntity(self.m_rootEntity) self.m_torusEntity.addComponent(self.m_torus) self.m_torusEntity.addComponent(torusMaterial) self.m_torusEntity.addComponent(torusTransform) # Cone shape data. cone = QConeMesh() cone.setTopRadius(0.5) cone.setBottomRadius(1) cone.setLength(3) cone.setRings(50) cone.setSlices(20) # ConeMesh transform. coneTransform = 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 = QPhongMaterial() coneMaterial.setDiffuse(QColor(0x928327)) # Cone. self.m_coneEntity = QEntity(self.m_rootEntity) self.m_coneEntity.addComponent(cone) self.m_coneEntity.addComponent(coneMaterial) self.m_coneEntity.addComponent(coneTransform) # Cylinder shape data. cylinder = QCylinderMesh() cylinder.setRadius(1) cylinder.setLength(3) cylinder.setRings(100) cylinder.setSlices(20) # CylinderMesh transform. cylinderTransform = 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 = QPhongMaterial() cylinderMaterial.setDiffuse(QColor(0x928327)) # Cylinder. self.m_cylinderEntity = QEntity(self.m_rootEntity) self.m_cylinderEntity.addComponent(cylinder) self.m_cylinderEntity.addComponent(cylinderMaterial) self.m_cylinderEntity.addComponent(cylinderTransform) # Cuboid shape data. cuboid = QCuboidMesh() # CuboidMesh transform. cuboidTransform = QTransform() cuboidTransform.setScale(4.0) cuboidTransform.setTranslation(QVector3D(5.0, -4.0, 0.0)) cuboidMaterial = QPhongMaterial() cuboidMaterial.setDiffuse(QColor(0x665423)) # Cuboid. self.m_cuboidEntity = QEntity(self.m_rootEntity) self.m_cuboidEntity.addComponent(cuboid) self.m_cuboidEntity.addComponent(cuboidMaterial) self.m_cuboidEntity.addComponent(cuboidTransform) # Plane shape data. planeMesh = QPlaneMesh() planeMesh.setWidth(2) planeMesh.setHeight(2) # Plane mesh transform. planeTransform = 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 = QPhongMaterial() planeMaterial.setDiffuse(QColor(0xa69929)) # Plane. self.m_planeEntity = QEntity(self.m_rootEntity) self.m_planeEntity.addComponent(planeMesh) self.m_planeEntity.addComponent(planeMaterial) self.m_planeEntity.addComponent(planeTransform) # Sphere shape data. sphereMesh = QSphereMesh() sphereMesh.setRings(20) sphereMesh.setSlices(20) sphereMesh.setRadius(2) # Sphere mesh transform. sphereTransform = QTransform() sphereTransform.setScale(1.3) sphereTransform.setTranslation(QVector3D(-5.0, -4.0, 0.0)) sphereMaterial = QPhongMaterial() sphereMaterial.setDiffuse(QColor(0xa69929)) # Sphere. self.m_sphereEntity = QEntity(self.m_rootEntity) self.m_sphereEntity.addComponent(sphereMesh) self.m_sphereEntity.addComponent(sphereMaterial) self.m_sphereEntity.addComponent(sphereTransform)
class View3D(QWidget): def __init__(self, robot_type, parent=None): super(View3D, self).__init__(parent) self.view = Qt3DWindow() self.parent = parent self.view.defaultFrameGraph().setClearColor(QColor(51, 51, 51)) self.container = self.createWindowContainer(self.view) self.setStyleSheet('background-color: white') self.robot_type = robot_type self.robot_entity = None self.setMouseTracking(True) vboxlayout = QHBoxLayout() vboxlayout.addWidget(self.container) self.setLayout(vboxlayout) self.scene = self.createScene() # Camera. self.initialiseCamera(self.scene) self.view.setRootEntity(self.scene) # t1 = threading.Thread(target=self.print_campos) # t1.start() # def print_campos(self): # while True: # print(self.robot_type, self.camera.position()) # time.sleep(0.5) def initialiseCamera(self, scene): # Camera. self.camera = self.view.camera() self.camera.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0) if self.robot_type == 'car': self.camera.setPosition(QVector3D(0.3, 1.5, 4.0)) elif self.robot_type == 'f1': self.camera.setPosition(QVector3D(0.3, 1.7, 4.5)) elif self.robot_type == 'drone' or self.robot_type == 'drone_l': self.camera.setPosition(QVector3D(0.2, 0.1, 0.5)) elif self.robot_type == 'roomba': self.camera.setPosition(QVector3D(0.0, 0.2, 0.6)) elif self.robot_type == 'turtlebot': self.camera.setPosition(QVector3D(0.0, 0.4, 0.8)) elif self.robot_type == 'pepper': self.camera.setPosition(QVector3D(0.17, 1.3, 1.6)) if self.robot_type == 'pepper': self.camera.setViewCenter(QVector3D(0.0, 0.6, 0.0)) elif self.robot_type == 'turtlebot': self.camera.setViewCenter(QVector3D(0.0, 0.1, 0.0)) else: self.camera.setViewCenter(QVector3D(0.0, 0.0, 0.0)) def activate_camera(self, scene): # # For camera controls. camController = QOrbitCameraController(scene) camController.setLinearSpeed(250.0) camController.setLookSpeed(250.0) camController.setCamera(self.camera) def change_window(self): print('finished robots, emiting---') self.parent.emit_and_destroy() self.sphereRotateTransformAnimation.deleteLater() def start_animation(self): self.stop_animation() self.sphereRotateTransformAnimation.start() def start_animation_with_duration(self, duration): self.stop_animation() self.sphereRotateTransformAnimation.setDuration(duration) self.sphereRotateTransformAnimation.setLoopCount(1) self.start_animation() self.sphereRotateTransformAnimation.finished.connect( self.change_window) def stop_animation(self): self.sphereRotateTransformAnimation.stop() def set_animation_speed(self, speed): """ speed of a 360deg rotation in seconds """ self.sphereRotateTransformAnimation.setDuration(speed) def createScene(self): # Root entity rootEntity = QEntity() light_entity = QEntity(rootEntity) light = QPointLight(light_entity) light.setColor(QColor(255, 255, 255)) light.setIntensity(0.6) trans = QTransform() trans.setTranslation(QVector3D(0, 0, 2)) light_entity.addComponent(trans) light_entity.addComponent(light) # Material material = QPhongMaterial(rootEntity) material.setAmbient(QColor(100, 100, 100)) # material.setShininess(0) self.robot_entity = QEntity(rootEntity) f1_mesh = QMesh() f1_mesh.setSource(QUrl('qrc:/assets/' + self.robot_type + '.obj')) self.robot_entity.addComponent(f1_mesh) self.robot_entity.addComponent(material) sphereTransform = QTransform() controller = OrbitTransformController(sphereTransform) controller.setTarget(sphereTransform) controller.setRadius(0.0) self.sphereRotateTransformAnimation = QPropertyAnimation( sphereTransform) self.sphereRotateTransformAnimation.setTargetObject(controller) self.sphereRotateTransformAnimation.setPropertyName(b"angle") self.sphereRotateTransformAnimation.setStartValue(0) self.sphereRotateTransformAnimation.setEndValue(360) self.sphereRotateTransformAnimation.setDuration(10000) self.sphereRotateTransformAnimation.setLoopCount(-1) self.robot_entity.addComponent(sphereTransform) self.start_animation() return rootEntity
container.setMaximumSize(screenSize) widget = QWidget() hLayout = QHBoxLayout(widget) vLayout = QVBoxLayout() vLayout.setAlignment(Qt.AlignTop) hLayout.addWidget(container, 1) hLayout.addLayout(vLayout) widget.setWindowTitle("Basic shapes") aspect = QInputAspect() view.registerAspect(aspect) # Root entity. rootEntity = QEntity() # Camera. cameraEntity = view.camera() cameraEntity.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0) cameraEntity.setPosition(QVector3D(0.0, 0.0, 20.0)) cameraEntity.setUpVector(QVector3D(0.0, 1.0, 0.0)) cameraEntity.setViewCenter(QVector3D(0.0, 0.0, 0.0)) # For camera controls. camController = QFirstPersonCameraController(rootEntity) camController.setCamera(cameraEntity) # Scene modifier. modifier = SceneModifier(rootEntity)