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

Cant Draw a Rive panel on Sprite #89

Closed
xFN10x opened this issue Jan 30, 2025 · 1 comment
Closed

Cant Draw a Rive panel on Sprite #89

xFN10x opened this issue Jan 30, 2025 · 1 comment

Comments

@xFN10x
Copy link

xFN10x commented Jan 30, 2025

i want to use rive for my games character. but I cant use a rive texture renderer on my sprite, because it doesn't work

@damzobridge
Copy link
Contributor

damzobridge commented Jan 31, 2025

Hello,

Rive currently renders to a Render Texture, which means Rive should work wherever Render Textures are supported in Unity. The issue is that Sprite Renderers don’t natively handle Render Textures well.

To work around this, we usually recommend rendering to a Quad using the RiveTextureRenderer component. However, if you run into sorting issues when mixing Quads with SpriteRenderers, an alternative is to use a custom shader/material with the SpriteRenderer to override its texture. Here’s an example:

Shader "Custom/SpriteTextureOverride"
{
    Properties
    {
        // SpriteRenderer will automatically assign a texture here at runtime
        // We declare it but won't use it
        _MainTex ("Default Sprite", 2D) = "white" {}
        
        // This is our actual texture that we want to display
        // Could be a RenderTexture or any other texture
        _OverrideTexture ("Override Texture", 2D) = "white" {}
        
        // Required for sprite renderer sorting
        [HideInInspector] _Cutoff ("Cutoff", Float) = 0.5
        [HideInInspector] _Color ("Tint", Color) = (1,1,1,1)
    }
    
    SubShader
    {
        Tags
        { 
            "Queue"="Transparent" 
            "RenderType"="Transparent" 
            "IgnoreProjector"="True"
            "PreviewType"="Plane"
        }
        
        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR;
            };
            
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR;
            }; 
            
            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _OverrideTexture;
            float4 _OverrideTexture_ST;
            fixed4 _Color;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.color = v.color * _Color;
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 color = tex2D(_OverrideTexture, i.uv);
                color.rgb *= color.a;
                return color * i.color;
            }
            ENDCG
        }
    }
}

This shader allows you to render Rive within SpriteRenderers while maintaining sort order. You would use the RiveTextureRenderer to specify the OverrideTexture property and assign your RenderTexture to it.

Image

Keep in mind that when using the custom shader approach with SpriteRenderers, pointer input is not automatically supported. This is because SpriteRenderers and 2D Colliders do not provide textureCoords like Quads and MeshColliders do.

If you need pointer input support, consider using the recommended Quad approach with RiveTextureRenderer instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants