Skip to content

Modifying materials (textures and shaders)

eArmada8 edited this page Aug 17, 2023 · 11 revisions

Modifying materials (textures and shaders)

This is a deep dive on materials and how to change shaders. If you just want to change a texture, please refer to the basic modding tutorial.

Understanding materials

Materials are the combination of textures and a pixel shader (a small program used by the GPU to draw the object). The shader will apply the textures to the object, and then modify the appearance (e.g. applying shininess and transparency). We can change textures but shaders cannot be modified. We can exchange one shader for another, however. For a deep discussion on why shaders cannot be directly modified, please refer to this tutorial.

Every mesh in a model must have a material applied to it. This assignment is made in the toolset using the .material file, which is a plain text file in JSON format. If you edit this file in notepad, you will see its assigned material. For example, in Emma CS4 default (C_CHR005_C10.pkg), her skirt and boots (polySurface4962_07.vb) are assigned to "new_uwagi_collada".

Contents of polySurface4962_07.material:

{
    "material": "new_uwagi_collada"
}

Looking in metadata.json, we can see the material "new_uwagi_collada". Note that build_collada.py will look in metadata.json for the actual material definition, as polySurface4962_07.material is nothing more than an assignment.

        "new_uwagi_collada": {
            "shader": "shaders/ed8_chr.fx#FD475805C05CFAA3CD419F156CC20E72",
            "skinned_shader": "shaders/ed8_chr.fx#0FBE93BD2C4F285287841E563DFA6A1C",
            "shaderTextures": {
                "DiffuseMapSampler": "assetconv_temp/dds_dae/D3D11/chr005_03_conv.dds",
                "NormalMapSampler": "assetconv_temp/dds_dae/D3D11/chr005_03n_conv.dds",
                "CartoonMapSampler": "assetconv_temp/dds_dae/D3D11/dlight_all_conv.dds"
            },
            "shaderParameters": {
                "PerMaterialMainLightClampFactor": [
                    1.0499999523162842
                ],
                "GameMaterialID": [
                    0.0
                ],
...continued...

There are 5 sections to each material:

  1. The shader itself. There are two for a model with skeletal animation, "shader" and "skinned_shader". (Without skeletal animation, there is only one.)
  2. The texture assignments. Above you can see a base texture (DiffuseMapSampler), a normal map (NormalMapSampler) and a gradient map (CartoonMapSampler).
  3. Tuneable shader parameters. The parameters themselves can be tuned by changing the numbers; however the parameters themselves cannot be changed. For example, if "PerMaterialMainLightClampFactor" is not present, you cannot add it. You also cannot delete parameters that the shader expects to be present.
  4. Sampler definitions. These are settings for the textures, such as whether texture coordinates (UV maps) wrap, clamp or mirror (ping-pong) etc. Similar to #3 above, you can change the settings, but you cannot add or remove sections.
  5. Shader switches. These tell the compiler how to configure the shader (what features are present or absent). These also cannnot be changed.

Modifying the material without changing it

To change textures, see the basic modding tutorial. Note that if you are not changing the material, then you can change the textures but NOT the texture slots. (e.g. If the material expects a normal map, you must provide one.) You cannot simply remove the slot and you cannot add new slots, unless you change the material.

To change the tuneable parameters, simple modify the numbers. Send "BloomIntensity" into the stratosphere if you like. We do not know what the valid ranges are for each parameter. Experiment, or look at other models / materials. Again, you cannot add or remove parameters without changing the material.

To change the texture sampler definitions, you can edit the values. Valid values for m_minFilter and m_magFilter include 'NEAREST', 'LINEAR', 'NEAREST_MIPMAP_NEAREST', 'LINEAR_MIPMAP_NEAREST', 'NEAREST_MIPMAP_LINEAR', and 'LINEAR_MIPMAP_LINEAR'. Valid values for m_wrapS, m_wrapT and m_wrapR include 'CLAMP', 'REPEAT', 'CLAMP_TO_EDGE', 'CLAMP_TO_BORDER', and 'MIRROR'. Again, you cannot add or remove sampler definitions without changing the material.

You cannot change the shaders or the shader switches without changing the material.

Changing the material

Materials can be swapped out with other valid materials. This is mainly if you need shader features that the current material does not provide.

For this tutorial, I am going to attempt to shorten Emma's skirt (polySurface4962_07) very slightly, using texture transparency. Here you can see I have removed the bottom of her skirt. (I have also deleted the fringe from polySurface4962_06.vb)

Notice however that the transparent part of the skirt is black, instead of transparent:

We can see why when we look at the material shader. Above, I had identified Emma's skirt as "new_uwagi_collada" by peeking inside polySurface4962_07.material. Going into "shaderSwitches" within "new_uwagi_collada" in metadata.json, I can see that "ALPHA_TESTING_ENABLED" is set to "0" instead of "1". Remember, I cannot change these switches.

Instead, I will replace the entire material. I will need to find a suitable donor material, either from this model or another model. Luckily for me, there is a material in Emma's model, "new_zubon_collada", that has identical features enabled, except it also has "ALPHA_TESTING_ENABLED". (Here you can see me comparing "new_uwagi_collada" against "new_zubon_collada" in WinMerge.)

I will replace "new_uwagi_collada" with a copy of "new_zubon_collada", while preserving as many tuneable parameters as possible. Note that shaders/ed8_chr.fx#FD475805C05CFAA3CD419F156CC20E72 has been changed to shaders/ed8_chr.fx#CB108FCD611C9550ED008F624F285C83 and shaders/ed8_chr.fx#0FBE93BD2C4F285287841E563DFA6A1C has been changed to shaders/ed8_chr.fx#FAF3642958A16D01BAC185C413FA58CE. This is because #CB108... and #FAF36... are the actual compiled shaders that have ALPHA_TESTING_ENABLED turned on, whereas #FD475... and #0FBE9... have it turned off.

NOTE: The following images may be a bit confusing. The left panel is "new_zubon_collada" which I am using as a template. The right panel is "new_uwagi_collada" after I have changed it. I am doing this to show you what I am copying over, and what I am not copying over.

  1. I am copying over the shaders. I am not changing the name of the material. Also notice I am not changing the textures. However, the slots must match the shader. So if "new_zubon_collada" has DiffuseMapSampler, NormalMapSampler, CartoonMapSampler, then the new "new_uwagi_collada" will need exactly those as well.

I have copied "AlphaThreshold", a new parameter that was not on the right, over to the right. It is a coincidence that ShadowColorShift and RimLitColor happen to be identical; had they been different, I would not have changed them (because I do not want to change the look).

Again, the samplers present must be the same, but the values can be different. This model is expecting the texture coordinates to mirror (ping-pong) on the Y-axis, not wrap (repeat). Let's keep m_wrapT set to "MIRROR".

The switches must match. We are using #CB108... and #FAF36... and they expect "ALPHA_TESTING_ENABLED" to be set to "1".

In this case, I copied the material directly from within the model, so the shader files are already in the build folder. If you look inside {Work Folder}/C_CHR005_C10/, you will see ed8_chr.fx#CB108FCD611C9550ED008F624F285C83.phyre and ed8_chr.fx#FAF3642958A16D01BAC185C413FA58CE.phyre present and ready to go. If I had chosen to copy the material from another .pkg (e.g. if I used something from Musse or whatever), I would have to copy the corresponding ed8_chr.fx#_____.phyre files into my build folder.

This time, after I compile the model, the transparency is working!