About materials EnableKeyword and bump maps
Posted: Fri Feb 24, 2017 10:25 pm
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
link1
link2Unity 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.
Any thoughs?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.