Changing 3D Object Colors with Unity Shader
Ashley Kwon (Spring 2023)
Shaders in Unity are powerful programs that allow you to change 3D objects' colors or apply post-processing methods to frames. Unity uses a shader language called HLSL. It is important to note that this language has a different structure from C#, although variables defined in an HLSL program can be used by C# programs. In this tutorial, I focus on writing a custom shader program in Unity that allows you to change a simple 3D object's colors upon user interaction as shown in the video on the left-hand side.
Right click in the Project window and create a new shader program as in the screenshot below. Although I chose to use Unlit Shader in this example, you can also use a different type of shader. You can read more about shader types here.
2. Open the shader program you just created. Variables listed under Properties as in the screenshot below are those that are accessible from C# programs outside of the shader program. Initiate your custom variables here.
3. You need to initiate your custom variables outside of Properties too. In the first Pass, initiate your variable again with the correct type as in line 38 in the screenshot below.
4. Define your own color transfer functions. To be applied directly to RGB values of your 3D object, these functions' return type should be float4 (R, G, B, A). You can reference the screenshot below to see how to set input and output types in an HLSL function.
5. If you scroll all the way down to the last few lines of your shader program, you should see a function called frag, which has the return type of fixed4. This is where the color transfer functions you defined in the previous step should be applied to RGB values of pixels in your 3D object. To do so, first access the pixel value at each location in your object's texture and assign the accessed value to a variable as in line 191 from the screenshot below.
Then, apply your color transfer functions. In the example below, DeuteranopiaSimulator, RGBToLMS, and LMSToRGB are custom color transfer functions that I defined. Please note that in order for your shader program to work without crashing on your headset, you can only have one return statement in your frag function. Also, all entries in the returned pixel value should be in the range from 0 to 1.
As in line 196, you can also access custom variables you defined in Properties.
6. Attach the shader you just made to your 3D object's material by dragging and dropping the shader into the material.
7. Recall that you defined custom variables in 2 and 3. You may want to change these variables' values during runtime (e.g., changing color assignment methods upon user interaction with a UI, assigning custom textures to use with your shader). To do so, you need to write a C# program. You can reference Public Methods on this page (especially those that start with "Set") to figure out how to manipulate variables defined in your shader program from a C# program.
The code snippet below shows how to set the value of an int variable (ChosenMethod), which is defined in a shader program, from a C# program.