XRayView.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os.path
  4. from PyQt6.QtGui import QOpenGLContext, QImage
  5. from UM.Application import Application
  6. from UM.Logger import Logger
  7. from UM.Math.Color import Color
  8. from UM.Resources import Resources
  9. from UM.Platform import Platform
  10. from UM.Event import Event
  11. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  12. from UM.View.RenderBatch import RenderBatch
  13. from UM.View.GL.OpenGL import OpenGL
  14. from cura.CuraApplication import CuraApplication
  15. from cura.CuraView import CuraView
  16. from cura.Scene.ConvexHullNode import ConvexHullNode
  17. from cura import XRayPass
  18. class XRayView(CuraView):
  19. """View used to display a see-through version of objects with errors highlighted."""
  20. def __init__(self):
  21. super().__init__(parent = None, use_empty_menu_placeholder = True)
  22. self._xray_shader = None
  23. self._xray_pass = None
  24. self._xray_composite_shader = None
  25. self._composite_pass = None
  26. self._old_composite_shader = None
  27. self._old_layer_bindings = None
  28. def beginRendering(self):
  29. scene = self.getController().getScene()
  30. renderer = self.getRenderer()
  31. if not self._xray_shader:
  32. self._xray_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "xray.shader"))
  33. self._xray_shader.setUniformValue("u_color", Color(*Application.getInstance().getTheme().getColor("xray").getRgb()))
  34. for node in BreadthFirstIterator(scene.getRoot()):
  35. # We do not want to render ConvexHullNode as it conflicts with the bottom of the X-Ray (z-fighting).
  36. if type(node) is ConvexHullNode:
  37. continue
  38. if not node.render(renderer):
  39. if node.getMeshData() and node.isVisible():
  40. renderer.queueNode(node,
  41. shader = self._xray_shader,
  42. type = RenderBatch.RenderType.Solid,
  43. blend_mode = RenderBatch.BlendMode.Additive,
  44. sort = -10,
  45. state_setup_callback = lambda gl: gl.glDepthFunc(gl.GL_ALWAYS),
  46. state_teardown_callback = lambda gl: gl.glDepthFunc(gl.GL_LESS)
  47. )
  48. def endRendering(self):
  49. pass
  50. def event(self, event):
  51. if event.type == Event.ViewActivateEvent:
  52. # FIX: on Max OS X, somehow QOpenGLContext.currentContext() can become None during View switching.
  53. # This can happen when you do the following steps:
  54. # 1. Start Cura
  55. # 2. Load a model
  56. # 3. Switch to Custom mode
  57. # 4. Select the model and click on the per-object tool icon
  58. # 5. Switch view to Layer view or X-Ray
  59. # 6. Cura will very likely crash
  60. # It seems to be a timing issue that the currentContext can somehow be empty, but I have no clue why.
  61. # This fix tries to reschedule the view changing event call on the Qt thread again if the current OpenGL
  62. # context is None.
  63. if Platform.isOSX():
  64. if QOpenGLContext.currentContext() is None:
  65. Logger.log("d", "current context of OpenGL is empty on Mac OS X, will try to create shaders later")
  66. CuraApplication.getInstance().callLater(lambda e = event: self.event(e))
  67. return
  68. if not self._xray_pass:
  69. # Currently the RenderPass constructor requires a size > 0
  70. # This should be fixed in RenderPass's constructor.
  71. self._xray_pass = XRayPass.XRayPass(1, 1)
  72. self.getRenderer().addRenderPass(self._xray_pass)
  73. if not self._xray_composite_shader:
  74. self._xray_composite_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "xray_composite.shader"))
  75. theme = Application.getInstance().getTheme()
  76. self._xray_composite_shader.setUniformValue("u_background_color", Color(*theme.getColor("viewport_background").getRgb()))
  77. self._xray_composite_shader.setUniformValue("u_outline_color", Color(*theme.getColor("model_selection_outline").getRgb()))
  78. self._xray_composite_shader.setUniformValue("u_flat_error_color_mix", 1.) # Show flat error color _only_ in xray-view.
  79. if not self._composite_pass:
  80. self._composite_pass = self.getRenderer().getRenderPass("composite")
  81. self._old_layer_bindings = self._composite_pass.getLayerBindings()
  82. self._composite_pass.setLayerBindings(["default", "selection", "xray"])
  83. self._old_composite_shader = self._composite_pass.getCompositeShader()
  84. self._composite_pass.setCompositeShader(self._xray_composite_shader)
  85. if event.type == Event.ViewDeactivateEvent:
  86. self.getRenderer().removeRenderPass(self._xray_pass)
  87. self._composite_pass.setLayerBindings(self._old_layer_bindings)
  88. self._composite_pass.setCompositeShader(self._old_composite_shader)