PreviewPass.py 4.9 KB

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