This commit is contained in:
SoulliesOfficial
2026-07-17 17:46:16 -04:00
parent b00ac27e3a
commit 40fa80cd70
1178 changed files with 51090 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fa89a0fdb680e154f89c18f1a7fea2c6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,521 @@
#ifndef HOUDINI_VAT_INCLUDED
#define HOUDINI_VAT_INCLUDED
// ─────────────────────────────────────────────────────────────────────
// Houdini VAT 3.0 — 统一实现SoftBody / RigidBody / DynamicRemeshing / ParticleSprite
// 集成于 ParticleBase.shader 的粒子着色器管线
// ─────────────────────────────────────────────────────────────────────
// ── 纹理声明 ──────────────────────────────────────────────────────────
TEXTURE2D(_posTexture); SAMPLER(sampler_posTexture);
TEXTURE2D(_posTexture2); SAMPLER(sampler_posTexture2);
TEXTURE2D(_rotTexture); SAMPLER(sampler_rotTexture);
TEXTURE2D(_colTexture); SAMPLER(sampler_colTexture);
TEXTURE2D(_lookupTable); SAMPLER(sampler_lookupTable);
#if defined(_VAT) && defined(_VAT_HOUDINI) && \
!defined(_HOUDINI_VAT_SOFTBODY) && \
!defined(_HOUDINI_VAT_RIGIDBODY) && \
!defined(_HOUDINI_VAT_DYNAMIC_REMESH) && \
!defined(_HOUDINI_VAT_PARTICLE_SPRITE)
#define _HOUDINI_VAT_SOFTBODY
#endif
// ── extern 材质属性Unity 自动从材质中查找同名值) ───────────────────
// Playback
extern float _B_autoPlayback;
extern float _gameTimeAtFirstFrame;
extern float _playbackSpeed;
extern float _houdiniFPS;
extern float _displayFrame;
extern float _B_interpolate;
extern float _animateFirstFrame;
extern float _frameCount;
// Bounds
extern float _boundMinX, _boundMinY, _boundMinZ;
extern float _boundMaxX, _boundMaxY, _boundMaxZ;
// Scale
extern float _globalPscaleMul;
extern float _B_pscaleAreInPosA;
// Particle Sprite
extern float _widthBaseScale;
extern float _heightBaseScale;
extern float _B_hideOverlappingOrigin;
extern float _originRadius;
extern float _B_CAN_SPIN;
extern float _B_spinFromHeading;
extern float _spinPhase;
extern float _scaleByVelAmount;
extern float _particleTexUScale;
extern float _particleTexVScale;
// Flags
extern float _B_LOAD_POS_TWO_TEX;
extern float _B_UNLOAD_ROT_TEX;
extern float _B_LOAD_COL_TEX;
extern float _B_LOAD_LOOKUP_TABLE;
// ─────────────────────────────────────────────────────────────────────
// 工具函数
// ─────────────────────────────────────────────────────────────────────
// 用单位四元数旋转向量
// 公式: v' = v + 2 * cross(q.xyz, q.w*v + cross(q.xyz, v))
float3 HVAT_RotateByQuat(float3 v, float4 q)
{
float3 t = cross(q.xyz, v);
return v + cross(q.xyz, t + v * q.w) * 2.0;
}
// 从 3 个最小组件重建单位四元数RigidBody 专用)
// maxComp (0-3) 标识被省略的分量
float4 HVAT_DecodeQuaternion(float3 xyz, float maxComp)
{
float w = sqrt(saturate(1.0 - dot(xyz, xyz)));
float4 q = float4(xyz.x, xyz.y, xyz.z, w);
int mc = (int)maxComp;
if (mc == 1) q = float4( w, xyz.y, xyz.z, xyz.x);
else if (mc == 2) q = float4(xyz.x, -w, xyz.z, -xyz.y);
else if (mc == 3) q = float4(xyz.x, xyz.y, -w, -xyz.z);
return q;
}
// 从 posTexture.a 解码 5-bit spheremap 压缩法线
float3 HVAT_DecodeCompressedNormal(float posA)
{
float scaledA = posA * 1024.0;
float xIdx = floor(scaledA / 32.0);
float yRaw = scaledA - xIdx * 32.0;
float xNorm = xIdx / 31.5;
float yNorm = yRaw / 31.5;
float2 xy = float2(xNorm, yNorm) * 4.0 - 2.0;
float d = dot(xy, xy);
float sqrtF = sqrt(saturate(1.0 - d * 0.25));
return float3(-sqrtF * xy.x, 1.0 - d * 0.5, sqrtF * xy.y);
}
// Lookup table 解码:从 RGBA 得到高精度采样 UV
float2 HVAT_DecodeLookupUV(float4 lookupSample, float boundMinX)
{
float lookupHDR = (frac(-boundMinX * 10.0) >= 0.5) ? 1.0 : 0.0;
float divisor = lookupHDR ? 2048.0 : 255.0;
float lookupX = lookupSample.r + lookupSample.g / divisor;
float lookupY = 1.0 - (lookupSample.b + lookupSample.a / divisor);
return float2(lookupX, lookupY);
}
// 计算 VAT 采样 UV
float2 HVAT_VatUV(float selectedFrame, float uv_r, float uv_g,
float oneMinusBoundMaxR, float multiplyBoundMinB, float totalFrames)
{
float wrapped = fmod(selectedFrame - 1.0, totalFrames);
float vBase = (1.0 - uv_g) * oneMinusBoundMaxR
+ (wrapped / totalFrames) * oneMinusBoundMaxR;
return float2(multiplyBoundMinB, 1.0 - vBase);
}
// 2D hash 随机 [0, 1]
float HVAT_HashRandom2D(float2 seed)
{
return frac(sin(dot(seed, float2(12.9898, 78.233))) * 43758.5453);
}
// ─────────────────────────────────────────────────────────────────────
// 帧选择
// ─────────────────────────────────────────────────────────────────────
float2 HVAT_GetVatUV1(AttributesParticle input)
{
return CheckLocalFlags1(FLAG_BIT_PARTICLE_1_IS_PARTICLE_SYSTEM)
? input.texcoords.zw
: input.Custom1.xy;
}
void HVAT_ComputeFrameSelection(AttributesParticle input, out float selectedFrame, out float frameAlpha)
{
float totalFrames = _frameCount;
float animTime = (_Time.y - _gameTimeAtFirstFrame)
* (_houdiniFPS / (totalFrames - 0.01))
* _playbackSpeed;
float frameFloat = frac(animTime) * totalFrames;
selectedFrame = _B_autoPlayback
? floor(frameFloat) + 1.0
: floor(_displayFrame);
frameAlpha = frac(_B_autoPlayback ? frameFloat : _displayFrame);
if (CheckLocalFlags1(FLAG_BIT_PARTICLE_1_IS_PARTICLE_SYSTEM))
{
float frameCustomData = GetCustomData(NB_CUSTOM_DATA_FLAG_2, FLAGBIT_POS_2_CUSTOMDATA_VAT_FRAME, -1.0, input.Custom1, input.Custom2);
if (frameCustomData >= 0.0)
{
float customFrame = saturate(frameCustomData) * max(totalFrames - 1.0, 0.0) + 1.0;
selectedFrame = floor(customFrame);
frameAlpha = frac(customFrame);
}
}
}
// ─────────────────────────────────────────────────────────────────────
// 主函数ApplyHoudiniVAT
// ─────────────────────────────────────────────────────────────────────
void ApplyHoudiniVAT(AttributesParticle input, inout float4 positionOS, inout float3 normalOS)
{
// ── 共享 Bounds 常量(不依赖 UV 的部分) ──
float comparisonBoundMaxb = (frac(_boundMaxZ * 10.0) >= 0.5) ? 1.0 : 0.0;
float oneMinusBoundMaxR = 1.0 - frac(_boundMaxX * (-10.0));
float boundMinMul10z = _boundMinZ * 10.0;
float oneMinusBoundMinB = 1.0 - (ceil(boundMinMul10z) - boundMinMul10z);
float pscaleDenom = max(1.0 - frac(_boundMaxY * 10.0), 1e-5);
// ── 帧选择 ──
float selectedFrame, frameAlpha;
HVAT_ComputeFrameSelection(input, selectedFrame, frameAlpha);
float totalFrames = _frameCount;
float3 boundsMax = float3(_boundMaxX, _boundMaxY, _boundMaxZ);
float3 boundsMin = float3(_boundMinX, _boundMinY, _boundMinZ);
// ─────────────────────────────────────────────────────────────────
// Sub Mode 0: SoftBody — 加法位移 + 四元数法线 / 压缩法线
// ─────────────────────────────────────────────────────────────────
#if defined(_HOUDINI_VAT_SOFTBODY)
{
float2 vatUV1 = HVAT_GetVatUV1(input);
float uv1r = vatUV1.r;
float uv1g = vatUV1.g;
float multiplyBoundMinB = uv1r * oneMinusBoundMinB;
float2 texUV = HVAT_VatUV(selectedFrame, uv1r, uv1g,
oneMinusBoundMaxR, multiplyBoundMinB, totalFrames);
// 采样位置
float4 posSample = SAMPLE_TEXTURE2D_LOD(_posTexture, sampler_posTexture, texUV, 0);
float3 posRGB = posSample.rgb;
float posA = posSample.a;
// 双纹理高精度位置
if (_B_LOAD_POS_TWO_TEX > 0.5)
{
float4 pos2 = SAMPLE_TEXTURE2D_LOD(_posTexture2, sampler_posTexture2, texUV, 0);
posRGB += pos2.rgb * 0.01;
}
// 解码位移
float3 posDecoded = posRGB * (boundsMax - boundsMin) + boundsMin;
float3 displacement = comparisonBoundMaxb ? posRGB : posDecoded;
// SoftBody: 原始位置 + 位移
positionOS.xyz += displacement;
// 法线
if (_B_UNLOAD_ROT_TEX > 0.5)
{
normalOS = normalize(HVAT_DecodeCompressedNormal(posA));
}
else
{
float4 rotSample = SAMPLE_TEXTURE2D_LOD(_rotTexture, sampler_rotTexture, texUV, 0);
float4 rotFinal = comparisonBoundMaxb ? rotSample : (rotSample - 0.5) * 2.0;
normalOS = normalize(HVAT_RotateByQuat(float3(0.0, 1.0, 0.0), rotFinal));
}
return;
}
// ─────────────────────────────────────────────────────────────────
// Sub Mode 1: RigidBody — Pivot 旋转 + Pscale + 帧间插值
// ─────────────────────────────────────────────────────────────────
#elif defined(_HOUDINI_VAT_RIGIDBODY)
{
if (CheckLocalFlags1(FLAG_BIT_PARTICLE_1_IS_PARTICLE_SYSTEM))
{
return;
}
float2 vatUV1 = HVAT_GetVatUV1(input);
float uv1r = vatUV1.r;
float uv1g = vatUV1.g;
float multiplyBoundMinB = uv1r * oneMinusBoundMinB;
// 当前帧和下一帧 UV
float2 texUV = HVAT_VatUV(selectedFrame, uv1r, uv1g,
oneMinusBoundMaxR, multiplyBoundMinB, totalFrames);
float2 texUV_next = HVAT_VatUV(selectedFrame + 1.0, uv1r, uv1g,
oneMinusBoundMaxR, multiplyBoundMinB, totalFrames);
// 采样旋转
float4 rotSample = SAMPLE_TEXTURE2D_LOD(_rotTexture, sampler_rotTexture, texUV, 0);
float4 rotRemapped = (rotSample - 0.5) * 2.0;
float4 rotFinal = comparisonBoundMaxb ? rotSample : rotRemapped;
// 采样位置
float4 posSample = SAMPLE_TEXTURE2D_LOD(_posTexture, sampler_posTexture, texUV, 0);
float3 posRGB = posSample.rgb;
float posA = posSample.a;
// 四元数 maxComp 索引(从 posA 整数部分)
float quatMaxIdxScaled = posA * 4.0;
float quatMaxIdx = floor(quatMaxIdxScaled);
// 解码四元数
float4 q = HVAT_DecodeQuaternion(rotFinal.rgb, quatMaxIdx);
// 解码位置
float3 posRawForDecode = posRGB;
if (_B_LOAD_POS_TWO_TEX > 0.5)
{
float4 pos2 = SAMPLE_TEXTURE2D_LOD(_posTexture2, sampler_posTexture2, texUV, 0);
posRawForDecode = posRGB + pos2.rgb * 0.01;
}
float3 posDecoded = posRawForDecode * (boundsMax - boundsMin) + boundsMin;
float3 piecePos = comparisonBoundMaxb ? posRawForDecode : posDecoded;
// Pscale
float pscaleFromPosA = (1.0 - frac(quatMaxIdxScaled)) / pscaleDenom;
float pscale = (_B_pscaleAreInPosA > 0.5) ? pscaleFromPosA : 1.0;
float totalScale = _globalPscaleMul * pscale;
// Rest pivot 从 UV2/UV3
// Custom2 = TEXCOORD2 (uv2), vatTexcoord5 = TEXCOORD4 (uv3)
float3 restPivot = float3(-input.Custom2.r, input.vatTexcoord5.r, 1.0 - input.vatTexcoord5.g);
// 旋转局部偏移
float3 localOffset = positionOS.xyz - restPivot;
float3 rotatedLocal = HVAT_RotateByQuat(localOffset, q);
float3 scaledLocal = rotatedLocal * totalScale;
// 帧间插值
float3 finalPiecePos = piecePos;
if (_B_interpolate > 0.5)
{
float4 posSampleNext = SAMPLE_TEXTURE2D_LOD(_posTexture, sampler_posTexture, texUV_next, 0);
float3 posRGBNext = posSampleNext.rgb;
if (_B_LOAD_POS_TWO_TEX > 0.5)
{
float4 pos2Next = SAMPLE_TEXTURE2D_LOD(_posTexture2, sampler_posTexture2, texUV_next, 0);
posRGBNext += pos2Next.rgb * 0.01;
}
float3 posDecodedNext = posRGBNext * (boundsMax - boundsMin) + boundsMin;
float3 piecePosNext = comparisonBoundMaxb ? posRGBNext : posDecodedNext;
finalPiecePos = lerp(piecePos, piecePosNext, frameAlpha);
}
// 组装最终位置
float3 animatedPos = scaledLocal + finalPiecePos;
// 首帧复位
float wrappedForCheck = fmod(selectedFrame - 1.0, totalFrames);
bool isRestFrame = (wrappedForCheck < 0.5) && !(_animateFirstFrame > 0.5);
float3 finalPosOS = isRestFrame ? positionOS.xyz : animatedPos;
// 崩塌:无 piece 关联
finalPosOS = (uv1g <= 0.1) ? float3(0, 0, 0) : finalPosOS;
// 法线
float3 rotatedNormalOS = isRestFrame
? normalOS
: normalize(HVAT_RotateByQuat(normalOS, q));
normalOS = rotatedNormalOS;
positionOS.xyz = finalPosOS;
return;
}
// ─────────────────────────────────────────────────────────────────
// Sub Mode 2: DynamicRemeshing — Lookup Table → 绝对位置
// ─────────────────────────────────────────────────────────────────
#elif defined(_HOUDINI_VAT_DYNAMIC_REMESH)
{
// 使用 uv0 (texcoords.r/g) 作为 piece UV
float uv0r = input.texcoords.r;
float uv0g = input.texcoords.g;
float multiplyBoundMinB = uv0r * oneMinusBoundMinB;
float2 vatUV = HVAT_VatUV(selectedFrame, uv0r, uv0g,
oneMinusBoundMaxR, multiplyBoundMinB, totalFrames);
// Lookup Table
float4 lookupSample = SAMPLE_TEXTURE2D_LOD(_lookupTable, sampler_lookupTable, vatUV, 0);
float2 lookupUV = HVAT_DecodeLookupUV(lookupSample, _boundMinX);
// 采样位置(绝对坐标)
float4 posSample = SAMPLE_TEXTURE2D_LOD(_posTexture, sampler_posTexture, lookupUV, 0);
float3 posRGB = posSample.rgb;
float posA = posSample.a;
// 双纹理高精度位置
if (_B_LOAD_POS_TWO_TEX > 0.5)
{
float4 pos2 = SAMPLE_TEXTURE2D_LOD(_posTexture2, sampler_posTexture2, lookupUV, 0);
posRGB += pos2.rgb * 0.01;
}
// 解码绝对位置
float3 posDecoded = posRGB * (boundsMax - boundsMin) + boundsMin;
float3 finalPosOS = comparisonBoundMaxb ? posRGB : posDecoded;
// 无有效 piece 塌陷
finalPosOS = (uv0g <= 0.1) ? float3(0, 0, 0) : finalPosOS;
positionOS.xyz = finalPosOS;
// 法线
if (_B_UNLOAD_ROT_TEX > 0.5)
{
normalOS = normalize(HVAT_DecodeCompressedNormal(posA));
}
else
{
float4 rotSample = SAMPLE_TEXTURE2D_LOD(_rotTexture, sampler_rotTexture, lookupUV, 0);
float4 rotFinal = comparisonBoundMaxb ? rotSample : (rotSample - 0.5) * 2.0;
normalOS = normalize(HVAT_RotateByQuat(float3(0.0, 1.0, 0.0), rotFinal));
}
return;
}
// ─────────────────────────────────────────────────────────────────
#elif defined(_HOUDINI_VAT_PARTICLE_SPRITE)
// Sub Mode 3: ParticleSprite — Billboard + Spin + Origin Mask
// ─────────────────────────────────────────────────────────────────
{
float2 vatUV1 = HVAT_GetVatUV1(input);
float uv1r = vatUV1.r;
float uv1g = vatUV1.g;
float multiplyBoundMinB = uv1r * oneMinusBoundMinB;
// 当前帧 + 下一帧 UV
float2 vatUV = HVAT_VatUV(selectedFrame, uv1r, uv1g,
oneMinusBoundMaxR, multiplyBoundMinB, totalFrames);
float2 vatUV_next = HVAT_VatUV(selectedFrame + 1.0, uv1r, uv1g,
oneMinusBoundMaxR, multiplyBoundMinB, totalFrames);
// 采样位置
float4 posSample = SAMPLE_TEXTURE2D_LOD(_posTexture, sampler_posTexture, vatUV, 0);
float4 posSample_next = SAMPLE_TEXTURE2D_LOD(_posTexture, sampler_posTexture, vatUV_next, 0);
float3 posRGB = posSample.rgb;
float3 posRGB_next = posSample_next.rgb;
float posA = posSample.a;
float posA_next = posSample_next.a;
// 双纹理高精度位置
if (_B_LOAD_POS_TWO_TEX > 0.5)
{
float4 pos2 = SAMPLE_TEXTURE2D_LOD(_posTexture2, sampler_posTexture2, vatUV, 0);
float4 pos2_next = SAMPLE_TEXTURE2D_LOD(_posTexture2, sampler_posTexture2, vatUV_next, 0);
posRGB += pos2.rgb * 0.01;
posRGB_next += pos2_next.rgb * 0.01;
}
// 解码粒子中心
float3 posDecoded = posRGB * (boundsMax - boundsMin) + boundsMin;
float3 posDecoded_next = posRGB_next * (boundsMax - boundsMin) + boundsMin;
float3 particlePos = comparisonBoundMaxb ? posRGB : posDecoded;
float3 particlePos_next = comparisonBoundMaxb ? posRGB_next : posDecoded_next;
// 帧间插值
float3 particlePosF = (_B_interpolate > 0.5)
? lerp(particlePos, particlePos_next, frameAlpha)
: particlePos;
// Pscale
float posA_f = (_B_interpolate > 0.5) ? lerp(posA, posA_next, frameAlpha) : posA;
float pscaleRaw = posA_f / pscaleDenom;
// 每粒子随机缩放
float perParticleRandom = HVAT_HashRandom2D(float2(uv1r, uv1g));
float additionalPscaleMul = 1.0 + perParticleRandom;
// 原点遮挡 mask
float distThis = distance(particlePos, float3(0, 0, 0));
float distNext = distance(particlePos_next, float3(0, 0, 0));
float maskThis = saturate(sign(distThis - _originRadius));
float maskNext = saturate(sign(distNext - _originRadius));
float maskF = (_B_interpolate > 0.5) ? lerp(maskThis, maskNext, frameAlpha) : maskThis;
float pscaleFinal;
if (_B_pscaleAreInPosA > 0.5)
{
pscaleFinal = (_B_hideOverlappingOrigin > 0.5)
? pscaleRaw * _globalPscaleMul * additionalPscaleMul * maskF
: pscaleRaw * _globalPscaleMul * additionalPscaleMul;
}
else
{
pscaleFinal = (_B_hideOverlappingOrigin > 0.5)
? _globalPscaleMul * additionalPscaleMul * maskF
: _globalPscaleMul * additionalPscaleMul;
}
// Billboard 方向轴
float3 viewRight = float3(1, 0, 0);
float3 viewUp = float3(0, 1, 0);
float velStretch = 1.0;
float3 headingViewDir = float3(0, 0, 0);
if (_B_CAN_SPIN > 0.5)
{
// 从颜色通道读取 heading
if (_B_LOAD_COL_TEX > 0.5)
{
float4 colThis = SAMPLE_TEXTURE2D_LOD(_colTexture, sampler_colTexture, vatUV, 0);
headingViewDir = float3(-colThis.r, colThis.g, colThis.b);
}
if (_B_spinFromHeading > 0.5)
{
float2 hXY = headingViewDir.xy;
float hLen = length(hXY);
float2 hDir = (hLen > 1e-5) ? hXY / hLen : float2(1, 0);
viewRight = float3(hDir.x, hDir.y, 0);
viewUp = cross(viewRight, float3(0, 0, -1));
velStretch = _scaleByVelAmount * hLen;
}
else
{
float angle = frac(_spinPhase) * 6.283185;
float c = cos(angle);
float s = sin(angle);
viewRight = float3(c, s, 0);
viewUp = cross(viewRight, float3(0, 0, -1));
}
}
// 视图空间 → 世界空间 → 物体空间
float3 worldRight = mul((float3x3)UNITY_MATRIX_I_V, viewRight);
float3 worldUp = mul((float3x3)UNITY_MATRIX_I_V, viewUp);
float3 worldFwd = mul((float3x3)UNITY_MATRIX_I_V, float3(0, 0, 1));
float3 rightOS = normalize(TransformWorldToObjectDir(worldRight));
float3 upOS = normalize(TransformWorldToObjectDir(worldUp));
float3 normalOS_new = normalize(TransformWorldToObjectDir(worldFwd));
// Billboard 顶点偏移
float cornerX = input.texcoords.r - 0.5;
float cornerY = input.texcoords.g - 0.5;
float3 offsetX = rightOS * cornerX * _widthBaseScale * pscaleFinal;
float3 offsetY = upOS * cornerY * _heightBaseScale * pscaleFinal * velStretch;
float3 finalPosOS = particlePosF + offsetX + offsetY;
// 崩塌
finalPosOS = (uv1g <= 0.1) ? float3(0, 0, 0) : finalPosOS;
positionOS.xyz = finalPosOS;
normalOS = normalOS_new;
return;
}
#endif
}
#endif // HOUDINI_VAT_INCLUDED

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 802859f4f9ef96442bf245ade4894f0d
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,356 @@
#ifndef SIX_WAY_SMOKE_LIT_HLSL
#define SIX_WAY_SMOKE_LIT_HLSL
//这部分尽量借鉴 UnityEditor.VFX.HDRP.SixWaySmokeLit
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
// Generated from UnityEditor.VFX.HDRP.SixWaySmokeLit+BSDFData
// PackingRules = Exact
struct BSDFData
{
uint materialFeatures;
float absorptionRange;
real4 diffuseColor;
real3 fresnel0;
real ambientOcclusion;
float3 normalWS;
float4 tangentWS;
real3 geomNormalWS;
real3 rigRTBk;
real3 rigLBtF;
real3 bakeDiffuseLighting0;//rigRTBk.x
real3 bakeDiffuseLighting1;//rigRTBk.y
real3 bakeDiffuseLighting2;// bsdfData.tangentWS.w > 0.0f ? rigRTBk.z : rigLBtF.z
real3 backBakeDiffuseLighting0;//rigLBtF.x
real3 backBakeDiffuseLighting1;//rigLBtF.y
real3 backBakeDiffuseLighting2;// bsdfData.tangentWS.w > 0.0f ? rigLBtF.z : rigRTBk.z
//-----NBShaders-----
real3 emission;
real emissionInput;//NegativeTex.a
real alpha;//PositiveTex.a
};
#define ABSORPTION_EPSILON max(REAL_MIN, 1e-5)
real3 ComputeDensityScales(real3 absorptionColor)
{
absorptionColor.rgb = max(ABSORPTION_EPSILON, absorptionColor.rgb);
// Empirical value used to parametrize absorption from color
const real absorptionStrength = 0.2f;
return 1.0f + log2(absorptionColor.rgb) / log2(absorptionStrength);
}
real3 GetTransmissionWithAbsorption(real transmission, real4 absorptionColor, real absorptionRange)
{
#if defined(VFX_SIX_WAY_ABSORPTION)
real3 densityScales = ComputeDensityScales(absorptionColor.rgb);
#ifdef VFX_BLENDMODE_PREMULTIPLY
absorptionRange *= (absorptionColor.a > 0) ? absorptionColor.a : 1.0f;
#endif
// real3 outTransmission = GetTransmissionWithAbsorption(transmission, densityScales, absorptionRange);
real3 outTransmission = pow(saturate(transmission / absorptionRange), densityScales);
outTransmission *= absorptionRange;
return outTransmission;
#else
return transmission.xxx * absorptionColor.rgb; // simple multiply
#endif
}
void ModifyBakedDiffuseLighting(BSDFData bsdfData, inout float3 bakeDiffuseLighting)
{
bakeDiffuseLighting = 0;
// Scale to be energy conserving: Total energy = 4*pi; divided by 6 directions
float scale = 4.0f * PI / 6.0f;
float3 frontBakeDiffuseLighting = bsdfData.tangentWS.w > 0.0f ? bsdfData.bakeDiffuseLighting2 : bsdfData.backBakeDiffuseLighting2;
float3 backBakeDiffuseLighting = bsdfData.tangentWS.w > 0.0f ? bsdfData.backBakeDiffuseLighting2 : bsdfData.bakeDiffuseLighting2;
float3x3 bakeDiffuseLightingMat;
bakeDiffuseLightingMat[0] = bsdfData.bakeDiffuseLighting0;
bakeDiffuseLightingMat[1] = bsdfData.bakeDiffuseLighting1;
bakeDiffuseLightingMat[2] = frontBakeDiffuseLighting;
bakeDiffuseLighting += GetTransmissionWithAbsorption(bsdfData.rigRTBk.x, bsdfData.diffuseColor, bsdfData.absorptionRange) * bakeDiffuseLightingMat[0];
bakeDiffuseLighting += GetTransmissionWithAbsorption(bsdfData.rigRTBk.y, bsdfData.diffuseColor, bsdfData.absorptionRange) * bakeDiffuseLightingMat[1];
bakeDiffuseLighting += GetTransmissionWithAbsorption(bsdfData.rigRTBk.z, bsdfData.diffuseColor, bsdfData.absorptionRange) * bakeDiffuseLightingMat[2];
bakeDiffuseLightingMat[0] = bsdfData.backBakeDiffuseLighting0;
bakeDiffuseLightingMat[1] = bsdfData.backBakeDiffuseLighting1;
bakeDiffuseLightingMat[2] = backBakeDiffuseLighting;
bakeDiffuseLighting += GetTransmissionWithAbsorption(bsdfData.rigLBtF.x, bsdfData.diffuseColor, bsdfData.absorptionRange) * bakeDiffuseLightingMat[0];
bakeDiffuseLighting += GetTransmissionWithAbsorption(bsdfData.rigLBtF.y, bsdfData.diffuseColor, bsdfData.absorptionRange) * bakeDiffuseLightingMat[1];
bakeDiffuseLighting += GetTransmissionWithAbsorption(bsdfData.rigLBtF.z, bsdfData.diffuseColor, bsdfData.absorptionRange) * bakeDiffuseLightingMat[2];
bakeDiffuseLighting *= scale;
}
//世界空间到切线空间方向转换
float3 TransformToLocalFrame(float3 L, BSDFData bsdfData)
{
float3 zVec = -bsdfData.normalWS;
float3 xVec = bsdfData.tangentWS.xyz;
float3 yVec = -cross(zVec, xVec) * bsdfData.tangentWS.w;//原代码没有负值,实际测试需要负值
float3x3 tbn = float3x3(xVec, yVec, zVec);
return mul(tbn, L);
}
CBSDF EvaluateBSDF(float3 L, BSDFData bsdfData)
{
CBSDF cbsdf;
ZERO_INITIALIZE(CBSDF, cbsdf);
float3 dir = TransformToLocalFrame(L, bsdfData);
float3 weights = dir >= 0 ? bsdfData.rigRTBk.xyz : bsdfData.rigLBtF.xyz;
float3 sqrDir = dir*dir;
cbsdf.diffR = GetTransmissionWithAbsorption(dot(sqrDir, weights), bsdfData.diffuseColor, bsdfData.absorptionRange);
return cbsdf;
}
//这一步最好在面板上做完
half GetAbsorptionRange(float absorptionStrenth)
{
return INV_PI + saturate(absorptionStrenth) * (1 - INV_PI);
}
//---------NBShaderUtility-----------
//UseInVerTex
void GetSixWayBakeDiffuseLight(real3 normalWS,real3 tangentWS,real3 biTangentWS,
inout half3 bakeDiffuseLighting0,inout half3 bakeDiffuseLighting1,inout half3 bakeDiffuseLighting2,
inout half3 backBakeDiffuseLighting0,inout half3 backBakeDiffuseLighting1,inout half3 backBakeDiffuseLighting2)
{
bakeDiffuseLighting0 = SampleSHVertex(tangentWS);
bakeDiffuseLighting1 = SampleSHVertex(biTangentWS);
bakeDiffuseLighting2 = SampleSHVertex(-normalWS);
backBakeDiffuseLighting0 = SampleSHVertex(-tangentWS);
backBakeDiffuseLighting1 = SampleSHVertex(-biTangentWS);
backBakeDiffuseLighting2 = SampleSHVertex(normalWS);
}
LightingData CreateSixWayLightingData(InputData inputData, half3 emission)
{
LightingData lightingData;
lightingData.giColor = inputData.bakedGI;
lightingData.emissionColor = emission;
lightingData.vertexLightingColor = 0;
lightingData.mainLightColor = 0;
lightingData.additionalLightsColor = 0;
return lightingData;
}
void GetSixWayEmission(inout BSDFData bsdfData,Texture2D rampMap,half4 emissionColor,bool isRampMap)
{
float input = pow(bsdfData.emissionInput,_SixWayInfo.y);
half3 emission = emissionColor * emissionColor.a;
if (isRampMap)
{
half4 rampSample = rampMap.Sample(sampler_linear_clamp,half2(input,0.5));
emission = emission * rampSample * rampSample.a;
}
else
{
emission *= input;
}
bsdfData.emission = emission;
}
half3 LightingSixWay(Light light,InputData inputData, BSDFData bsdfData)
{
half3 cbsdf_R = EvaluateBSDF(light.direction,bsdfData).diffR;
half3 radiance = light.color * light.distanceAttenuation * light.shadowAttenuation;
return PI * cbsdf_R * radiance;
}
//光照流程--->原型为UniversalFragmentBlinnPhong
half4 UniversalFragmentSixWay(InputData inputData,BSDFData bsdfData)
{
// #if defined(DEBUG_DISPLAY)
// half4 debugColor;
//
// if (CanDebugOverrideOutputColor(inputData, surfaceData, debugColor))
// {
// return debugColor;
// }
// #endif
#ifdef _LIGHT_LAYERS
uint meshRenderingLayers = GetMeshRenderingLayer();
#endif
half4 shadowMask = CalculateShadowMask(inputData);
// AmbientOcclusionFactor aoFactor = CreateAmbientOcclusionFactor(inputData, surfaceData);
AmbientOcclusionFactor aoFactor;
aoFactor.directAmbientOcclusion = 1;
aoFactor.indirectAmbientOcclusion = 1;
Light mainLight = GetMainLight(inputData, shadowMask, aoFactor);
// MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, aoFactor);
// inputData.bakedGI *= surfaceData.albedo;
// LightingData lightingData = CreateLightingData(inputData, surfaceData);
LightingData lightingData = CreateSixWayLightingData(inputData,bsdfData.emission);
#ifdef _LIGHT_LAYERS
if (IsMatchingLightLayer(mainLight.layerMask, meshRenderingLayers))
#endif
{
lightingData.mainLightColor += LightingSixWay(mainLight, inputData, bsdfData);
}
#if defined(_ADDITIONAL_LIGHTS)
uint pixelLightCount = GetAdditionalLightsCount();
#if USE_FORWARD_PLUS
for (uint lightIndex = 0; lightIndex < min(URP_FP_DIRECTIONAL_LIGHTS_COUNT, MAX_VISIBLE_LIGHTS); lightIndex++)
{
FORWARD_PLUS_SUBTRACTIVE_LIGHT_CHECK
Light light = GetAdditionalLight(lightIndex, inputData, shadowMask, aoFactor);
#ifdef _LIGHT_LAYERS
if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
#endif
{
lightingData.additionalLightsColor += LightingSixWay(light, inputData, bsdfData);
}
}
#endif
LIGHT_LOOP_BEGIN(pixelLightCount)
Light light = GetAdditionalLight(lightIndex, inputData, shadowMask, aoFactor);
#ifdef _LIGHT_LAYERS
if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
#endif
{
lightingData.additionalLightsColor += LightingSixWay(light, inputData, bsdfData);
}
LIGHT_LOOP_END
#endif
#if defined(_ADDITIONAL_LIGHTS_VERTEX)
lightingData.vertexLightingColor += inputData.vertexLighting * surfaceData.albedo;
#endif
return CalculateFinalColor(lightingData, bsdfData.alpha);
}
half3 LightingHalfLambert(half3 lightColor, half3 lightDir, half3 normal)
{
half NdotL = saturate(dot(normal, lightDir));
NdotL = NdotL*0.5 + 0.5;
return lightColor * NdotL;
}
half3 CalculateHalfLambertBlinnPhong(Light light, InputData inputData, SurfaceData surfaceData)
{
half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
half3 lightDiffuseColor = LightingHalfLambert(attenuatedLightColor, light.direction, inputData.normalWS);
half3 lightSpecularColor = half3(0,0,0);
#if defined(_SPECGLOSSMAP) || defined(_SPECULAR_COLOR)
half smoothness = exp2(10 * surfaceData.smoothness + 1);
lightSpecularColor += LightingSpecular(attenuatedLightColor, light.direction, inputData.normalWS, inputData.viewDirectionWS, half4(surfaceData.specular, 1), smoothness);
#endif
#if _ALPHAPREMULTIPLY_ON
return lightDiffuseColor * surfaceData.albedo * surfaceData.alpha + lightSpecularColor;
#else
return lightDiffuseColor * surfaceData.albedo + lightSpecularColor;
#endif
}
half4 UniversalFragmentHalfLambert(InputData inputData, half3 diffuse, half4 specularGloss, half smoothness, half3 emission, half alpha, half3 normalTS)
{
SurfaceData surfaceData;
surfaceData.albedo = diffuse;
surfaceData.alpha = alpha;
surfaceData.emission = emission;
surfaceData.metallic = 0;
surfaceData.occlusion = 1;
surfaceData.smoothness = smoothness;
surfaceData.specular = specularGloss.rgb;
surfaceData.clearCoatMask = 0;
surfaceData.clearCoatSmoothness = 1;
surfaceData.normalTS = normalTS;
// #if defined(DEBUG_DISPLAY)
// half4 debugColor;
//
// if (CanDebugOverrideOutputColor(inputData, surfaceData, debugColor))
// {
// return debugColor;
// }
// #endif
#ifdef _LIGHT_LAYERS
uint meshRenderingLayers = GetMeshRenderingLayer();
#endif
half4 shadowMask = CalculateShadowMask(inputData);
AmbientOcclusionFactor aoFactor = CreateAmbientOcclusionFactor(inputData, surfaceData);
Light mainLight = GetMainLight(inputData, shadowMask, aoFactor);
MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, aoFactor);
inputData.bakedGI *= surfaceData.albedo;
LightingData lightingData = CreateLightingData(inputData, surfaceData);
#ifdef _LIGHT_LAYERS
if (IsMatchingLightLayer(mainLight.layerMask, meshRenderingLayers))
#endif
{
lightingData.mainLightColor += CalculateHalfLambertBlinnPhong(mainLight, inputData, surfaceData);
}
#if defined(_ADDITIONAL_LIGHTS)
uint pixelLightCount = GetAdditionalLightsCount();
#if USE_FORWARD_PLUS
for (uint lightIndex = 0; lightIndex < min(URP_FP_DIRECTIONAL_LIGHTS_COUNT, MAX_VISIBLE_LIGHTS); lightIndex++)
{
FORWARD_PLUS_SUBTRACTIVE_LIGHT_CHECK
Light light = GetAdditionalLight(lightIndex, inputData, shadowMask, aoFactor);
#ifdef _LIGHT_LAYERS
if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
#endif
{
lightingData.additionalLightsColor += CalculateBlinnPhong(light, inputData, surfaceData);
}
}
#endif
LIGHT_LOOP_BEGIN(pixelLightCount)
Light light = GetAdditionalLight(lightIndex, inputData, shadowMask, aoFactor);
#ifdef _LIGHT_LAYERS
if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
#endif
{
lightingData.additionalLightsColor += CalculateHalfLambertBlinnPhong(light, inputData, surfaceData);
}
LIGHT_LOOP_END
#endif
#if defined(_ADDITIONAL_LIGHTS_VERTEX)
lightingData.vertexLightingColor += inputData.vertexLighting * surfaceData.albedo;
#endif
return CalculateFinalColor(lightingData, surfaceData.alpha);
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 70e94bdedafe40bfb7f8e77cdc4bef0f
timeCreated: 1751704445

View File

@@ -0,0 +1,585 @@
#ifndef TYFLOW_VAT_INCLUDED
#define TYFLOW_VAT_INCLUDED
#define TYFLOW_VAT_SKIN_MAX_BONES 7
#if defined(_VAT) && defined(_VAT_TYFLOW) && \
!defined(_TYFLOW_VAT_ABSOLUTE) && \
!defined(_TYFLOW_VAT_RELATIVE) && \
!defined(_TYFLOW_VAT_SKIN_R) && \
!defined(_TYFLOW_VAT_SKIN_PR) && \
!defined(_TYFLOW_VAT_SKIN_PRSAVE) && \
!defined(_TYFLOW_VAT_SKIN_PRSXYZ)
#define _TYFLOW_VAT_ABSOLUTE
#endif
#if defined(_TYFLOW_VAT_SKIN_R) || \
defined(_TYFLOW_VAT_SKIN_PR) || \
defined(_TYFLOW_VAT_SKIN_PRSAVE) || \
defined(_TYFLOW_VAT_SKIN_PRSXYZ)
#define TYFLOW_VAT_SKIN_MODE
#endif
struct TyflowVatMatrix3
{
float3 row0;
float3 row1;
float3 row2;
float3 row3;
};
struct TyflowVatTMParts
{
float3 pos;
float4 rot;
float3 scale;
};
inline int TyflowVatUnpackIntRGBA(int4 bytes)
{
return ((bytes.x << 24) + (bytes.y << 16) + (bytes.z << 8) + (bytes.w << 0));
}
inline float TyflowVatUnpackFloatRGBA(int4 bytes)
{
int sign = (bytes.r & 128) > 0 ? -1 : 1;
int expR = (bytes.r & 127) << 1;
int expG = bytes.g >> 7;
int exponent = expR + expG;
int signifG = (bytes.g & 127) << 16;
int signifB = bytes.b << 8;
float significand = (signifG + signifB + bytes.a) / pow(2, 23);
significand += 1;
return sign * significand * pow(2, exponent - 127);
}
inline half TyflowVatUnpackHalfRGBA(int2 bytes)
{
uint value = (bytes.x << 8) | bytes.y;
uint sign = (value & 0x8000) > 0;
int exponent = (value & 0x7C00) >> 10;
uint mantissa = (value & 0x03FF);
if ((value & 0x7FFF) == 0)
{
return sign ? -0 : 0;
}
if (exponent == 0x001F)
{
if (mantissa == 0)
{
return sign ? -1e+28 : 1e+28;
}
return 1e+28;
}
if (exponent > 0)
{
float result = pow(2.0, exponent - 15) * (1 + mantissa * (1 / 1024.0f));
return sign ? -result : result;
}
float subnormal = pow(2.0, -24) * mantissa;
return sign ? -subnormal : subnormal;
}
float4 TyflowVatSampleTexel(uint x, uint y)
{
float2 uv = float2(
((float)x + 0.5f) * _VATTex_TexelSize.x,
1.0f - (((float)y + 0.5f) * _VATTex_TexelSize.y));
return SAMPLE_TEXTURE2D_LOD(_VATTex, sampler_point_clamp, uv, 0);
}
float4 TyflowVatSampleEncodedTexel(uint x, uint y)
{
float4 sample = TyflowVatSampleTexel(x, y);
if (_LinearToGamma > 0.5f)
{
sample.r = LinearToGammaSpaceExact(sample.r);
sample.g = LinearToGammaSpaceExact(sample.g);
sample.b = LinearToGammaSpaceExact(sample.b);
}
return sample;
}
int TyflowVatTex2DInt(int arrInx)
{
uint width = (uint)_VATTex_TexelSize.z;
uint x = (uint)arrInx % width;
uint y = (uint)arrInx / width;
float4 bytes = TyflowVatSampleEncodedTexel(x, y);
int4 byteInts = int4(round(bytes.x * 255), round(bytes.y * 255), round(bytes.z * 255), round(bytes.w * 255));
return TyflowVatUnpackIntRGBA(byteInts.wzyx);
}
float TyflowVatTex2DFloat(int arrInx)
{
uint width = (uint)_VATTex_TexelSize.z;
uint x = (uint)arrInx % width;
uint y = (uint)arrInx / width;
float4 bytes = TyflowVatSampleEncodedTexel(x, y);
int4 byteInts = int4(round(bytes.x * 255), round(bytes.y * 255), round(bytes.z * 255), round(bytes.w * 255));
return TyflowVatUnpackFloatRGBA(byteInts.wzyx);
}
half TyflowVatTex2DHalf(float arrInxF)
{
uint arrInx = (uint)floor(arrInxF + 0.1f);
uint width = (uint)_VATTex_TexelSize.z;
uint x = arrInx % width;
uint y = arrInx / width;
float4 bytes = TyflowVatSampleEncodedTexel(x, y);
int4 byteInts = int4(round(bytes.x * 255), round(bytes.y * 255), round(bytes.z * 255), round(bytes.w * 255));
if (abs(arrInxF - round(arrInxF)) > 0.25f)
{
return TyflowVatUnpackHalfRGBA(byteInts.wz);
}
return TyflowVatUnpackHalfRGBA(byteInts.yx);
}
half2 TyflowVatTex2DHalfs2(float arrInxF)
{
uint arrInx = (uint)floor(arrInxF + 0.1f);
uint width = (uint)_VATTex_TexelSize.z;
uint x = arrInx % width;
uint y = arrInx / width;
float4 bytes = TyflowVatSampleEncodedTexel(x, y);
int4 byteInts = int4(round(bytes.x * 255), round(bytes.y * 255), round(bytes.z * 255), round(bytes.w * 255));
return half2(TyflowVatUnpackHalfRGBA(byteInts.yx), TyflowVatUnpackHalfRGBA(byteInts.wz));
}
float3 TyflowVatMultiplyPosition(TyflowVatMatrix3 matrixValue, float3 pos)
{
return float3(
pos.x * matrixValue.row0[0] + pos.y * matrixValue.row1[0] + pos.z * matrixValue.row2[0] + matrixValue.row3[0],
pos.x * matrixValue.row0[1] + pos.y * matrixValue.row1[1] + pos.z * matrixValue.row2[1] + matrixValue.row3[1],
pos.x * matrixValue.row0[2] + pos.y * matrixValue.row1[2] + pos.z * matrixValue.row2[2] + matrixValue.row3[2]);
}
TyflowVatMatrix3 TyflowVatQuaternionToTM(float4 quat)
{
TyflowVatMatrix3 matrixValue;
float x = quat.x;
float y = quat.y;
float z = quat.z;
float w = quat.w;
matrixValue.row0[0] = 1 - 2 * (y * y + z * z);
matrixValue.row0[1] = 2 * (x * y + z * w);
matrixValue.row0[2] = 2 * (x * z - y * w);
matrixValue.row1[0] = 2 * (x * y - z * w);
matrixValue.row1[1] = 1 - 2 * (x * x + z * z);
matrixValue.row1[2] = 2 * (y * z + x * w);
matrixValue.row2[0] = 2 * (x * z + y * w);
matrixValue.row2[1] = 2 * (y * z - x * w);
matrixValue.row2[2] = 1 - 2 * (x * x + y * y);
matrixValue.row3 = float3(0, 0, 0);
return matrixValue;
}
TyflowVatMatrix3 TyflowVatScaleTM(TyflowVatMatrix3 matrixValue, float3 scale)
{
matrixValue.row0 *= scale.x;
matrixValue.row1 *= scale.y;
matrixValue.row2 *= scale.z;
return matrixValue;
}
TyflowVatMatrix3 TyflowVatTranslateTM(TyflowVatMatrix3 matrixValue, float3 translation)
{
matrixValue.row3 = translation;
return matrixValue;
}
float4 TyflowVatQlerp(float4 a, float4 b, float blend)
{
float s1 = 1.0f - blend;
float s2 = dot(a, b) < 0.0f ? -blend : blend;
return normalize(float4(
s1 * a.x + s2 * b.x,
s1 * a.y + s2 * b.y,
s1 * a.z + s2 * b.z,
s1 * a.w + s2 * b.w));
}
int TyflowVatGetMetaDataSize()
{
#if defined(_TYFLOW_VAT_SKIN_PRSXYZ)
return 12;
#else
if (_DeformingSkin > 0.5f)
{
return 12;
}
return 3;
#endif
}
float3 TyflowVatGetTMPos(float startIndex)
{
float3 result;
for (int i = 0; i < 3; i++)
{
result[i] = TyflowVatTex2DFloat(startIndex + i);
}
return result;
}
float4 TyflowVatGetTMRot(float startIndex)
{
half2 rotHalfs1 = TyflowVatTex2DHalfs2(startIndex);
half2 rotHalfs2 = TyflowVatTex2DHalfs2(startIndex + 1);
return float4(-rotHalfs1.x, -rotHalfs1.y, -rotHalfs2.x, rotHalfs2.y);
}
float3 TyflowVatGetTMScaleXYZ(float startIndex)
{
half2 scaleHalfs1 = TyflowVatTex2DHalfs2(startIndex);
half2 scaleHalfs2 = TyflowVatTex2DHalfs2(startIndex + 1);
return float3(scaleHalfs1.x, scaleHalfs1.y, scaleHalfs2.x);
}
float3 TyflowVatGetTMScaleAve(float startIndex)
{
half scaleHalf = TyflowVatTex2DHalf(startIndex);
return float3(scaleHalf, scaleHalf, scaleHalf);
}
TyflowVatMatrix3 TyflowVatTMFromParts(TyflowVatTMParts parts)
{
TyflowVatMatrix3 matrixValue = TyflowVatQuaternionToTM(parts.rot);
matrixValue = TyflowVatScaleTM(matrixValue, parts.scale);
matrixValue = TyflowVatTranslateTM(matrixValue, parts.pos);
return matrixValue;
}
TyflowVatMatrix3 TyflowVatGetVertexInvTM(int tmInx)
{
TyflowVatMatrix3 matrixValue;
int pixelsPerTM = TyflowVatGetMetaDataSize();
float tmRowInx = 2 + (pixelsPerTM * tmInx);
matrixValue.row0 = TyflowVatGetTMPos(tmRowInx);
tmRowInx += 3;
matrixValue.row1 = TyflowVatGetTMPos(tmRowInx);
tmRowInx += 3;
matrixValue.row2 = TyflowVatGetTMPos(tmRowInx);
tmRowInx += 3;
matrixValue.row3 = TyflowVatGetTMPos(tmRowInx);
return matrixValue;
}
TyflowVatTMParts TyflowVatGetVertexTMPartsAtFrame(int tmInx, int frame, int numTMs)
{
float4 rot = float4(0, 0, 0, 1);
float3 pos = float3(0, 0, 0);
float3 scale = float3(1, 1, 1);
float pixelsPerTM = 0;
#if defined(_TYFLOW_VAT_SKIN_R)
pixelsPerTM = 2;
#elif defined(_TYFLOW_VAT_SKIN_PR)
pixelsPerTM = 5;
#elif defined(_TYFLOW_VAT_SKIN_PRSXYZ)
pixelsPerTM = 7;
#elif defined(_TYFLOW_VAT_SKIN_PRSAVE)
pixelsPerTM = 6;
#endif
float frameTMInx = (2 + numTMs * TyflowVatGetMetaDataSize()) + (frame * numTMs * pixelsPerTM) + (pixelsPerTM * tmInx);
#if defined(_TYFLOW_VAT_SKIN_R)
pos = TyflowVatGetTMPos(2 + tmInx * TyflowVatGetMetaDataSize());
rot = TyflowVatGetTMRot(frameTMInx);
#elif defined(_TYFLOW_VAT_SKIN_PR)
pos = TyflowVatGetTMPos(frameTMInx);
frameTMInx += 3;
rot = TyflowVatGetTMRot(frameTMInx);
#elif defined(_TYFLOW_VAT_SKIN_PRSXYZ)
pos = TyflowVatGetTMPos(frameTMInx);
frameTMInx += 3;
rot = TyflowVatGetTMRot(frameTMInx);
frameTMInx += 2;
scale = TyflowVatGetTMScaleXYZ(frameTMInx);
#elif defined(_TYFLOW_VAT_SKIN_PRSAVE)
pos = TyflowVatGetTMPos(frameTMInx);
frameTMInx += 3;
rot = TyflowVatGetTMRot(frameTMInx);
frameTMInx += 2;
scale = TyflowVatGetTMScaleAve(frameTMInx);
#endif
TyflowVatTMParts parts;
parts.pos = pos;
parts.rot = rot;
parts.scale = scale;
return parts;
}
float4 TyflowVatGetVertexValueAtFrame(uint vertexIndex, int vertexCount, int frame, int frameOffset)
{
float4 result = float4(0, 0, 0, 0);
vertexIndex += (vertexCount * frame) + (vertexCount * frameOffset);
if (_RGBAEncoded > 0.5f)
{
if (_RGBAHalf > 0.5f)
{
vertexIndex *= 3;
for (int i = 0; i < 3; i++)
{
float arrInxF = (vertexIndex + i) * 0.5f;
result[i] = TyflowVatTex2DHalf(arrInxF);
}
}
else
{
vertexIndex *= 3;
for (int i = 0; i < 3; i++)
{
result[i] = TyflowVatTex2DFloat(vertexIndex + i);
}
}
}
else
{
uint width = (uint)_VATTex_TexelSize.z;
uint x = vertexIndex % width;
uint y = vertexIndex / width;
result = TyflowVatSampleTexel(x, y);
}
return result;
}
float3 TyflowVatGetLocalVertexPosFromTM(float3 pos, TyflowVatMatrix3 invTM)
{
float3 localPos = ((pos / _ImportScale) * float3(-1, 1, 1));
return TyflowVatMultiplyPosition(invTM, localPos);
}
float3 TyflowVatGetLocalVertexPosFromPos(float3 pos, float boneInx)
{
int tmInx = 2 + (int)round(boneInx) * TyflowVatGetMetaDataSize();
float3 tmPos = float3(0, 0, 0);
for (int i = 0; i < 3; i++)
{
tmPos[i] = TyflowVatTex2DFloat(tmInx + i);
}
return ((pos / _ImportScale) * float3(-1, 1, 1)) - tmPos;
}
float2 TyflowVatGetSkinTexcoord(AttributesParticle input, int index)
{
if (index == 0) return input.Custom1.xy;
if (index == 1) return input.Custom2.xy;
#if !defined(_FLIPBOOKBLENDING_ON)
if (index == 2) return input.vatTexcoord4;
#endif
if (index == 3) return input.vatTexcoord5;
if (index == 4) return input.vatTexcoord6;
if (index == 5) return input.vatTexcoord7;
if (index == 6) return input.vatTexcoord8;
return float2(0, 0);
}
void ApplyTyflowVAT(AttributesParticle input, inout float4 positionOS, inout float3 normalOS)
{
#if defined(SHADOWS_DEPTH)
if (_AffectsShadows < 0.5f)
{
return;
}
#endif
float frameBase = _Frame;
float frameCustomData = GetCustomData(NB_CUSTOM_DATA_FLAG_2, FLAGBIT_POS_2_CUSTOMDATA_VAT_FRAME, -1.0f, input.Custom1, input.Custom2);
if (frameCustomData >= 0.0f)
{
frameBase = saturate(frameCustomData) * max(_Frames - 1.0f, 0.0f);
}
float frame = abs(frameBase + ((_Autoplay > 0.5f) ? (time * 30.0f * _AutoplaySpeed) : 0.0f));
frame = (_Loop > 0.5f) ? fmod(frame, _Frames) : min(frame, _Frames - 1.0f);
if ((_Loop > 0.5f) && (_InterpolateLoop < 0.5f) && (frame >= _Frames - 1.0f))
{
frame = _Frames - 1.0f;
}
uint frame0 = (uint)floor(frame);
uint frame1 = (uint)ceil(frame) % (uint)_Frames;
float frameInterp = frame - frame0;
#if defined(TYFLOW_VAT_SKIN_MODE)
if (CheckLocalFlags1(FLAG_BIT_PARTICLE_1_IS_PARTICLE_SYSTEM))
{
return;
}
else
{
int numTMs = TyflowVatTex2DInt(1);
float3 combinedPos = float3(0, 0, 0);
float3 combinedNormal = float3(0, 0, 0);
int loopCount = (_DeformingSkin > 0.5f) ? (int)_SkinBoneCount : 1;
[unroll]
for (int i = 0; i < TYFLOW_VAT_SKIN_MAX_BONES; i++)
{
if (i >= loopCount)
{
break;
}
float weight = 1.0f;
float tmInx = round(input.Custom1.y);
if (_DeformingSkin > 0.5f)
{
float2 texcoord = TyflowVatGetSkinTexcoord(input, i);
tmInx = round(texcoord.x);
weight = texcoord.y;
}
TyflowVatTMParts tmParts0 = TyflowVatGetVertexTMPartsAtFrame((int)tmInx, frame0, numTMs);
if (_FrameInterpolation > 0.5f)
{
TyflowVatTMParts tmParts1 = TyflowVatGetVertexTMPartsAtFrame((int)tmInx, frame1, numTMs);
tmParts0.pos = lerp(tmParts0.pos, tmParts1.pos, frameInterp);
tmParts0.rot = TyflowVatQlerp(tmParts0.rot, tmParts1.rot, frameInterp);
tmParts0.scale = lerp(tmParts0.scale, tmParts1.scale, frameInterp);
}
TyflowVatMatrix3 tm = TyflowVatTMFromParts(tmParts0);
TyflowVatMatrix3 invStartTM;
#if defined(_TYFLOW_VAT_SKIN_PRSXYZ)
bool useInvStartTM = true;
#else
bool useInvStartTM = _DeformingSkin > 0.5f;
#endif
if (useInvStartTM)
{
invStartTM = TyflowVatGetVertexInvTM((int)tmInx);
}
else
{
invStartTM.row0 = float3(0, 0, 0);
invStartTM.row1 = float3(0, 0, 0);
invStartTM.row2 = float3(0, 0, 0);
invStartTM.row3 = float3(0, 0, 0);
}
float3 localPos;
if (useInvStartTM)
{
localPos = TyflowVatGetLocalVertexPosFromTM(positionOS.xyz, invStartTM);
}
else
{
localPos = TyflowVatGetLocalVertexPosFromPos(positionOS.xyz, tmInx);
}
float3 pos = TyflowVatMultiplyPosition(tm, localPos) * float3(-1, 1, 1);
combinedPos += (pos * _ImportScale) * weight;
#if !defined(SHADOWS_DEPTH)
{
float3 animatedNormal = normalOS;
tm.row3 = float3(0, 0, 0);
if (useInvStartTM)
{
invStartTM.row3 = float3(0, 0, 0);
float3 localNormal = TyflowVatGetLocalVertexPosFromTM(animatedNormal, invStartTM);
animatedNormal = normalize(TyflowVatMultiplyPosition(tm, localNormal)) * float3(-1, 1, 1);
}
else
{
tm = TyflowVatTranslateTM(tm, float3(0, 0, 0));
animatedNormal = normalize(TyflowVatMultiplyPosition(tm, animatedNormal * float3(-1, 1, 1))) * float3(-1, 1, 1);
}
combinedNormal += animatedNormal * weight;
}
#endif
}
positionOS.xyz = combinedPos;
normalOS = combinedNormal;
}
#elif defined(_TYFLOW_VAT_ABSOLUTE) || defined(_TYFLOW_VAT_RELATIVE)
{
float2 tyflowVatIndexData = CheckLocalFlags1(FLAG_BIT_PARTICLE_1_IS_PARTICLE_SYSTEM)
? input.texcoords.zw
: input.Custom1.xy;
uint vertexIndex = (uint)round(tyflowVatIndexData.x);
uint vertexCount = (uint)round(tyflowVatIndexData.y);
float4 vertexOffset0 = TyflowVatGetVertexValueAtFrame(vertexIndex, vertexCount, frame0, 0);
float4 vertexOffset = vertexOffset0;
if (_FrameInterpolation > 0.5f)
{
float4 vertexOffset1 = TyflowVatGetVertexValueAtFrame(vertexIndex, vertexCount, frame1, 0);
vertexOffset = lerp(vertexOffset0, vertexOffset1, frameInterp);
}
#if defined(_TYFLOW_VAT_RELATIVE)
positionOS.xyz += (vertexOffset * float4(-1, 1, 1, 1) * _ImportScale).xyz;
#else
positionOS.xyz = (vertexOffset * float4(-1, 1, 1, 1) * _ImportScale).xyz;
#endif
#if !defined(SHADOWS_DEPTH)
if (_VATIncludesNormals > 0.5f)
{
float4 normal0 = TyflowVatGetVertexValueAtFrame(vertexIndex, vertexCount, frame0, (int)_Frames);
float4 animatedNormal = normal0;
if (_FrameInterpolation > 0.5f)
{
float4 normal1 = TyflowVatGetVertexValueAtFrame(vertexIndex, vertexCount, frame1, (int)_Frames);
animatedNormal = lerp(normal0, normal1, frameInterp) * float4(-1, 1, 1, 1);
}
normalOS = normalize(animatedNormal.xyz);
}
#endif
}
#endif
}
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: e5bde8a4dd9946d4ab49c8b1f3bc6d12
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,23 @@
#ifndef PARTICLE_VAT_INCLUDED
#define PARTICLE_VAT_INCLUDED
#include "HoudiniVAT.hlsl"
#include "TyflowVAT.hlsl"
void ApplyVAT(AttributesParticle input, inout float4 positionOS, inout float3 normalOS)
{
#if defined(_VAT)
if (_VAT_Toggle < 0.5f)
{
return;
}
#if defined(_VAT_HOUDINI)
ApplyHoudiniVAT(input, positionOS, normalOS);
#elif defined(_VAT_TYFLOW)
ApplyTyflowVAT(input, positionOS, normalOS);
#endif
#endif
}
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a0126cd1b294e894f9ac10e46b7776d6
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,391 @@
#ifndef XUANXUAN_UTILITY
#define XUANXUAN_UTILITY
//引入自UnityCG.cginc
#define UNITY_PI 3.14159265359f
#define UNITY_TWO_PI 6.28318530718f
#define UNITY_FOUR_PI 12.56637061436f
#define UNITY_INV_PI 0.31830988618f
#define UNITY_INV_TWO_PI 0.15915494309f
#define UNITY_INV_FOUR_PI 0.07957747155f
#define UNITY_HALF_PI 1.57079632679f
#define UNITY_INV_HALF_PI 0.636619772367f
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
inline half NB_Remap(half x, half inMin, half inMax, half outMin, half outMax)
{
// x= clamp(x,inMin,inMax);
return saturate((x - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin;
}
inline half NB_Remap01(half x, half inMin, half inMax)
{
// x= clamp(x,inMin,inMax);
return saturate((x - inMin) / (inMax - inMin));
}
inline half NB_RemapNoClamp(half x, half inMin, half inMax, half outMin, half outMax)
{
// x= clamp(x,inMin,inMax);
return ((x - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin;
}
//原生SmoothStep开销很大https://zhuanlan.zhihu.com/p/34629262
//在很多时候可以用简单线性映射代替。
half SimpleSmoothstep(half min,half max,half interp)
{
return saturate((interp - min)/(max-min));
}
half4 TryLinearize(half4 color)
{
#if defined(UNITY_COLORSPACE_GAMMA)
return pow(color, 2.2f);
#else
return color;
#endif
}
half4 TryLinearizeWithoutAlpha(half4 color)
{
#if defined(UNITY_COLORSPACE_GAMMA)
return half4(pow(color.rgb, 2.2f), color.a);
#else
return color;
#endif
}
half3 TryLinearize(half3 color)
{
#if defined(UNITY_COLORSPACE_GAMMA)
return pow(color, 2.2f);
#else
return color;
#endif
}
half2 TryLinearize(half2 color)
{
#if defined(UNITY_COLORSPACE_GAMMA)
return pow(color, 2.2f);
#else
return color;
#endif
}
half TryLinearize(half color)
{
#if defined(UNITY_COLORSPACE_GAMMA)
return pow(color, 2.2f);
#else
return color;
#endif
}
half4 tex2D_TryLinearizeWithoutAlpha(sampler2D tex, float2 uv)
{
#if defined(UNITY_COLORSPACE_GAMMA)
return TryLinearizeWithoutAlpha(tex2D(tex, uv));
#else
return tex2D(tex, uv);
#endif
}
inline float LinearToGammaSpaceExact (float value)
{
if (value <= 0.0F)
return 0.0F;
else if (value <= 0.0031308F)
return 12.92F * value;
else if (value < 1.0F)
return 1.055F * pow(value, 0.4166667F) - 0.055F;
else
return pow(value, 0.45454545F);
}
// real FastSRGBToLinear(real c)
// {
// return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);
// }
// float2 Rotate_Radians_float(float2 UV, float2 Center, float Rotation)
// {
// if(Rotation == 0)
// {
// return UV;
// }
//
// // Rotation = Rotation / 180 * 3.14; //从角度转为弧度。
// Rotation *= 0.01745329222; //从角度转为弧度。
// UV -= Center;
// float s = sin(Rotation);
// float c = cos(Rotation);
// float2x2 rMatrix = float2x2(c, -s, s, c);
// rMatrix *= 0.5;
// rMatrix += 0.5;
// rMatrix = rMatrix * 2 - 1;
// UV.xy = mul(UV.xy, rMatrix);
// UV += Center;
// return UV;
// }
float2 Rotate_Radians_float(float2 UV, float2 Center, float Rotation)
{
if(Rotation == 0)
{
return UV;
}
// Rotation = Rotation / 180 * 3.14; //从角度转为弧度。
Rotation *= 0.01745329222; //从角度转为弧度。
float s, c; // 如果 angle 是 uniform每帧或每物件算一次更省
sincos(Rotation, s, c); // HLSL 内置,一次算出 sin/cos
float2 d = UV - Center;
// 旋转 (x', y') = ( c*x - s*y, s*x + c*y )
float2 r = float2(dot(d, float2( c, -s)),
dot(d, float2( s, c)));
return r + Center;
}
inline half luminance(half3 color)
{
return dot(color, float3(0.2126f, 0.7152f, 0.0722f));
}
inline half DepthFactor(float Z, float near, float far)
{
Z = saturate((Z-near)/(far - near));
return Z;
}
//抽自Unity.cginc
inline half3 LinearToGammaSpace (half3 linRGB)
{
linRGB = max(linRGB, half3(0.h, 0.h, 0.h));
// An almost-perfect approximation from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
return max(1.055h * pow(linRGB, 0.416666667h) - 0.055h, 0.h);
// Exact version, useful for debugging.
//return half3(LinearToGammaSpaceExact(linRGB.r), LinearToGammaSpaceExact(linRGB.g), LinearToGammaSpaceExact(linRGB.b));
}
//ASE
float2 voronoihash1( float2 p )
{
p = float2( dot( p, float2( 127.1, 311.7 ) ), dot( p, float2( 269.5, 183.3 ) ) );
return frac( sin( p ) *43758.5453);
}
//ASE
float voronoi1( float2 v, float time, inout float2 id, inout float2 mr, float smoothness )
{
float2 n = floor( v );
float2 f = frac( v );
float F1 = 8.0;
float F2 = 8.0; float2 mg = 0;
for ( int j = -1; j <= 1; j++ )
{
for ( int i = -1; i <= 1; i++ )
{
float2 g = float2( i, j );
float2 o = voronoihash1( n + g );
o = ( sin( time + o * 6.2831 ) * 0.5 + 0.5 ); float2 r = f - g - o;
float d = 0.5 * dot( r, r );
if( d<F1 ) {
F2 = F1;
F1 = d; mg = g; mr = r; id = o;
} else if( d<F2 ) {
F2 = d;
}
}
}
return F2 - F1;
}
void voroniForgraphfunc_half(half2 uv,half angle,half scale,out float outVoroni1 )
{
uv = uv*scale;
float2 id1 = 0;
float2 uv1 = 0;
outVoroni1 = voronoi1( uv, angle, id1, uv1, 0 );
}
inline float2 unity_voronoi_noise_randomVector (float2 UV, float offset)
{
float2x2 m = float2x2(15.27, 47.63, 99.41, 89.98);
UV = frac(sin(mul(UV, m)) * 46839.32);
return float2(sin(UV.y*+offset)*0.5+0.5, cos(UV.x*offset)*0.5+0.5);
}
void Unity_Voronoi_float(float2 UV, float AngleOffset, float CellDensity, out float Out, out float Cells)
{
float2 g = floor(UV * CellDensity);
float2 f = frac(UV * CellDensity);
float t = 8.0;
float3 res = float3(8.0, 0.0, 0.0);
for(int y=-1; y<=1; y++)
{
for(int x=-1; x<=1; x++)
{
float2 lattice = float2(x,y);
float2 offset = unity_voronoi_noise_randomVector(lattice + g, AngleOffset);
float d = distance(lattice + offset, f);
if(d < res.x)
{
res = float3(d, offset.x, offset.y);
Out = res.x;
Cells = res.y;
}
}
}
}
void Unity_Blend_Overlay_float4(float4 Base, float4 Blend, float Opacity, out float4 Out)
{
float4 result1 = 1.0 - 2.0 * (1.0 - Base) * (1.0 - Blend);
float4 result2 = 2.0 * Base * Blend;
float4 zeroOrOne = step(Base, 0.5);
Out = result2 * zeroOrOne + (1 - zeroOrOne) * result1;
Out = lerp(Base, Out, Opacity);
}
void Unity_Blend_HardLight_half(half Base, half Blend, half Opacity, out half Out)
{
half result1 = 1.0 - 2.0 * (1.0 - Base) * (1.0 - Blend);
half result2 = 2.0 * Base * Blend;
half zeroOrOne = step(Blend, 0.5);
Out = result2 * zeroOrOne + (1 - zeroOrOne) * result1;
Out = lerp(Base, Out, Opacity);
}
void Blend_HardLight_half(half Base, half Blend, out half Out)
{
half result1 = 1.0 - 2.0 * (1.0 - Base) * (1.0 - Blend);
half result2 = 2.0 * Base * Blend;
half zeroOrOne = step(Blend, 0.5);
Out = result2 * zeroOrOne + (1 - zeroOrOne) * result1;
}
float2 randomGradient(float2 p) {
p = p + 0.02;
float x = dot(p, float2(123.4, 234.5));
float y = dot(p, float2(234.5, 345.6));
float2 gradient = float2(x, y);
gradient = sin(gradient);
gradient = gradient * 43758.5453;
// part 4.5 - update noise function with time
// gradient = sin(gradient + u_time);
return gradient;
// gradient = sin(gradient);
// return gradient;
}
#include "./jp.keijiro.noiseshader/Shader/SimplexNoise3D.hlsl"
half SimplexNoise(float2 uv,float time)
{
return SimplexNoise(float3(uv,time))*0.5+0.5;
}
#include "./jp.keijiro.noiseshader/Shader/ClassicNoise3D.hlsl"
half PerlinNoise(float2 uv,float time)
{
return ClassicNoise(float3(uv,time))*0.5+0.5;
}
float2 PolarCoordinates(float2 UV, float2 _PCCenter)
{
// float2 uvsource = float2(0, 0);
float2 delta = UV - _PCCenter.xy;//校准UV到中心
float radius = length(delta) * 2;
float angle = atan2(delta.x, delta.y) * UNITY_INV_TWO_PI ;
return float2(angle, radius);//翻转可以调整横向和纵向。
}
float2 PolarCoordinatesStrengthAndST(float2 UVBeforPollarCoord,float2 UVAfterPolarCoord,float polarStrenth ,float4 tex_ST)
{
UVAfterPolarCoord = UVAfterPolarCoord*tex_ST.xy + tex_ST.zw;//利用相应功能贴图的坐标对ST进行修改。
UVAfterPolarCoord = lerp(UVBeforPollarCoord, UVAfterPolarCoord, polarStrenth);
return UVAfterPolarCoord;
}
//极坐标//transformation为极坐标强度
float2 PolarCoordinates(float2 UV, float3 _PCCenter, float4 tex_ST)
{
// float2 uvsource = float2(0, 0);
//
// float2 delta = UV - _PCCenter.xy;//校准UV到中心
// float radius = length(delta) * 2;
// float angle = atan2(delta.x, delta.y) * UNITY_INV_TWO_PI ;
// uvsource = float2(angle, radius);//翻转可以调整横向和纵向。
// uvsource = uvsource*tex_ST.xy + tex_ST.zw;//利用相应功能贴图的坐标对ST进行修改。
// uvsource = lerp(UV, uvsource, _PCCenter.z);
// return uvsource;
//拆分成两步第一步求极坐标太耗atan一般源UV都是一样的只是ST不同。
float2 uvsource = PolarCoordinates(UV,_PCCenter.xy);
uvsource = PolarCoordinatesStrengthAndST(UV,uvsource,_PCCenter.z,tex_ST);
return uvsource;
}
float2 CylinderCoordinate(float3 positionOS)
{
float angle = atan2(positionOS.x,positionOS.z)* UNITY_INV_TWO_PI ;
return float2(angle,positionOS.y);
}
float2 UVOffsetAnimaiton(float2 UV,half2 OffsetSpeed,float time)
{
float2 newUV = float2(OffsetSpeed.x*time+UV.x,OffsetSpeed.y*time+UV.y);
return newUV;
}
half3 rgb2hsv(half3 c)
{
half4 K = half4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
half4 p = lerp(half4(c.bg, K.wz), half4(c.gb, K.xy), step(c.b, c.g));
half4 q = lerp(half4(p.xyw, c.r), half4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
half3 hsv2rgb(half3 c)
{
half4 K = half4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
half3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * lerp(K.xxx, saturate(p - K.xxx), c.y);
}
half3 ColorSaturate(half3 color, half colorSaturation)
{
half3 lum = luminance(color);
return max(0, lerp(lum, color, colorSaturation));
}
half2 Rotate(half2 v, half cos0, half sin0)
{
return half2(v.x * cos0 - v.y * sin0,
v.x * sin0 + v.y * cos0);
}
half SmoothStep01(half interval)//让01线性过渡变成SmoothStep过渡但是控制计算量。
{
interval = saturate(interval);
return interval * interval * ( 3 - 2 * interval );
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9f34986983f558c40a7e30a353dc3c34
timeCreated: 1684725413

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e95a8f1b5d23f4141b5121f98f90f10a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,56 @@
Noise Shader Library for Unity
==============================
**NoiseShader** is a Unity package that provides 2D/3D gradient noise
functions written in the shader language. These functions are ported from the
[webgl-noise] library that is originally written by Stefan Gustavson and Ahima
Arts.
[webgl-noise]: https://github.com/ashima/webgl-noise
At the moment, it contains the following functions:
- Classic Perlin noise (2D/3D)
- Periodic Perlin noise (2D/3D)
- Simplex noise (2D/3D)
- Analytical derivatives of simplex noise (2D/3D)
How To Install
--------------
This package uses the [scoped registry] feature to resolve package dependencies.
Please add the following sections to the manifest file (Packages/manifest.json).
[scoped registry]: https://docs.unity3d.com/Manual/upm-scoped.html
To the `scopedRegistries` section:
```
{
"name": "Keijiro",
"url": "https://registry.npmjs.com",
"scopes": [ "jp.keijiro" ]
}
```
To the `dependencies` section:
```
"jp.keijiro.noiseshader": "2.0.0"
```
After changes, the manifest file should look like below:
```
{
"scopedRegistries": [
{
"name": "Keijiro",
"url": "https://registry.npmjs.com",
"scopes": [ "jp.keijiro" ]
}
],
"dependencies": {
"jp.keijiro.noiseshader": "2.0.0",
...
```

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b92baefe22663afa5ad0ab303b3dcc20
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 77b21af505211947eb730e678820eb8b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,66 @@
//
// GLSL textureless classic 2D noise "cnoise",
// with an RSL-style periodic variant "pnoise".
// Author: Stefan Gustavson (stefan.gustavson@liu.se)
// Version: 2011-08-22
//
// Many thanks to Ian McEwan of Ashima Arts for the
// ideas for permutation and gradient selection.
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
#ifndef _INCLUDE_JP_KEIJIRO_NOISESHADER_CLASSIC_NOISE_2D_HLSL_
#define _INCLUDE_JP_KEIJIRO_NOISESHADER_CLASSIC_NOISE_2D_HLSL_
#include "Common.hlsl"
float ClassicNoise_impl(float2 pi0, float2 pf0, float2 pi1, float2 pf1)
{
pi0 = wglnoise_mod289(pi0); // To avoid truncation effects in permutation
pi1 = wglnoise_mod289(pi1);
float4 ix = float2(pi0.x, pi1.x).xyxy;
float4 iy = float2(pi0.y, pi1.y).xxyy;
float4 fx = float2(pf0.x, pf1.x).xyxy;
float4 fy = float2(pf0.y, pf1.y).xxyy;
float4 i = wglnoise_permute(wglnoise_permute(ix) + iy);
float4 phi = i / 41 * 3.14159265359 * 2;
float2 g00 = float2(cos(phi.x), sin(phi.x));
float2 g10 = float2(cos(phi.y), sin(phi.y));
float2 g01 = float2(cos(phi.z), sin(phi.z));
float2 g11 = float2(cos(phi.w), sin(phi.w));
float n00 = dot(g00, float2(fx.x, fy.x));
float n10 = dot(g10, float2(fx.y, fy.y));
float n01 = dot(g01, float2(fx.z, fy.z));
float n11 = dot(g11, float2(fx.w, fy.w));
float2 fade_xy = wglnoise_fade(pf0);
float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
return 1.44 * n_xy;
}
// Classic Perlin noise
float ClassicNoise(float2 p)
{
float2 i = floor(p);
float2 f = frac(p);
return ClassicNoise_impl(i, f, i + 1, f - 1);
}
// Classic Perlin noise, periodic variant
float PeriodicNoise(float2 p, float2 rep)
{
float2 i0 = wglnoise_mod(floor(p), rep);
float2 i1 = wglnoise_mod(i0 + 1, rep);
float2 f = frac(p);
return ClassicNoise_impl(i0, f, i1, f - 1);
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 8ec48974b9689234f9a4a7c1927e2a7b
timeCreated: 1498976326
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,93 @@
//
// GLSL textureless classic 3D noise "cnoise",
// with an RSL-style periodic variant "pnoise".
// Author: Stefan Gustavson (stefan.gustavson@liu.se)
// Version: 2011-10-11
//
// Many thanks to Ian McEwan of Ashima Arts for the
// ideas for permutation and gradient selection.
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
#ifndef _INCLUDE_JP_KEIJIRO_NOISESHADER_CLASSIC_NOISE_3D_HLSL_
#define _INCLUDE_JP_KEIJIRO_NOISESHADER_CLASSIC_NOISE_3D_HLSL_
#include "Common.hlsl"
float ClassicNoise_impl(float3 pi0, float3 pf0, float3 pi1, float3 pf1)
{
pi0 = wglnoise_mod289(pi0);
pi1 = wglnoise_mod289(pi1);
float4 ix = float4(pi0.x, pi1.x, pi0.x, pi1.x);
float4 iy = float4(pi0.y, pi0.y, pi1.y, pi1.y);
float4 iz0 = pi0.z;
float4 iz1 = pi1.z;
float4 ixy = wglnoise_permute(wglnoise_permute(ix) + iy);
float4 ixy0 = wglnoise_permute(ixy + iz0);
float4 ixy1 = wglnoise_permute(ixy + iz1);
float4 gx0 = lerp(-1, 1, frac(floor(ixy0 / 7) / 7));
float4 gy0 = lerp(-1, 1, frac(floor(ixy0 % 7) / 7));
float4 gz0 = 1 - abs(gx0) - abs(gy0);
bool4 zn0 = gz0 < -0.01;
gx0 += zn0 * (gx0 < -0.01 ? 1 : -1);
gy0 += zn0 * (gy0 < -0.01 ? 1 : -1);
float4 gx1 = lerp(-1, 1, frac(floor(ixy1 / 7) / 7));
float4 gy1 = lerp(-1, 1, frac(floor(ixy1 % 7) / 7));
float4 gz1 = 1 - abs(gx1) - abs(gy1);
bool4 zn1 = gz1 < -0.01;
gx1 += zn1 * (gx1 < -0.01 ? 1 : -1);
gy1 += zn1 * (gy1 < -0.01 ? 1 : -1);
float3 g000 = normalize(float3(gx0.x, gy0.x, gz0.x));
float3 g100 = normalize(float3(gx0.y, gy0.y, gz0.y));
float3 g010 = normalize(float3(gx0.z, gy0.z, gz0.z));
float3 g110 = normalize(float3(gx0.w, gy0.w, gz0.w));
float3 g001 = normalize(float3(gx1.x, gy1.x, gz1.x));
float3 g101 = normalize(float3(gx1.y, gy1.y, gz1.y));
float3 g011 = normalize(float3(gx1.z, gy1.z, gz1.z));
float3 g111 = normalize(float3(gx1.w, gy1.w, gz1.w));
float n000 = dot(g000, pf0);
float n100 = dot(g100, float3(pf1.x, pf0.y, pf0.z));
float n010 = dot(g010, float3(pf0.x, pf1.y, pf0.z));
float n110 = dot(g110, float3(pf1.x, pf1.y, pf0.z));
float n001 = dot(g001, float3(pf0.x, pf0.y, pf1.z));
float n101 = dot(g101, float3(pf1.x, pf0.y, pf1.z));
float n011 = dot(g011, float3(pf0.x, pf1.y, pf1.z));
float n111 = dot(g111, pf1);
float3 fade_xyz = wglnoise_fade(pf0);
float4 n_z = lerp(float4(n000, n100, n010, n110),
float4(n001, n101, n011, n111), fade_xyz.z);
float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y);
float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x);
return 1.46 * n_xyz;
}
// Classic Perlin noise
float ClassicNoise(float3 p)
{
float3 i = floor(p);
float3 f = frac(p);
return ClassicNoise_impl(i, f, i + 1, f - 1);
}
// Classic Perlin noise, periodic variant
float PeriodicNoise(float3 p, float3 rep)
{
float3 i0 = wglnoise_mod(floor(p), rep);
float3 i1 = wglnoise_mod(i0 + 1, rep);
float3 f = frac(p);
return ClassicNoise_impl(i0, f, i1, f - 1);
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2688615dc3cd4ed4cbca4cd0135d8e8a
timeCreated: 1498976326
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,64 @@
#ifndef _INCLUDE_JP_KEIJIRO_NOISESHADER_COMMON_HLSL_
#define _INCLUDE_JP_KEIJIRO_NOISESHADER_COMMON_HLSL_
float wglnoise_mod(float x, float y)
{
return x - y * floor(x / y);
}
float2 wglnoise_mod(float2 x, float2 y)
{
return x - y * floor(x / y);
}
float3 wglnoise_mod(float3 x, float3 y)
{
return x - y * floor(x / y);
}
float4 wglnoise_mod(float4 x, float4 y)
{
return x - y * floor(x / y);
}
float2 wglnoise_fade(float2 t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
}
float3 wglnoise_fade(float3 t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
}
float wglnoise_mod289(float x)
{
return x - floor(x / 289) * 289;
}
float2 wglnoise_mod289(float2 x)
{
return x - floor(x / 289) * 289;
}
float3 wglnoise_mod289(float3 x)
{
return x - floor(x / 289) * 289;
}
float4 wglnoise_mod289(float4 x)
{
return x - floor(x / 289) * 289;
}
float3 wglnoise_permute(float3 x)
{
return wglnoise_mod289((x * 34 + 1) * x);
}
float4 wglnoise_permute(float4 x)
{
return wglnoise_mod289((x * 34 + 1) * x);
}
#endif

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 443fb5ca2e0ec42bc8a96eb43c8470fe
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
preprocessorOverride: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,63 @@
//
// Description : Array and textureless GLSL 2D simplex noise function.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
#ifndef _INCLUDE_JP_KEIJIRO_NOISESHADER_SIMPLEX_NOISE_2D_HLSL_
#define _INCLUDE_JP_KEIJIRO_NOISESHADER_SIMPLEX_NOISE_2D_HLSL_
#include "Common.hlsl"
float3 SimplexNoiseGrad(float2 v)
{
const float C1 = (3 - sqrt(3)) / 6;
const float C2 = (sqrt(3) - 1) / 2;
// First corner
float2 i = floor(v + dot(v, C2));
float2 x0 = v - i + dot(i, C1);
// Other corners
float2 i1 = x0.x > x0.y ? float2(1, 0) : float2(0, 1);
float2 x1 = x0 + C1 - i1;
float2 x2 = x0 + C1 * 2 - 1;
// Permutations
i = wglnoise_mod289(i); // Avoid truncation effects in permutation
float3 p = wglnoise_permute( i.y + float3(0, i1.y, 1));
p = wglnoise_permute(p + i.x + float3(0, i1.x, 1));
// Gradients: 41 points uniformly over a unit circle.
// The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
float3 phi = p / 41 * 3.14159265359 * 2;
float2 g0 = float2(cos(phi.x), sin(phi.x));
float2 g1 = float2(cos(phi.y), sin(phi.y));
float2 g2 = float2(cos(phi.z), sin(phi.z));
// Compute noise and gradient at P
float3 m = float3(dot(x0, x0), dot(x1, x1), dot(x2, x2));
float3 px = float3(dot(g0, x0), dot(g1, x1), dot(g2, x2));
m = max(0.5 - m, 0);
float3 m3 = m * m * m;
float3 m4 = m * m3;
float3 temp = -8 * m3 * px;
float2 grad = m4.x * g0 + temp.x * x0 +
m4.y * g1 + temp.y * x1 +
m4.z * g2 + temp.z * x2;
return 99.2 * float3(grad, dot(m4, px));
}
float SimplexNoise(float2 v)
{
return SimplexNoiseGrad(v).z;
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 66b8605d5d2f14d408b0f4aee5f9d25a
timeCreated: 1498976326
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,76 @@
//
// Description : Array and textureless GLSL 2D/3D/4D simplex
// noise functions.
// Author : Ian McEwan, Ashima Arts.
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License : Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
#ifndef _INCLUDE_JP_KEIJIRO_NOISESHADER_SIMPLEX_NOISE_3D_HLSL_
#define _INCLUDE_JP_KEIJIRO_NOISESHADER_SIMPLEX_NOISE_3D_HLSL_
#include "Common.hlsl"
float4 SimplexNoiseGrad(float3 v)
{
// First corner
float3 i = floor(v + dot(v, 1.0 / 3));
float3 x0 = v - i + dot(i, 1.0 / 6);
// Other corners
float3 g = x0.yzx <= x0.xyz;
float3 l = 1 - g;
float3 i1 = min(g.xyz, l.zxy);
float3 i2 = max(g.xyz, l.zxy);
float3 x1 = x0 - i1 + 1.0 / 6;
float3 x2 = x0 - i2 + 1.0 / 3;
float3 x3 = x0 - 0.5;
// Permutations
i = wglnoise_mod289(i); // Avoid truncation effects in permutation
float4 p = wglnoise_permute( i.z + float4(0, i1.z, i2.z, 1));
p = wglnoise_permute(p + i.y + float4(0, i1.y, i2.y, 1));
p = wglnoise_permute(p + i.x + float4(0, i1.x, i2.x, 1));
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float4 gx = lerp(-1, 1, frac(floor(p / 7) / 7));
float4 gy = lerp(-1, 1, frac(floor(p % 7) / 7));
float4 gz = 1 - abs(gx) - abs(gy);
bool4 zn = gz < -0.01;
gx += zn * (gx < -0.01 ? 1 : -1);
gy += zn * (gy < -0.01 ? 1 : -1);
float3 g0 = normalize(float3(gx.x, gy.x, gz.x));
float3 g1 = normalize(float3(gx.y, gy.y, gz.y));
float3 g2 = normalize(float3(gx.z, gy.z, gz.z));
float3 g3 = normalize(float3(gx.w, gy.w, gz.w));
// Compute noise and gradient at P
float4 m = float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3));
float4 px = float4(dot(g0, x0), dot(g1, x1), dot(g2, x2), dot(g3, x3));
m = max(0.5 - m, 0);
float4 m3 = m * m * m;
float4 m4 = m * m3;
float4 temp = -8 * m3 * px;
float3 grad = m4.x * g0 + temp.x * x0 +
m4.y * g1 + temp.y * x1 +
m4.z * g2 + temp.z * x2 +
m4.w * g3 + temp.w * x3;
return 107 * float4(grad, dot(m4, px));
}
float SimplexNoise(float3 v)
{
return SimplexNoiseGrad(v).w;
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2b1bb9b67aabb824998a7d2e026d2606
timeCreated: 1498976326
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,13 @@
{
"author": "Keijiro Takahashi",
"description": "Noise Shader provides lightweight 2D/3D gradient noise functions for shader use.",
"displayName": "Noise Shader",
"keywords": [ "unity" ],
"license": "MIT",
"name": "jp.keijiro.noiseshader",
"repository": "github:keijiro/NoiseShader",
"unity": "2019.3",
"unityRelease": "0f1",
"version": "2.0.0"
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 39ccfaa9af9f4dc84a876b1b1c3ae8bb
PackageManifestImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d7093501998f1b24483fa6c8eddcf8fb
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant: