ConvexHullNode.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Scene.SceneNode import SceneNode
  4. from UM.Resources import Resources
  5. from UM.Math.Color import Color
  6. from UM.Mesh.MeshBuilder import MeshBuilder # To create a mesh to display the convex hull with.
  7. from UM.View.GL.OpenGL import OpenGL
  8. class ConvexHullNode(SceneNode):
  9. ## Convex hull node is a special type of scene node that is used to display an area, to indicate the
  10. # location an object uses on the buildplate. This area (or area's in case of one at a time printing) is
  11. # then displayed as a transparent shadow. If the adhesion type is set to raft, the area is extruded
  12. # to represent the raft as well.
  13. def __init__(self, node, hull, thickness, parent = None):
  14. super().__init__(parent)
  15. self.setCalculateBoundingBox(False)
  16. self._shader = None
  17. self._original_parent = parent
  18. # Color of the drawn convex hull
  19. self._color = Color(35, 35, 35, 192)
  20. # The y-coordinate of the convex hull mesh. Must not be 0, to prevent z-fighting.
  21. self._mesh_height = 0.1
  22. # The node this mesh is "watching"
  23. self._node = node
  24. self._node.decoratorsChanged.connect(self._onNodeDecoratorsChanged)
  25. self._onNodeDecoratorsChanged(self._node)
  26. self._convex_hull_head_mesh = None
  27. self._hull = hull
  28. self._thickness = thickness
  29. if self._hull:
  30. hull_mesh_builder = MeshBuilder()
  31. if hull_mesh_builder.addConvexPolygonExtrusion(
  32. self._hull.getPoints()[::-1], # bottom layer is reversed
  33. self._mesh_height-thickness, self._mesh_height, color=self._color):
  34. hull_mesh = hull_mesh_builder.build()
  35. self.setMeshData(hull_mesh)
  36. convex_hull_head = self._node.callDecoration("getConvexHullHead")
  37. if convex_hull_head:
  38. convex_hull_head_builder = MeshBuilder()
  39. convex_hull_head_builder.addConvexPolygon(convex_hull_head.getPoints(), self._mesh_height-thickness)
  40. self._convex_hull_head_mesh = convex_hull_head_builder.build()
  41. def getHull(self):
  42. return self._hull
  43. def getThickness(self):
  44. return self._thickness
  45. def getWatchedNode(self):
  46. return self._node
  47. def render(self, renderer):
  48. if not self._shader:
  49. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  50. self._shader.setUniformValue("u_diffuseColor", self._color)
  51. self._shader.setUniformValue("u_opacity", 0.6)
  52. if self.getParent():
  53. if self.getMeshData():
  54. renderer.queueNode(self, transparent = True, shader = self._shader, backface_cull = True, sort = -8)
  55. if self._convex_hull_head_mesh:
  56. renderer.queueNode(self, shader = self._shader, transparent = True, mesh = self._convex_hull_head_mesh, backface_cull = True, sort = -8)
  57. return True
  58. def _onNodeDecoratorsChanged(self, node):
  59. self._color = Color(35, 35, 35, 0.5)
  60. if not node:
  61. return