Main texture not shown up, lwrp (Solved)

Good day!

Using lwrp for about 6 month. Yesterday went from 2018.3.12f1 to 2019.1.0f2, an now have an issue in changing textures on the fly, with script.

Simple thing. It was)

rend = GetComponent<MeshRenderer>();
rend.material.maintexture = newTexture;

This lines of code actually changes the texture (checked with names on maintexture) - but it is not displayed, i can see an old texture all the time.

If changed through inspector - texture changes. But it seems, that displayed texture and material.maintexture have nothing in common. Because even if we set maintexture (through script) to null - nothing changes.

If we get to standart hdrp - all is fine.

Tried getting some info through .material.shaderKeywords - no luck. Only _normalmap and _occlusionmap.

Feel, like it’s something simple, but couldn’t find the answer.

Can anyone give me a hand?

Unfortunately cannot give you a hand, but am also experiencing this issue

Lets wait together, then. Welcome aboard!)

https://docs.unity3d.com/ScriptReference/Material-mainTexture.html

If the shader you’re using doesn’t use a texture named _MainTex as it’s main texture, it won’t work. Also, .maintexture appears to considered deprecated by Unity, though I think they keep forgetting to actually make it deprecated so people know to stop using it. It’s also entirely possible they outright broke it in 2019 as, like I mentioned, they treat it as if it’s deprecated.

4 Likes

This has nothing to do with material properties like textures, colors, etc. Keywords are used to pick different shader variants. Many of Unity’s shaders happen to have a keyword to enable or disable the use of normal maps which just happens to use a keyword that looks like it should be the material property name (it’s often not!). Also note that the name that appears in the inspector is just a display name and has nothing to do with the internal property name. The easiest way to know what the actual property name is would be to look at the .shader file itself. Alternatively you can use the debug inspector (right click on the inspector tab, select debug) and you can see a list of all the set properties with their actual property names.

Here’s the LWRP’s Lit shader:
https://github.com/Unity-Technologies/ScriptableRenderPipeline/blob/master/com.unity.render-pipelines.lightweight/Shaders/Lit.shader
Note the main texture property is called “_BaseMap”, and the normal map is called “_BumpMap”, not “_NORMALMAP” like the keyword.

9 Likes

Thank you for your help, Bgolus! And for expanded explanation as well, didn’t know more, than half of that.

rend.material.SetTexture("_BaseMap", tex); - it worked!)

8 Likes

Thanks bgolus. I was having trouble setting material texture in HDRP. And it seems main texture property is _BaseColorMap and not _BaseMap and even not _MainTex.