SolidView.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. from cura.Settings.ExtrudersModel import ExtrudersModel
  14. import math
  15. ## Standard view for mesh models.
  16. class SolidView(View):
  17. def __init__(self):
  18. super().__init__()
  19. Application.getInstance().getPreferences().addPreference("view/show_overhang", True)
  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 = ExtrudersModel()
  25. self._theme = None
  26. def beginRendering(self):
  27. scene = self.getController().getScene()
  28. renderer = self.getRenderer()
  29. if not self._theme:
  30. self._theme = Application.getInstance().getTheme()
  31. if not self._enabled_shader:
  32. self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  33. self._enabled_shader.setUniformValue("u_overhangColor", Color(*self._theme.getColor("model_overhang").getRgb()))
  34. if not self._disabled_shader:
  35. self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  36. self._disabled_shader.setUniformValue("u_diffuseColor1", Color(*self._theme.getColor("model_unslicable").getRgb()))
  37. self._disabled_shader.setUniformValue("u_diffuseColor2", Color(*self._theme.getColor("model_unslicable_alt").getRgb()))
  38. self._disabled_shader.setUniformValue("u_width", 50.0)
  39. if not self._non_printing_shader:
  40. self._non_printing_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  41. self._non_printing_shader.setUniformValue("u_diffuseColor", Color(*self._theme.getColor("model_non_printing").getRgb()))
  42. self._non_printing_shader.setUniformValue("u_opacity", 0.6)
  43. if not self._support_mesh_shader:
  44. self._support_mesh_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  45. self._support_mesh_shader.setUniformValue("u_vertical_stripes", True)
  46. self._support_mesh_shader.setUniformValue("u_width", 5.0)
  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 = Application.getInstance().getExtruderManager().getExtruderStack(support_extruder_nr)
  51. if support_angle_stack is not None and Application.getInstance().getPreferences().getValue("view/show_overhang"):
  52. angle = support_angle_stack.getProperty("support_angle", "value")
  53. # Make sure the overhang angle is valid before passing it to the shader
  54. # Note: if the overhang angle is set to its default value, it does not need to get validated (validationState = None)
  55. if angle is not None and global_container_stack.getProperty("support_angle", "validationState") in [None, ValidatorState.Valid]:
  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