ConvexHullNode.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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(0.4, 0.4, 0.4, 1.0)
  20. # The y-coordinate of the convex hull mesh. Must not be 0, to prevent z-fighting.
  21. self._mesh_height = 0.1
  22. self._thickness = thickness
  23. # The node this mesh is "watching"
  24. self._node = node
  25. self._convex_hull_head_mesh = None
  26. self._node.decoratorsChanged.connect(self._onNodeDecoratorsChanged)
  27. self._onNodeDecoratorsChanged(self._node)
  28. self._hull = hull
  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. def getHull(self):
  37. return self._hull
  38. def getThickness(self):
  39. return self._thickness
  40. def getWatchedNode(self):
  41. return self._node
  42. def render(self, renderer):
  43. if not self._shader:
  44. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  45. self._shader.setUniformValue("u_diffuseColor", self._color)
  46. self._shader.setUniformValue("u_opacity", 0.6)
  47. if self.getParent():
  48. if self.getMeshData():
  49. renderer.queueNode(self, transparent = True, shader = self._shader, backface_cull = True, sort = -8)
  50. if self._convex_hull_head_mesh:
  51. renderer.queueNode(self, shader = self._shader, transparent = True, mesh = self._convex_hull_head_mesh, backface_cull = True, sort = -8)
  52. return True
  53. def _onNodeDecoratorsChanged(self, node):
  54. self._color = Color(35, 35, 35, 0.5)
  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