PreviewPass.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Application import Application
  4. from UM.Math.Color import Color
  5. from UM.Resources import Resources
  6. from UM.View.RenderPass import RenderPass
  7. from UM.View.GL.OpenGL import OpenGL
  8. from UM.View.RenderBatch import RenderBatch
  9. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  10. from typing import Optional
  11. MYPY = False
  12. if MYPY:
  13. from UM.Scene.Camera import Camera
  14. ## A render pass subclass that renders slicable objects with default parameters.
  15. # It uses the active camera by default, but it can be overridden to use a different camera.
  16. #
  17. # This is useful to get a preview image of a scene taken from a different location as the active camera.
  18. class PreviewPass(RenderPass):
  19. def __init__(self, width: int, height: int):
  20. super().__init__("preview", width, height, 0)
  21. self._camera = None # type: Optional[Camera]
  22. self._renderer = Application.getInstance().getRenderer()
  23. self._shader = None
  24. self._scene = Application.getInstance().getController().getScene()
  25. # Set the camera to be used by this render pass
  26. # if it's None, the active camera is used
  27. def setCamera(self, camera: Optional["Camera"]):
  28. self._camera = camera
  29. def render(self) -> None:
  30. if not self._shader:
  31. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  32. self._shader.setUniformValue("u_overhangAngle", 1.0)
  33. self._gl.glClearColor(0.0, 0.0, 0.0, 0.0)
  34. self._gl.glClear(self._gl.GL_COLOR_BUFFER_BIT | self._gl.GL_DEPTH_BUFFER_BIT)
  35. # Create a new batch to be rendered
  36. batch = RenderBatch(self._shader)
  37. # Fill up the batch with objects that can be sliced. `
  38. for node in DepthFirstIterator(self._scene.getRoot()):
  39. if node.callDecoration("isSliceable") and node.getMeshData() and node.isVisible():
  40. uniforms = {}
  41. uniforms["diffuse_color"] = node.getDiffuseColor()
  42. batch.addItem(node.getWorldTransformation(), node.getMeshData(), uniforms = uniforms)
  43. self.bind()
  44. if self._camera is None:
  45. batch.render(Application.getInstance().getController().getScene().getActiveCamera())
  46. else:
  47. batch.render(self._camera)
  48. self.release()