Writing Your First Shader
In this quick tutorial you'll learn how to write a simple custom shader for MagicaVoxel.
To write your first shader all you need is a simple text editor: my favourite is Visual Studio Code, but anything will be just fine.
Creating the File
First of all you'll have to create your shader file inside the shader folder of your MagicaVoxel installation:
Navigate to tour MagicaVoxel executable location and open the shader folder
Create a new text file and name it color-replacement.txt
Open the file
Adding the Map Function
First of all we are going to add thew map(vec3 v)
method that is going to create a voxel at position v:
Next we are going to retrieve the voxel color index at position v
: we will need this to check if the voxel color should be replaced. Let's add the following code inside the curly brackets:
Retrieving Parameters
We are now going to get a user defined parameter, that will be the new color index. Parameters are passed as an array called iArgs. Just before the closing bracket, add:
The value 0
means we are getting the first element of the params array.
Checking the Selected Color Index
We now need to check if the voxel we are checking is of the selected color in the MagicaVoxel editor. If this is true, we replace the color index (i.e.: we return the new value):
The iColorIndex
is a built-in value indicating the actual selected color.
If the voxel is not of the needed color index, we return the old value. Add the following line just before the last closing bracket:
The Final Code
The complete shader code is the following:
Save the file and open MagicaVoxel.
Testing the Shader
To check the shader functionality we have to simply open a voxel model and call the commend from the console:
Open a Voxel model (I choose chr_sword)
Select a color in your model (I choose the yellow gold one)
Insert in the console the following code (the index 36 is a bright red in the default palette 0) and hit Enter:
Your model should change from this:
To this:
Congratulations! You've just built your first shader.
Last updated