Skip to content

Commit 5740aab

Browse files
committed
Added chroma key shader tutorial in Playing Videos
1 parent 70dc1d4 commit 5740aab

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
3.98 KB
Loading
1.01 MB
Loading
2.1 KB
Loading

tutorials/animation/playing_videos.rst

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,110 @@ significantly if the source is recorded at a higher resolution than 720p:
230230
::
231231

232232
ffmpeg -i input.mp4 -vf "scale=-1:720" -q:v 6 -q:a 6 output.ogv
233+
234+
235+
.. Chroma Key Functionality Documentation
236+
237+
Chroma Key Videos
238+
-----------------
239+
240+
Chroma key, commonly known as the "green screen" or "blue screen" effect, allows you to remove a specific color from an image or video and replace it with another background. This effect is widely used in video production to composite different elements together seamlessly.
241+
242+
.. image:: img/chroma_key_video.webp
243+
244+
We will achieve the chroma key effect by writing a custom shader in GDScript and using a `VideoStreamPlayer` node to display the video content.
245+
246+
Scene Setup
247+
^^^^^^^^^^^
248+
249+
Ensure that the scene contains a `VideoStreamPlayer` node to play the video and a `Control` node to hold the UI elements for controlling the chroma key effect.
250+
251+
.. image:: img/chroma_key_scene.webp
252+
253+
Writing the Custom Shader
254+
^^^^^^^^^^^^^^^^^^^^^^^^^
255+
256+
To implement the chroma key effect, follow these steps:
257+
258+
1. Select the `VideoStreamPlayer` node in the scene and go to its properties. Under `CanvasItem > Material`, create a new shader named "ChromaKeyShader.gdshader."
259+
260+
2. In the "ChromaKeyShader.gdshader" file, write the custom shader code as shown below:
261+
262+
.. code-block:: gd
263+
264+
shader_type canvas_item;
265+
266+
# Uniform variables for chroma key effect
267+
uniform vec3 chroma_key_color : source_color = vec3(0.0, 1.0, 0.0);
268+
uniform float pickup_range : hint_range(0.0, 1.0) = 0.1;
269+
uniform float fade_amount : hint_range(0.0, 1.0) = 0.1;
270+
271+
void fragment() {
272+
# Get the color from the texture at the given UV coordinates
273+
vec4 color = texture(TEXTURE, UV);
274+
275+
# Calculate the distance between the current color and the chroma key color
276+
float distance = length(color.rgb - chroma_key_color);
277+
278+
# If the distance is within the pickup range, discard the pixel
279+
# the lesser the distance more likely the colors are
280+
if (distance <= pickup_range) {
281+
discard;
282+
}
283+
284+
# Calculate the fade factor based on the pickup range and fade amount
285+
float fade_factor = smoothstep(pickup_range, pickup_range + fade_amount, distance);
286+
287+
# Set the output color with the original RGB values and the calculated fade factor
288+
COLOR = vec4(color.rgb, fade_factor);
289+
}
290+
291+
The shader uses the distance calculation to identify pixels close to the chroma key color and discards them,
292+
effectively removing the selected color. Pixels that are slightly further away from the chroma key color are
293+
faded based on the fade_factor, blending them smoothly with the surrounding colors.
294+
This process creates the desired chroma key effect, making it appear as if the background has been replaced with
295+
another image or video.
296+
297+
The code above represents a simple demonstration of the Chroma Key shader,
298+
and users can customize it according to their specific requirements.
299+
300+
UI Controls
301+
^^^^^^^^^^^
302+
303+
To allow users to manipulate the chroma key effect in real-time, we created sliders in the `Control` node. The `Control` node's script contains the following functions:
304+
305+
.. code-block:: gd
306+
307+
extends Control
308+
309+
func _on_color_picker_button_color_changed(color):
310+
# Update the "chroma_key_color" shader parameter of the VideoStreamPlayer's material
311+
$VideoStreamPlayer.material.set("shader_parameter/chroma_key_color", color)
312+
313+
func _on_h_slider_value_changed(value):
314+
# Update the "pickup_range" shader parameter of the VideoStreamPlayer's material
315+
$VideoStreamPlayer.material.set("shader_parameter/pickup_range", value)
316+
317+
func _on_h_slider_2_value_changed(value):
318+
# Update the "fade_amount" shader parameter of the VideoStreamPlayer's material
319+
$VideoStreamPlayer.material.set("shader_parameter/fade_amount", value)
320+
321+
func _on_video_stream_player_finished():
322+
# Restart the video playback when it's finished
323+
$VideoStreamPlayer.play()
324+
325+
also make sure that the range of the sliders are appropriate, our settings are :
326+
327+
.. image:: img/slider_range.webp
328+
329+
Signal Handling
330+
^^^^^^^^^^^^^^^^
331+
332+
Connect the appropriate signal from the UI elements to the `Control` node's script.
333+
you created in the `Control` node's script to control the chroma key effect.
334+
These signal handlers will update the shader's uniform variables
335+
in response to user input.
336+
337+
Save and run the scene to see the chroma key effect in action! With the provided UI controls,
338+
you can now adjust the chroma key color, pickup range, and fade amount in real-time, achieving the desired
339+
chroma key functionality for your video content.

0 commit comments

Comments
 (0)