diff --git a/Manual/contents/GameMaker_Language/GML_Reference/Asset_Management/Rooms/General_Layer_Functions/layer_shader.htm b/Manual/contents/GameMaker_Language/GML_Reference/Asset_Management/Rooms/General_Layer_Functions/layer_shader.htm index 5ab169a1a..0af64f38f 100644 --- a/Manual/contents/GameMaker_Language/GML_Reference/Asset_Management/Rooms/General_Layer_Functions/layer_shader.htm +++ b/Manual/contents/GameMaker_Language/GML_Reference/Asset_Management/Rooms/General_Layer_Functions/layer_shader.htm @@ -1,67 +1,70 @@ - + -
- -This function assigns a shader resource to any given layer and the layer will then be rendered using that shader.
-You supply either The handle of the layer or the name of the layer (as a string - this will have a performance impact) , along with the ID of the shader to use. The shader must have been created previously in the Asset Browser and the shader index (the name of the shader resource) is then passed to this function. If the layer assigned has instances added to it, then the shader will be applied to all the draw events that the instance uses - for example if the instance has a Draw GUI Begin event, then the shader will be applied automatically to it. The shader will also affect any other graphic elements drawn on that layer, like sprite assets, tile maps or particle systems.
-The function is not meant to be called in any draw events or step events, but rather only needs to be called at the start of the room in the Room Creation Code or in the Create Event / Room Start Event of an instance.
--
layer_shader(layer_id, shader)
-Argument | -Type | -Description | -
---|---|---|
layer_id | -String or Layer ID | -The handle of the layer to target (or the layer name as a string) | -
shader | -Shader Asset | -The shader index to assign to the layer | -
-
N/A
--
var lay_id = layer_get_id("Instances");
-
- layer_shader(lay_id, shd_Sepia);
The above code will assign the shader resource shd_Sepia to the given layer for all drawing.
--
-
This function assigns a shader asset to any given layer and the layer will then be rendered using that shader.
+You supply either the handle of the layer or the name of the layer (as a string - this will have a performance impact) , along with the shader to use. The shader must have been created previously in the Asset Browser and the shader asset is then passed to this function. If the layer assigned has instances added to it, then the shader will be applied to all the draw events that the instance uses - for example if the instance has a Draw GUI Begin event, then the shader will be applied automatically to it. The shader will also affect any other graphic elements drawn on that layer, like sprite assets, tile maps or particle systems.
+The function is not meant to be called in any Draw events or Step events, but rather only needs to be called at the start of the room in the Room Creation Code or in the Create Event / Room Start Event of an instance.
++
layer_shader(layer_id, shader)
+Argument | +Type | +Description | +
---|---|---|
layer_id | +String or Layer | +The handle of the layer to target (or the layer name as a string) | +
shader | +Shader Asset | +The shader to assign to the layer | +
+
N/A
++
var _lay_id = layer_get_id("Instances");
+ layer_shader(_lay_id, shd_sepia);
+
The above code will assign the shader asset shd_sepia to the given layer for all drawing.
++
+
Shaders are an incredibly powerful tool for manipulating what and how things are rendered to the screen by the graphics card. Since these tiny programs are actually run on the graphics card itself, this means that they are extremely fast to process, freeing up valuable CPU cycles for more game logic.
To create a shader you will need to have written both a Vertex Shader and a Fragment Shader (also know as a Pixel Shader) using The Shader Editor, and even if (for example) you only wish to change the vertex positions for an instance being drawn, or if you only want to change the colour values for the pixels, you will still need both programs for a complete shader to work.
-Shaders do not permit you to change the value of any variables that you pass into them, and so these will be called shader constants (or uniforms) in all the documentation that refers to them.
-For a complete overview of the language specification of GLSL ES that GameMaker currently uses, including the functions and variables that you can use to program the shaders themselves, please refer to the GLSL ES Specification 1.00. If you're looking for a quick overview of these functions and variables, the following link is useful as well as it contains some quick reference cards for the OpenGL ES API on the last two cards: OpenGL ES Reference Cards.
+Note that shaders do not permit you to change the value of any variables that you pass into them, and so these will be called shader constants (or uniforms) in all the documentation that refers to them.
+The default shader language is GLSL ES. For a complete overview of the language specification of GLSL ES that GameMaker currently uses, including the functions and variables that you can use to program the shaders themselves, please refer to the GLSL ES Specification 1.00. If you're looking for a quick overview of these functions and variables, the following link is useful as well as it contains some quick reference cards for the OpenGL ES API on the last two cards: OpenGL ES Reference Cards.
+If you are new to shaders or want a more complete guide to creating and using them in GameMaker, see Guide To Using Shaders.
+Using a shader in your projects is very simple, and only requires a couple of lines of code to get the most basic of use from it:
shader_set(myShader);
draw_self();
shader_reset();
As you can see, they are used in a similar manner to blend modes and surfaces, where you first select (set) the shader, draw what you want using it, then reset the draw target again afterwards. If you wish to render the entire screen through a shader, and not just a single sprite or background, you will need to set up a surface to catch the current view, and then pass that through to the shader (see Surfaces for more information).
+As you can see, they are used in a similar manner to blend modes and surfaces, where you first select (set) the shader, draw what you want using it, then reset the shader again afterwards. If you wish to render the entire screen through a shader, and not just a single sprite or background, you will need to set up a surface to catch the current view, and then pass that through to the shader (see Surfaces for more information).
Shaders, like anything related to drawing, can only be used in the Draw events. It is also worth noting that if you are trying to use a colour value in a shader and the object has no texture, the results will turn out black.
+If the shader you are using has input values, these are set using the uniform functions. You would first get the uniform handle (which is essentially an ID value for the uniform to be set) using the function shader_get_uniform in the Create Event of the instance using the shader, and then store these handles in variables, something like this:
colour_to_find = shader_get_uniform(sShaderDemo5, "f_Colour1");
colour_to_set = shader_get_uniform(sShaderDemo5, "f_Colour2");
One final thing to note is that although shaders are accepted across all platforms, they are still device specific and if the hardware or software of the device cannot use shaders then you will get an error. To check this you can call the function shaders_are_supported, which returns whether the hardware supports shaders.
-Even though your game can be run specific shaders may not have been compiled for a variety of reasons. Therefore, you are recommended to check that the shader is compiled before setting uniforms or using the shader itself by using shader_is_compiled, like this:
+Although shaders are accepted across all platforms, they are still device specific and if the hardware or software of the device cannot use shaders then you will get an error. To check this you can call the function shaders_are_supported, which returns whether the hardware supports shaders.
+Even though your game runs fine, specific shaders may not have been compiled for a variety of reasons. Therefore, you are recommended to check that the shader is compiled before setting uniforms or using the shader itself by using shader_is_compiled, like this:
if (shader_is_compiled(myShader))
{
shader_set(myShader);
draw_self();
shader_reset();
}
- else show_debug_message("Shader failed");
The reasons why a shader could not be compiled can vary, depending on the shader language used on the target platform and what that language supports or doesn't, according to its specification.
In general you'd do these checks on game start and store the results as a global variable to then check later.
-It is important to note that GameMaker also supports some conditional compile Macros which can be used within GLSL ES shaders so they can perform alternative code on specific supported platforms. The macros and the platforms they will be generated on are shown in the table below:
-+
GameMaker supports some conditional compile macros which can be used within GLSL ES shaders so they can perform alternative code on specific supported platforms. The macros and the platforms they will be generated on are shown in the table below:
_YY_HLSL11_ | 3 | -Windows, XboxOne | +Windows, Xbox One |
_YY_PSSL_ | @@ -76,20 +83,34 @@
-
When you compile your GameMaker project on any one of the listed platforms using a GLSL ES format shader, one of the above macros will be generated which can then be used checked in the shader code like this:
+When you compile your GameMaker project on any one of the listed platforms using a GLSL ES format shader, one of the above macros will be generated which can then be used for checks in the shader code like this:
#ifdef _YY_HLSL11_
// HLSL shader code here
#else
// GLSL shader code here
#endif
Shaders use a certain precision to store variables. By default, this may not be the highest precision. You can add the following line of code:
-precision highp float;
-at the top of any pixel shader that requires a higher degree of mathematical precision. This can be useful on e.g. some Android devices that run their shaders in low precision mode.
-If you are new to shaders or want a more complete guide to creating and use them using GameMaker, then please see the following page of the manual:
+While this manual will not cover any of the OpenGL shader functions and variables, it does contain a list of the ones that are unique to GameMaker. These constants are not part of the OpenGL specification for shaders and are supplied to simplify the integration of shaders within your projects.
+ +Also note that the following names are used for built-in functions and can therefore not be used:
Note that these names are not highlighted in the Shader Editor.
+Shaders use a certain precision to store variables. By default, this may not be the highest precision. You can add the following line of code:
+precision highp float;
+at the top of any pixel shader that requires a higher degree of mathematical precision. This can be useful on e.g. some Android devices that run their shaders in low precision mode.
The following functions are available for drawing and setting shaders:
-
While this manual will not cover any of the OpenGL shader functions and variables, it does contain a list of the ones that are unique to GameMaker. These constants are not part of the OpenGL specification for shaders and are supplied to simplify the integration of shaders within your projects.
- -
Finally, GameMaker permits you to define your own Vertex Formats from which you can create your own custom primitives. This can greatly speed up shader operations or can be used to extend their capabilities and create surprising effects. You can find information on this in the following sections:
This function returns the background colour of the game window. This colour represents that which will be used for those areas of the game window that are not occupied by any views. The following image illustrates this:
-The above image has two views with two view ports, each one drawn at different positions. This stretches the game window to accommodate both ports and uses the window colour to colour the background where no view is shown.
This function returns the background colour of the game window.
+This colour represents that which will be used for those areas of the game window that are not occupied by any views. The following image illustrates this:
+The above image has two views with two view ports, each one drawn at a different position. This stretches the game window to accommodate both ports and uses the window colour to colour the background where no view is shown.
window_get_colour();
+window_get_colour();
Colour Constant
+
if (window_get_colour() != c_black)
+
if (window_get_colour() != c_black)
{
window_set_colour(c_black);
}
The above code will check the window colour to see if it is set as black or not, and if it is not it sets it to black.
-
With this function you can get the y position of the mouse cursor (in pixels) within the browser if it is an HTML5 game or within the display if it is a Windows, Ubuntu (Linux) or macOS game.
window_mouse_get_y();
+window_mouse_get_y();
wy = window_mouse_get_y();
-The above code stores the current y axis window position of the mouse in the variable "wy".
+wy = window_mouse_get_y();
+The above code stores the current y axis window position of the mouse in the variable wy.
This function can set the background colour of the game window. This colour represents that which will be used for those areas of the game window that are not occupied by any views. The following image illustrates this:
-The above image has two views with two view ports, each one drawn at different positions. This stretches the game window to accommodate both ports and uses the window colour to colour the background where no view is shown.
This function sets the background colour of the game window.
+This colour represents that which will be used for those areas of the game window that are not occupied by any views. The following image illustrates this:
+The above image has two views with two view ports, each one drawn at a different position. This stretches the game window to accommodate both ports and uses the window colour to colour the background where no view is shown.
window_set_colour(colour);
+window_set_colour(colour);
Argument | Type | +Argument | +Type | Description | -
---|---|---|---|---|
colour | + | colour | +Colour | The colour to set the region. | -
+
N/A
if (window_get_colour() != c_black)
+
if (window_get_colour() != c_black)
{
- window_set_colour(c_black);
+ window_set_colour(c_black);
}
The above code will check the window colour to see if it is set as black or not, and if it is not it sets it to black.
-