|
@@ -6,22 +6,22 @@ Screen-reading shaders
|
|
|
Introduction
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
-Very often it is desired to make a shader that reads from the same
|
|
|
-screen it's writing to. 3D APIs such as OpenGL or DirectX make this very
|
|
|
+Very often, it is desired to make a shader that reads from the same
|
|
|
+screen to which it's writing. 3D APIs, such as OpenGL or DirectX, make this very
|
|
|
difficult because of internal hardware limitations. GPUs are extremely
|
|
|
-parallel, so reading and writing causes all sort of cache and coherency
|
|
|
+parallel, so reading and writing causes all sorts of cache and coherency
|
|
|
problems. As a result, not even the most modern hardware supports this
|
|
|
properly.
|
|
|
|
|
|
The workaround is to make a copy of the screen, or a part of the screen,
|
|
|
to a back-buffer and then read from it while drawing. Godot provides a
|
|
|
-few tools that makes this process easy!
|
|
|
+few tools that make this process easy!
|
|
|
|
|
|
SCREEN_TEXTURE built-in texture
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
-Godot :ref:`doc_shading_language` has a special texture, "SCREEN_TEXTURE" (and "DEPTH_TEXTURE" for depth, in case of 3D).
|
|
|
-It takes as parameter the UV of the screen and returns a vec3 RGB with the color. A
|
|
|
+Godot :ref:`doc_shading_language` has a special texture, "SCREEN_TEXTURE" (and "DEPTH_TEXTURE" for depth, in the case of 3D).
|
|
|
+It takes as argument the UV of the screen and returns a vec3 RGB with the color. A
|
|
|
special built-in varying: SCREEN_UV can be used to obtain the UV for
|
|
|
the current fragment. As a result, this simple 2D fragment shader:
|
|
|
|
|
@@ -42,7 +42,7 @@ of blur at no cost.
|
|
|
SCREEN_TEXTURE example
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
-SCREEN_TEXTURE can be used for a lot of things. There is a
|
|
|
+SCREEN_TEXTURE can be used for many things. There is a
|
|
|
special demo for *Screen Space Shaders*, that you can download to see
|
|
|
and learn. One example is a simple shader to adjust brightness, contrast
|
|
|
and saturation:
|
|
@@ -87,7 +87,7 @@ source for SCREEN_TEXTURE as the first one below, so the first one
|
|
|
In 3D, this is unavoidable because copying happens when opaque rendering
|
|
|
completes.
|
|
|
|
|
|
-In 2D this can be corrected via the :ref:`BackBufferCopy <class_BackBufferCopy>`
|
|
|
+In 2D, this can be corrected via the :ref:`BackBufferCopy <class_BackBufferCopy>`
|
|
|
node, which can be instantiated between both spheres. BackBufferCopy can work by
|
|
|
either specifying a screen region or the whole screen:
|
|
|
|
|
@@ -105,9 +105,9 @@ Godot:
|
|
|
|
|
|
- If a node uses the SCREEN_TEXTURE, the entire screen is copied to the
|
|
|
back buffer before drawing that node. This only happens the first
|
|
|
- time, subsequent nodes do not trigger this.
|
|
|
+ time; subsequent nodes do not trigger this.
|
|
|
- If a BackBufferCopy node was processed before the situation in the
|
|
|
- point above (even if SCREEN_TEXTURE was not used), this behavior
|
|
|
+ point above (even if SCREEN_TEXTURE was not used), the behavior
|
|
|
described in the point above does not happen. In other words,
|
|
|
automatic copying of the entire screen only happens if SCREEN_TEXTURE is
|
|
|
used in a node for the first time and no BackBufferCopy node (not
|
|
@@ -124,7 +124,7 @@ DEPTH_TEXTURE
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
For 3D Shaders, it's also possible to access the screen depth buffer. For this,
|
|
|
-the DEPTH_TEXTURE built-in is used. This texture is not linear, it must be
|
|
|
+the DEPTH_TEXTURE built-in is used. This texture is not linear; it must be
|
|
|
converted via the inverse projection matrix.
|
|
|
|
|
|
The following code retrieves the 3D position below the pixel being drawn:
|