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(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 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 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 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)