Skip to content

Commit ac928bd

Browse files
authored
Merge pull request godotengine#7725 from Ymanawat/chroma_key_shader
Added chroma key shader tutorial in Playing Videos
2 parents 2958e6d + 5740aab commit ac928bd

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
@@ -239,3 +239,110 @@ significantly if the source is recorded at a higher resolution than 720p:
239239
::
240240

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

0 commit comments

Comments
 (0)