I am finding applying a normal map at runtime to a material with URP does not render on the object until I open to see the properties of the material in the inspector. Behaviour extends to AO.
Affects both Editor and Play mode, so play mode the normal map never renders.
Here code I’m using:
public Texture2D normalSourceTex;//attach in inspector normal map classified with Texture Type "Normal map"
thisMaterial.SetTexture("_BumpMap", normalSourceTex);
This means that if there is no material with normal map in your project, unity will not compile shader variant with this option. Setup a normal map in material inspector toggles unity to compile this variant.
// Normal Map
if(material.HasProperty("_BumpMap"))
CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap"));
Setting up it’s manually with script - will not.
You can try to setting up “_NORMALMAP” keyword by your self. In 2017 and below this is forces unity to compile this variant at runtime, afaik in 2018 and above runtime compilation doesn’t work.
Also, you can modify URP shader, and change keyword to be a multi_compile instead of a shader_feature.
Test this in build. In Editor unity can recompile shader, but i don’t sure about build. Last time this is not working for me, and i just replace all shader_feature in my shader to multi_compile:)(and get 19000 shader variants :()
Oh @BattleAngelAlita that’s right, it works on editor but not on build
Thank you so much for explaining the background. It appears that at the cost of performance I can attach a dummy normal map and switch at runtime - e.g. Start/thereafter and it works but player has to wait a bit longer
Unfortunately I’m not a shader guru at all
Cheers!
Hi,
I was creating a new Material through script in runtime in URP, but when it is applied, it doesn’t render, probably because of the reason of unity not force compiling shader until I click on it. Is it possible to do compile shader manually through script like a function to make it work after creating it?
Any chance you’d be able to give me a bit more detail?
I’m generating normal maps at runtime and adding them to a pre-existing material which has a normal map when it was compiled, but the generated normal map still doesn’t appear in the build.
SOLVED:
The solution was stated above:
For those who want a bit more detail, I did the following:
Made a new shader
Copied all the code from the standard URP Lit Shader
Edited the following line, from this:
#pragma shader_feature_local _NORMALMAP```
to this:
```// Material Keywords
#pragma multi_compile_local _NORMALMAP```
And this line:
```Shader "Universal Render Pipeline/Lit"```
to this:
```Shader "Custom/NewLit"```
4. Saved new shader
5. Made a dummy object that used this shader as a material with a dummy normal map texture.
This forces Unity to compile multiply variants of this particular shader for use at runtime. It also increases build times a little bit, but not too much.