SolidView.py 7.7 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.Preferences import Preferences
  9. from UM.View.RenderBatch import RenderBatch
  10. from UM.Settings.Validator import ValidatorState
  11. from UM.Math.Color import Color
  12. from UM.View.GL.OpenGL import OpenGL
  13. from cura.Settings.ExtruderManager import ExtruderManager
  14. from cura.Settings.ExtrudersModel import ExtrudersModel
  15. import math
  16. ## Standard view for mesh models.
  17. class SolidView(View):
  18. def __init__(self):
  19. super().__init__()
  20. Preferences.getInstance().addPreference("view/show_overhang", True)
  21. self._enabled_shader = None
  22. self._disabled_shader = None
  23. self._non_printing_shader = None
  24. self._support_mesh_shader = None
  25. self._extruders_model = ExtrudersModel()
  26. self._theme = None
  27. def beginRendering(self):
  28. scene = self.getController().getScene()
  29. renderer = self.getRenderer()
  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 Preferences.getInstance().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. # Note: if the overhang angle is set to its default value, it does not need to get validated (validationState = None)
  56. if angle is not None and global_container_stack.getProperty("support_angle", "validationState") in [None, ValidatorState.Valid]:
  57. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - angle)))
  58. else:
  59. 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.
  60. else:
  61. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))
  62. for node in DepthFirstIterator(scene.getRoot()):
  63. if not node.render(renderer):
  64. if node.getMeshData() and node.isVisible() and not node.callDecoration("getLayerData"):
  65. uniforms = {}
  66. shade_factor = 1.0
  67. per_mesh_stack = node.callDecoration("getStack")
  68. extruder_index = node.callDecoration("getActiveExtruderPosition")
  69. if extruder_index is None:
  70. extruder_index = "0"
  71. extruder_index = int(extruder_index)
  72. # Use the support extruder instead of the active extruder if this is a support_mesh
  73. if per_mesh_stack:
  74. if per_mesh_stack.getProperty("support_mesh", "value"):
  75. extruder_index = int(global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  76. try:
  77. material_color = self._extruders_model.getItem(extruder_index)["color"]
  78. except KeyError:
  79. material_color = self._extruders_model.defaultColors[0]
  80. if extruder_index != ExtruderManager.getInstance().activeExtruderIndex:
  81. # Shade objects that are printed with the non-active extruder 25% darker
  82. shade_factor = 0.6
  83. try:
  84. # Colors are passed as rgb hex strings (eg "#ffffff"), and the shader needs
  85. # an rgba list of floats (eg [1.0, 1.0, 1.0, 1.0])
  86. uniforms["diffuse_color"] = [
  87. shade_factor * int(material_color[1:3], 16) / 255,
  88. shade_factor * int(material_color[3:5], 16) / 255,
  89. shade_factor * int(material_color[5:7], 16) / 255,
  90. 1.0
  91. ]
  92. except ValueError:
  93. pass
  94. if node.callDecoration("isNonPrintingMesh"):
  95. if per_mesh_stack and (per_mesh_stack.getProperty("infill_mesh", "value") or per_mesh_stack.getProperty("cutting_mesh", "value")):
  96. renderer.queueNode(node, shader = self._non_printing_shader, uniforms = uniforms, transparent = True)
  97. else:
  98. renderer.queueNode(node, shader = self._non_printing_shader, transparent = True)
  99. elif getattr(node, "_outside_buildarea", False):
  100. renderer.queueNode(node, shader = self._disabled_shader)
  101. elif per_mesh_stack and per_mesh_stack.getProperty("support_mesh", "value"):
  102. # Render support meshes with a vertical stripe that is darker
  103. shade_factor = 0.6
  104. uniforms["diffuse_color_2"] = [
  105. uniforms["diffuse_color"][0] * shade_factor,
  106. uniforms["diffuse_color"][1] * shade_factor,
  107. uniforms["diffuse_color"][2] * shade_factor,
  108. 1.0
  109. ]
  110. renderer.queueNode(node, shader = self._support_mesh_shader, uniforms = uniforms)
  111. else:
  112. renderer.queueNode(node, shader = self._enabled_shader, uniforms = uniforms)
  113. if node.callDecoration("isGroup") and Selection.isSelected(node):
  114. renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(), mode = RenderBatch.RenderMode.LineLoop)
  115. def endRendering(self):
  116. pass