XRayView.py 5.2 KB

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