XRayView.py 5.3 KB

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