Skip to content

Removing a mesh from a ClippingPrimitive resets all the properties from the material #202

@Maesla

Description

@Maesla

Describe the bug

Removing a mesh from a ClippingPrimitive resets all the properties from the material. It clears the properties that the clipping system has set, but the issue is that it also clears all the properties that other systems have set to the material using a MaterialPropertyBlock

To reproduce

  1. Set a color to a material using a MaterialPropertyBlock
  2. Add the render to a clipping primitive
  3. Remove the render from the clipping primitive
  4. Check that the color set in step 1 is lost

You can use this code to reproduce it

using Microsoft.MixedReality.GraphicsTools;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class RemoveFromClippingPrimitiveBugTest: MonoBehaviour
{
    public bool randomColor = false;
    public Color color = Color.yellow;
    public Material material;


    public List<MeshRenderer> renderers;

    private MaterialPropertyBlock propBlock;

    public ClippingPrimitive clippingPrimitive;

    public float waitTime = 1f;

    public void OnEnable()
    {
        renderers = this.GetComponentsInChildren<MeshRenderer>().ToList();

        foreach (var renderer in renderers)
        {
            renderer.sharedMaterial = material;
        }

        propBlock = new MaterialPropertyBlock();
    }

    public IEnumerator Start()
    {
        clippingPrimitive.gameObject.SetActive(false);
        yield return new WaitForSeconds(waitTime);
        SetColor(); //Use property block to set color
        yield return new WaitForSeconds(waitTime);
        AddToClippingPrimitive(); //Add all renderers to clipping primitive
        yield return new WaitForSeconds(waitTime);
        RemoveFromClippingPrimitive(); //Remove all renderers to clipping primitive. See how color is lost
    }

    private void SetColor()
    {
        foreach (var renderer in renderers)
        {
            Color c = randomColor ? Random.ColorHSV() : color;
            renderer.GetPropertyBlock(propBlock);
            propBlock.SetColor("_Color", c);
            renderer.SetPropertyBlock(propBlock);
        }
    }
    
    private void AddToClippingPrimitive()
    {
        clippingPrimitive.gameObject.SetActive(true);

        foreach (var renderer in renderers)
        {
            clippingPrimitive.AddRenderer(renderer);
        }
    }

    private void RemoveFromClippingPrimitive()
    {
        clippingPrimitive.gameObject.SetActive(false);

        foreach (var renderer in renderers)
        {
            clippingPrimitive.RemoveRenderer(renderer);
        }
    }



    public void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            SetColor();
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            ClearPropertyBlock();
        }

    }



    private void ClearPropertyBlock()
    {
        foreach (var renderer in renderers)
        {
            propBlock.Clear();
            renderer.SetPropertyBlock(propBlock);
        }
    }
}

This is happing because ClippingPrimitive.RemoveRenderer calls to ClippingPrimitive.ResetRenderer that has this call

materialPropertyBlock.Clear();
_renderer.SetPropertyBlock(materialPropertyBlock);

Expected behavior

The color (or any other property that is used outside the clipping system) remains.
This was the behavior in MRTK 2.x

Screenshots

2023-12-29.11-25-11.mp4

Your setup (please complete the following information)

  • 2022.3.14f1.0.19043
  • Graphics Tools Version 0.6.5

Target platform (please complete the following information)

  • Editor
  • Hololens 2

Additional context

The used shader is Graphics Tool/Standard

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions