Search Unity

Bug Cannot use Unity.Mathematics types in custom VFXType

Discussion in 'Visual Effect Graph' started by spajus, Aug 29, 2022.

  1. spajus

    spajus

    Joined:
    Jun 10, 2015
    Posts:
    47
    Code (CSharp):
    1. using Unity.Mathematics;
    2. using UnityEngine;
    3. using UnityEngine.VFX;
    4. namespace Game.VFXR {
    5.     [VFXType(VFXTypeAttribute.Usage.GraphicsBuffer), StructLayout(LayoutKind.Sequential)]
    6.     public struct VFXRSpriteData {
    7.         public float3 Pos;
    8.         ...
    9.     }
    10. }
    11.  
    Trying to compile this gives the following error:


    Error while processing VFXType
    The type Unity.Mathematics.float3 doesn't use the expected [VFXType] attribute.
    The field 'Pos' (Unity.Mathematics.float3) in type 'Game.VFXR.VFXRSpriteData' isn't valid.


    Does that mean that the new and shiny Visual Effect Graph cannot use the new and shiny Unity.Mathematics types, and we are forced to use the old Vector3 and friends? With all the boxing involved if everything else is float3?
     
  2. JulienF_Unity

    JulienF_Unity

    Unity Technologies

    Joined:
    Dec 17, 2015
    Posts:
    326
    Hello! You are right, this is an oversight from our side. As VFX Graph package has no dependency on mathematics, it was not considered. But I don't see any technical limitation that would prevent us to support mathematics types. Would you mind filing an issue so that it can be tracked and processed faster on our side?
     
  3. spajus

    spajus

    Joined:
    Jun 10, 2015
    Posts:
    47
    Sure, created a ticket:
    IN-15274
     
  4. JulienF_Unity

    JulienF_Unity

    Unity Technologies

    Joined:
    Dec 17, 2015
    Posts:
    326
    Thanks a lot
     
  5. frankfringe

    frankfringe

    Joined:
    Feb 9, 2019
    Posts:
    105
    Is there any progress on this? Where can I find the ticket?
     
  6. spajus

    spajus

    Joined:
    Jun 10, 2015
    Posts:
    47
    The ticket was closed as "wontfix"...
     
  7. JulienF_Unity

    JulienF_Unity

    Unity Technologies

    Joined:
    Dec 17, 2015
    Posts:
    326
    Hello !

    Yes there's little chance this is adressed before we have an actual dependency on Unity.Mathematics (which should happen at some point).

    Now I guess there's a workaround:

    You can try to duplicate your struct and have one just to bind it to VFX Graph so that it knows what the fields semantics are (in this case a 3 component float vector). As the layout matches those two structs can be aliased without issue.

    Code (CSharp):
    1. struct MyStruct
    2. {
    3.    float3 a;
    4.    float b;
    5. }
    6.  
    7. [VFXType(VFXTypeAttribute.Usage.GraphicsBuffer)]
    8. struct MyVFXAliasStruct
    9. {
    10.    Vector3 a;
    11.    float b;
    12. }
    In your C# code, you use only your struct with the actual Mathematics types, and you just declare the VFX alias struct with "standard" types instead and [VFXType] attribute so that it's visible in the graph and can be used with SampleBuffer operator for instance.

    Note that this has absolutely no runtime performance incidence, it's only declarative.
    More globally, as long as the stride matches, you can reinterpret data from GraphicsBuffer as you want really.
     
    Last edited: Jan 31, 2024
    frankfringe likes this.