116 lines
4.3 KiB
HLSL
116 lines
4.3 KiB
HLSL
// CloudDeform.hlsl
|
|
//
|
|
// Include this in a Shader Graph "Custom Function Node" (Type: File) to deform
|
|
// a cloud mesh's vertices around any player standing on or near it.
|
|
//
|
|
// Player world positions are supplied every frame by CloudDeformFeeder.cs via
|
|
// global shader properties -- this file only reads them. It does not manage
|
|
// networking itself; it relies on Somnium's own avatar-position replication,
|
|
// so every client runs this same math against the same inputs.
|
|
|
|
#ifndef CLOUD_DEFORM_INCLUDED
|
|
#define CLOUD_DEFORM_INCLUDED
|
|
|
|
// Must match MaxTrackedPlayers in CloudDeformFeeder.cs.
|
|
#define CLOUD_DEFORM_MAX_PLAYERS 8
|
|
|
|
float4 _CloudDeformPlayerPositions[CLOUD_DEFORM_MAX_PLAYERS];
|
|
int _CloudDeformPlayerCount;
|
|
|
|
// 0..1: how "deformed" this point is. 1 = right at a player's feet,
|
|
// 0 = outside DeformRadius of every tracked player.
|
|
float CloudDeformInfluence(float3 worldPos, float deformRadius)
|
|
{
|
|
float influence = 0.0;
|
|
|
|
// Loop bound must be a compile-time constant; the runtime count is checked
|
|
// inside instead so the shader still compiles on platforms that dislike
|
|
// variable-length loops.
|
|
for (int i = 0; i < CLOUD_DEFORM_MAX_PLAYERS; i++)
|
|
{
|
|
if (i >= _CloudDeformPlayerCount) break;
|
|
|
|
float dist = distance(worldPos, _CloudDeformPlayerPositions[i].xyz);
|
|
float raw = 1.0 - saturate(dist / max(deformRadius, 0.0001));
|
|
influence = max(influence, smoothstep(0.0, 1.0, raw));
|
|
}
|
|
|
|
return influence;
|
|
}
|
|
|
|
// World-space offset for a single point: pushes it down and slightly outward,
|
|
// away from the nearest tracked player, proportional to proximity.
|
|
float3 CloudDeformOffset(float3 worldPos, float deformRadius, float pushDown, float pushOut)
|
|
{
|
|
float nearestDist = 1e9;
|
|
float3 nearestPlayerPos = worldPos;
|
|
|
|
for (int i = 0; i < CLOUD_DEFORM_MAX_PLAYERS; i++)
|
|
{
|
|
if (i >= _CloudDeformPlayerCount) break;
|
|
|
|
float3 playerPos = _CloudDeformPlayerPositions[i].xyz;
|
|
float dist = distance(worldPos, playerPos);
|
|
if (dist < nearestDist)
|
|
{
|
|
nearestDist = dist;
|
|
nearestPlayerPos = playerPos;
|
|
}
|
|
}
|
|
|
|
float influence = smoothstep(0.0, 1.0, 1.0 - saturate(nearestDist / max(deformRadius, 0.0001)));
|
|
|
|
float3 offset = float3(0, 0, 0);
|
|
offset.y -= influence * pushDown;
|
|
|
|
float3 away = worldPos - nearestPlayerPos;
|
|
away.y = 0;
|
|
float awayLen = length(away);
|
|
if (awayLen > 0.0001)
|
|
{
|
|
offset += (away / awayLen) * influence * pushOut;
|
|
}
|
|
|
|
return offset;
|
|
}
|
|
|
|
// Entry point for the Shader Graph Custom Function Node. Wire this into the
|
|
// Vertex stage using World-space Position and Normal as inputs.
|
|
//
|
|
// Inputs: WorldPos, WorldNormal, DeformRadius, PushDown, PushOut, SampleOffset
|
|
// Outputs: OutWorldPos, OutWorldNormal
|
|
void CloudDeformVertex_float(
|
|
float3 WorldPos, float3 WorldNormal, float DeformRadius, float PushDown, float PushOut,
|
|
float SampleOffset,
|
|
out float3 OutWorldPos, out float3 OutWorldNormal)
|
|
{
|
|
float3 offset = CloudDeformOffset(WorldPos, DeformRadius, PushDown, PushOut);
|
|
OutWorldPos = WorldPos + offset;
|
|
|
|
// Recompute the normal from two nearby samples (finite-difference) so
|
|
// lighting follows the dimple instead of staying flat.
|
|
float3 tangentX = float3(SampleOffset, 0, 0);
|
|
float3 tangentZ = float3(0, 0, SampleOffset);
|
|
|
|
float3 posX = WorldPos + tangentX + CloudDeformOffset(WorldPos + tangentX, DeformRadius, PushDown, PushOut);
|
|
float3 posZ = WorldPos + tangentZ + CloudDeformOffset(WorldPos + tangentZ, DeformRadius, PushDown, PushOut);
|
|
|
|
float3 recomputedNormal = normalize(cross(posZ - OutWorldPos, posX - OutWorldPos));
|
|
// If lighting looks inside-out on your mesh, flip to cross(posX - OutWorldPos, posZ - OutWorldPos).
|
|
|
|
// Blend toward the recomputed normal in proportion to deformation, so
|
|
// untouched areas of the cloud keep their original (sculpted) normal.
|
|
float influence = CloudDeformInfluence(WorldPos, DeformRadius);
|
|
OutWorldNormal = normalize(lerp(WorldNormal, recomputedNormal, influence));
|
|
}
|
|
|
|
// Separate entry point so the Fragment stage can also read deform proximity
|
|
// (e.g. to darken the cloud slightly where a player is pressing into it),
|
|
// independent of the vertex displacement above.
|
|
void CloudDeformInfluence_float(float3 WorldPos, float DeformRadius, out float Influence)
|
|
{
|
|
Influence = CloudDeformInfluence(WorldPos, DeformRadius);
|
|
}
|
|
|
|
#endif
|