Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ARFoundation ARCore FaceTracking - How do we update material of faceMesh prefab??

Discussion in 'AR' started by cuzner, Mar 7, 2020.

  1. cuzner


    Oct 1, 2015
    I am using specifically AR Foundation (ARCore) FaceTracking and simply trying to create a random material at Runtime on a button click but it is not working.

    How do I access the faceMesh Prefab and update it via script?
    I have logged the randomize script and can see it is working in the console but when clicking it is removing the existing material from the prefab, not assigning the newly generated one.
    The script works on an basic scene with a cube prefab - when the prefab Gameobject is available in scene and the button is clicked the material changes.

    This is an example of what I have so far:

    Code (CSharp):
    2. [SerializeField]
    3. private ARFaceManager faceManager;
    5. private MeshRenderer faceManagerMeshRenderer;
    6. .....
    7. void Start()
    8.     {
    9. GameObject faceObject = faceManager.facePrefab;
    10.         faceManagerMeshRenderer = faceObject.GetComponent<MeshRenderer>();
    11. applyRandomMaterialButton.onClick.AddListener(GenerateRandomMaterial);
    12.     }
    13. void GenerateRandomMaterial()
    14.     {
    15.         if (faceManagerMeshRenderer == null)
    16.         {
    17.             return;
    18.         }
    19.         GenerateMaterial();
    20.     }
    21. private void GenerateMaterial()
    22.     {
    23.         Material genMaterial = new Material(Shader.Find("Unlit/Color"));
    24.         genMaterial.color = GetRandomColor();
    25.         faceManagerRenderer.sharedMaterial = genMaterial;
    26. }
    27. static Color GetRandomColor() => Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
  2. cuzner


    Oct 1, 2015
    I have a solution / workaround that is working for me currently. (although it is not my ideal solution)

    After much looking at the docs and trial and error scripting I have discovered that the only way I can get this to work for me is if I create a dummy material in the Assets folder.

    Then pass this to the script as a Public property
    Code (CSharp):
    1. public Material baseMaterial;
    Then apply this in the GenerateMaterial() method.
    Code (CSharp):
    1. private void GenerateMaterial()
    2.     {
    3.         Material genMaterial = altMaterial;
    4.         Material[] materialsCopy = faceManagerMeshRenderer.sharedMaterials;
    6.         materialsCopy[0] = genMaterial;
    7.         faceManagerRenderer.sharedMaterials = materialsCopy;
    9.     }
    If I try to create the Material at runtime it does not build and apply the Material to the Prefab. I must be doing something wrong, but I do not know what it is. So this will have to do for now.

    If anyone knows what is wrong, it would be great to understand.