Antoshidza / NSprites

Unity DOTS Sprite Rendering Package
Other
536 stars 42 forks source link

Custom properties formats #7

Closed Antoshidza closed 1 year ago

Antoshidza commented 1 year ago

From now you can use whatever component data layout you want while it's blittable. Previously NSprites was able to only use 1 field components with prefefined set of formats. Now component can contain any set of field you need.

For example:

using NSprites;
using Unity.Entities;
using UnityEngine;

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

#if defined(UNITY_INSTANCING_ENABLED) || defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) || defined(UNITY_STEREO_INSTANCING_ENABLED)
    StructuredBuffer<int> _propertyPointers;
    StructuredBuffer<Colors> _colorBuffer;
#endif

// ...

half4 UnlitFragment(Varyings varyings, uint instanceID : SV_InstanceID) : SV_Target
{
#if defined(UNITY_INSTANCING_ENABLED) || defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) || defined(UNITY_STEREO_INSTANCING_ENABLED)
    int propertyIndex = _propertyPointers[instanceID];
    Colors colors = _colorBuffer[propertyIndex];
#else
    Colors colors = (Colors)0;
#endif    
    half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, varyings.uv);
    clip(texColor.w - 0.5);
    return texColor * colors.color * colors.intensity;
}

Upgraid guide: There is no more PropertyFormat so all your InstancedPropertyComponent should be used without PropertyFormat parameter. So for example [assembly: InstancedPropertyComponent(typeof(SpriteColor), "_color", PropertyFormat.Float4)] becomes [assembly: InstancedPropertyComponent(typeof(SpriteColor), "_color")]