Using the Valheim Shaders - Valheim-Modding/Wiki GitHub Wiki
Sometimes in your modding adventures you might wish to use one of Valheims custom built shaders to complete the in game feel of your object
A few of the shaders I have found myself using often are the Custom/Creature, Custom/Piece, Custom/LitParticles
So I have written a tool that will allow us to actually make use of these shaders by applying them at runtime to your objects.
This tool comes in the format of a .dll file that will serve a dual purpose:
- Will be used in your unity project as a reference on your GameObjects
- Will be repacked inside your mod so your GameObjects know how to find the script reference.
First you will need to download the attached ShaderReplacer.dll file and drag and drop it into your Unity project folder. I chose to put mine into the folder called Assemblies where I keep all my Valheim assemblies (some of you use a scripts folder instead of the assemblies, this technique will work with your style as well)
As you can see The ShaderReplacer.dll is now in my unity project and it looks like I can fold it down to see the script contents of the dll
Now we have our tool placed in Unity. Lets use it!
For my Example I am working with a weapon item, Weapons in Valheim use the Custom/Creature shader (idk why) You will notice in Unity the object looks VERY off.... that is because the shaders that get exported with asset ripper are merely dummy shaders. They do not actually contain the code to compile into the custom shader that we see in game
so with that in mind your Unity Preview will look very off
Let's apply ShaderReplacer to our object The way That I am choosing to do this in my object is to find the first rendered item that uses a shader I wish to apply In my hierarchy that is BronzeCestus
I will go to the "Add Component" button and then choose ShaderReplacer
Once this component is added we need to tell it the total count of Renderer's to use (in my case I have 2 objects so that's 2x renderers an object that goes on left hand and right hand all within my hierarchy)
Next I will click this radio button to the right hand side of each element slot
You should see a window similar to this pop up
Since we are selecting from our own hierarchy we can know that there is 3 renderers
1x for when the object is on the ground (I dont care about this one)
and 2x for when the object is worn by player (1 for left hand 1 for right hand)
So I will elect BronzeCestus and BrnozeCestus.001 as the renderers for my ShaderReplacer as they are the actual in game objects I need the shader replaced on.
Now you can see that the Component is setup within Unity
After this I can compile my asset bundle and go to my preferred IDE and start working on my mod.
You will need to install the NuGet package ILRepack.Lib.MSBuild.Task
This will enable you to repack the ShaderReplacer.dll into your mod once it is compiled
Once you have added that NuGet package you should then copy ShaderReplacer.dll into your project
For easy copying I have set it as a reference in order to ensure the dll gets copied to my build directory every build
Once you have completed those steps create a file called ILRepack.targets in the root of your project folder
paste the following code into that file
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="ILRepacker" AfterTargets="Build">
<ItemGroup>
<InputAssemblies Include="$(TargetPath)" />
<InputAssemblies Include="$(OutputPath)\ShaderReplacer.dll" />
</ItemGroup>
<ILRepack Parallel="true" DebugInfo="true" Internalize="true" InputAssemblies="@(InputAssemblies)" OutputFile="$(TargetPath)" TargetKind="SameAsPrimaryAssembly" LibraryPath="$(OutputPath)" />
</Target>
</Project>
and shader replacer will be working for you :D