SolidView.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (c) 2015 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.Settings.Validator import ValidatorState
  10. from UM.Math.Color import Color
  11. from UM.View.GL.OpenGL import OpenGL
  12. from cura.Settings.ExtruderManager import ExtruderManager
  13. import math
  14. ## Standard view for mesh models.
  15. class SolidView(View):
  16. def __init__(self):
  17. super().__init__()
  18. Application.getInstance().getPreferences().addPreference("view/show_overhang", True)
  19. self._enabled_shader = None
  20. self._disabled_shader = None
  21. self._non_printing_shader = None
  22. self._support_mesh_shader = None
  23. self._extruders_model = None
  24. self._theme = None
  25. def beginRendering(self):
  26. scene = self.getController().getScene()
  27. renderer = self.getRenderer()
  28. if not self._extruders_model:
  29. self._extruders_model = Application.getInstance().getExtrudersModel()
  30. if not self._theme:
  31. self._theme = Application.getInstance().getTheme()
  32. if not self._enabled_shader:
  33. self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  34. self._enabled_shader.setUniformValue("u_overhangColor", Color(*self._theme.getColor("model_overhang").getRgb()))
  35. if not self._disabled_shader:
  36. self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  37. self._disabled_shader.setUniformValue("u_diffuseColor1", Color(*self._theme.getColor("model_unslicable").getRgb()))
  38. self._disabled_shader.setUniformValue("u_diffuseColor2", Color(*self._theme.getColor("model_unslicable_alt").getRgb()))
  39. self._disabled_shader.setUniformValue("u_width", 50.0)
  40. if not self._non_printing_shader:
  41. self._non_printing_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  42. self._non_printing_shader.setUniformValue("u_diffuseColor", Color(*self._theme.getColor("model_non_printing").getRgb()))
  43. self._non_printing_shader.setUniformValue("u_opacity", 0.6)
  44. if not self._support_mesh_shader:
  45. self._support_mesh_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  46. self._support_mesh_shader.setUniformValue("u_vertical_stripes", True)
  47. self._support_mesh_shader.setUniformValue("u_width", 5.0)
  48. global_container_stack = Application.getInstance().getGlobalContainerStack()
  49. if global_container_stack:
  50. support_extruder_nr = global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr")
  51. support_angle_stack = Application.getInstance().getExtruderManager().getExtruderStack(support_extruder_nr)
  52. if support_angle_stack is not None and Application.getInstance().getPreferences().getValue("view/show_overhang"):
  53. angle = support_angle_stack.getProperty("support_angle", "value")
  54. # Make sure the overhang angle is valid before passing it to the shader
  55. if angle is not None and angle >= 0 and angle <= 90:
  56. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - angle)))
  57. else:
  58. 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.
  59. else:
  60. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))
  61. for node in DepthFirstIterator(scene.getRoot()):
  62. if not node.render(renderer):
  63. if node.getMeshData() and node.isVisible() and not node.callDecoration("getLayerData"):
  64. uniforms = {}
  65. shade_factor = 1.0
  66. per_mesh_stack = node.callDecoration("getStack")
  67. extruder_index = node.callDecoration("getActiveExtruderPosition")
  68. if extruder_index is None:
  69. extruder_index = "0"
  70. extruder_index = int(extruder_index)
  71. # Use the support extruder instead of the active extruder if this is a support_mesh
  72. if per_mesh_stack:
  73. if per_mesh_stack.getProperty("support_mesh", "value"):
  74. extruder_index = int(global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  75. try:
  76. material_color = self._extruders_model.getItem(extruder_index)["color"]
  77. except KeyError:
  78. material_color = self._extruders_model.defaultColors[0]
  79. if extruder_index != ExtruderManager.getInstance().activeExtruderIndex:
  80. # Shade objects that are printed with the non-active extruder 25% darker
  81. shade_factor = 0.6
  82. try:
  83. # Colors are passed as rgb hex strings (eg "#ffffff"), and the shader needs
  84. # an rgba list of floats (eg [1.0, 1.0, 1.0, 1.0])
  85. uniforms["diffuse_color"] = [
  86. shade_factor * int(material_color[1:3], 16) / 255,
  87. shade_factor * int(material_color[3:5], 16) / 255,
  88. shade_factor * int(material_color[5:7], 16) / 255,
  89. 1.0
  90. ]
  91. except ValueError:
  92. pass
  93. if node.callDecoration("isNonPrintingMesh"):
  94. if per_mesh_stack and (per_mesh_stack.getProperty("infill_mesh", "value") or per_mesh_stack.getProperty("cutting_mesh", "value")):
  95. renderer.queueNode(node, shader = self._non_printing_shader, uniforms = uniforms, transparent = True)
  96. else:
  97. renderer.queueNode(node, shader = self._non_printing_shader, transparent = True)
  98. elif getattr(node, "_outside_buildarea", False):
  99. renderer.queueNode(node, shader = self._disabled_shader)
  100. elif per_mesh_stack and per_mesh_stack.getProperty("support_mesh", "value"):
  101. # Render support meshes with a vertical stripe that is darker
  102. shade_factor = 0.6
  103. uniforms["diffuse_color_2"] = [
  104. uniforms["diffuse_color"][0] * shade_factor,
  105. uniforms["diffuse_color"][1] * shade_factor,
  106. uniforms["diffuse_color"][2] * shade_factor,
  107. 1.0
  108. ]
  109. renderer.queueNode(node, shader = self._support_mesh_shader, uniforms = uniforms)
  110. else:
  111. renderer.queueNode(node, shader = self._enabled_shader, uniforms = uniforms)
  112. if node.callDecoration("isGroup") and Selection.isSelected(node):
  113. renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(), mode = RenderBatch.RenderMode.LineLoop)
  114. def endRendering(self):
  115. pass