From 07730827f4e895f21756a3e6265740ed176fdceb Mon Sep 17 00:00:00 2001 From: DeMuenu <96650288+DeMuenu@users.noreply.github.com> Date: Thu, 25 Sep 2025 02:21:26 +0200 Subject: [PATCH] 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. --- README.md.meta | 7 +++ Shader/BlendinShader.shader | 18 ++---- Shader/Includes.meta | 8 +++ Shader/Includes/DefaultSetup.hlsl | 11 ++++ Shader/Includes/DefaultSetup.hlsl.meta | 7 +++ Shader/Includes/Lambert.hlsl | 7 +++ Shader/Includes/Lambert.hlsl.meta | 7 +++ Shader/Includes/LightStrength.hlsl | 2 + Shader/Includes/LightStrength.hlsl.meta | 7 +++ Shader/LitParticles.shader | 42 +++----------- Shader/Water.shader | 73 ++++++++++++------------- 11 files changed, 103 insertions(+), 86 deletions(-) create mode 100644 README.md.meta create mode 100644 Shader/Includes.meta create mode 100644 Shader/Includes/DefaultSetup.hlsl create mode 100644 Shader/Includes/DefaultSetup.hlsl.meta create mode 100644 Shader/Includes/Lambert.hlsl create mode 100644 Shader/Includes/Lambert.hlsl.meta create mode 100644 Shader/Includes/LightStrength.hlsl.meta diff --git a/README.md.meta b/README.md.meta new file mode 100644 index 0000000..2876d37 --- /dev/null +++ b/README.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 93fa212d3a1466d4aa137657d2a1a25e +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Shader/BlendinShader.shader b/Shader/BlendinShader.shader index 9db31bd..466a621 100644 --- a/Shader/BlendinShader.shader +++ b/Shader/BlendinShader.shader @@ -33,6 +33,8 @@ Shader "DeMuenu/World/Hoppou/RevealStandart" #include "UnityCG.cginc" #include "Includes/LightStrength.hlsl" + #include "Includes/Lambert.hlsl" + #include "Includes/DefaultSetup.hlsl" //MoonsLight Defines #define MAX_LIGHTS 80 // >= maxPlayers in script @@ -125,23 +127,11 @@ Shader "DeMuenu/World/Hoppou/RevealStandart" [loop] for (int LightCounter = 0; LightCounter < MAX_LIGHTS; LightCounter++) { - if (LightCounter >= count) break; - float3 Lightposition = _LightPositions[LightCounter].xyz; + InLoopSetup(_LightPositions, LightCounter, count, i); //defines distanceFromLight, contrib - float distanceFromLight = length(i.worldPos - Lightposition); - if (distanceFromLight > _LightCutoffDistance) continue; - float sd = 0.0; - float contrib = 0.0; - - - float invSqMul = max(1e-4, _InverseSqareMultiplier); - - //Lambertian diffuse - float3 L = normalize(Lightposition - i.worldPos); // Lightposition = light position - float NdotL = saturate(dot(N, L) * 0.5 + 0.5); // one-sided Lambert - if (NdotL <= 0) continue; + Lambert(_LightPositions[LightCounter].xyz ,i, N); //defines NdotL LightTypeCalculations(_LightColors, LightCounter, i, NdotL, dIntensity, _LightPositions[LightCounter].a, _LightPositions[LightCounter].xyz); diff --git a/Shader/Includes.meta b/Shader/Includes.meta new file mode 100644 index 0000000..bff60bb --- /dev/null +++ b/Shader/Includes.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d8ef6797851b2e446a3f1febe3a82d75 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Shader/Includes/DefaultSetup.hlsl b/Shader/Includes/DefaultSetup.hlsl new file mode 100644 index 0000000..580cf63 --- /dev/null +++ b/Shader/Includes/DefaultSetup.hlsl @@ -0,0 +1,11 @@ +#ifndef InLoopSetup +#define InLoopSetup(_LightPositions, LightCounter, count, i) \ + if (LightCounter >= count) break; \ + \ + float distanceFromLight = length(i.worldPos - _LightPositions[LightCounter].xyz); \ + if (distanceFromLight > _LightCutoffDistance) continue; \ + \ + float contrib = 0.0; \ + + +#endif \ No newline at end of file diff --git a/Shader/Includes/DefaultSetup.hlsl.meta b/Shader/Includes/DefaultSetup.hlsl.meta new file mode 100644 index 0000000..7502ca4 --- /dev/null +++ b/Shader/Includes/DefaultSetup.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 40344ee82b97c734b9f8cd0ce21e57a7 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Shader/Includes/Lambert.hlsl b/Shader/Includes/Lambert.hlsl new file mode 100644 index 0000000..1a8d7df --- /dev/null +++ b/Shader/Includes/Lambert.hlsl @@ -0,0 +1,7 @@ +#ifndef Lambert +#define Lambert(q ,i, N) \ + 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; \ + +#endif \ No newline at end of file diff --git a/Shader/Includes/Lambert.hlsl.meta b/Shader/Includes/Lambert.hlsl.meta new file mode 100644 index 0000000..a534274 --- /dev/null +++ b/Shader/Includes/Lambert.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 095e42d7e6cefba45aaf73b9994b97a6 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Shader/Includes/LightStrength.hlsl b/Shader/Includes/LightStrength.hlsl index ce1977f..fac7e2d 100644 --- a/Shader/Includes/LightStrength.hlsl +++ b/Shader/Includes/LightStrength.hlsl @@ -1,5 +1,7 @@ #ifndef LightTypeCalculations #define LightTypeCalculations(_LightColors ,LightCounter, i, NdotL, dIntensity, radius, Lightposition) \ + float invSqMul = max(1e-4, _InverseSqareMultiplier); \ + \ if(_LightType[LightCounter] == 0) \ { \ contrib = _LightColors[LightCounter].a / max(1e-4, max(0, max(1, distanceFromLight - radius) * invSqMul) * max(0, max(1, distanceFromLight - radius) * invSqMul)); \ diff --git a/Shader/Includes/LightStrength.hlsl.meta b/Shader/Includes/LightStrength.hlsl.meta new file mode 100644 index 0000000..ae1ead1 --- /dev/null +++ b/Shader/Includes/LightStrength.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2b5943a3997186745ad720993e483310 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Shader/LitParticles.shader b/Shader/LitParticles.shader index 4ebc479..61d82ce 100644 --- a/Shader/LitParticles.shader +++ b/Shader/LitParticles.shader @@ -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; diff --git a/Shader/Water.shader b/Shader/Water.shader index 8dcaccf..61a98bd 100644 --- a/Shader/Water.shader +++ b/Shader/Water.shader @@ -23,6 +23,14 @@ Shader "DeMuenu/World/Hoppou/Water" _FresnelPower ("Fresnel Power", Range(1,8)) = 5 _ReflectionStrength ("Reflection Strength", Range(0,1)) = 0.7 //MoonsLight END + + _WaveInput ("Wave Input", 2D) = "black" {} + _WaveTex ("Wave Texture", 2D) = "black" {} + _CameraScale ("Camera Scale", Float) = 15 + _CameraPositionZ ("Camera Position Z", Float) = 0 + _CameraPositionX ("Camera Position X", Float) = 0 + + _WaveScale ("Wave Scale", Range(0.001, 100)) = 1 } SubShader { @@ -38,6 +46,9 @@ Shader "DeMuenu/World/Hoppou/Water" #pragma fragment frag #include "UnityCG.cginc" + #include "Includes/LightStrength.hlsl" + #include "Includes/Lambert.hlsl" + #include "Includes/DefaultSetup.hlsl" //MoonsLight Defines #define MAX_LIGHTS 80 // >= maxPlayers in script @@ -88,6 +99,15 @@ Shader "DeMuenu/World/Hoppou/Water" float _SpecPower, _SpecIntensity; float3 _AmbientFloor; + sampler2D _WaveInput; + sampler2D _WaveTex; + float2 _WaveTex_ST; + float _CameraScale; + float _CameraPositionZ; + float _CameraPositionX; + float _WaveScale; + //Watershader specific END + float _F0, _FresnelPower, _ReflectionStrength; @@ -121,10 +141,20 @@ Shader "DeMuenu/World/Hoppou/Water" float3 NormalOffset1 = UnpackNormal(norm).xyz; float3 NormalOffset2 = UnpackNormal(norm2).xyz; + float2 waveUV = float2(_CameraPositionX - i.worldPos.x, _CameraPositionZ - i.worldPos.z) / _CameraScale / 2 + 0.5; + fixed4 Wave = tex2D(_WaveInput, waveUV); + if ((waveUV.x < 0.1) || (waveUV.x > 0.9) || (waveUV.y < 0.1) || (waveUV.y > 0.9)){ + Wave = float4(0,0,0,0); + } + + //i.vertex += float4(0, Wave.g * _WaveScale, 0, 0); + //i.worldPos += float3(0, Wave.g * _WaveScale, 0); + + //MoonsLight int count = (int)_PlayerCount; - float3 N = normalize(i.worldNormal + NormalOffset1 * _NormalMapStrength1 + NormalOffset2 * _NormalMapStrength2); //for lambertian diffuse + float3 N = normalize(i.worldNormal + NormalOffset1 * _NormalMapStrength1 + NormalOffset2 * _NormalMapStrength2 + Wave * _WaveScale); //for lambertian diffuse //Waterspecific @@ -140,53 +170,22 @@ Shader "DeMuenu/World/Hoppou/Water" 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 + Lambert(_LightPositions[LightCounter].xyz ,i, N); - float invSqMul = max(1e-4, _InverseSqareMultiplier); - + LightTypeCalculations(_LightColors, LightCounter, i, NdotL, dIntensity, _LightPositions[LightCounter].a, _LightPositions[LightCounter].xyz); - //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; - 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; //Watershader specific //float fres = Schlick(saturate(dot(N, V)), _F0, _FresnelPower); float3 R = reflect(-V, N); float spec = pow(saturate(dot(R, L)), _SpecPower); //return float4(spec, spec, spec,1); - dmax.rgb += _LightColors[idx].rgb * contrib + _LightColors[idx].rgb * _SpecIntensity * spec * contrib; + dmax.rgb += _LightColors[LightCounter].rgb * contrib + _LightColors[LightCounter].rgb * _SpecIntensity * spec * contrib; dmax.a -= _SpecIntensity * spec; //dmax = dmax + contrib * float4(LightColor, 1); // accumulate light contributions