PreviewPass.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, TYPE_CHECKING, cast, List
  4. from UM.Application import Application
  5. from UM.Logger import Logger
  6. from UM.Resources import Resources
  7. from UM.View.RenderPass import RenderPass
  8. from UM.View.GL.OpenGL import OpenGL
  9. from UM.View.RenderBatch import RenderBatch
  10. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  11. from cura.Scene.CuraSceneNode import CuraSceneNode
  12. if TYPE_CHECKING:
  13. from UM.View.GL.ShaderProgram import ShaderProgram
  14. from UM.Scene.Camera import Camera
  15. def prettier_color(color_list: List[float]) -> List[float]:
  16. """Make color brighter by normalizing
  17. maximum factor 2.5 brighter
  18. :param color_list: a list of 4 elements: [r, g, b, a], each element is a float 0..1
  19. :return: a normalized list of 4 elements: [r, g, b, a], each element is a float 0..1
  20. """
  21. maximum = max(color_list[:3])
  22. if maximum > 0:
  23. factor = min(1 / maximum, 2.5)
  24. else:
  25. factor = 1.0
  26. return [min(i * factor, 1.0) for i in color_list]
  27. class PreviewPass(RenderPass):
  28. """A :py:class:`Uranium.UM.View.RenderPass` subclass that renders slicable objects with default parameters.
  29. It uses the active camera by default, but it can be overridden to use a different camera.
  30. This is useful to get a preview image of a scene taken from a different location as the active camera.
  31. """
  32. def __init__(self, width: int, height: int) -> None:
  33. super().__init__("preview", width, height, 0)
  34. self._camera = None # type: Optional[Camera]
  35. self._renderer = Application.getInstance().getRenderer()
  36. self._shader = None # type: Optional[ShaderProgram]
  37. self._non_printing_shader = None # type: Optional[ShaderProgram]
  38. self._support_mesh_shader = None # type: Optional[ShaderProgram]
  39. self._scene = Application.getInstance().getController().getScene()
  40. # Set the camera to be used by this render pass
  41. # if it's None, the active camera is used
  42. def setCamera(self, camera: Optional["Camera"]):
  43. self._camera = camera
  44. def render(self) -> None:
  45. if not self._shader:
  46. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  47. if self._shader:
  48. self._shader.setUniformValue("u_overhangAngle", 1.0)
  49. self._shader.setUniformValue("u_ambientColor", [0.1, 0.1, 0.1, 1.0])
  50. self._shader.setUniformValue("u_specularColor", [0.6, 0.6, 0.6, 1.0])
  51. self._shader.setUniformValue("u_shininess", 20.0)
  52. self._shader.setUniformValue("u_renderError", 0.0) # We don't want any error markers!.
  53. self._shader.setUniformValue("u_faceId", -1) # Don't render any selected faces in the preview.
  54. else:
  55. Logger.error("Unable to compile shader program: overhang.shader")
  56. if not self._non_printing_shader:
  57. self._non_printing_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  58. if self._non_printing_shader:
  59. self._non_printing_shader.setUniformValue("u_diffuseColor", [0.5, 0.5, 0.5, 0.5])
  60. self._non_printing_shader.setUniformValue("u_opacity", 0.6)
  61. if not self._support_mesh_shader:
  62. self._support_mesh_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  63. if self._support_mesh_shader:
  64. self._support_mesh_shader.setUniformValue("u_vertical_stripes", True)
  65. self._support_mesh_shader.setUniformValue("u_width", 5.0)
  66. self._gl.glClearColor(0.0, 0.0, 0.0, 0.0)
  67. self._gl.glClear(self._gl.GL_COLOR_BUFFER_BIT | self._gl.GL_DEPTH_BUFFER_BIT)
  68. # Create batches to be rendered
  69. batch = RenderBatch(self._shader)
  70. batch_support_mesh = RenderBatch(self._support_mesh_shader)
  71. # Fill up the batch with objects that can be sliced.
  72. for node in DepthFirstIterator(self._scene.getRoot()):
  73. if hasattr(node, "_outside_buildarea") and not getattr(node, "_outside_buildarea"):
  74. if node.callDecoration("isSliceable") and node.getMeshData() and node.isVisible():
  75. per_mesh_stack = node.callDecoration("getStack")
  76. if node.callDecoration("isNonThumbnailVisibleMesh"):
  77. # Non printing mesh
  78. continue
  79. elif per_mesh_stack is not None and per_mesh_stack.getProperty("support_mesh", "value"):
  80. # Support mesh
  81. uniforms = {}
  82. shade_factor = 0.6
  83. diffuse_color = cast(CuraSceneNode, node).getDiffuseColor()
  84. diffuse_color2 = [
  85. diffuse_color[0] * shade_factor,
  86. diffuse_color[1] * shade_factor,
  87. diffuse_color[2] * shade_factor,
  88. 1.0]
  89. uniforms["diffuse_color"] = prettier_color(diffuse_color)
  90. uniforms["diffuse_color_2"] = diffuse_color2
  91. batch_support_mesh.addItem(node.getWorldTransformation(copy = False), node.getMeshData(), uniforms = uniforms)
  92. else:
  93. # Normal scene node
  94. uniforms = {}
  95. uniforms["diffuse_color"] = prettier_color(cast(CuraSceneNode, node).getDiffuseColor())
  96. batch.addItem(node.getWorldTransformation(copy = False), node.getMeshData(), uniforms = uniforms)
  97. self.bind()
  98. if self._camera is None:
  99. render_camera = Application.getInstance().getController().getScene().getActiveCamera()
  100. else:
  101. render_camera = self._camera
  102. batch.render(render_camera)
  103. batch_support_mesh.render(render_camera)
  104. self.release()