URP light 2d normalmap gets disabled on runtime
Hi, I am using URP as 2D renderer and everything is ok: I can create 2d lights and they work very nice using normalmaps during design time, but when I click on "Run", the property gets disabled and can't be changed via scripts as it is a read only property:
private UnityEngine.Experimental.Rendering.Universal.Light2D _myLight; _myLight = transform.parent.GetComponentInChildren(); _myLight.normalMapQuality= ...
Please, somebody help me!!
Answer by YupiNawate · Apr 16, 2021 at 05:13 PM
Aaaaahm... I dunno if it is about a bug or not, but I found something at:
C:\Program Files\Unity\Hub\Editor\2021.1.3f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.render-pipelines.universal\Runtime\2D\Light2D.cs
There's a boolean serialized field that is set to false:
[SerializeField] bool m_UseNormalMap = false; // This is now deprecated. Keep it here for backwards compatibility.
And then, in Awake() we have this:
if (!m_UseNormalMap && m_NormalMapQuality != NormalMapQuality.Disabled)
m_NormalMapQuality = NormalMapQuality.Disabled;
So we have that NormalMapQuality is always set to Disabled. As I said, I dunno if it is a bug, but I found no solution anywhere, no answers in forums and I needed this working, so I changed the initial value from false to true in the Light2D.cs file (I had to grant write permissions to do it), I started my project and I replaced all my spot 2d lights with new instances. Result: working.
@YupiNawate This seems like a great solution! I just have a question as to how you grant write permissions to the cs file. When I write over the code in the Light2D.cs file, it doesn't get applied in-game. Can you explain exactly how you were able to edit the file? Thanks!
Hi A$$anonymous$$_CookieDohStudios,
I tried to edit it using the Visual Studio editor, but I couldn't save it, but with another editor (like notepad) I was able to do it. After this, you need to replace every instance of the 2d light class (delete and add again) in order this works. If you have too many instances already, maybe the solution proposed by @aydin_khp would help you (editing the scene file, find all the instances of m_UseNormalMap and set their value to 1).
Good luck!
That seemed to do the trick! Thanks so much for the quick reply.
Answer by aydin_khp · Jul 12, 2021 at 05:14 PM
I think an easier solution based on @YupiNawate suggestion is to set the value for all the m_UseNormalMap
fields in the scene file (.unity file) to 1 once.
Hi Aydin_khp, I tried to update the m_UseNormalMap in runtime, but the value can't be changed. How do you do this? Kind regards.
I didn't mean at runtime; You have to edit the [your-scene].unity file, find all the instances of m_UseNormalMap
and set their value to 1.
I see. It's good to know, thank you. For the moment, I will continue with my own solution as it allows me not to have to change each new instance I add, but your method will be very useful for projects with many 2d lights added before the fix I commented.
Thank you for your idea.
Answer by RickSaada1 · Aug 24, 2021 at 06:10 PM
My answer to this was to fix it in the editor for Light2d, so that if you set a value for the quality OTHER than disabled it changes m_UseNormalMap to true. Then things get saved properly and not reset on load.
This is the function that handles the Normal Map settings. I added the one line marked.
void DrawNormalMapGroup()
{
CoreEditorUtils.DrawSplitter(false);
m_NormalMapsSettingsFoldout.value = CoreEditorUtils.DrawHeaderFoldout(Styles.normalMapsSettingsFoldout, m_NormalMapsSettingsFoldout.value);
if (m_NormalMapsSettingsFoldout.value)
{
EditorGUILayout.PropertyField(m_NormalMapQuality, Styles.generalNormalMapLightQuality);
EditorGUI.BeginDisabledGroup(m_NormalMapQuality.intValue == (int)Light2D.NormalMapQuality.Disabled);
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_NormalMapZDistance, Styles.generalNormalMapZDistance);
if (EditorGUI.EndChangeCheck())
m_NormalMapZDistance.floatValue = Mathf.Max(0.0f, m_NormalMapZDistance.floatValue);
//// Add this line to make it preserve the values you set
m_UseNormalMap.boolValue = (m_NormalMapQuality.intValue != (int)Light2D.NormalMapQuality.Disabled);
//// End Changes
EditorGUI.EndDisabledGroup();
}
}
Oh, I should have noted I had to change both the master version: C:\Program Files\Unity\Hub\Editor\2021.1.13f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.render-pipelines.universal\Editor\2D\light2deditor.cs
and the local version in my library package cache, or it would restore the broken version.
Your answer
Follow this Question
Related Questions
2D shader / lighting like Terraria or Starbound 2 Answers
Issue with Isometric Tiles with Normal Map near 2d lights 0 Answers
Sprite Normal Map Outline 1 Answer
Toon shader for 2d sprtes with normal map 0 Answers
some sprites are turning transparent while using a normal map and others don't 0 Answers