- Home /
LWRP: Set custom shader at runtime
Hi!
I'm working on a project that targets several low end platforms (android webGL, VR), where performance is important. So we changed to LWRP. I had many problems at the begin, but most of them I was able to solve. But I have this issue that proved itself a hard nut. I am loading furnitures from Asset Bundles. I needed a new shader that works almost identical to the LWRP/ Lit shader, and I also have a shader for highlight. The problem is, when a furniture is loaded with transparent material (like glass) it looks like this:
The blue colored material is the transparent one. The camera clear flags do this, it works as it would set to solid color, although it is set to Skybox, and it is obvious that this blue is not the skybox. I think it is just a symptom, not the cause.
Here comes the funny part. If I fold down the material in the inspector at runtime, it suddenly fixes itself.
Same happens if I reapply the shader on it from the inspector. But simply reapply the shader from code with this line:
material.shader = Shader.Find("CustomLit");
...just breaks it again. I have to set the shader from code, because I have a few different cases where I have to change between shaders.
THere are another problem. Only in build the materials with CustomLit shader reflects things even if they are matte textils. I think this is tied to the previous problem.
I am a little bit lost at the moment... Any help is appreiceated!
Answer by GubaLord · Oct 11, 2019 at 03:59 PM
I still don't know why this happens, but I have found a solution. If I copy the material's properties to a temporary material, and then copy them back to the original material after applying the shader, then it works, and the transparent material looks as it should.
Material tempMat = new Material(originalmaterial);
originalmaterial.shader = Shader.Find(originalmaterial.shader.name);
originalmaterial.CopyPropertiesFromMaterial(tempMat);
Your answer
Follow this Question
Related Questions
Unity - Render XR game objects onto processed camera image 0 Answers
Render tooltip on top of everything with MRTK 0 Answers
Material Validator in LightWeight Rendering Pipeline 0 Answers
Camera flickers to black in build, but not in Unity 0 Answers
Capture a Screenshot Through a Camera in the Lightweight Render Pipeline 1 Answer