Refactor lighting code and add wave support to shaders

Modularized lighting calculations by introducing DefaultSetup.hlsl and Lambert.hlsl includes, and updated BlendinShader, LitParticles, and Water shaders to use these macros. Added new wave-related properties and logic to Water.shader for enhanced wave effects. Improved maintainability and consistency across shaders by centralizing light and Lambertian diffuse calculations.
This commit is contained in:
DeMuenu
2025-09-25 02:21:26 +02:00
parent b9bfd0b559
commit 07730827f4
11 changed files with 103 additions and 86 deletions

View File

@@ -32,6 +32,9 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
#define MAX_LIGHTS 80 // >= maxPlayers in script
#include "UnityCG.cginc"
#include "Includes/LightStrength.hlsl"
#include "Includes/Lambert.hlsl"
#include "Includes/DefaultSetup.hlsl"
@@ -116,46 +119,15 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
float4 dmax = float4(0,0,0,1);
float dIntensity = 0;
[loop]
for (int idx = 0; idx < MAX_LIGHTS; idx++)
for (int LightCounter = 0; LightCounter < MAX_LIGHTS; LightCounter++)
{
if (idx >= count) break;
float radius = _LightPositions[idx].a;
float3 q = _LightPositions[idx].xyz;
float distanceFromLight = length(i.worldPos - q);
if (distanceFromLight > _LightCutoffDistance) continue;
float sd = 0.0;
float contrib = 0.0;
InLoopSetup(_LightPositions, LightCounter, count, i); //defines distanceFromLight, contrib
float invSqMul = max(1e-4, _InverseSqareMultiplier);
Lambert(_LightPositions[LightCounter].xyz ,i, N); //defines NdotL
//Lambertian diffuse
float3 L = normalize(q - i.worldPos); // q = light position
float NdotL = saturate(dot(N, L) * 0.5 + 0.5); // one-sided Lambert
if (NdotL <= 0) continue;
LightTypeCalculations(_LightColors, LightCounter, i, NdotL, dIntensity, _LightPositions[LightCounter].a, _LightPositions[LightCounter].xyz);
if(_LightType[idx] == 0)
{
float invSq = _LightColors[idx].a / max(1e-4, max(0, max(1, distanceFromLight - radius) * invSqMul) * max(0, max(1, distanceFromLight - radius) * invSqMul));
contrib = invSq;
//contrib = contrib * step(-distance(i.worldPos, q), -1 + radius * 1); // 0 if outside sphere
dIntensity += contrib * NdotL;
}
else if (_LightType[idx] == 1)
{
float invSq = _LightColors[idx].a / max(1e-4, (distanceFromLight * invSqMul) * (distanceFromLight * invSqMul));
float threshold = (-1 + _LightDirections[idx].w / 180);
contrib = min(dot(normalize(i.worldPos - q), -normalize(_LightDirections[idx].xyz)), 0);
contrib= 1 - step(threshold, contrib);
contrib = contrib * invSq;
dIntensity += contrib * NdotL;
}
float3 LightColor = _LightColors[idx].xyz; // * NormalDirMult;