ConvexHullNode.py 3.5 KB

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