Refactor Moonlight lighting system in shaders

Replaces direct variable declarations and code blocks related to 'MoonsLight' with a new 'MoonlightGlobalVariables' macro included from Variables.hlsl. Adds OutLoopSetup macro for loop setup, updates all relevant shaders to use the new macros, and renames comments and identifiers from 'MoonsLight' to 'Moonlight' for consistency. Removes the obsolete MoonsLight.cingc include and its meta file.
This commit is contained in:
DeMuenu
2025-09-25 16:09:24 +02:00
parent 07730827f4
commit 0f049de062
7 changed files with 64 additions and 195 deletions

View File

@@ -12,10 +12,10 @@ Shader "DeMuenu/World/Hoppou/RevealStandart"
_EmmissiveStrength ("Emmissive Strength", Range(0,10)) = 0
//MoonsLight
//Moonlight
_InverseSqareMultiplier ("Inverse Square Multiplier", Float) = 1
_LightCutoffDistance ("Light Cutoff Distance", Float) = 100
//MoonsLight END
//Moonlight END
@@ -35,10 +35,11 @@ Shader "DeMuenu/World/Hoppou/RevealStandart"
#include "Includes/LightStrength.hlsl"
#include "Includes/Lambert.hlsl"
#include "Includes/DefaultSetup.hlsl"
#include "Includes/Variables.hlsl"
//MoonsLight Defines
//Moonlight Defines
#define MAX_LIGHTS 80 // >= maxPlayers in script
//MoonsLight Defines END
//Moonlight Defines END
struct appdata
{
@@ -55,11 +56,11 @@ Shader "DeMuenu/World/Hoppou/RevealStandart"
//UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
//MoonsLight
//Moonlight
float3 worldPos : TEXCOORD2;
float3 worldNormal: TEXCOORD3;
//MoonsLight END
//Moonlight END
};
@@ -78,16 +79,7 @@ Shader "DeMuenu/World/Hoppou/RevealStandart"
float _EmmissiveStrength;
//MoonsLight variables
float _InverseSqareMultiplier;
float _LightCutoffDistance;
float4 _LightPositions[MAX_LIGHTS]; // xyz = position
float4 _LightColors[MAX_LIGHTS]; // xyz = position
float4 _LightDirections[MAX_LIGHTS]; // xyz = direction, w = cos(halfAngle)
float _LightType[MAX_LIGHTS]; // 0 = sphere, 1 = cone
float _PlayerCount; // set via SetFloat
//MoonsLight variables END
MoonlightGlobalVariables
v2f vert (appdata v)
@@ -99,11 +91,11 @@ Shader "DeMuenu/World/Hoppou/RevealStandart"
o.uvEmmis = TRANSFORM_TEX(v.uv, _EmmisiveText);
//MoonsLight Vertex
//Moonlight Vertex
float4 wp = mul(unity_ObjectToWorld, v.vertex);
o.worldPos = wp.xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//MoonsLight Vertex END
//Moonlight Vertex END
return o;
}
@@ -117,13 +109,11 @@ Shader "DeMuenu/World/Hoppou/RevealStandart"
fixed4 emmis = tex2D(_EmmisiveText, i.uvEmmis);
//MoonsLight
int count = (int)_PlayerCount;
//Moonlight
float3 N = normalize(i.worldNormal); /*for lambertian diffuse*/
OutLoopSetup(i, _PlayerCount) //defines count, N, dmax, dIntensity
float3 N = normalize(i.worldNormal); //for lambertian diffuse
// Example: compute distance to nearest player
float4 dmax = float4(0,0,0,1);
float dIntensity = 0;
[loop]
for (int LightCounter = 0; LightCounter < MAX_LIGHTS; LightCounter++)
{
@@ -135,22 +125,14 @@ Shader "DeMuenu/World/Hoppou/RevealStandart"
LightTypeCalculations(_LightColors, LightCounter, i, NdotL, dIntensity, _LightPositions[LightCounter].a, _LightPositions[LightCounter].xyz);
dmax = dmax + contrib * float4(LightColor, 1) * NdotL; // accumulate light contributions
}
//dmax.xyz = min(dmax * dIntensity, 1.0);
dmax.w = 1.0;
dmax = dmax;
//MoonsLight END
//Moonlight END
return col * _Color * dmax + emmis * _EmmissiveStrength * _EmmissiveColor;
}

View File

@@ -8,4 +8,12 @@
float contrib = 0.0; \
#endif
#ifndef OutLoopSetup
#define OutLoopSetup(i, _PlayerCount) \
int count = (int)_PlayerCount; \
\
float4 dmax = float4(0,0,0,1); \
float dIntensity = 0; \
#endif

View File

@@ -0,0 +1,13 @@
#ifndef MoonlightGlobalVariables
#define MoonlightGlobalVariables \
\
float _InverseSqareMultiplier; \
float _LightCutoffDistance; \
\
float4 _LightPositions[MAX_LIGHTS]; /* xyz = position */ \
float4 _LightColors[MAX_LIGHTS]; /* xyz = position */ \
float4 _LightDirections[MAX_LIGHTS]; /* xyz = direction, w = cos(halfAngle) */ \
float _LightType[MAX_LIGHTS]; /* 0 = sphere, 1 = cone */ \
float _PlayerCount; /* set via SetFloat */ \
\
#endif

View File

@@ -6,10 +6,10 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
_Color ("Color", Color) = (1,1,1,1)
//MoonsLight
//Moonlight
_InverseSqareMultiplier ("Inverse Square Multiplier", Float) = 1
_LightCutoffDistance ("Light Cutoff Distance", Float) = 100
//MoonsLight END
//Moonlight END
@@ -35,6 +35,7 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
#include "Includes/LightStrength.hlsl"
#include "Includes/Lambert.hlsl"
#include "Includes/DefaultSetup.hlsl"
#include "Includes/Variables.hlsl"
@@ -53,13 +54,13 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
//MoonsLight
//Moonlight
float3 worldPos : TEXCOORD2;
float4 color : COLOR;
float3 worldNormal: TEXCOORD3;
//MoonsLight END
//Moonlight END
};
sampler2D _MainTex;
@@ -75,16 +76,7 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
float _EmmissiveStrength;
//MoonsLight
float _InverseSqareMultiplier;
float _LightCutoffDistance;
float4 _LightPositions[MAX_LIGHTS]; // xyz = position
float4 _LightColors[MAX_LIGHTS]; // xyz = position
float4 _LightDirections[MAX_LIGHTS]; // xyz = direction, w = cos(halfAngle)
float _LightType[MAX_LIGHTS]; // 0 = sphere, 1 = cone
float _PlayerCount; // set via SetFloat
//MoonsLight END
MoonlightGlobalVariables
v2f vert (appdata v)
@@ -94,12 +86,12 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
//MoonsLight
//Moonlight
float4 wp = mul(unity_ObjectToWorld, v.vertex);
o.worldPos = wp.xyz;
o.color = v.color;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//MoonsLight END
//Moonlight END
return o;
@@ -111,13 +103,11 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
fixed4 col = tex2D(_MainTex, i.uv);
//MoonsLight
int count = (int)_PlayerCount;
//Moonlight
float3 N = normalize(i.worldNormal); /*for lambertian diffuse*/
float3 N = normalize(i.worldNormal); //for lambertian diffuse
// Example: compute distance to nearest player
float4 dmax = float4(0,0,0,1);
float dIntensity = 0;
OutLoopSetup(i, _PlayerCount) //defines count, N, dmax, dIntensity
[loop]
for (int LightCounter = 0; LightCounter < MAX_LIGHTS; LightCounter++)
{
@@ -128,24 +118,14 @@ Shader "DeMuenu/World/Hoppou/Particles/LitParticles"
LightTypeCalculations(_LightColors, LightCounter, i, NdotL, dIntensity, _LightPositions[LightCounter].a, _LightPositions[LightCounter].xyz);
dmax = dmax + contrib * float4(LightColor, 1) * NdotL; // accumulate light contributions
}
//dmax.xyz = min(dmax * dIntensity, 1.0);
dmax.w = 1.0;
dmax = dmax;
//MoonsLight END
//Moonlight END
return col * _Color * min(dmax, 1.0) * i.color;

View File

@@ -1,90 +0,0 @@
#ifndef MOONSLIGHT_INCLUDED
#define MOONSLIGHT_INCLUDED
// You can override this before including if TEXCOORD2 is taken:
// #define MOONSLIGHT_TEXCOORD TEXCOORD4
#ifndef MOONSLIGHT_TEXCOORD
#define MOONSLIGHT_TEXCOORD TEXCOORD2
#endif
// Pick a safe max for your target hardware.
#ifndef MAX_LIGHTS
#define MAX_LIGHTS 80
#endif
#include "UnityCG.cginc"
// ---------- Uniforms (set from script as Global/Material arrays) ----------
uniform float4 _LightPositions[MAX_LIGHTS]; // xyz = pos, w = radius
uniform float4 _LightColors[MAX_LIGHTS]; // xyz = rgb (not normalized), w = intensity scalar
uniform float4 _LightDirections[MAX_LIGHTS]; // xyz = dir, w = half-angle in degrees (cone)
uniform float _LightType[MAX_LIGHTS]; // 0 = sphere, 1 = cone
uniform float _PlayerCount;
// ---------- Helpers you can inject into your v2f / vertex ----------
#define MOONSLIGHT_V2F_MEMBERS float3 worldPos : MOONSLIGHT_TEXCOORD;
inline void MoonsLight_FillV2F(float4 vertexOS, out float3 worldPos)
{
worldPos = mul(unity_ObjectToWorld, vertexOS).xyz;
}
// ---------- Core lighting (return value is 0..1 rgba contribution) ----------
inline float4 MoonsLight_Accumulate(float3 worldPos, float3 worldNormal)
{
float3 N = normalize(worldNormal);
float4 accum = float4(0,0,0,1);
float dIntensity = 0.0;
int count = (int)_PlayerCount;
[loop]
for (int idx = 0; idx < MAX_LIGHTS; idx++)
{
if (idx >= count) break;
float3 q = _LightPositions[idx].xyz;
float radius = _LightPositions[idx].w;
float3 L = normalize(q - worldPos);
float NdotL = saturate(dot(N, L) * 0.5 + 0.5); // one-sided Lambert
float contrib = 0.0;
if (_LightType[idx] == 0.0)
{
// Sphere (SDF-ish falloff reused from your code)
float sd = length(worldPos - q) - radius;
sd = lerp(1.0, -sd, step(0.0, sd));
contrib = min(1.0, max(max(sd, max(0.01, _LightColors[idx].a) / (sd * sd)), 0.01));
dIntensity += contrib * NdotL;
}
else
{
// Cone (directional spot)
float threshold = (-1 + _LightDirections[idx].w / 180.0);
contrib = min(dot(normalize(worldPos - q), -normalize(_LightDirections[idx].xyz)), 0.0);
contrib = 1.0 - step(threshold, contrib);
float distanceFromLight = length(worldPos - q);
contrib = min(1.0, contrib * (max(0.01, _LightColors[idx].a) / (distanceFromLight * distanceFromLight)));
dIntensity += contrib * NdotL;
}
float3 lightRgb = normalize(_LightColors[idx].xyz);
accum += contrib * float4(lightRgb, 1.0);
}
accum.xyz = normalize(accum.xyz);
accum.xyz = min(accum.xyz * dIntensity, 1.0);
accum.w = 1.0;
return min(accum, 1.0);
}
// Optional: reuse your camera-distance fade
inline float4 MoonsLight_ApplyDistanceFade(float4 color, float3 worldPos, float distanceFadeMultiplier, float distanceMin)
{
float dist = length(_WorldSpaceCameraPos.xyz - worldPos) / 100.0;
color.xyz *= min(1.0, max(distanceMin, max(0.0, 1.0 - abs(dist) * distanceFadeMultiplier)));
return color;
}
#endif // MOONSLIGHT_INCLUDED

View File

@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 76476ee372212af4e9094c2e42bc3cc5
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -11,7 +11,7 @@ Shader "DeMuenu/World/Hoppou/Water"
_NormalMapScrollSpeed ("Normal Map Scroll Speed", Float) = 0.1
_NormalMapScrollSpeed2 ("Normal Map 2 Scroll Speed", Float) = 0.05
//MoonsLight
//Moonlight
_InverseSqareMultiplier ("Inverse Square Multiplier", Float) = 1
_LightCutoffDistance ("Light Cutoff Distance", Float) = 100
@@ -22,7 +22,7 @@ Shader "DeMuenu/World/Hoppou/Water"
_F0 ("F0", Range(0,1)) = 0.02
_FresnelPower ("Fresnel Power", Range(1,8)) = 5
_ReflectionStrength ("Reflection Strength", Range(0,1)) = 0.7
//MoonsLight END
//Moonlight END
_WaveInput ("Wave Input", 2D) = "black" {}
_WaveTex ("Wave Texture", 2D) = "black" {}
@@ -49,10 +49,11 @@ Shader "DeMuenu/World/Hoppou/Water"
#include "Includes/LightStrength.hlsl"
#include "Includes/Lambert.hlsl"
#include "Includes/DefaultSetup.hlsl"
#include "Includes/Variables.hlsl"
//MoonsLight Defines
//Moonlight Defines
#define MAX_LIGHTS 80 // >= maxPlayers in script
//MoonsLight Defines END
//Moonlight Defines END
struct appdata
{
@@ -67,10 +68,10 @@ Shader "DeMuenu/World/Hoppou/Water"
float2 uvnorm : TEXCOORD1;
float4 vertex : SV_POSITION;
//MoonsLight
//Moonlight
float3 worldPos : TEXCOORD2;
float3 worldNormal: TEXCOORD3;
//MoonsLight END
//Moonlight END
};
sampler2D _MainTex;
@@ -84,16 +85,8 @@ Shader "DeMuenu/World/Hoppou/Water"
float _NormalMapScrollSpeed;
float _NormalMapScrollSpeed2;
//MoonsLight variables
float _InverseSqareMultiplier;
float _LightCutoffDistance;
float4 _LightPositions[MAX_LIGHTS]; // xyz = position
float4 _LightColors[MAX_LIGHTS]; // xyz = position
float4 _LightDirections[MAX_LIGHTS]; // xyz = direction, w = cos(halfAngle)
float _LightType[MAX_LIGHTS]; // 0 = sphere, 1 = cone
float _PlayerCount; // set via SetFloat
MoonlightGlobalVariables
//Watershader specific
float _SpecPower, _SpecIntensity;
@@ -116,7 +109,7 @@ Shader "DeMuenu/World/Hoppou/Water"
float f = pow(saturate(1.0 - NoV), power);
return saturate(F0 + (1.0 - F0) * f);
}
//MoonsLight variables END
//Moonlight variables END
v2f vert (appdata v)
{
@@ -124,11 +117,11 @@ Shader "DeMuenu/World/Hoppou/Water"
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uvnorm = TRANSFORM_TEX(v.uv, _NormalMap);
//MoonsLight Vertex
//Moonlight Vertex
float4 wp = mul(unity_ObjectToWorld, v.vertex);
o.worldPos = wp.xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//MoonsLight Vertex END
//Moonlight Vertex END
return o;
}
@@ -151,9 +144,7 @@ Shader "DeMuenu/World/Hoppou/Water"
//i.worldPos += float3(0, Wave.g * _WaveScale, 0);
//MoonsLight
int count = (int)_PlayerCount;
//Moonlight
float3 N = normalize(i.worldNormal + NormalOffset1 * _NormalMapStrength1 + NormalOffset2 * _NormalMapStrength2 + Wave * _WaveScale); //for lambertian diffuse
@@ -161,14 +152,9 @@ Shader "DeMuenu/World/Hoppou/Water"
float3 V = normalize(_WorldSpaceCameraPos - i.worldPos);
float3 R = reflect(-V, N); //for reflection vector
//Waterspecific END
//return float4(R,1);
OutLoopSetup(i, _PlayerCount) //defines count, N, dmax, dIntensity
// Example: compute distance to nearest player
float4 dmax = float4(0,0,0,1);
float dIntensity = 0;
[loop]
for (int LightCounter = 0; LightCounter < MAX_LIGHTS; LightCounter++)
{
@@ -201,10 +187,7 @@ Shader "DeMuenu/World/Hoppou/Water"
dmax.w = 1.0;
dmax.a = dmax.a * _ReflectionStrength * fres;
//MoonsLight END
//Moonlight END
// Final color
return col * _Color * dmax ;