Search Unity

Enabling subsurface scattering mode in HDRP/Lit from Editor script

Discussion in 'High Definition Render Pipeline' started by Balcora, Jun 28, 2022.

  1. Balcora

    Balcora

    Joined:
    Jan 15, 2020
    Posts:
    5
    I am attempting to set Material Type from "Standard" to "Subsurface Scattering" via an Editor script in an existing material, but I am having no luck.

    This is what I want to set:
    upload_2022-6-28_9-48-6.png

    Code I've tried:
    Code (CSharp):
    1. material.SetFloat("_MaterialID", 1.0f);
    2. material.SetFloat("_TransmissionEnable", 1.0f);
    3. var diffusionProfile = AssetDatabase.LoadAssetAtPath<DiffusionProfileSettings>("Assets/Settings/HDRPDefaultResources/SkinDiffusionProfile.asset");
    4. HDMaterial.SetDiffusionProfile(material, diffusionProfile);
    5. material.EnableKeyword("_MATERIAL_FEATURE_SUBSURFACE_SCATTERING");
    6. material.EnableKeyword("_MATERIAL_FEATURE_TRANSMISSION");
    7. HDMaterial.ValidateMaterial(material);
    8.  
    9. Debug.Log("Subsurface is: "+material.IsKeywordEnabled("_MATERIAL_FEATURE_SUBSURFACE_SCATTERING"));
    Debug.Log is false! (and the material is still "Standard" material type in Inspector)

    Thanks for any help!
     
  2. Balcora

    Balcora

    Joined:
    Jan 15, 2020
    Posts:
    5
    I'm still struggling with this. Does anyone have any ideas for directions to explore? It seems the best workaround might be to programmatically copy an existing material with SSS enabled and modify from there.
     
  3. Win3xploder

    Win3xploder

    Joined:
    Dec 5, 2014
    Posts:
    161
    Last edited: Jul 13, 2022
  4. adrien-de-tocqueville

    adrien-de-tocqueville

    Unity Technologies

    Joined:
    Mar 16, 2020
    Posts:
    270
    the material ID for subsurface scattering is 0, and in general for other types, it's the index of the entry in the dropdown list (so 1 is ID for type Standard)
    Following code should work

    Code (CSharp):
    1. material.SetFloat("_MaterialID", 0.0f);
    2. material.SetFloat("_TransmissionEnable", 1.0f);
    3. HDMaterial.SetDiffusionProfile(material, diffusionProfile);
    4. HDMaterial.ValidateMaterial(material);