PreviewPass.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. # Make color brighter by normalizing it (maximum factor 2.5 brighter)
  15. # color_list is a list of 4 elements: [r, g, b, a], each element is a float 0..1
  16. def prettier_color(color_list):
  17. maximum = max(color_list[:3])
  18. if maximum > 0:
  19. factor = min(1 / maximum, 2.5)
  20. else:
  21. factor = 1.0
  22. return [min(i * factor, 1.0) for i in color_list]
  23. ## A render pass subclass that renders slicable objects with default parameters.
  24. # It uses the active camera by default, but it can be overridden to use a different camera.
  25. #
  26. # This is useful to get a preview image of a scene taken from a different location as the active camera.
  27. class PreviewPass(RenderPass):
  28. def __init__(self, width: int, height: int):
  29. super().__init__("preview", width, height, 0)
  30. self._camera = None # type: Optional[Camera]
  31. self._renderer = Application.getInstance().getRenderer()
  32. self._shader = None
  33. self._scene = Application.getInstance().getController().getScene()
  34. # Set the camera to be used by this render pass
  35. # if it's None, the active camera is used
  36. def setCamera(self, camera: Optional["Camera"]):
  37. self._camera = camera
  38. def render(self) -> None:
  39. if not self._shader:
  40. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  41. self._shader.setUniformValue("u_overhangAngle", 1.0)
  42. self._shader.setUniformValue("u_ambientColor", [0.1, 0.1, 0.1, 1.0])
  43. self._shader.setUniformValue("u_specularColor", [0.6, 0.6, 0.6, 1.0])
  44. self._shader.setUniformValue("u_shininess", 20.0)
  45. self._gl.glClearColor(0.0, 0.0, 0.0, 0.0)
  46. self._gl.glClear(self._gl.GL_COLOR_BUFFER_BIT | self._gl.GL_DEPTH_BUFFER_BIT)
  47. # Create a new batch to be rendered
  48. batch = RenderBatch(self._shader)
  49. # Fill up the batch with objects that can be sliced. `
  50. for node in DepthFirstIterator(self._scene.getRoot()):
  51. if node.callDecoration("isSliceable") and node.getMeshData() and node.isVisible():
  52. uniforms = {}
  53. uniforms["diffuse_color"] = prettier_color(node.getDiffuseColor())
  54. batch.addItem(node.getWorldTransformation(), node.getMeshData(), uniforms = uniforms)
  55. self.bind()
  56. if self._camera is None:
  57. batch.render(Application.getInstance().getController().getScene().getActiveCamera())
  58. else:
  59. batch.render(self._camera)
  60. self.release()