Register components as properties - Antoshidza/NSprites GitHub Wiki

First of all to let system render anything we need to send data to GPU. System does this using ComputeBuffer. So we need to tell system which entity component contains data related to render process. Such components should be blittable.

How to register component as render property

Using InstancedPropertyComponent attribute

// ...
[assembly: InstancedPropertyComponent(typeof(WorldPosition2D), "_pos2D")]
// ...
public struct WorldPosition2D : IComponentData
{
    public float2 value;
}

All components marked with this attribute will be gathered automatically using reflection. So this is the most handy one way to do it.

Note: attribute use assembly level to let client mark components which are outside from local assembly.

Using RenderArchetypeStorage.BindComponent method

Though you can do all things by yourself if you want

// access to system singleton to register data
if (!SystemAPI.ManagedAPI.TryGetSingleton<RenderArchetypeStorage>(out var renderArchetypeStorage))
    return;

renderArchetypeStorage.BindComponentToShaderProperty("_pos2D", typeof(WorldPosition2D));
// or overload method
var pos2D_propID = Shader.PropertyToID("_pos2D");
renderArchetypeStorage.BindComponentToShaderProperty(pos2D_propID, typeof(WorldPosition2D));

❕ Note: If entity being rendered as sprite but has no declared component properties error will appear in console

💡 Tip: You can use NSPRITES_PROPERTY_FALLBACK_ENABLE directive to let system write default values if no component was attached to entity

How to use components with more than 1 field inside

You can use any data layout (from 3.0.0 version) as long as component stays blittable. Like component below:

// ...
[assembly: InstancedPropertyComponent(typeof(SpriteColor), "_colorBuffer")]
// ...
public struct SpriteColor : IComponentData
{
    public Color color;
    public float intensity;
}

In shader then you need use custom struct instead of just HLSL built-in component. So instead of single StructuredBuffer<float4> we would define our custom struct CustomColor.

struct CustomColor
{
    float4 color;
    float intensity;
}

// ...

#if defined(UNITY_INSTANCING_ENABLED) || defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) || defined(UNITY_STEREO_INSTANCING_ENABLED)
    // ...
    StructuredBuffer<CustomColor> _colorBuffer;
#endif
⚠️ **GitHub.com Fallback** ⚠️