c-andrews

Hi, can anyone tell me how I can set the saturation of the skeleton at runtime please?

I am using the URP shader "Universal Render Pipeline/Spine/Sprite"
Is this the correct shader to use or is there a modified "Universal Render Pipeline/Spine/Skeleton" shader that gives you a saturation property to modify?

TIA, Chris
c-andrews
  • Messaggi: 5

Harald

You can have a look at the example scene Spine Examples/Other Examples/Per Instance Material Proeprties. In your case you'll want to set the _Saturation float property at the material. You also need to be sure to have the _COLOR_ADJUST keyword enabled which corresponds to Color Adjustment in the Material Inspector (to enable it at runtime use material.EnableKeyword("_COLOR_ADJUST");), otherwise the shader has the respective feature disabled and won't do anything.

You can also find documentation here, in subsection MaterialPropertyBlocks:
spine-unity Runtime Documentation: Changing Materials Per Instance
Avatar utente
Harald

Harri
  • Messaggi: 4104

c-andrews

Thanks thats really helpful.
I'm not able to use
skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial;
as my character has a lot of different skins and grabbing the primary is the first skin. How should I get the current material that is being used by the Mesh Renderer or is it ok to use
meshRenderer.sharedMaterial
.
var meshRenderer = m_character.skeleton.GetComponent<MeshRenderer>();
var originalMaterial = meshRenderer.sharedMaterial;

m_overrideMaterial = new Material(originalMaterial);
m_overrideMaterial.name = m_overrideMaterial.name + "_override";
m_overrideMaterial.EnableKeyword("_COLOR_ADJUST");
m_overrideMaterial.SetFloat("_Saturation", m_saturation);
m_overrideMaterial.SetFloat("_Brightness", m_brightness);

m_character.skeleton.CustomMaterialOverride[originalMaterial] = m_overrideMaterial;
c-andrews
  • Messaggi: 5

Harald

It's safe to access the MeshRenderer's sharedMaterial, but please be sure to register your method where you access it at the skeletonRenderer.OnMeshAndMaterialsUpdated delegate to ensure it's called at the right time (after the materials have been assigned according to your active attachments). This is also described here in the documentation pages.

A side note: in case it's not only simplified pseudo-code: you should not call m_overrideMaterial = new Material(originalMaterial); all the time, better create a Dictionary of your replacement materials and re-use the respective entries, or directly use skeleton.CustomMaterialOverride[originalMaterial] instead of m_overrideMaterial to store the materials.

In general, the available callback delegates are listed here on the documentation pages, section SkeletonAnimation Update Callbacks:
spine-unity Runtime Documentation: Life cycle
Avatar utente
Harald

Harri
  • Messaggi: 4104


Torna a Unity