bvle-voxels/shaders/voxelTopingPS.hlsl
Samuel Bouchet ef89bd8c49 Phase 4.2: grass blade tufts, stone corner fills/caps, vegetation shading
Stone: add corner fill triangles at adjacent open edges and cap
triangles at strip terminaisons. Grass: replace bevel strips with
tuft-based grass blades — clusters of 3-9 curved double-sided
blades with per-tuft height/lean personality and hash-driven
placement (quadratic inset 0-0.30 from edge). Vegetation PS uses
half-Lambert wrap lighting + translucency for soft stylized shading
(inspired by Airborn Trees). Stone keeps classic Lambert.
2026-03-26 18:48:35 +01:00

70 lines
3 KiB
HLSL

// BVLE Voxels - Toping Pixel Shader (Phase 4.2)
// Simplified version of voxelPS: triplanar texture sampling + basic lighting.
// No height-based blending (topings are small decorative meshes).
#include "voxelCommon.hlsli"
Texture2DArray<float4> materialTextures : register(t1);
SamplerState texSampler : register(s0);
struct PSInput {
float4 position : SV_POSITION;
float3 worldPos : WORLDPOS;
float3 normal : NORMAL;
nointerpolation uint materialID : MATERIALID;
};
[RootSignature(VOXEL_ROOTSIG)]
float4 main(PSInput input) : SV_TARGET0 {
float3 N = normalize(input.normal);
float tiling = textureTiling;
// Material texture index (materialID 1-5 → array layer 0-4)
uint texIdx = clamp(input.materialID - 1u, 0u, 4u);
// Triplanar sampling (same as voxel PS)
float3 blend = abs(N);
blend = blend / (blend.x + blend.y + blend.z + 0.001);
float4 xSample = materialTextures.Sample(texSampler, float3(input.worldPos.yz * tiling, (float)texIdx));
float4 ySample = materialTextures.Sample(texSampler, float3(input.worldPos.xz * tiling, (float)texIdx));
float4 zSample = materialTextures.Sample(texSampler, float3(input.worldPos.xy * tiling, (float)texIdx));
float3 texColor = (xSample.rgb * blend.x + ySample.rgb * blend.y + zSample.rgb * blend.z);
float3 L = normalize(-sunDirection.xyz);
float rawNdotL = dot(N, L);
// ── Material-dependent lighting ─────────────────────────────
// Stone (materialID=3): standard diffuse like voxel faces
// Vegetation (grass etc.): stylized wrap lighting + translucency
// inspired by Airborn Trees (simonschreibt.de/gat/airborn-trees/)
float3 lit;
if (input.materialID == 3u) {
// Stone: classic Lambert + cool ambient (matches voxel PS)
float NdotL = max(rawNdotL, 0.0);
float3 ambient = float3(0.15, 0.18, 0.25);
lit = texColor * (sunColor.rgb * NdotL + ambient);
} else {
// ── Vegetation: soft wrap lighting ──────────────────────
// Half-Lambert: wraps light around the surface, no hard terminator
float halfLambert = rawNdotL * 0.5 + 0.5;
float wrap = halfLambert * halfLambert; // squared for falloff shape
// Translucency: thin blades let light through from behind
// Simple SSS approximation: light arriving from the back side
float3 V = normalize(cameraPosition.xyz - input.worldPos);
float backLight = saturate(dot(V, L)); // view aligned with light = backlit
float transAmount = (1.0 - saturate(rawNdotL)) * 0.4; // stronger when facing away from light
float translucency = backLight * transAmount;
// Warm vegetation ambient (higher than stone, slightly green-tinted)
float3 vegAmbient = float3(0.22, 0.28, 0.20);
float3 diffuse = sunColor.rgb * (wrap * 0.7 + translucency * 0.5);
lit = texColor * (diffuse + vegAmbient);
}
return float4(lit, 1.0);
}