SolidView.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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._extruders_model = ExtrudersModel()
  24. def beginRendering(self):
  25. scene = self.getController().getScene()
  26. renderer = self.getRenderer()
  27. if not self._enabled_shader:
  28. self._enabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "overhang.shader"))
  29. theme = Application.getInstance().getTheme()
  30. self._enabled_shader.setUniformValue("u_overhangColor", Color(*theme.getColor("model_overhang").getRgb()))
  31. if not self._disabled_shader:
  32. self._disabled_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "striped.shader"))
  33. theme = Application.getInstance().getTheme()
  34. self._disabled_shader.setUniformValue("u_diffuseColor1", Color(*theme.getColor("model_unslicable").getRgb()))
  35. self._disabled_shader.setUniformValue("u_diffuseColor2", Color(*theme.getColor("model_unslicable_alt").getRgb()))
  36. self._disabled_shader.setUniformValue("u_width", 50.0)
  37. multi_extrusion = False
  38. global_container_stack = Application.getInstance().getGlobalContainerStack()
  39. if global_container_stack:
  40. multi_extrusion = global_container_stack.getProperty("machine_extruder_count", "value") > 1
  41. if multi_extrusion:
  42. support_extruder_nr = global_container_stack.getProperty("support_extruder_nr", "value")
  43. support_angle_stack = ExtruderManager.getInstance().getExtruderStack(support_extruder_nr)
  44. if not support_angle_stack:
  45. support_angle_stack = global_container_stack
  46. else:
  47. support_angle_stack = global_container_stack
  48. if Preferences.getInstance().getValue("view/show_overhang"):
  49. angle = support_angle_stack.getProperty("support_angle", "value")
  50. # Make sure the overhang angle is valid before passing it to the shader
  51. # Note: if the overhang angle is set to its default value, it does not need to get validated (validationState = None)
  52. if angle is not None and global_container_stack.getProperty("support_angle", "validationState") in [None, ValidatorState.Valid]:
  53. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(90 - angle)))
  54. else:
  55. 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.
  56. else:
  57. self._enabled_shader.setUniformValue("u_overhangAngle", math.cos(math.radians(0)))
  58. for node in DepthFirstIterator(scene.getRoot()):
  59. if not node.render(renderer):
  60. if node.getMeshData() and node.isVisible():
  61. uniforms = {}
  62. shade_factor = 1.0
  63. if not multi_extrusion:
  64. if global_container_stack:
  65. material = global_container_stack.findContainer({ "type": "material" })
  66. material_color = material.getMetaDataEntry("color_code", default = self._extruders_model.defaultColors[0]) if material else self._extruders_model.defaultColors[0]
  67. else:
  68. material_color = self._extruders_model.defaultColors[0]
  69. else:
  70. # Get color to render this mesh in from ExtrudersModel
  71. extruder_index = 0
  72. extruder_id = node.callDecoration("getActiveExtruder")
  73. if extruder_id:
  74. extruder_index = max(0, self._extruders_model.find("id", extruder_id))
  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 hasattr(node, "_outside_buildarea"):
  94. if node._outside_buildarea:
  95. renderer.queueNode(node, shader = self._disabled_shader)
  96. else:
  97. renderer.queueNode(node, shader = self._enabled_shader, uniforms = uniforms)
  98. else:
  99. renderer.queueNode(node, material = self._enabled_shader, uniforms = uniforms)
  100. if node.callDecoration("isGroup") and Selection.isSelected(node):
  101. renderer.queueNode(scene.getRoot(), mesh = node.getBoundingBoxMesh(), mode = RenderBatch.RenderMode.LineLoop)
  102. def endRendering(self):
  103. pass
  104. #def _onPreferenceChanged(self, preference):
  105. #if preference == "view/show_overhang": ## Todo: This a printer only setting. Should be removed from Uranium.
  106. #self._enabled_material = None