Split body distance marching from material shading

This commit is contained in:
Meta Vens 2026-05-12 00:51:51 +01:00
commit dbe32138d2
14 changed files with 175 additions and 49 deletions

View file

@ -4,6 +4,14 @@ static const int BODY_INDEX = 4;
#include "D3D12SdfMath.hlsli"
#include "D3D12AgentCharacters.hlsli"
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
float radius = max(agent.centerRadius.w, 0.001);
float3 local = (p - agent.centerRadius.xyz) / radius;
return agentBodySdf(local, agent).distanceValue * radius;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
@ -18,6 +26,10 @@ BodySurface bodySurface(float3 p, int agentIndex)
surface.roleId = agent.previousCenterRole.w;
surface.lodTier = agent.lodIndexFlags.x;
surface.costTier = agentSurface.costTier;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = 0.0;
return surface;
}

View file

@ -80,6 +80,10 @@ struct BodySurface
float roleId;
float lodTier;
float costTier;
float3 albedo;
float roughness;
float3 f0;
float3 emission;
};
void cameraBasis(float3 camera, float2 center, out float3 forward, out float3 right, out float3 up)
@ -250,6 +254,15 @@ float3 studioIrradianceDiffuseRadiance(float3 p, float3 normal, float3 albedo, f
return diffuseShare * albedo * irradiance * (STUDIO_IRRADIANCE_INTENSITY / PI);
}
float3 shadeBodyPbr(float3 p, float3 normal, BodySurface surface)
{
return surface.emission
+ surface.albedo * bodyLightIrradianceAt(p, normal) / PI
+ studioIrradianceDiffuseRadiance(p, normal, surface.albedo, surface.roughness, surface.f0)
+ bodyLightSpecularRadiance(p, normal, surface.roughness, surface.f0, 7.0)
+ studioPmremSpecularRadiance(p, normal, surface.roughness, surface.f0);
}
float3 shadeRoleAgentBody(float3 p, float3 normal, int agentIndex)
{
float roleId = agentVisuals[agentIndex].previousCenterRole.w;

View file

@ -17,25 +17,22 @@
float3 bodyNormal(float3 p, int agentIndex)
{
float epsilon = 0.006;
float dx = bodySurface(p + float3(epsilon, 0.0, 0.0), agentIndex).distanceValue - bodySurface(p - float3(epsilon, 0.0, 0.0), agentIndex).distanceValue;
float dy = bodySurface(p + float3(0.0, epsilon, 0.0), agentIndex).distanceValue - bodySurface(p - float3(0.0, epsilon, 0.0), agentIndex).distanceValue;
float dz = bodySurface(p + float3(0.0, 0.0, epsilon), agentIndex).distanceValue - bodySurface(p - float3(0.0, 0.0, epsilon), agentIndex).distanceValue;
float dx = bodyDistance(p + float3(epsilon, 0.0, 0.0), agentIndex) - bodyDistance(p - float3(epsilon, 0.0, 0.0), agentIndex);
float dy = bodyDistance(p + float3(0.0, epsilon, 0.0), agentIndex) - bodyDistance(p - float3(0.0, epsilon, 0.0), agentIndex);
float dz = bodyDistance(p + float3(0.0, 0.0, epsilon), agentIndex) - bodyDistance(p - float3(0.0, 0.0, epsilon), agentIndex);
return normalize(float3(dx, dy, dz));
}
bool refineBodyHit(float3 origin, float3 direction, int agentIndex, float lowTravel, float highTravel, out float travel, out float3 normal, out BodySurface surface)
{
BodySurface highSurface = bodySurface(origin + direction * highTravel, agentIndex);
[unroll]
for (int i = 0; i < 8; i++)
{
float midTravel = (lowTravel + highTravel) * 0.5;
BodySurface midSurface = bodySurface(origin + direction * midTravel, agentIndex);
if (midSurface.distanceValue <= 0.0)
float midDistance = bodyDistance(origin + direction * midTravel, agentIndex);
if (midDistance <= 0.0)
{
highTravel = midTravel;
highSurface = midSurface;
}
else
{
@ -44,8 +41,9 @@ bool refineBodyHit(float3 origin, float3 direction, int agentIndex, float lowTra
}
travel = highTravel;
surface = highSurface;
normal = bodyNormal(origin + direction * travel, agentIndex);
float3 p = origin + direction * travel;
surface = bodySurface(p, agentIndex);
normal = bodyNormal(p, agentIndex);
return true;
}
@ -69,9 +67,18 @@ bool traceBody(float3 origin, float3 direction, int agentIndex, out float travel
travel = max(-b - h, 0.0);
normal = 0.0;
stepCount = 0.0;
surface = bodySurface(origin + direction * travel, agentIndex);
surface.distanceValue = 0.0;
surface.materialId = 0.0;
surface.fieldId = FIELD_ID_GRID;
surface.roleId = 0.0;
surface.lodTier = 0.0;
surface.costTier = 0.0;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = 0.0;
float previousTravel = travel;
float previousDistance = surface.distanceValue;
float previousDistance = bodyDistance(origin + direction * travel, agentIndex);
float maxStep = max(agent.centerRadius.w * BODY_TRACE_MAX_STEP_RADIUS_SCALE, BODY_TRACE_MIN_STEP);
[loop]
@ -83,22 +90,23 @@ bool traceBody(float3 origin, float3 direction, int agentIndex, out float travel
}
float3 p = origin + direction * travel;
surface = bodySurface(p, agentIndex);
float distanceValue = bodyDistance(p, agentIndex);
stepCount = (float)(stepIndex + 1);
if (surface.distanceValue <= 0.0)
if (distanceValue <= 0.0)
{
if (previousDistance > 0.0)
{
return refineBodyHit(origin, direction, agentIndex, previousTravel, travel, travel, normal, surface);
}
surface = bodySurface(p, agentIndex);
normal = bodyNormal(p, agentIndex);
return true;
}
previousTravel = travel;
previousDistance = surface.distanceValue;
travel += clamp(abs(surface.distanceValue) * BODY_TRACE_STEP_SCALE, BODY_TRACE_MIN_STEP, maxStep);
previousDistance = distanceValue;
travel += clamp(abs(distanceValue) * BODY_TRACE_STEP_SCALE, BODY_TRACE_MIN_STEP, maxStep);
}
return false;

View file

@ -5,6 +5,15 @@ static const int BODY_INDEX = 8;
static const float CURSOR_RADIUS = 0.56;
struct CursorHibiscusParts
{
float petals;
float throat;
float stamen;
float calyx;
float distanceValue;
};
float2 hibiscusPetalCenter(float u)
{
float radial = 0.92 * u * u * (1.0 - 0.10 * u);
@ -55,7 +64,7 @@ float sdHibiscusStamen(float3 local)
return min(column, beads);
}
float sdHibiscusCursor(float3 local, float timeSeconds)
CursorHibiscusParts cursorHibiscusParts(float3 local, float timeSeconds)
{
float sway = 0.10 * sin(timeSeconds * 0.85);
float petal0 = sdHibiscusCursorPetal(local, 1.45 + sway, timeSeconds);
@ -76,54 +85,56 @@ float sdHibiscusCursor(float3 local, float timeSeconds)
float blossom = smoothUnion(petals, throat, 0.10);
blossom = smoothUnion(blossom, stamen, 0.065);
return smoothUnion(blossom, calyx, 0.120);
CursorHibiscusParts parts;
parts.petals = petals;
parts.throat = throat;
parts.stamen = stamen;
parts.calyx = calyx;
parts.distanceValue = smoothUnion(blossom, calyx, 0.120);
return parts;
}
float cursorHibiscusSdf(float3 p)
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual cursor = agentVisuals[CURSOR_OBJECT_INDEX];
float3 local = (p - cursor.centerRadius.xyz) / CURSOR_RADIUS;
return sdHibiscusCursor(local, timeSeconds) * CURSOR_RADIUS;
return cursorHibiscusParts(local, timeSeconds).distanceValue * CURSOR_RADIUS;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
AgentVisual cursor = agentVisuals[CURSOR_OBJECT_INDEX];
float3 local = (p - cursor.centerRadius.xyz) / CURSOR_RADIUS;
CursorHibiscusParts parts = cursorHibiscusParts(local, timeSeconds);
float blossomPart = min(parts.petals, min(parts.throat, parts.stamen));
float isCalyx = parts.calyx <= blossomPart ? 1.0 : 0.0;
float isStamen = (1.0 - isCalyx) * (parts.stamen <= min(parts.petals, parts.throat) ? 1.0 : 0.0);
float isThroat = (1.0 - isCalyx) * (1.0 - isStamen) * (parts.throat <= parts.petals ? 1.0 : 0.0);
float isPetal = (1.0 - isCalyx) * (1.0 - isStamen) * (1.0 - isThroat);
float petalTone = 0.55 + 0.45 * cos(atan2(local.y, local.x) * 3.0 - timeSeconds * 1.8);
float3 petalAlbedo = lerp(float3(1.0, 0.34, 0.62), float3(1.0, 0.78, 0.90), petalTone);
float3 throatAlbedo = float3(1.0, 0.20, 0.48);
float3 stamenAlbedo = float3(1.0, 0.72, 0.18);
float3 calyxAlbedo = float3(0.34, 0.66, 0.12);
BodySurface surface;
surface.distanceValue = cursorHibiscusSdf(p);
surface.materialId = 5.0;
surface.distanceValue = parts.distanceValue * CURSOR_RADIUS;
surface.materialId = 5.0 + isStamen * 0.1 + isCalyx * 0.2 + isThroat * 0.3;
surface.fieldId = FIELD_ID_CURSOR;
surface.roleId = 0.0;
surface.lodTier = 0.0;
surface.costTier = 1.0;
surface.albedo = petalAlbedo * isPetal + throatAlbedo * isThroat + stamenAlbedo * isStamen + calyxAlbedo * isCalyx;
surface.roughness = isCalyx > 0.5 ? 0.46 : 0.22;
surface.f0 = lerp(float3(1.0, 0.38, 0.72), float3(0.04, 0.06, 0.03), isCalyx);
surface.emission = surface.albedo * (isCalyx > 0.5 ? 0.05 : 0.56);
return surface;
}
float3 cursorEmissionRadiance(float3 p, float3 normal)
{
AgentVisual cursor = agentVisuals[CURSOR_OBJECT_INDEX];
float3 local = (p - cursor.centerRadius.xyz) / CURSOR_RADIUS;
float petal = 0.55 + 0.45 * cos(atan2(local.y, local.x) * 3.0 - timeSeconds * 1.8);
float throat = exp(-dot(local.xy, local.xy) * 7.0) * smoothstep(0.30, -0.18, local.z);
float stamen = smoothstep(0.018, -0.035, sdHibiscusStamen(local));
float calyxCore = sdQuadraticTube(local, float3(0.0, 0.0, -1.0), float3(0.06, -0.04, -0.62), float3(0.0, 0.0, -0.18), 0.018, 0.130);
float calyxSepals = min(sdHibiscusSepal(local, 1.57), min(sdHibiscusSepal(local, 3.66), sdHibiscusSepal(local, 5.75)));
float calyx = smoothstep(0.08, -0.02, min(calyxCore, calyxSepals));
float rim = pow(1.0 - saturate(dot(normal, normalize(cameraPosition - p))), 2.0);
float3 petalColor = lerp(float3(1.12, 0.28, 0.58), float3(1.0, 0.78, 0.90), petal);
float3 throatColor = float3(1.0, 0.18, 0.45);
float3 stamenColor = float3(1.0, 0.70, 0.10);
float3 calyxColor = float3(0.36, 0.68, 0.10);
float3 blossom = petalColor * (0.82 + rim * 0.75) + throatColor * throat * 1.7 + stamenColor * stamen * 1.35;
return lerp(blossom, calyxColor * (0.55 + rim * 0.25), calyx);
}
float3 shadeBody(float2 uv, float travel, float3 p, float3 normal, int agentIndex, BodySurface surface)
{
static const float CursorRoughness = 0.22;
float3 cursorF0 = float3(1.0, 0.38, 0.72);
return cursorEmissionRadiance(p, normal)
+ bodyLightSpecularRadiance(p, normal, CursorRoughness, cursorF0, 7.0)
+ studioPmremSpecularRadiance(p, normal, CursorRoughness, cursorF0);
return shadeBodyPbr(p, normal, surface);
}
#include "D3D12BodyProxy.hlsli"

View file

@ -4,6 +4,14 @@ static const int BODY_INDEX = 3;
#include "D3D12SdfMath.hlsli"
#include "D3D12AgentCharacters.hlsli"
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
float radius = max(agent.centerRadius.w, 0.001);
float3 local = (p - agent.centerRadius.xyz) / radius;
return agentFallbackSdf(local, agent).distanceValue * radius;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
@ -18,6 +26,10 @@ BodySurface bodySurface(float3 p, int agentIndex)
surface.roleId = agent.previousCenterRole.w;
surface.lodTier = agent.lodIndexFlags.x;
surface.costTier = agentSurface.costTier;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = 0.0;
return surface;
}

View file

@ -4,6 +4,14 @@ static const int BODY_INDEX = 1;
#include "D3D12SdfMath.hlsli"
#include "D3D12AgentCharacters.hlsli"
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
float radius = max(agent.centerRadius.w, 0.001);
float3 local = (p - agent.centerRadius.xyz) / radius;
return agentFallbackSdf(local, agent).distanceValue * radius;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
@ -18,6 +26,10 @@ BodySurface bodySurface(float3 p, int agentIndex)
surface.roleId = agent.previousCenterRole.w;
surface.lodTier = agent.lodIndexFlags.x;
surface.costTier = agentSurface.costTier;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = 0.0;
return surface;
}

View file

@ -4,6 +4,14 @@ static const int BODY_INDEX = 5;
#include "D3D12SdfMath.hlsli"
#include "D3D12AgentCharacters.hlsli"
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
float radius = max(agent.centerRadius.w, 0.001);
float3 local = (p - agent.centerRadius.xyz) / radius;
return agentFallbackSdf(local, agent).distanceValue * radius;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
@ -18,6 +26,10 @@ BodySurface bodySurface(float3 p, int agentIndex)
surface.roleId = agent.previousCenterRole.w;
surface.lodTier = agent.lodIndexFlags.x;
surface.costTier = agentSurface.costTier;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = 0.0;
return surface;
}

View file

@ -4,6 +4,14 @@ static const int BODY_INDEX = 2;
#include "D3D12SdfMath.hlsli"
#include "D3D12AgentCharacters.hlsli"
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
float radius = max(agent.centerRadius.w, 0.001);
float3 local = (p - agent.centerRadius.xyz) / radius;
return agentImaginationSdf(local, agent, timeSeconds).distanceValue * radius;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
@ -18,6 +26,10 @@ BodySurface bodySurface(float3 p, int agentIndex)
surface.roleId = agent.previousCenterRole.w;
surface.lodTier = agent.lodIndexFlags.x;
surface.costTier = agentSurface.costTier;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = 0.0;
return surface;
}

View file

@ -4,6 +4,14 @@ static const int BODY_INDEX = 7;
#include "D3D12SdfMath.hlsli"
#include "D3D12AgentCharacters.hlsli"
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
float radius = max(agent.centerRadius.w, 0.001);
float3 local = (p - agent.centerRadius.xyz) / radius;
return agentFallbackSdf(local, agent).distanceValue * radius;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
@ -18,6 +26,10 @@ BodySurface bodySurface(float3 p, int agentIndex)
surface.roleId = agent.previousCenterRole.w;
surface.lodTier = agent.lodIndexFlags.x;
surface.costTier = agentSurface.costTier;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = 0.0;
return surface;
}

View file

@ -3,19 +3,27 @@ static const int BODY_INDEX = 0;
#include "D3D12BodyCommon.hlsli"
#include "D3D12SdfMath.hlsli"
BodySurface bodySurface(float3 p, int agentIndex)
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
float radius = max(agent.centerRadius.w, 0.001);
float3 local = (p - agent.centerRadius.xyz) / radius;
return sdSphere(local, 1.0) * radius;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
BodySurface surface;
surface.distanceValue = sdSphere(local, 1.0) * radius;
surface.distanceValue = bodyDistance(p, agentIndex);
surface.materialId = 0.0;
surface.fieldId = FIELD_ID_SELF;
surface.roleId = 0.0;
surface.lodTier = 0.0;
surface.costTier = 0.0;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = primitiveEmissionRadiance(FIELD_ID_SELF);
return surface;
}

View file

@ -4,6 +4,14 @@ static const int BODY_INDEX = 6;
#include "D3D12SdfMath.hlsli"
#include "D3D12AgentCharacters.hlsli"
float bodyDistance(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
float radius = max(agent.centerRadius.w, 0.001);
float3 local = (p - agent.centerRadius.xyz) / radius;
return agentFallbackSdf(local, agent).distanceValue * radius;
}
BodySurface bodySurface(float3 p, int agentIndex)
{
AgentVisual agent = agentVisuals[agentIndex];
@ -18,6 +26,10 @@ BodySurface bodySurface(float3 p, int agentIndex)
surface.roleId = agent.previousCenterRole.w;
surface.lodTier = agent.lodIndexFlags.x;
surface.costTier = agentSurface.costTier;
surface.albedo = 0.0;
surface.roughness = 0.0;
surface.f0 = 0.0;
surface.emission = 0.0;
return surface;
}