Rasagar/Library/PackageCache/com.unity.visualeffectgraph/Shaders/VFXParticleCommon.template
2024-08-26 23:07:20 +03:00

505 lines
15 KiB
Plaintext

${VFXBegin:VFXVertexCommonProcess}
#if VFX_USE_COLOR_CURRENT && defined(VFX_VARYING_COLOR)
o.VFX_VARYING_COLOR = attributes.color;
#endif
#if VFX_USE_ALPHA_CURRENT && defined(VFX_VARYING_ALPHA)
o.VFX_VARYING_ALPHA = attributes.alpha;
#endif
#ifdef VFX_VARYING_EXPOSUREWEIGHT
${VFXLoadParameter:{exposureWeight}}
o.VFX_VARYING_EXPOSUREWEIGHT = exposureWeight;
#endif
#if USE_SOFT_PARTICLE && defined(VFX_VARYING_INVSOFTPARTICLEFADEDISTANCE)
${VFXLoadParameter:{invSoftParticlesFadeDistance}}
o.VFX_VARYING_INVSOFTPARTICLEFADEDISTANCE = invSoftParticlesFadeDistance;
#endif
#if (USE_ALPHA_TEST || VFX_FEATURE_MOTION_VECTORS_FORWARD) && (!VFX_SHADERGRAPH || !HAS_SHADERGRAPH_PARAM_ALPHACLIPTHRESHOLD) && defined(VFX_VARYING_ALPHATHRESHOLD)
${VFXLoadParameter:{alphaThreshold}}
o.VFX_VARYING_ALPHATHRESHOLD = alphaThreshold;
#endif
#if USE_UV_SCALE_BIAS
${VFXLoadParameter:{uvScale}}
${VFXLoadParameter:{uvBias}}
#if defined (VFX_VARYING_UV)
o.VFX_VARYING_UV.xy = o.VFX_VARYING_UV.xy * uvScale + uvBias;
#endif
#endif
#ifdef VFX_VARYING_ANGLEFADE
${VFXLoadParameter:{angleFade}}
o.VFX_VARYING_ANGLEFADE = angleFade;
#endif
#ifdef VFX_VARYING_FADEFACTOR
${VFXLoadParameter:{fadeFactor}}
o.VFX_VARYING_FADEFACTOR = fadeFactor;
#endif
#ifdef VFX_VARYING_DECALLAYER
${VFXLoadParameter:{decalLayerMask}}
o.VFX_VARYING_DECALLAYER = decalLayerMask;
#endif
#if defined(VFX_VARYING_POSWS)
o.VFX_VARYING_POSWS = TransformPositionVFXToWorld(vPos);
#endif
#if VFX_USE_INSTANCING
#ifdef UNITY_INSTANCING_ENABLED
o.VFX_VARYINGS_INSTANCE_CURRENT_INDEX = unity_InstanceID;
#endif
o.VFX_VARYINGS_INSTANCE_ACTIVE_INDEX = instanceActiveIndex;
#endif
${VFXEnd}
${VFXBegin:VFXGetRTScalingFactor}
float3 rtScale = float3(1,1,1);
#if VFX_USE_RT_CUSTOM_SCALE
${VFXLoadParameter:{rayTracedScaling}}
rtScale = float3(rayTracedScaling, 1.0f);
#else
#ifdef VFX_RT_DEFAULT_SCALE
rtScale = VFX_RT_DEFAULT_SCALE;
#endif
#endif
${VFXEnd}
${VFXBegin:VFXLoadSize}
float3 size3 = float3(attributes.size,attributes.size,attributes.size);
#if VFX_USE_SCALEX_CURRENT
size3.x *= attributes.scaleX;
#endif
#if VFX_USE_SCALEY_CURRENT
size3.y *= attributes.scaleY;
#endif
#if VFX_USE_SCALEZ_CURRENT
size3.z *= attributes.scaleZ;
#endif
${VFXEnd}
${VFXBegin:VFXLoadSizeRT}
float3 size3 = float3(attributes.size,attributes.size,attributes.size);
${VFXGetRTScalingFactor}
size3 *= rtScale;
#if VFX_USE_SCALEX_CURRENT
size3.x *= attributes.scaleX;
#endif
#if VFX_USE_SCALEY_CURRENT
size3.y *= attributes.scaleY;
#endif
#if VFX_USE_SCALEZ_CURRENT
size3.z *= attributes.scaleZ;
#endif
${VFXEnd}
${VFXBegin:VFXVertexSetFlipbooksInterpolants}
#if USE_FLIPBOOK && defined(VFX_VARYING_UV)
#if USE_FLIPBOOK_ARRAY_LAYOUT
${VFXLoadParameter:{flipBookSize}}
#if USE_FLIPBOOK_INTERPOLATION
VFXUVData uvData = GetUVData(flipBookSize, o.VFX_VARYING_UV.xy, attributes.texIndex, attributes.texIndexBlend);
#else
VFXUVData uvData = GetUVData(flipBookSize, o.VFX_VARYING_UV.xy, attributes.texIndex);
#endif
o.VFX_VARYING_UV.xyz = uvData.uvs.xyz;
#if USE_FLIPBOOK_INTERPOLATION && defined(VFX_VARYING_UV) && defined (VFX_VARYING_FRAMEBLEND)
o.VFX_VARYING_UV.w = uvData.uvs.w;
o.VFX_VARYING_FRAMEBLEND = uvData.blend;
#if USE_FLIPBOOK_MOTIONVECTORS && defined(VFX_VARYING_MOTIONVECTORSCALE)
${VFXLoadParameter:{motionVectorScale}}
o.VFX_VARYING_MOTIONVECTORSCALE = motionVectorScale;
#endif
#endif
#else
${VFXLoadParameter:{invFlipBookSize}}
${VFXLoadParameter:{flipBookSize}}
#if USE_FLIPBOOK_INTERPOLATION
VFXUVData uvData = GetUVData(flipBookSize, invFlipBookSize, o.VFX_VARYING_UV.xy, attributes.texIndex, attributes.texIndexBlend);
#else
VFXUVData uvData = GetUVData(flipBookSize, invFlipBookSize, o.VFX_VARYING_UV.xy, attributes.texIndex);
#endif
o.VFX_VARYING_UV.xy = uvData.uvs.xy;
#if USE_FLIPBOOK_INTERPOLATION && defined(VFX_VARYING_UV) && defined (VFX_VARYING_FRAMEBLEND)
o.VFX_VARYING_UV.zw = uvData.uvs.zw;
o.VFX_VARYING_FRAMEBLEND = uvData.blend;
#if USE_FLIPBOOK_MOTIONVECTORS && defined(VFX_VARYING_MOTIONVECTORSCALE)
${VFXLoadParameter:{motionVectorScale}}
o.VFX_VARYING_MOTIONVECTORSCALE = motionVectorScale * invFlipBookSize;
#endif
#endif
#endif
#endif
${VFXEnd}
${VFXBegin:VFXLoadAttributesOrCull}
#if HAS_STRIPS && !VFX_HAS_INDIRECT_DRAW
// We render one particle less for each strip in this case
nbMax -= STRIP_COUNT;
#endif
uint deadCount = 0;
#if USE_DEAD_LIST_COUNT
deadCount = deadList[instanceIndex];
#endif
#if VFX_USE_INSTANCING
if (index >= nbMax - deadCount)
#else
if (index >= asuint(nbMax) - deadCount)
#endif
{
CULL_VERTEX(o);
}
VFXAttributes attributes = (VFXAttributes)0;
VFXSourceAttributes sourceAttributes = (VFXSourceAttributes)0;
#if VFX_HAS_INDIRECT_DRAW
if (index >= indirectBuffer[instanceActiveIndex])
{
CULL_VERTEX(o);
}
index = indirectBuffer[VFXGetIndirectBufferIndex(index, instanceActiveIndex)];
#endif
#if HAS_STRIPS
StripData stripData;
uint relativeIndexInStrip = 0;
if (!FindIndexInStrip(index, id, instanceIndex, relativeIndexInStrip, stripData))
{
CULL_VERTEX(o);
}
#endif
#if VFX_HAS_INDIRECT_DRAW
${VFXLoadAttributes}
#else
${VFXLoadAttributes:{alive}}
#if !HAS_STRIPS
if (!attributes.alive)
{
CULL_VERTEX(o);
}
#endif
${VFXLoadAttributes:{(?!(alive))(\b\w)}}
#endif
// Initialize built-in needed attributes
#if HAS_STRIPS
InitStripAttributes(index, attributes, stripData);
#endif
${VFXEnd}
${VFXBegin:VFXVertexComputeCurrentAndPreviousClipPos}
#if VFX_FEATURE_MOTION_VECTORS && defined(VFX_VARYING_VELOCITY_CPOS_PREVIOUS) && defined(VFX_VARYING_VELOCITY_CPOS)
#ifdef VFX_FEATURE_MOTION_VECTORS_VERTS
o.VFX_VARYING_VELOCITY_CPOS_PREVIOUS = o.VFX_VARYING_VELOCITY_CPOS = (float2)0.0f;
#else
o.VFX_VARYING_VELOCITY_CPOS_PREVIOUS = o.VFX_VARYING_VELOCITY_CPOS = float4(0.0f, 0.0f, 0.0f, 1.0f);
#endif
${VFXLoadParameter:{currentFrameIndex}}
uint elementToVFXBaseIndex;
if (TryGetElementToVFXBaseIndex(index, instanceIndex, elementToVFXBaseIndex, currentFrameIndex))
{
float4 cPos = TransformPositionVFXToNonJitteredClip(vPos);
#ifdef VFX_FEATURE_MOTION_VECTORS_VERTS
o.VFX_VARYING_VELOCITY_CPOS_PREVIOUS = VFXGetPreviousClipPosition(elementToVFXBaseIndex, id).xy;
o.VFX_VARYING_VELOCITY_CPOS = cPos.xy / cPos.w;
#else
float4x4 previousElementToVFX = VFXGetPreviousElementToVFX(elementToVFXBaseIndex);
float3 oldvPos = mul(previousElementToVFX, float4(inputVertexPosition, 1.0f)).xyz;
o.VFX_VARYING_VELOCITY_CPOS_PREVIOUS = TransformPositionVFXToPreviousClip(oldvPos);
o.VFX_VARYING_VELOCITY_CPOS = cPos;
#endif
}
#endif
${VFXEnd}
${VFXBegin:VFXVertexComputeDisplacement}
float displacement = 0.0;
#if VFX_FEATURE_MOTION_VECTORS
uint elementToVFXBaseIndex;
float3 previousWS;
${VFXLoadParameter:{currentFrameIndex}}
if (TryGetElementToVFXBaseIndex(index, instanceIndex, elementToVFXBaseIndex, currentFrameIndex))
{
float4x4 previousElementToVFX = VFXGetPreviousElementToVFX(elementToVFXBaseIndex);
previousWS = TransformPreviousVFXPositionToWorld(mul(previousElementToVFX, float4(inputVertexPosition, 1.0f)).xyz);
displacement = length(currentWS - previousWS);
}
#endif
${VFXEnd}
${VFXBegin:VFXComputeOutputMotionVector}
//No w division with fast path of motion vectors
#ifdef VFX_FEATURE_MOTION_VECTORS_VERTS
float2 velocity = i.VFX_VARYING_VELOCITY_CPOS - i.VFX_VARYING_VELOCITY_CPOS_PREVIOUS;
#else
float2 velocity = (i.VFX_VARYING_VELOCITY_CPOS.xy/i.VFX_VARYING_VELOCITY_CPOS.w) - (i.VFX_VARYING_VELOCITY_CPOS_PREVIOUS.xy/i.VFX_VARYING_VELOCITY_CPOS_PREVIOUS.w);
#endif
#if UNITY_UV_STARTS_AT_TOP
velocity.y = -velocity.y;
#endif
float4 encodedMotionVector = 0.0f;
VFXEncodeMotionVector(velocity * 0.5f, encodedMotionVector);
${VFXEnd}
${VFXBegin:VFXApplyColor}
float4 color = VFXGetFragmentColor(i);
#ifndef VFX_TEXTURE_COLOR
#define VFX_TEXTURE_COLOR float4(1.0,1.0,1.0,1.0)
#endif
#if VFX_COLORMAPPING_DEFAULT
o.color = color * VFX_TEXTURE_COLOR;
#endif
#if VFX_COLORMAPPING_GRADIENTMAPPED
${VFXLoadParameter:{gradient}}
o.color = SampleGradient(gradient, VFX_TEXTURE_COLOR.a * color.a) * float4(color.rgb,1.0);
#endif
${VFXEnd}
${VFXBegin:VFXPassDepthCommonFragmentUnlit}
#ifndef VFX_SUPPORT_MAIN_TEXTURE_SAMPLING_IN_FRAGMENT_DEPTH
#define VFX_SUPPORT_MAIN_TEXTURE_SAMPLING_IN_FRAGMENT_DEPTH 0
#endif
#ifdef VFX_SHADERGRAPH
#if (SHADERPASS == SHADERPASS_DEPTHNORMALSONLY)
${SHADERGRAPH_PIXEL_CODE_DEPTHNORMALS}
#else
${SHADERGRAPH_PIXEL_CODE_DEPTHONLY}
#endif
#endif
#if VFX_PASSDEPTH == VFX_PASSDEPTH_SELECTION
int _ObjectId;
int _PassValue;
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_PICKING
float4 _SelectionID;
#endif
#pragma fragment frag
void frag(ps_input i
#if VFX_PASSDEPTH == VFX_PASSDEPTH_MOTION_VECTOR
#ifdef WRITE_MSAA_DEPTH
// We need the depth color as SV_Target0 for alpha to coverage
, out float4 outDepthColor : SV_Target0
, out float4 outMotionVector : SV_Target1
#else
// When no MSAA, the motion vector is always the first buffer
, out float4 outMotionVector : SV_Target0
#endif
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_ACTUAL
#if defined(WRITE_MSAA_DEPTH)
#if defined(WRITE_NORMAL_BUFFER)
#error Unexpected depth setup mssa + depth normal
#endif
, out float4 outDepthColor : SV_Target0
#elif defined(WRITE_NORMAL_BUFFER)
, out float4 outNormalBuffer : SV_Target0
#else
, out float4 dummy : SV_Target0
#endif
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_SELECTION || VFX_PASSDEPTH == VFX_PASSDEPTH_PICKING
, out float4 outSelection : SV_Target0
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_SHADOW
, out float4 dummy : SV_Target0
#endif
)
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
VFXTransformPSInputs(i);
${VFXComputeNormalWS}
#ifdef VFX_SHADERGRAPH
${VFXAdditionalInterpolantsPreparation}
#if (SHADERPASS == SHADERPASS_DEPTHNORMALSONLY)
${SHADERGRAPH_PIXEL_CALL_DEPTHNORMALS}
#else
${SHADERGRAPH_PIXEL_CALL_DEPTHONLY}
#endif
float alpha = OUTSG.${SHADERGRAPH_PARAM_ALPHA};
#else
float alpha = VFXGetFragmentColor(i).a;
#if VFX_SUPPORT_MAIN_TEXTURE_SAMPLING_IN_FRAGMENT_DEPTH
#ifdef VFX_PROCEDURAL_UV
alpha *= VFXGetTextureColorWithProceduralUV(VFX_SAMPLER(mainTexture),i,VFX_PROCEDURAL_UV(i)).a;
#else
alpha *= VFXGetTextureColor(VFX_SAMPLER(mainTexture),i).a;
#endif
#endif
#endif
VFXClipFragmentColor(alpha,i);
#ifdef WRITE_MSAA_DEPTH
outDepthColor = i.VFX_VARYING_POSCS.z;
#if VFX_USE_ALPHA_TO_MASK
outDepthColor.a = alpha;
#endif
#endif
#ifdef WRITE_NORMAL_BUFFER
#ifdef VFX_VARYING_NORMAL
VFXComputePixelOutputToNormalBuffer(i, normalWS, GetUVData(i), outNormalBuffer);
#else
//Fallback for point and lines, render normal as if those are face camera plane
VFXComputePixelOutputToNormalBuffer(i, VFXGetWorldToViewRotMatrix()[2], GetUVData(i), outNormalBuffer);
#endif
#endif
#if VFX_PASSDEPTH == VFX_PASSDEPTH_MOTION_VECTOR
${VFXComputeOutputMotionVector}
outMotionVector = encodedMotionVector;
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_SELECTION
outSelection = float4(_ObjectId, _PassValue, 1.0, 1.0);
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_PICKING
outSelection = _SelectionID;
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_ACTUAL
#if !defined(WRITE_MSAA_DEPTH) && !defined(WRITE_NORMAL_BUFFER)
dummy = float4(i.VFX_VARYING_POSCS.z, 0,0,0);
#endif
#elif VFX_PASSDEPTH == VFX_PASSDEPTH_SHADOW
dummy = (float4)0;
#else
#error VFX_PASSDEPTH undefined
#endif
}
${VFXEnd}
// Strips tangent computation
${VFXBegin:VFXDeclareGetStripTangent}
float3 GetParticlePosition(uint index, uint instanceIndex)
{
VFXAttributes attributes = (VFXAttributes)0;
${VFXLoadAttributes:{position}}
return attributes.position;
}
float3 GetStripTangent(float3 currentPos, uint instanceIndex, uint relativeIndex, const StripData stripData)
{
float3 prevTangent = (float3)0.0f;
if (relativeIndex > 0)
{
uint prevIndex = GetParticleIndex(relativeIndex - 1,stripData);
float3 tangent = currentPos - GetParticlePosition(prevIndex,instanceIndex);
float sqrLength = dot(tangent, tangent);
if (sqrLength > VFX_EPSILON * VFX_EPSILON)
prevTangent = tangent * rsqrt(sqrLength);
}
float3 nextTangent = (float3)0.0f;
if (relativeIndex < stripData.nextIndex - 1)
{
uint nextIndex = GetParticleIndex(relativeIndex + 1,stripData);
float3 tangent = GetParticlePosition(nextIndex, instanceIndex) - currentPos;
float sqrLength = dot(tangent, tangent);
if (sqrLength > VFX_EPSILON * VFX_EPSILON)
nextTangent = tangent * rsqrt(sqrLength);
}
return normalize(prevTangent + nextTangent);
}
${VFXEnd}
${VFXBegin:VFXInitInstancing}
uint instanceIndex, instanceActiveIndex, instanceCurrentIndex;
index = VFXInitInstancing(index, instanceIndex, instanceActiveIndex, instanceCurrentIndex);
${VFXEnd}
${VFXBegin:VFXInitInstancingCompute}
uint instanceIndex, instanceActiveIndex, instanceCurrentIndex;
#if defined(VFX_INSTANCING_FIXED_SIZE)
uint index = GetThreadId(groupId, 0, dispatchWidth);
index = VFXInitInstancing(index, instanceIndex, instanceActiveIndex, instanceCurrentIndex);
index += groupThreadId.x;
#else
uint index = VFXInitInstancing(id, instanceIndex, instanceActiveIndex, instanceCurrentIndex);
#endif
${VFXEnd}
${VFXBegin:VFXInstancingConstants}
float4 instancingConstants;
uint2 instancingBufferOffsets;
${VFXEnd}
${VFXBegin:VFXFeedSortingKeys}
Kvp output;
#if VFX_CUSTOM_SORT_KEY
${VFXLoadParameter:{sortKey}}
output.sortKey = -1.0f * sortKey; //Lowest values are rendered first
#elif VFX_DISTANCE_SORT_KEY
#if VFX_LOCAL_SPACE
float3 posRWS = TransformObjectToWorld(attributes.position);
#else
float3 posRWS = GetCameraRelativePositionWS(attributes.position);
#endif
float3 camToPos = posRWS - GetCurrentViewPosition();
output.sortKey = dot(camToPos,camToPos); // sqr distance to the camera
#elif VFX_DEPTH_SORT_KEY
#if VFX_LOCAL_SPACE
float3 posRWS = TransformObjectToWorld(attributes.position);
#else
float3 posRWS = GetCameraRelativePositionWS(attributes.position);
#endif
float3 zAxisCam = -GetWorldToViewMatrix()[2].xyz;
float depth = dot(posRWS, zAxisCam);
output.sortKey = depth;
#elif VFX_YOUNGEST_SORT_KEY
${VFXLoadAttributes:{age}}
output.sortKey = attributes.age;
#endif //VFX_[CRITERION]_SORT_KEY
output.index = index;
output.sortKey *= SORTING_SIGN;
${VFXEnd}
${VFXBegin:VFXGetIndexFromRTPrimitiveIndex}
#ifdef VFX_RT_DECIMATION_FACTOR
int rayTracingDecimationFactor = VFX_RT_DECIMATION_FACTOR;
#else
int rayTracingDecimationFactor = 1;
#endif
uint index = PrimitiveIndex() * rayTracingDecimationFactor;
uint instanceIndex = asuint(_InstanceIndex);
uint instanceActiveIndex = asuint(_InstanceActiveIndex);
VFXGetInstanceCurrentIndex(index);
${VFXEnd}
${VFXBegin:VFXDeclareAppendOutputIndirectBuffer}
void AppendOutputBuffer(RWStructuredBuffer<IndirectOutputType> outputBuffer, IndirectOutputType output, uint instanceActiveIndex, uint increment = 1)
{
uint indirectIndex;
#if VFX_FEATURE_SORT
InterlockedAdd(outputBuffer[instanceActiveIndex].index, increment, indirectIndex);
#else
InterlockedAdd(outputBuffer[instanceActiveIndex], increment, indirectIndex);
#endif
indirectIndex /= increment;
indirectIndex += instancingBatchSize + instanceActiveIndex * RAW_CAPACITY;
outputBuffer[indirectIndex] = output;
}
${VFXEnd}