- Modifié
Possible bug when material is replaced
I've made a spine shader that supports HUE recolor based on Spine-Skeleton shader, by slightly editing float4 frag of the Normal pass. This shader is used only occasionally, because only a small number of attachments needs HUE recoloring. When I need to replace the material, I do this:
var HUE_Material = new Material(HUE_Shader);
HUE_Material.CopyPropertiesFromMaterial(All_Slots[s].Attachment.GetMaterial());
HUE_Material.SetFloat("_HueSin", Mathf.Sin(our_chibi.Clothes_Hue[color_slot.Key]));
HUE_Material.SetFloat("_HueCos", Mathf.Cos(our_chibi.Clothes_Hue[color_slot.Key]));
our_chibi_skeleton.CustomSlotMaterials[All_Slots[s]] = HUE_Material;
It works great in editor, but I've noticed graphical artifacts in build. Turns out this part of the HUE shader doesn't work in builds:
#if defined(_STRAIGHT_ALPHA_INPUT)
texColor.rgb *= texColor.a;
#endif
and to solve this issue I removed the condition from the HUE shader, since straight alpha input is always used by our skeletons anyway. Now it's just texColor.rgb *= texColor.a.
I'm not sure why it happens though. I copy the properties of the non-HUE material which straight alpha is always on, and it works as intended in the editor. Perhaps there is a bug of some kind that affects only builds?
I'm using the newest 3.8 runtime and URP shaders.
DarkTI a écritI'm not sure why it happens though. I copy the properties of the non-HUE material which straight alpha is always on, and it works as intended in the editor. Perhaps there is a bug of some kind that affects only builds?
This sounds strange indeed. The _STRAIGHT_ALPHA_INPUT
keyword should be copied over along with the other parameters. Could you have a try if the same problem also persists when you are using the normal unmodified Spine shader in the Material copy in this line:
var HUE_Material = new Material(originalSpineShader);
Ok, by default our characters use Spine-Skeleton shader, the one provided with Spine runtime.
So I tested two setups: in the first one I tried
var HUE_Material = new Material(Spine-Skeleton.shader);
this works properly in the build.
Then I made an exact copy of Spine-Skeleton.shader, renamed it to Spine-Skeleton1.shader, and didn't change anything else in it.
In this case
var HUE_Material = new Material(Spine-Skeleton1.shader);
led to _STRAIGHT_ALPHA_INPUT not working in the build.
So seems like the very attempt of replacing shader by another, even if identical one, causes this issue.
I'm using unity 2020.3.11f1, but I observed this issue in 2019.4 versions as well.
DarkTI a écritThen I made an exact copy of Spine-Skeleton.shader, renamed it to Spine-Skeleton1.shader, and didn't change anything else in it.
You should however change the name id (the first line in the shader, Shader "Spine/Skeleton"
). It might cause side effects if you have identical identifiers for registered shaders.
Harald a écritYou should however change the name id (the first line in the shader,
Shader "Spine/Skeleton"
). It might cause side effects if you have identical identifiers for registered shaders.
Yeah, I did, to Spine-Skeleton1 as well.
DarkTI a écritand didn't change anything else in it.
..
Yeah, I did, to Spine-Skeleton1 as well.
Ok . Did you perhaps modify anything else as well?
Does the material property copy operation also fail in the other direction, from a properly setup Spine-Skeleton1
material to a Spine-Skeleton
material? If so, then perhaps there is a bug with the CopyPropertiesFromMaterial
implementation.
In any way, you could also test if individually querying and setting the keyword via originalMaterial.IsKeywordEnabled()
and targetMaterial.EnableKeyword()
works.
Harald a écritOk
. Did you perhaps modify anything else as well?
I mean, I renamed it after you mentioned it
I looked into the issue a bit more (using IsKeywordEnabled), surprisingly the keyword is always enabled, in both editor and the build, so CopyPropertiesFromMaterial does its job.
At this point I can't even guess what's wrong and why it affects only builds, but at least I know how to edit shaders to avoid it. Hopefully it will help others if they encounter this issue.
DarkTI a écritHarald a écritOk
. Did you perhaps modify anything else as well?
I mean, I renamed it after you mentioned it
![]()
DarkTI a écritI looked into the issue a bit more (using IsKeywordEnabled), surprisingly the keyword is always enabled, in both editor and the build, so CopyPropertiesFromMaterial does its job.
At this point I can't even guess what's wrong and why it affects only builds, but at least I know how to edit shaders to avoid it. Hopefully it will help others if they encounter this issue.
Oh, the keyword is copied correctly. Then the problem is most likely due to shader_variant
being used to compile multiple shader branches in the shader instead of multi_compile
. You can change it to multi_compile
to always include all variants, even when no Material references it in your build, and it is set only via code.
You can see the documentation pages here on more info if you're interested:
https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html
Yes indeed, using multi_compile in the HUE shader solved the issue. Thanks!
Glad to hear, sorry that I haven't thought of it earlier!