Files
Ipseity-Demo/Assets/_Content/Shaders/HouseOfLeaves/Include/HouseOfLeaves.hlsl
T

238 lines
6.1 KiB
HLSL

#ifndef HOUSE_OF_LEAVES_INCLUDED
#define HOUSE_OF_LEAVES_INCLUDED
#include "Assets/_Content/Shaders/Include/Noises.hlsl"
#include "Assets/_Content/Shaders/Include/Details.hlsl"
#include "Assets/_Content/Shaders/Include/Utilities.hlsl"
float Hash(float n)
{
return frac(sin(n * 127.1) * 43758.5453123);
}
float3 ColorRemapRandom
(
float rnd,
float3 ColorA,
float3 ColorB,
float3 ColorC,
float3 ColorD,
float3 ColorE,
float wA,
float wB,
float wC,
float wD
)
{
if (rnd < wA)
return ColorA;
if (rnd < wA + wB)
return ColorB;
if (rnd < wA + wB + wC)
return ColorC;
if (rnd < wA + wB + wC + wD)
return ColorD;
return ColorE;
}
float3 ColorRemapLinear
(
float Gradient,
float3 ColorA,
float3 ColorB,
float3 ColorC,
float3 ColorD,
float3 ColorE
)
{
float3 col = ColorA;
col = lerp(col, ColorB, smoothstep(0.0, .25, Gradient));
col = lerp(col, ColorC, smoothstep(.25, .50, Gradient));
col = lerp(col, ColorD, smoothstep(.50, .75, Gradient));
col = lerp(col, ColorE, smoothstep(.75, 1.0, Gradient));
return col;
}
void Sand_float
(
float4 ColorA,
float4 ColorB,
float4 ColorC,
float4 ColorD,
float4 ColorE,
float Progression,
float3 PositionWS,
float3 NormalWS,
out float3 Color,
out float3 Position,
out float Height,
out float Alpha
)
{
float depth = length(PositionWS - _WorldSpaceCameraPos.xyz);
float arkMask = 0;
float t = Progression;
t -= RidgedNoise3D(PositionWS, .3, 1, 2, .5) * .3;
t -= pow(smoothstep(0, 100, depth), .5) * .6;
//t += pow(abs(PositionWS.x), 3) * .02;
//t -= pow(saturate(1 - abs(PositionWS.x * .5)), 2) * .3;
t += .15;
t = saturate(t);
// Sand
float sandLayers = 6;
float sandDirection = cos(t * sandLayers * 3.14);
float sandPatern = RidgedNoise3D(PositionWS + float3(sandDirection, 0, 0), .6, 1, 2, .5) * .8;
sandPatern += 1 - RidgedNoise3D(PositionWS + float3(0, 0, t * sandLayers * 2), .8, 2, 2, .5) * .2;
float sandDensity = sqrt(smoothstep(0, 2, abs(PositionWS.x) + sandPatern));
float sandGradientSign = (floor(t * sandLayers) % 2 == 0) ? 1 : -1;
float sandGradient = smoothstep(2 * -sandGradientSign, 2 * sandGradientSign, PositionWS.x);
float sandPhase = frac(t * sandLayers);
float sandFlow = smoothstep(sandPhase * 2, sandPhase * 2 - 1, sandGradient);
sandFlow = (sandFlow + floor(t * sandLayers)) / sandLayers;
float sandMask = sandFlow * 1.5 - t * .8 + (sandPatern * 2 - 1) * sandDensity * t * .3;
sandMask *= sandDensity * .7 + .3;
// Output
Position = PositionWS;
Position.y += sandMask;
Position.y *= 1 + saturate(pow(abs(PositionWS.x * .5), 3)) * lerp(-.2, 1.4, sandMask);
float n = PerlinNoise3D(Position, 50);
Color = ColorRemapLinear(saturate(pow(smoothstep(5, 30, (Position.z - _WorldSpaceCameraPos.z * .8) * sandPatern), .5)), ColorA, ColorB, ColorC, ColorD, ColorE);
Color *= lerp(1, n, smoothstep(30, 0, depth));
Height = sandMask;
Alpha = 1 - /*smoothstep(1.5, 1.3, abs(PositionWS.x)) **/ smoothstep(0, .3, abs(Position.y));
}
void HouseOfLeaves_float
(
float4 ColorA,
float4 ColorB,
float4 ColorC,
float4 ColorD,
float4 ColorE,
float3 PositionWS,
float3 NormalWS,
float ProjectionsBlend,
UnityTexture2D DetailTexA,
UnityTexture2D DetailTexB,
float DetailIntensityA,
float DetailIntensityB,
float DetailSizeA,
float DetailSizeB,
out float3 Output
)
{
float n0 = FBMNoise3D(PositionWS, 01, 2, 2, .5);
float n1 = FBMNoise3D(PositionWS * float3(1, 1, .5) + n0 + float3(floor(_Time.y * 1.6), 0, 0), 60, 2, 2, .5);
float n2 = PerlinNoise3D(PositionWS * float3(1, 1, 30) + float3(floor(_Time.y * 4), 0, 0), 1);
float n3 = RidgedNoise3D(PositionWS * float3(n0, n1, n2 + 4) + float3(floor(_Time.y * 1), 0, 0), .3, 2, 2, 1);
float depth = distance(_WorldSpaceCameraPos.xyz, PositionWS);
float gradient = smoothstep(-30, 100, PositionWS.z);
gradient += ((n1 * 2 - 1) * step(n0, .5)) * .001;
gradient += n2 * .006;
gradient -= NormalWS.y * .001;
float t = 0;
float bands = 300 + t;
float bandPos = gradient * bands;
float bandIndex = floor(bandPos);
float bandFrac = frac(bandPos);
float wA = pow(1.0 - gradient, 4.0);
float wB = pow(1.0 - abs(gradient - .25) * 2.0, 2.0);
float wC = pow(1.0 - abs(gradient - .50) * 2.0, 2.0);
float wD = pow(1.0 - abs(gradient - .75) * 2.0, 2.0);
float wE = .1 + pow(gradient, 4.0);
float total = wA + wB + wC + wD + wE;
wA /= total;
wB /= total;
wC /= total;
wD /= total;
wE /= total;
float rnd0 = Hash(bandIndex);
float3 col0 = ColorRemapRandom(
rnd0,
ColorA.rgb,
ColorB.rgb,
ColorC.rgb,
ColorD.rgb,
ColorE.rgb,
wA, wB, wC, wD
);
float depthAtten = smoothstep(50, 100, depth);
float3 col = lerp(col0, _ColorB, n0 - n1);
col = lerp(col, _ColorA, smoothstep(.8 - gradient, .7 - gradient, n3) * .8);
col = lerp(col, _ColorE, pow(depthAtten, 2));
col = lerp(col, _ColorA, pow(smoothstep(.2 + n2 * 4, 0, PositionWS.y), 2) * (1 - depthAtten) * .95);
col = lerp(col, _ColorA * .1, smoothstep(0, -30, PositionWS.z));
float blendWidth = .05;
float blend = smoothstep(
1.0 - blendWidth,
1.0,
bandFrac
);
// Detail
float4 detailA = SampleProjectedDetailLOD
(
DetailTexA,
PositionWS,
NormalWS,
ProjectionsBlend,
DetailSizeA,
depth
);
float4 detailB = SampleProjectedDetailLOD
(
DetailTexB,
PositionWS,
NormalWS,
ProjectionsBlend,
DetailSizeB,
depth
);
float detailMaskA = smoothstep(0, .8, detailA.a);
float detailMaskB = smoothstep(0, .8, detailB.a);
Output = col;
Output = lerp(Output.rgb, detailA.rgb, detailMaskA * DetailIntensityA);
Output = lerp(Output.rgb, detailB.rgb, detailMaskB * DetailIntensityB);
}
#endif