PickingPass.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, TYPE_CHECKING
  4. from UM.Qt.QtApplication import QtApplication
  5. from UM.Math.Vector import Vector
  6. from UM.Resources import Resources
  7. from UM.View.RenderPass import RenderPass
  8. from UM.View.GL.OpenGL import OpenGL
  9. from UM.View.RenderBatch import RenderBatch
  10. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  11. if TYPE_CHECKING:
  12. from UM.View.GL.ShaderProgram import ShaderProgram
  13. ## A RenderPass subclass that renders a the distance of selectable objects from the active camera to a texture.
  14. # The texture is used to map a 2d location (eg the mouse location) to a world space position
  15. #
  16. # Note that in order to increase precision, the 24 bit depth value is encoded into all three of the R,G & B channels
  17. class PickingPass(RenderPass):
  18. def __init__(self, width: int, height: int) -> None:
  19. super().__init__("picking", width, height)
  20. self._renderer = QtApplication.getInstance().getRenderer()
  21. self._shader = None #type: Optional[ShaderProgram]
  22. self._scene = QtApplication.getInstance().getController().getScene()
  23. def render(self) -> None:
  24. if not self._shader:
  25. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "camera_distance.shader"))
  26. width, height = self.getSize()
  27. self._gl.glViewport(0, 0, width, height)
  28. self._gl.glClearColor(1.0, 1.0, 1.0, 0.0)
  29. self._gl.glClear(self._gl.GL_COLOR_BUFFER_BIT | self._gl.GL_DEPTH_BUFFER_BIT)
  30. # Create a new batch to be rendered
  31. batch = RenderBatch(self._shader)
  32. # Fill up the batch with objects that can be sliced. `
  33. for node in DepthFirstIterator(self._scene.getRoot()): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  34. if node.callDecoration("isSliceable") and node.getMeshData() and node.isVisible():
  35. batch.addItem(node.getWorldTransformation(), node.getMeshData())
  36. self.bind()
  37. batch.render(self._scene.getActiveCamera())
  38. self.release()
  39. ## Get the distance in mm from the camera to at a certain pixel coordinate.
  40. def getPickedDepth(self, x: int, y: int) -> float:
  41. output = self.getOutput()
  42. window_size = self._renderer.getWindowSize()
  43. px = (0.5 + x / 2.0) * window_size[0]
  44. py = (0.5 + y / 2.0) * window_size[1]
  45. if px < 0 or px > (output.width() - 1) or py < 0 or py > (output.height() - 1):
  46. return -1
  47. distance = output.pixel(px, py) # distance in micron, from in r, g & b channels
  48. distance = (distance & 0x00ffffff) / 1000. # drop the alpha channel and covert to mm
  49. return distance
  50. ## Get the world coordinates of a picked point
  51. def getPickedPosition(self, x: int, y: int) -> Vector:
  52. distance = self.getPickedDepth(x, y)
  53. camera = self._scene.getActiveCamera()
  54. if camera:
  55. return camera.getRay(x, y).getPointAlongRay(distance)
  56. return Vector()