From b9bfd0b55921a380d8d1e0b770326d87dc3dbeb4 Mon Sep 17 00:00:00 2001 From: DeMuenu <96650288+DeMuenu@users.noreply.github.com> Date: Wed, 24 Sep 2025 17:51:23 +0200 Subject: [PATCH 1/4] Refactor light calculations into LightStrength.hlsl include Moved light type calculation logic from BlendinShader.shader to a new include file, LightStrength.hlsl, for better modularity and maintainability. Updated the shader to use the new macro for light calculations, improving code clarity and reducing duplication. --- Shader/BlendinShader.shader | 32 +++++++----------------------- Shader/Includes/LightStrength.hlsl | 23 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 25 deletions(-) create mode 100644 Shader/Includes/LightStrength.hlsl diff --git a/Shader/BlendinShader.shader b/Shader/BlendinShader.shader index a744825..9db31bd 100644 --- a/Shader/BlendinShader.shader +++ b/Shader/BlendinShader.shader @@ -32,6 +32,7 @@ Shader "DeMuenu/World/Hoppou/RevealStandart" #pragma fragment frag #include "UnityCG.cginc" + #include "Includes/LightStrength.hlsl" //MoonsLight Defines #define MAX_LIGHTS 80 // >= maxPlayers in script @@ -122,13 +123,12 @@ Shader "DeMuenu/World/Hoppou/RevealStandart" 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; + if (LightCounter >= count) break; + float3 Lightposition = _LightPositions[LightCounter].xyz; - float distanceFromLight = length(i.worldPos - q); + float distanceFromLight = length(i.worldPos - Lightposition); if (distanceFromLight > _LightCutoffDistance) continue; float sd = 0.0; @@ -139,29 +139,11 @@ Shader "DeMuenu/World/Hoppou/RevealStandart" //Lambertian diffuse - float3 L = normalize(q - i.worldPos); // q = light position + 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; - 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; + LightTypeCalculations(_LightColors, LightCounter, i, NdotL, dIntensity, _LightPositions[LightCounter].a, _LightPositions[LightCounter].xyz); diff --git a/Shader/Includes/LightStrength.hlsl b/Shader/Includes/LightStrength.hlsl new file mode 100644 index 0000000..ce1977f --- /dev/null +++ b/Shader/Includes/LightStrength.hlsl @@ -0,0 +1,23 @@ +#ifndef LightTypeCalculations +#define LightTypeCalculations(_LightColors ,LightCounter, i, NdotL, dIntensity, radius, Lightposition) \ + 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)); \ + \ + dIntensity += contrib * NdotL; \ + } \ + else if (_LightType[LightCounter] == 1) \ + { \ + float invSq = _LightColors[LightCounter].a / max(1e-4, (distanceFromLight * invSqMul) * (distanceFromLight * invSqMul)); \ + float threshold = (-1 + _LightDirections[LightCounter].w / 180); \ + \ + contrib = min(dot(normalize(i.worldPos - Lightposition), -normalize(_LightDirections[LightCounter].xyz)), 0); \ + contrib= 1 - step(threshold, contrib); \ + \ + contrib = contrib * invSq; \ + dIntensity += contrib * NdotL; \ + } \ + float3 LightColor = _LightColors[LightCounter].xyz; \ + + +#endif \ No newline at end of file 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 2/4] 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 From 0f049de06299da4772d65c7736df27a4192754b3 Mon Sep 17 00:00:00 2001 From: DeMuenu <96650288+DeMuenu@users.noreply.github.com> Date: Thu, 25 Sep 2025 16:09:24 +0200 Subject: [PATCH 3/4] 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. --- Shader/BlendinShader.shader | 48 ++++++----------- Shader/Includes/DefaultSetup.hlsl | 8 +++ Shader/Includes/Variables.hlsl | 13 +++++ Shader/LitParticles.shader | 46 +++++----------- Shader/MoonsLight.cingc | 90 ------------------------------- Shader/MoonsLight.cingc.meta | 7 --- Shader/Water.shader | 47 ++++++---------- 7 files changed, 64 insertions(+), 195 deletions(-) create mode 100644 Shader/Includes/Variables.hlsl delete mode 100644 Shader/MoonsLight.cingc delete mode 100644 Shader/MoonsLight.cingc.meta diff --git a/Shader/BlendinShader.shader b/Shader/BlendinShader.shader index 466a621..1389553 100644 --- a/Shader/BlendinShader.shader +++ b/Shader/BlendinShader.shader @@ -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; } diff --git a/Shader/Includes/DefaultSetup.hlsl b/Shader/Includes/DefaultSetup.hlsl index 580cf63..9ee1e01 100644 --- a/Shader/Includes/DefaultSetup.hlsl +++ b/Shader/Includes/DefaultSetup.hlsl @@ -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 \ No newline at end of file diff --git a/Shader/Includes/Variables.hlsl b/Shader/Includes/Variables.hlsl new file mode 100644 index 0000000..c994e75 --- /dev/null +++ b/Shader/Includes/Variables.hlsl @@ -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 \ No newline at end of file diff --git a/Shader/LitParticles.shader b/Shader/LitParticles.shader index 61d82ce..7a347a8 100644 --- a/Shader/LitParticles.shader +++ b/Shader/LitParticles.shader @@ -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; diff --git a/Shader/MoonsLight.cingc b/Shader/MoonsLight.cingc deleted file mode 100644 index 3da0818..0000000 --- a/Shader/MoonsLight.cingc +++ /dev/null @@ -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 diff --git a/Shader/MoonsLight.cingc.meta b/Shader/MoonsLight.cingc.meta deleted file mode 100644 index f32c089..0000000 --- a/Shader/MoonsLight.cingc.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 76476ee372212af4e9094c2e42bc3cc5 -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Shader/Water.shader b/Shader/Water.shader index 61a98bd..5c318b6 100644 --- a/Shader/Water.shader +++ b/Shader/Water.shader @@ -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 ; From dc939d0ef6ea42992fa1310a8b7ed4331940adb5 Mon Sep 17 00:00:00 2001 From: DeMuenu <96650288+DeMuenu@users.noreply.github.com> Date: Fri, 26 Sep 2025 20:11:49 +0200 Subject: [PATCH 4/4] Add WaterParticle shader and minor water shader tweaks Introduced a new WaterParticle shader for rendering water particles with alpha blending. Made minor formatting and calculation adjustments in Water.shader and DefaultSetup.hlsl, and updated Variables.hlsl. Added corresponding .meta files for new shader assets. --- Shader/Includes/DefaultSetup.hlsl | 11 ++--- Shader/Includes/Variables.hlsl | 1 + Shader/Includes/Variables.hlsl.meta | 7 ++++ Shader/Water.shader | 6 +-- Shader/WaterParticle.shader | 62 +++++++++++++++++++++++++++++ Shader/WaterParticle.shader.meta | 9 +++++ 6 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 Shader/Includes/Variables.hlsl.meta create mode 100644 Shader/WaterParticle.shader create mode 100644 Shader/WaterParticle.shader.meta diff --git a/Shader/Includes/DefaultSetup.hlsl b/Shader/Includes/DefaultSetup.hlsl index 9ee1e01..7879d1a 100644 --- a/Shader/Includes/DefaultSetup.hlsl +++ b/Shader/Includes/DefaultSetup.hlsl @@ -4,10 +4,9 @@ \ float distanceFromLight = length(i.worldPos - _LightPositions[LightCounter].xyz); \ if (distanceFromLight > _LightCutoffDistance) continue; \ - \ - float contrib = 0.0; \ - - + \ + float contrib = 0.0; + #endif #ifndef OutLoopSetup @@ -15,5 +14,7 @@ int count = (int)_PlayerCount; \ \ float4 dmax = float4(0,0,0,1); \ - float dIntensity = 0; \ + float dIntensity = 0; + + #endif \ No newline at end of file diff --git a/Shader/Includes/Variables.hlsl b/Shader/Includes/Variables.hlsl index c994e75..bbdf1e2 100644 --- a/Shader/Includes/Variables.hlsl +++ b/Shader/Includes/Variables.hlsl @@ -10,4 +10,5 @@ float _LightType[MAX_LIGHTS]; /* 0 = sphere, 1 = cone */ \ float _PlayerCount; /* set via SetFloat */ \ \ + #endif \ No newline at end of file diff --git a/Shader/Includes/Variables.hlsl.meta b/Shader/Includes/Variables.hlsl.meta new file mode 100644 index 0000000..d5c5ed0 --- /dev/null +++ b/Shader/Includes/Variables.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c46c91c88f0f9954b9d65378c73103a5 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Shader/Water.shader b/Shader/Water.shader index 5c318b6..36e9e83 100644 --- a/Shader/Water.shader +++ b/Shader/Water.shader @@ -146,7 +146,7 @@ Shader "DeMuenu/World/Hoppou/Water" //Moonlight float3 N = normalize(i.worldNormal + NormalOffset1 * _NormalMapStrength1 + NormalOffset2 * _NormalMapStrength2 + Wave * _WaveScale); //for lambertian diffuse - + //Waterspecific float3 V = normalize(_WorldSpaceCameraPos - i.worldPos); @@ -162,7 +162,7 @@ Shader "DeMuenu/World/Hoppou/Water" Lambert(_LightPositions[LightCounter].xyz ,i, N); - LightTypeCalculations(_LightColors, LightCounter, i, NdotL, dIntensity, _LightPositions[LightCounter].a, _LightPositions[LightCounter].xyz); + LightTypeCalculations(_LightColors, LightCounter, i, 1, dIntensity, _LightPositions[LightCounter].a, _LightPositions[LightCounter].xyz); @@ -185,7 +185,7 @@ Shader "DeMuenu/World/Hoppou/Water" float fres = SchlickFresnel(NoV, _F0, _FresnelPower); dmax.w = 1.0; - dmax.a = dmax.a * _ReflectionStrength * fres; + dmax.a = dmax.a * _ReflectionStrength * fres; //Moonlight END diff --git a/Shader/WaterParticle.shader b/Shader/WaterParticle.shader new file mode 100644 index 0000000..097aba7 --- /dev/null +++ b/Shader/WaterParticle.shader @@ -0,0 +1,62 @@ +Shader "DeMuenu/World/Hoppou/WaterParticle" +{ + Properties + { + _MainTex ("Texture", 2D) = "white" {} + _AlphaMap ("Alpha Map", 2D) = "white" {} + _Color ("Tint", Color) = (1,1,1,1) + } + SubShader + { + Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" } + Blend SrcAlpha OneMinusSrcAlpha // normal alpha blend (not additive) + ZWrite Off + Lighting Off + Cull Off + LOD 100 + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + #include "UnityCG.cginc" + + struct appdata + { + float4 vertex : POSITION; + float2 uv : TEXCOORD0; + float4 color : COLOR; + }; + + struct v2f + { + float2 uv : TEXCOORD0; + float4 vertex : SV_POSITION; + float4 color : COLOR; + }; + + sampler2D _MainTex; + float4 _MainTex_ST; + sampler2D _AlphaMap; + fixed4 _Color; + + v2f vert (appdata v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.uv = TRANSFORM_TEX(v.uv, _MainTex); + o.color = v.color; + return o; + } + + fixed4 frag (v2f i) : SV_Target + { + fixed4 col = tex2D(_MainTex, i.uv) * _Color; + float alpha = tex2D(_AlphaMap, i.uv).r; + return col * alpha * i.color; // col.a drives the blend + } + ENDCG + } + } +} diff --git a/Shader/WaterParticle.shader.meta b/Shader/WaterParticle.shader.meta new file mode 100644 index 0000000..55d9267 --- /dev/null +++ b/Shader/WaterParticle.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 257e74905e73c3a40bcd4a4b35d3d647 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: