Skip to content

Shader Time!

Lilac edited this page Mar 16, 2022 · 6 revisions

Now that you've created a resource pack, you can start editing it!

Feel free to edit the .json or .fsh file, but this guide will mostly focus on the .vsh file for now.

The Shader

At the top of the shader file, before void main(), you can see a bunch of variables.
A quick explanation:
in variables are passed in from Minecraft to the shader, specific to this shader. The Position, obviously, defines the position of the vertices. The Color, again, obviously, defines the colour. You probably won't need to touch the UVs, but a UV is 2D coordinate on a texture. For example the letter 'A' in the default ascii.png is at 8, 32. uniform variables are available, and have the same value, in all shader files. We'll probably use a few of these. out variables are passed from the shader to the game. These are the things that we will be editing!

This is the main function in the shader, and the function you'll be playing with all the time. This defines what happens in the shader. I'll explain each line quickly.

void main() {
    // 'Mat', short for matrix, can be a little confusing. You can think of these as 3D positions.
    // This multiplies the projection position, object position and vertex position
    gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0);

    // This has litle effect on these shaders and so won't be explained.
    vertexDistance = fog_distance(ModelViewMat, IViewRotMat * Position, FogShape);

    // The vertexColor is exactly what it sounds like, the colour of the vertex
    // This line multiplies the input colour (for example, if you colour text using &c) and multiplies it by the colour of the texture.
    vertexColor = Color * texelFetch(Sampler2, UV2 / 16, 0);

    // This returns the UV coordinate.
    texCoord0 = UV0;
}

You might be wondering what a vertex is, so I'll tell you! It's a point on a 3D object, but text isn't 3D, is it? Well... it is rendered in a 3D space, and text can even be moved back and forth. This is an example of where the vertices are on a letter. The shader is ran for each of these vertices, so each one can move independently.

Vertices

Before editing this, you should know a few useful things. Firstly, Minecraft won't give you any errors if there's a problem with the code, and all your resource packs will be unloaded. 🙄 But that's okay! You can enable this setting in the Minecraft Launcher, when there's an issue with a shader, the error will be shown in the log.

Minecraft Launcher Settings

Secondly, you can use F3+T to reload resource packs, so you don't need to go into the resource pack menu after every change.

Here's the fun part, try editing some of it! You might do some cool things, or make the game look like this... Unreadable Text

Some things I'd recommend you to test:

gl_Position = ProjMat * ModelViewMat * vec4(Position, 1.0);

By editing this line, you can edit where the text is rendered to.

Try doing something like this, and see what it does.

gl_Position = ProjMat * ModelViewMat * vec4(Position + vec3(0.0, -5.0, 0.0), 1.0);

I explained this line earlier, but this part vec4(Position + vec3(0.0, -5.0, 0.0), 1.0) is where the magic happens. The Position variable is being added to a vec3. This moves all the vertices up by adding 5 to the current position.

vertexColor = Color * texelFetch(Sampler2, UV2 / 16, 0);

This line, as explained above, decides the colour of the text. The colour from the game is multiplied by the colour of the texture. However, you can change this to any colour you like.

This will make all text red!

vertexColor = vec4(1.0, 0.0, 0.0, 1.0);

I chose these two examples as they are most related to the next stage - editing the colour and position of text.

Clone this wiki locally