BITFALL/Assets/Plugins/Polaris - Low Poly Ecosystem/Polaris - Low Poly Terrain .../Runtime/Shaders/Internal/SplineTool/RampMaker.shader

103 lines
2.4 KiB
Plaintext
Raw Normal View History

2023-12-30 17:37:48 +08:00
Shader "Hidden/Griffin/RampMaker"
{
2024-03-05 17:34:41 +08:00
Properties
{
_HeightMap("Height Map", 2D) = "black"{}
}
2023-12-30 17:37:48 +08:00
CGINCLUDE
2024-03-05 17:34:41 +08:00
#include "UnityCG.cginc"
2023-12-30 17:37:48 +08:00
#include "SplineToolCommon.cginc"
struct appdata
2024-03-05 17:34:41 +08:00
{
float4 vertex: POSITION;
float4 color: COLOR;
};
struct v2f
{
float4 vertex: SV_POSITION;
float4 color: COLOR;
float3 positionNormalized: TEXCOORD0;
float4 positionWS: TEXCOORD1;
};
2023-12-30 17:37:48 +08:00
sampler2D _HeightMap;
sampler2D _Falloff;
sampler2D _FalloffNoise;
float4 _FalloffNoise_ST;
2024-03-05 17:34:41 +08:00
float _HeightOffset;
2023-12-30 17:37:48 +08:00
int _LowerHeight;
int _RaiseHeight;
float _AdditionalMeshResolution;
sampler2D _TerrainMask;
int _StepCount;
2024-03-05 17:34:41 +08:00
float4x4 _WorldToNormalized;
2023-12-30 17:37:48 +08:00
float stepValue(float v, int stepCount)
{
float step = 1.0 / stepCount;
return v - v % step;
}
2024-03-05 17:34:41 +08:00
v2f vert(appdata v)
{
2023-12-30 17:37:48 +08:00
v2f o;
2024-03-05 17:34:41 +08:00
v.vertex.y += _HeightOffset;
2023-12-30 17:37:48 +08:00
o.vertex = UnityObjectToClipPos(v.vertex);
2024-03-05 17:34:41 +08:00
o.color = v.color;
o.positionWS = mul(unity_ObjectToWorld, v.vertex);
o.positionNormalized = mul(_WorldToNormalized, o.positionWS);
2023-12-30 17:37:48 +08:00
return o;
2024-03-05 17:34:41 +08:00
}
2023-12-30 17:37:48 +08:00
2024-03-05 17:34:41 +08:00
fixed4 fragRamp(v2f i): SV_Target
{
float4 heightMapColor = tex2D(_HeightMap, i.positionNormalized.xz);
2023-12-30 17:37:48 +08:00
float currentHeight = GriffinDecodeFloatRG(heightMapColor.rg);
2024-03-05 17:34:41 +08:00
float splineHeight = clamp(i.positionNormalized.y, 0, 1);
2023-12-30 17:37:48 +08:00
float delta = splineHeight - currentHeight;
2024-03-05 17:34:41 +08:00
float targetHeight = currentHeight + (delta < 0) * _LowerHeight * delta + (delta >= 0) * _RaiseHeight * saturate(delta);
2023-12-30 17:37:48 +08:00
2024-03-05 17:34:41 +08:00
float2 noiseUV = float2(i.positionWS.x * _FalloffNoise_ST.x, i.positionWS.z * _FalloffNoise_ST.y);
float falloffNoise = tex2D(_FalloffNoise, noiseUV).r;
float t = clamp(i.color.r, 0, 1);
float falloffCurve = tex2D(_Falloff, float2(t, 0.5)).r;
float falloff = (falloffCurve - falloffNoise) * (i.color.r < 1) + i.color.r * (i.color.r == 1);
falloff = saturate(falloff);
2023-12-30 17:37:48 +08:00
2024-03-05 17:34:41 +08:00
float terrainMask = 1 - tex2D(_TerrainMask, i.positionNormalized.xz).r;
2023-12-30 17:37:48 +08:00
float h = lerp(currentHeight, targetHeight, falloff);
h = lerp(currentHeight, h, terrainMask);
h = stepValue(h, _StepCount);
h = max(0, min(0.999999, h));
2024-03-05 17:34:41 +08:00
2023-12-30 17:37:48 +08:00
float2 encodedHeight = GriffinEncodeFloatRG(h);
float addRes = lerp(0, _AdditionalMeshResolution, terrainMask);
return saturate(float4(encodedHeight.rg, addRes, heightMapColor.a));
}
ENDCG
2024-03-05 17:34:41 +08:00
SubShader
{
Tags { "RenderType" = "Transparent" }
2023-12-30 17:37:48 +08:00
Cull Off
2024-03-05 17:34:41 +08:00
Pass
{
2023-12-30 17:37:48 +08:00
Name "Ramp"
Blend One Zero
BlendOp Add
2024-03-05 17:34:41 +08:00
CGPROGRAM
#pragma vertex vert
#pragma fragment fragRamp
ENDCG
}
}
2023-12-30 17:37:48 +08:00
}