About materials EnableKeyword and bump maps

Discuss coding questions, pull requests, and implementation details.
Post Reply
User avatar
TheLacus
Posts: 1305
Joined: Wed Sep 14, 2016 6:22 pm

About materials EnableKeyword and bump maps

Post by TheLacus »

Somehow i missed this one for all this time, but i found a problem with the import of bump maps: they are applied on the materials only if there is at least one material with a bump map in resources. On vanilla Daggerfall Unity there are none, so the bump maps imported on real time don't work even if material.EnableKeyword("_NORMALMAP") is used.

link1
Unity does compile a standard shader which holds "all combinations" or "variants" of features included in the standard material. It does this via the #pragma multi_compile and #pragma shader_feature directives. Read more on them on their manual page, especially the part about the "Difference between shader_feature and multi_compile".

These directives use keywords to determine what bits of shader will be used at runtime to render the object. (e.g.: #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON)

You can then specify which of these elements you want to use, with the Shader.EnableKeyword("") and Shader.DisableKeyword(""), like Yanik said.

However, if the shader is using shader_feature as opposed to multi_compile, then the unused variants of shader_feature will not be included into your game Build.

I had a quick look at unity5's new standard shader source code and I see that it uses shader_feature a lot. I haven't tested this yet but it must mean that you must make a build where you have one dummy object with a standard material using all the features you will require at runtime, in order for the full shader to make it into the build.
link2
Using the keywords above is enough to get your scripted Material modifications working while running in the editor.

However, because Unity only checks for Materials used in your project to determine which variants to include in your build, it will not include variants that are only encountered via script at runtime.

This means if you enable the _PARALLAXMAP keyword for a Material in your script, but you do not have a Material used in your project matching that same feature combination, the parallax mapping will not work in your final build - even though it appears to work in the editor. This is because that variant will have been omitted from the build because it seemed to not be required.
Any thoughs?

User avatar
TheLacus
Posts: 1305
Joined: Wed Sep 14, 2016 6:22 pm

Re: About materials EnableKeyword and bump maps

Post by TheLacus »

Unfortunately we lost a few posts, but Interkarma already helped me with this one. :)

User avatar
Nystul
Posts: 1501
Joined: Mon Mar 23, 2015 8:31 am

Re: About materials EnableKeyword and bump maps

Post by Nystul »

Since I started trying to convert my mods I reraise a question of mine that got lost with the forum crash...

I have several custom shaders in my mods that got stripped out by unity in the first place.

Interkarma fixed this by adding a dummy material in the build.

My fear now is that once I moved my mod to the new mod system the problem will come back since the shaders might get removed again.

I am not sure if dummy materials will work from inside the mod system. What do you think?
Or will this problem never occur because the mod system will prevent removal by design?

User avatar
LypyL
Posts: 512
Joined: Sun Mar 22, 2015 3:48 am

Re: About materials EnableKeyword and bump maps

Post by LypyL »

I haven't experienced any issues with shaders / materials not being included in the mod...with the exception of the material I used for the sky in my sky mod - I wasn't including the shader in the mod as it's one of the shaders built into Unity, but it seems that was what caused your pink sky issue (not sure why it still worked for me!).

When you build the mod, you'll see a .manifest file in the same folder as your mod - this is just a text file and it will show the assets that were included.

User avatar
Nystul
Posts: 1501
Joined: Mon Mar 23, 2015 8:31 am

Re: About materials EnableKeyword and bump maps

Post by Nystul »

ah ok thx for the info, so we will have to see how it works out then ;)
I am optimistic now

Post Reply