forked from Ultimaker/Uranium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompositePass.py
101 lines (79 loc) · 3.92 KB
/
CompositePass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright (c) 2018 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.
from UM.Application import Application
from UM.Resources import Resources
from UM.Math.Color import Color
from UM.View.RenderPass import RenderPass
from UM.View.GL.OpenGL import OpenGL
from typing import List
MYPY = False
if MYPY:
from UM.View.GL.ShaderProgram import ShaderProgram
## A RenderPass subclass providing the final composition render.
#
# This render pass uses the other render passes to render a final composited image.
# By default, this consists of the output of the default pass, with an outline
# rendered on top of it using a convolution filter.
#
# You can use setCompositeShader() to override the shader used for the composition.
# Additionally, setLayerBindings() can be used to set layer bindings, that is set,
# which layer is bound to which texture unit.
#
# \note The CompositePass should always be last in the Renderer's rendering order.
# Therefore, when subclassing RenderPass make sure to use a priority lower than
# RenderPass.MaximumPriority.
class CompositePass(RenderPass):
def __init__(self, width, height):
super().__init__("composite", width, height, RenderPass.MaximumPriority)
self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "composite.shader"))
theme = Application.getInstance().getTheme()
self._shader.setUniformValue("u_background_color", Color(*theme.getColor("viewport_background").getRgb()))
self._shader.setUniformValue("u_outline_color", Color(*theme.getColor("model_selection_outline").getRgb()))
self._gl = OpenGL.getInstance().getBindingsObject()
self._renderer = Application.getInstance().getRenderer()
self._layer_bindings = ["default", "selection"]
## Get the shader currently used for compositing.
def getCompositeShader(self) -> "ShaderProgram":
return self._shader
## Set the shader to use for compositing.
def setCompositeShader(self, shader: "ShaderProgram") -> None:
self._shader = shader
## Get the current layer bindings.
def getLayerBindings(self) -> List[str]:
return self._layer_bindings
## Set the layer bindings to use.
#
# This should be a list of RenderPass names. The passes will be bound
# to different texture units in the order specified. By default, the output
# of the "default" RenderPass is bound to texture unit 0 and the output of
# the "selection" RenderPass is bound to texture unit 1.
#
# \param bindings The list of layer bindings to use.
def setLayerBindings(self, bindings: List[str]) -> None:
self._layer_bindings = bindings
## Perform the actual rendering of the render pass.
def render(self) -> None:
self._shader.bind()
outline_size = 2.0
step_x = outline_size / self._width
step_y = outline_size / self._height
offset = [
[-step_x, -step_y], [0.0, -step_y], [step_x, -step_y],
[-step_x, 0.0], [0.0, 0.0], [step_x, 0.0],
[-step_x, step_y], [0.0, step_y], [step_x, step_y]
]
self._shader.setUniformValue("u_offset", offset)
texture_unit = 0
for binding in self._layer_bindings:
render_pass = self._renderer.getRenderPass(binding)
if not render_pass:
continue
self._gl.glActiveTexture(getattr(self._gl, "GL_TEXTURE{0}".format(texture_unit)))
self._gl.glBindTexture(self._gl.GL_TEXTURE_2D, render_pass.getTextureId())
texture_unit += 1
self._renderer.renderFullScreenQuad(self._shader)
for i in range(texture_unit):
self._gl.glActiveTexture(getattr(self._gl, "GL_TEXTURE{0}".format(i)))
self._gl.glBindTexture(self._gl.GL_TEXTURE_2D, 0)
self._shader.release()
self._gl.glActiveTexture(self._gl.GL_TEXTURE0)