SolidView.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.View.View import View
  4. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  5. from UM.Scene.Selection import Selection
  6. from UM.Resources import Resources
  7. from UM.Application import Application
  8. from UM.View.RenderBatch import RenderBatch
  9. from UM.Math.Color import Color
  10. from UM.View.GL.OpenGL import OpenGL
  11. from cura.Settings.ExtruderManager import ExtruderManager
  12. import math
  13. ## Standard view for mesh models.
  14. class SolidView(View):
  15. def __init__(self):
  16. super().__init__()
  17. application = Application.getInstance()
  18. application.getPreferences().addPreference("view/show_overhang", True)
  19. application.globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  20. self._enabled_shader = None
  21. self._disabled_shader = None
  22. self._non_printing_shader = None
  23. self._support_mesh_shader = None
  24. self._extruders_model = None
  25. self._theme = None
  26. self._support_angle = 90
  27. self._global_stack = None
  28. Application.getInstance().engineCreatedSignal.connect(self._onGlobalContainerChanged)
  29. def _onGlobalContainerChanged(self) -> None:
  30. if self._global_stack:
  31. try:
  32. self._global_stack.propertyChanged.disconnect(self._onPropertyChanged)
  33. except TypeError:
  34. pass
  35. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  36. extruder_stack.propertyChanged.disconnect(self._onPropertyChanged)
  37. self._global_stack = Application.getInstance().getGlobalContainerStack()
  38. if self._global_stack:
  39. self._global_stack.propertyChanged.connect(self._onPropertyChanged)
  40. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  41. extruder_stack.propertyChanged.connect(self._onPropertyChanged)
  42. self._onPropertyChanged("support_angle", "value") # Force an re-evaluation
  43. def _onPropertyChanged(self, key: str, property_name: str) -> None:
  44. if key != "support_angle" or property_name != "value":
  45. return
  46. # As the rendering is called a *lot* we really, dont want to re-evaluate the property every time. So we store em!
  47. global_container_stack = Application.getInstance().getGlobalContainerStack()
  48. if global_container_stack:
  49. support_extruder_nr = global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr")
  50. support_angle_stack = global_container_stack.extruders.get(str(support_extruder_nr))
  51. if support_angle_stack:
  52. self._support_angle = support_angle_stack.getProperty("support_angle", "value")
  53. def beginRendering(self):
  54. scene = self.getController().getScene()
  55. renderer = self.getRenderer()
  56. if not self._extruders_model:
  57. self._extruders_model = Application.getInstance().getExtrudersModel()
  58. if not self._theme:
  59. self._theme = Application.getInstance().getTheme()
  60. if not self._enabled_shader:
  61. self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  62. self._enabled_shader.setUniformValue("u_overhangColor", Color(*self._theme.getColor("model_overhang").getRgb()))
  63. if not self._disabled_shader:
  64. self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  65. self._disabled_shader.setUniformValue("u_diffuseColor1", Color(*self._theme.getColor("model_unslicable").getRgb()))
  66. self._disabled_shader.setUniformValue("u_diffuseColor2", Color(*self._theme.getColor("model_unslicable_alt").getRgb()))
  67. self._disabled_shader.setUniformValue("u_width", 50.0)
  68. if not self._non_printing_shader:
  69. self._non_printing_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  70. self._non_printing_shader.setUniformValue("u_diffuseColor", Color(*self._theme.getColor("model_non_printing").getRgb()))
  71. self._non_printing_shader.setUniformValue("u_opacity", 0.6)
  72. if not self._support_mesh_shader:
  73. self._support_mesh_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  74. self._support_mesh_shader.setUniformValue("u_vertical_stripes", True)
  75. self._support_mesh_shader.setUniformValue("u_width", 5.0)
  76. global_container_stack = Application.getInstance().getGlobalContainerStack()
  77. if global_container_stack:
  78. if Application.getInstance().getPreferences().getValue("view/show_overhang"):
  79. # Make sure the overhang angle is valid before passing it to the shader
  80. if self._support_angle is not None and self._support_angle >= 0 and self._support_angle <= 90:
  81. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - self._support_angle)))
  82. else:
  83. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0))) #Overhang angle of 0 causes no area at all to be marked as overhang.
  84. else:
  85. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))
  86. for node in DepthFirstIterator(scene.getRoot()):
  87. if not node.render(renderer):
  88. if node.getMeshData() and node.isVisible() and not node.callDecoration("getLayerData"):
  89. uniforms = {}
  90. shade_factor = 1.0
  91. per_mesh_stack = node.callDecoration("getStack")
  92. extruder_index = node.callDecoration("getActiveExtruderPosition")
  93. if extruder_index is None:
  94. extruder_index = "0"
  95. extruder_index = int(extruder_index)
  96. # Use the support extruder instead of the active extruder if this is a support_mesh
  97. if per_mesh_stack:
  98. if per_mesh_stack.getProperty("support_mesh", "value"):
  99. extruder_index = int(global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  100. try:
  101. material_color = self._extruders_model.getItem(extruder_index)["color"]
  102. except KeyError:
  103. material_color = self._extruders_model.defaultColors[0]
  104. if extruder_index != ExtruderManager.getInstance().activeExtruderIndex:
  105. # Shade objects that are printed with the non-active extruder 25% darker
  106. shade_factor = 0.6
  107. try:
  108. # Colors are passed as rgb hex strings (eg "#ffffff"), and the shader needs
  109. # an rgba list of floats (eg [1.0, 1.0, 1.0, 1.0])
  110. uniforms["diffuse_color"] = [
  111. shade_factor * int(material_color[1:3], 16) / 255,
  112. shade_factor * int(material_color[3:5], 16) / 255,
  113. shade_factor * int(material_color[5:7], 16) / 255,
  114. 1.0
  115. ]
  116. # Color the currently selected face-id. (Disable for now.)
  117. #face = Selection.getHoverFace()
  118. uniforms["hover_face"] = -1 #if not face or node != face[0] else face[1]
  119. except ValueError:
  120. pass
  121. if node.callDecoration("isNonPrintingMesh"):
  122. if per_mesh_stack and (per_mesh_stack.getProperty("infill_mesh", "value") or per_mesh_stack.getProperty("cutting_mesh", "value")):
  123. renderer.queueNode(node, shader = self._non_printing_shader, uniforms = uniforms, transparent = True)
  124. else:
  125. renderer.queueNode(node, shader = self._non_printing_shader, transparent = True)
  126. elif getattr(node, "_outside_buildarea", False):
  127. renderer.queueNode(node, shader = self._disabled_shader)
  128. elif per_mesh_stack and per_mesh_stack.getProperty("support_mesh", "value"):
  129. # Render support meshes with a vertical stripe that is darker
  130. shade_factor = 0.6
  131. uniforms["diffuse_color_2"] = [
  132. uniforms["diffuse_color"][0] * shade_factor,
  133. uniforms["diffuse_color"][1] * shade_factor,
  134. uniforms["diffuse_color"][2] * shade_factor,
  135. 1.0
  136. ]
  137. renderer.queueNode(node, shader = self._support_mesh_shader, uniforms = uniforms)
  138. else:
  139. renderer.queueNode(node, shader = self._enabled_shader, uniforms = uniforms)
  140. if node.callDecoration("isGroup") and Selection.isSelected(node):
  141. renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(), mode = RenderBatch.RenderMode.LineLoop)
  142. def endRendering(self):
  143. pass