180 lines
6.1 KiB
Plaintext
180 lines
6.1 KiB
Plaintext
Shader "FlashEffect"
|
|
{
|
|
Properties
|
|
{
|
|
[NoScaleOffset] _MainTex("Main Texture", 2D) = "white" {}
|
|
[HDR] _Color("Color", Color) = (1, 1, 1, 1)
|
|
_StartTime("Start Time", Float) = 0
|
|
_Duration("Duration", Float) = 1.0
|
|
_FadeInTime("Fade In Time", Float) = 0.1
|
|
_FadeOutTime("Fade Out Time", Float) = 0.1
|
|
_TimeScale("Time Scale", Float) = 1.0
|
|
_TimeOffsetRange("Time Offset Range", Float) = 5.0
|
|
_CycleInterval("Cycle Interval", Float) = 1.0
|
|
_Delay("Delay", Float) = 0.1
|
|
[Toggle] _Paused("Paused", Float) = 0.0
|
|
[Toggle] _Loop("Loop", Float) = 0.0
|
|
}
|
|
|
|
SubShader
|
|
{
|
|
Tags { "RenderPipeline"="UniversalPipeline" "RenderType"="Opaque" "Queue"="Geometry" }
|
|
|
|
Pass
|
|
{
|
|
Tags { "LightMode"="UniversalForward" }
|
|
Blend SrcAlpha OneMinusSrcAlpha
|
|
ZWrite Off
|
|
Cull Off
|
|
|
|
HLSLPROGRAM
|
|
#pragma target 3.0
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
#pragma multi_compile_instancing
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
|
|
|
|
struct Attributes
|
|
{
|
|
float4 positionOS : POSITION;
|
|
half2 uv : TEXCOORD0;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
float4 positionCS : SV_POSITION;
|
|
half2 uv : TEXCOORD0;
|
|
half elapsedTime : TEXCOORD1;
|
|
half isActive : TEXCOORD2;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
};
|
|
|
|
CBUFFER_START(UnityPerMaterial)
|
|
// Keep only texture sampler in CBUFFER for SRP Batcher compatibility
|
|
CBUFFER_END
|
|
|
|
#ifdef UNITY_INSTANCING_ENABLED
|
|
UNITY_INSTANCING_BUFFER_START(InstanceProperties)
|
|
UNITY_DEFINE_INSTANCED_PROP(float, _StartTime)
|
|
UNITY_DEFINE_INSTANCED_PROP(half4, _Color)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _Duration)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _FadeInTime)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _FadeOutTime)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _TimeScale)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _TimeOffsetRange)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _CycleInterval)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _Delay)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _Paused)
|
|
UNITY_DEFINE_INSTANCED_PROP(half, _Loop)
|
|
UNITY_INSTANCING_BUFFER_END(InstanceProperties)
|
|
#define _StartTime_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _StartTime)
|
|
#define _Color_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _Color)
|
|
#define _Duration_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _Duration)
|
|
#define _FadeInTime_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _FadeInTime)
|
|
#define _FadeOutTime_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _FadeOutTime)
|
|
#define _TimeScale_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _TimeScale)
|
|
#define _TimeOffsetRange_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _TimeOffsetRange)
|
|
#define _CycleInterval_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _CycleInterval)
|
|
#define _Delay_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _Delay)
|
|
#define _Paused_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _Paused)
|
|
#define _Loop_Local UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _Loop)
|
|
#else
|
|
float _StartTime;
|
|
half4 _Color;
|
|
half _Duration;
|
|
half _FadeInTime;
|
|
half _FadeOutTime;
|
|
half _TimeScale;
|
|
half _TimeOffsetRange;
|
|
half _CycleInterval;
|
|
half _Delay;
|
|
half _Paused;
|
|
half _Loop;
|
|
#define _StartTime_Local _StartTime
|
|
#define _Color_Local _Color
|
|
#define _Duration_Local _Duration
|
|
#define _FadeInTime_Local _FadeInTime
|
|
#define _FadeOutTime_Local _FadeOutTime
|
|
#define _TimeScale_Local _TimeScale
|
|
#define _TimeOffsetRange_Local _TimeOffsetRange
|
|
#define _CycleInterval_Local _CycleInterval
|
|
#define _Delay_Local _Delay
|
|
#define _Paused_Local _Paused
|
|
#define _Loop_Local _Loop
|
|
#endif
|
|
|
|
TEXTURE2D(_MainTex);
|
|
SAMPLER(sampler_MainTex);
|
|
|
|
Varyings vert(Attributes input)
|
|
{
|
|
Varyings output;
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_TRANSFER_INSTANCE_ID(input, output);
|
|
|
|
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
|
|
output.uv = input.uv;
|
|
|
|
// Optimized hash calculation
|
|
half hash = frac(dot(input.positionOS.xyz, half3(12.9898, 78.233, 45.164)));
|
|
half timeOffset = hash * _TimeOffsetRange_Local;
|
|
|
|
// Use float for time calculations to avoid precision issues
|
|
float currentTime = (_TimeParameters.x - _StartTime_Local + timeOffset) * _TimeScale_Local;
|
|
float timeSinceStart = currentTime - _Delay_Local;
|
|
float cycleInterval = max(_CycleInterval_Local, 0.001);
|
|
|
|
// Optimized loop calculation: avoid lerp, use multiply pattern
|
|
float t = max(0.0, timeSinceStart);
|
|
float invCycleInterval = rcp(cycleInterval);
|
|
float loopedT = t - floor(t * invCycleInterval) * cycleInterval;
|
|
float elapsedTime = t + (loopedT - t) * _Loop_Local;
|
|
|
|
output.elapsedTime = half(elapsedTime);
|
|
|
|
// Optimized active check: combine conditions efficiently
|
|
float notPaused = step(_Paused_Local, 0.499);
|
|
float inRange = step(0.0, elapsedTime) * step(elapsedTime, _Duration_Local);
|
|
output.isActive = half(inRange * notPaused);
|
|
|
|
return output;
|
|
}
|
|
|
|
half4 frag(Varyings input) : SV_Target
|
|
{
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
|
|
half elapsedTime = input.elapsedTime;
|
|
half isActive = input.isActive;
|
|
|
|
// Pre-calculate reciprocals
|
|
half invFadeInTime = rcp(max(_FadeInTime_Local, 0.001));
|
|
half invFadeOutTime = rcp(max(_FadeOutTime_Local, 0.001));
|
|
|
|
// Fade in calculation
|
|
half fadeInAlpha = saturate(elapsedTime * invFadeInTime);
|
|
|
|
// Optimized fade out: reduce instructions
|
|
half fadeOutStart = _Duration_Local - _FadeOutTime_Local;
|
|
half fadeOutTime = elapsedTime - fadeOutStart;
|
|
half fadeOutAlpha = saturate(1.0 - fadeOutTime * invFadeOutTime * step(fadeOutStart, elapsedTime));
|
|
|
|
// Combined alpha calculation
|
|
half alpha = fadeInAlpha * fadeOutAlpha * isActive;
|
|
|
|
// Single texture sample and multiply
|
|
half4 finalColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv) * _Color_Local;
|
|
finalColor.a *= alpha;
|
|
|
|
return finalColor;
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
Fallback Off
|
|
}
|
|
|