Search Unity

access to VFXRenderer

Discussion in 'Visual Effect Graph' started by nehvaleem, Jun 27, 2020.

  1. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    438
    Hi,
    How can I access VFXRenderer class from my code? It seems that it isn't available due its protection level. I want to be able to iterate over GameObject renderers that I am getting like this:
    .GetComponentsInChildren<Renderer>();
    and do something for VFXRenderer specifically.
     
    thebarryman likes this.
  2. JulienF_Unity

    JulienF_Unity

    Unity Technologies

    Joined:
    Dec 17, 2015
    Posts:
    326
    You cannot cast to VFXRenderer directly but you should be able to retrieve the Renderer component anyway. Be aware that modifying the VFXRenderer can be unsafe though as some of the renderer state is directly set from the VFX compilation.
     
  3. gsylvain

    gsylvain

    Joined:
    Aug 6, 2014
    Posts:
    104
    This is currently a problem for us. I have a custom culling group script that needs to get all ParticleSystemRenderer + all VFXRenderer in order to filter out every other types of renderer. I need to know the exact type of the object I'm analyzing at runtime.

    Let's say we have many complex prefabs. Some contains multiple meshRenderer for the visual, additional meshRenderer for the Outline, some ParticleSystemRender and of course, some VisualEffectGraph renderer. How would you get a list containing exclusively ParticleSystemRender + VFXRenderer if you don't expose all types?

    Thank you!
     
    thebarryman and MagiJedi like this.
  4. MagiJedi

    MagiJedi

    Joined:
    Apr 17, 2020
    Posts:
    32
    I'm having a similar issue to gsylvain. I need to omit/ignore the VFX Renderers I'm getting when I call GetComponentInChildren for renderers. Can you recommend a way around this odd limitation?

    EDIT: For any future travelers, here's the workaround: renderer.GetType().Name != "VFXRenderer"
     
    Last edited: Oct 1, 2021
  5. Redhook_Galen

    Redhook_Galen

    Joined:
    Nov 21, 2018
    Posts:
    11
    Ran into the same problem as above as well. I have some complicated property block code that needs to ignore Particle Systems and these type of protection level decisions make our codebase slower and messier.
     
  6. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    I am having the same issue. From the API perspective there is no reason the VFXRenderer class shouldn't be accessible just like MeshRenderer, SkinnedMeshRenderer, TrailRenderer, and so on. There are many use cases for having visibility of this class that don't involve modifying its internal members.
     
    Last edited: Aug 18, 2022
    gsylvain and leftClaw like this.
  7. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    Yeah, same problem here. I have a script that should replace all materials on renderers that are not VFXRenderer, but I cannot make the check.

    upload_2022-11-14_12-29-42.png

    My workaround:


    Code (CSharp):
    1. for (var i = 0; i < _renderers.Count; i++) {
    2.   var renderer = _renderers[i];
    3.   // NOTE: Render is of type Unity.VFXRenderer, but this is not public.
    4.   if (renderer.TryGetComponent<VisualEffect>(out _)) {
    5.     continue;
    6.   }
    7.   var c = renderer.gameObject.AddComponent<ColorFlashController>();
    8.   c.Initialize(renderer, hitFlashMaterial);
    9.   _colorFlashControllers.Add(c);
    10. }
    This type should be public (and potentially visible in the inspector).
     
  8. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
  9. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
  10. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    Other possible workaround:

    Code (CSharp):
    1.     // https://forum.unity.com/threads/access-to-vfxrenderer.920966/
    2.     public static readonly Type VFXRendererType = typeof(VisualEffect).Assembly.GetType(
    3.       "UnityEngine.VFX.VFXRenderer"
    4.     );
    5.  
    6. // ...
    7.  
    8.       // Get renderers.
    9.       _renderers = ListPool<Renderer>.Get();
    10.       GetComponentsInChildren(includeInactive: true, _renderers);
    11.       _renderers.RemoveAll(r => r.GetType() == VisualEffectUtility.VFXRendererType);
     
    timmehhhhhhh and MagiJedi like this.