Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Maesla opened this issue Dec 29, 2023 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@Maesla
Copy link

Maesla commented Dec 29, 2023

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

@Cameron-Micka
Copy link
Member

Thank you for the very detailed bug report @Maesla, it's appreciated, and we will follow up!

@Cameron-Micka Cameron-Micka added bug Something isn't working good first issue Good for newcomers labels Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants