I’m trying to do a flipbook shader where the framerate is variable per particles using custom data in shuriken system.
I’m no gonna lie the shader come from IA model langage. (claude opus).
It works when same min and max speed but if I set them different it produce wrong result where images add together.
Someone can see the error ?
Shader "Custom/FlipbookParticleUnlit"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Rows ("Rows", Float) = 4
_Columns ("Columns", Float) = 4
_MinSpeed ("Min Speed", Float) = 5
_MaxSpeed ("Max Speed", Float) = 15
_Color ("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Transparent" "RenderPipeline"="UniversalPipeline"}
LOD 100
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
ENDHLSL
Pass
{
Name "Unlit"
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
float4 custom1 : TEXCOORD1;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
float speed : TEXCOORD1;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
float _Rows;
float _Columns;
float _MinSpeed;
float _MaxSpeed;
float4 _Color;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
OUT.color = IN.color;
OUT.speed = lerp(_MinSpeed, _MaxSpeed, IN.custom1.x);
return OUT;
}
float4 frag(Varyings IN) : SV_Target
{
float2 uv = IN.uv;
float totalFrames = _Rows * _Columns;
float frame = fmod(floor(_Time.y * IN.speed), totalFrames);
float2 offsets = float2(
(frame % _Columns) / _Columns,
(floor(frame / _Columns)) / _Rows
);
uv /= float2(_Columns, _Rows);
uv += offsets;
float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
color *= _Color * IN.color;
return color;
}
ENDHLSL
}
}
}
