SolidView.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = int(global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  50. try:
  51. support_angle_stack = global_container_stack.extruderList[support_extruder_nr]
  52. except IndexError:
  53. pass
  54. else:
  55. self._support_angle = support_angle_stack.getProperty("support_angle", "value")
  56. def _checkSetup(self):
  57. if not self._extruders_model:
  58. self._extruders_model = Application.getInstance().getExtrudersModel()
  59. if not self._theme:
  60. self._theme = Application.getInstance().getTheme()
  61. if not self._enabled_shader:
  62. self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  63. self._enabled_shader.setUniformValue("u_overhangColor", Color(*self._theme.getColor("model_overhang").getRgb()))
  64. if not self._disabled_shader:
  65. self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  66. self._disabled_shader.setUniformValue("u_diffuseColor1", Color(*self._theme.getColor("model_unslicable").getRgb()))
  67. self._disabled_shader.setUniformValue("u_diffuseColor2", Color(*self._theme.getColor("model_unslicable_alt").getRgb()))
  68. self._disabled_shader.setUniformValue("u_width", 50.0)
  69. if not self._non_printing_shader:
  70. self._non_printing_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  71. self._non_printing_shader.setUniformValue("u_diffuseColor", Color(*self._theme.getColor("model_non_printing").getRgb()))
  72. self._non_printing_shader.setUniformValue("u_opacity", 0.6)
  73. if not self._support_mesh_shader:
  74. self._support_mesh_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  75. self._support_mesh_shader.setUniformValue("u_vertical_stripes", True)
  76. self._support_mesh_shader.setUniformValue("u_width", 5.0)
  77. def beginRendering(self):
  78. scene = self.getController().getScene()
  79. renderer = self.getRenderer()
  80. self._checkSetup()
  81. global_container_stack = Application.getInstance().getGlobalContainerStack()
  82. if global_container_stack:
  83. if Application.getInstance().getPreferences().getValue("view/show_overhang"):
  84. # Make sure the overhang angle is valid before passing it to the shader
  85. if self._support_angle is not None and self._support_angle >= 0 and self._support_angle <= 90:
  86. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - self._support_angle)))
  87. else:
  88. 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.
  89. else:
  90. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))
  91. for node in DepthFirstIterator(scene.getRoot()):
  92. if not node.render(renderer):
  93. if node.getMeshData() and node.isVisible() and not node.callDecoration("getLayerData"):
  94. uniforms = {}
  95. shade_factor = 1.0
  96. per_mesh_stack = node.callDecoration("getStack")
  97. extruder_index = node.callDecoration("getActiveExtruderPosition")
  98. if extruder_index is None:
  99. extruder_index = "0"
  100. extruder_index = int(extruder_index)
  101. # Use the support extruder instead of the active extruder if this is a support_mesh
  102. if per_mesh_stack:
  103. if per_mesh_stack.getProperty("support_mesh", "value"):
  104. extruder_index = int(global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  105. try:
  106. material_color = self._extruders_model.getItem(extruder_index)["color"]
  107. except KeyError:
  108. material_color = self._extruders_model.defaultColors[0]
  109. if extruder_index != ExtruderManager.getInstance().activeExtruderIndex:
  110. # Shade objects that are printed with the non-active extruder 25% darker
  111. shade_factor = 0.6
  112. try:
  113. # Colors are passed as rgb hex strings (eg "#ffffff"), and the shader needs
  114. # an rgba list of floats (eg [1.0, 1.0, 1.0, 1.0])
  115. uniforms["diffuse_color"] = [
  116. shade_factor * int(material_color[1:3], 16) / 255,
  117. shade_factor * int(material_color[3:5], 16) / 255,
  118. shade_factor * int(material_color[5:7], 16) / 255,
  119. 1.0
  120. ]
  121. # Color the currently selected face-id. (Disable for now.)
  122. #face = Selection.getHoverFace()
  123. uniforms["hover_face"] = -1 #if not face or node != face[0] else face[1]
  124. except ValueError:
  125. pass
  126. if node.callDecoration("isNonPrintingMesh"):
  127. if per_mesh_stack and (per_mesh_stack.getProperty("infill_mesh", "value") or per_mesh_stack.getProperty("cutting_mesh", "value")):
  128. renderer.queueNode(node, shader = self._non_printing_shader, uniforms = uniforms, transparent = True)
  129. else:
  130. renderer.queueNode(node, shader = self._non_printing_shader, transparent = True)
  131. elif getattr(node, "_outside_buildarea", False):
  132. renderer.queueNode(node, shader = self._disabled_shader)
  133. elif per_mesh_stack and per_mesh_stack.getProperty("support_mesh", "value"):
  134. # Render support meshes with a vertical stripe that is darker
  135. shade_factor = 0.6
  136. uniforms["diffuse_color_2"] = [
  137. uniforms["diffuse_color"][0] * shade_factor,
  138. uniforms["diffuse_color"][1] * shade_factor,
  139. uniforms["diffuse_color"][2] * shade_factor,
  140. 1.0
  141. ]
  142. renderer.queueNode(node, shader = self._support_mesh_shader, uniforms = uniforms)
  143. else:
  144. renderer.queueNode(node, shader = self._enabled_shader, uniforms = uniforms)
  145. if node.callDecoration("isGroup") and Selection.isSelected(node):
  146. renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(), mode = RenderBatch.RenderMode.LineLoop)
  147. def endRendering(self):
  148. pass