diff --git a/Packages/zzwater/Mats/st.mat b/Packages/zzwater/Mats/HorizenOcean.mat similarity index 62% rename from Packages/zzwater/Mats/st.mat rename to Packages/zzwater/Mats/HorizenOcean.mat index b7325c2..f0c3afd 100644 --- a/Packages/zzwater/Mats/st.mat +++ b/Packages/zzwater/Mats/HorizenOcean.mat @@ -7,8 +7,8 @@ Material: m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_Name: st - m_Shader: {fileID: 4800000, guid: 5e1bb555fbe9641c1b49b207771e3641, type: 3} + m_Name: HorizenOcean + m_Shader: {fileID: 4800000, guid: 838018c478c7946c7a600a5350e7d4e1, type: 3} m_Parent: {fileID: 0} m_ModifiedSerializedProperties: 0 m_ValidKeywords: [] @@ -47,10 +47,6 @@ Material: m_Texture: {fileID: 0} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - - _IntersectionNoise: - m_Texture: {fileID: 2800000, guid: 6e4f6abf9612841adaf37e0d026dd5a4, type: 3} - m_Scale: {x: 1, y: 1} - m_Offset: {x: 0, y: 0} - _MainTex: m_Texture: {fileID: 0} m_Scale: {x: 1, y: 1} @@ -94,78 +90,36 @@ Material: - _ClearCoatSmoothness: 0 - _Cull: 2 - _Cutoff: 0.5 - - _DepthHorizontal: 1.78 - - _DepthVertical: 7.44 - _DetailAlbedoMapScale: 1 - _DetailNormalMapScale: 1 - _DstBlend: 0 - _DstBlendAlpha: 0 - - _EdgeFade: 19.08 - _EnvironmentReflections: 1 - _GlossMapScale: 0 - _Glossiness: 0 - _GlossyReflections: 0 - - _HorizonDistance: 26.6 - - _InterscetionSpeed: 1 - - _IntersectionClipping: 0.553 - - _IntersectionDurationMulti: 1 - - _IntersectionDurationMulti1: 2.53 - - _IntersectionDurationMulti2: 0.83 - - _IntersectionFalloff: 1 - - _IntersectionLength: 2.03 - - _IntersectionSpeed: 0.2 - - _IntersectionTiling: 1 - _Metallic: 0 - _OcclusionStrength: 1 - _Parallax: 0.005 - _QueueOffset: 0 - _ReceiveShadows: 1 - - _ReflectionBlur: 0.049 - - _ReflectionFresnel: 4.92 - - _ReflectionStrength: 1 - - _RefractionStrength: 0.213 + - _ReflectionBlur: 0.01 + - _ReflectionStrength: 0 - _Smoothness: 0.5 - _SmoothnessTextureChannel: 0 - _SpecularHighlights: 1 - - _Speed: 1 - _SrcBlend: 1 - _SrcBlendAlpha: 1 - _Surface: 0 - - _TessMax: 15 - - _TessMin: 0 - - _TessValue: 16 - - _TranslucencyCurvatureMask: 0.708 - - _TranslucencyExp: 10 - - _TranslucencyReflectionMask: 0.764 - - _TranslucencyStrength: 0.7 - - _TranslucencyStrengthDirect: 0.443 - - _WaveCount: 2 - - _WaveDistance: 0.808 - - _WaveFoamBaseLvl: -2.66 - - _WaveFoamClipping: 0.328 - - _WaveFoamExp: 5 - - _WaveFoamLength: 1 - - _WaveHeight: 0.23 - - _WaveMesureMax: -2.51 - - _WaveMesureMin: -2.68 - - _WaveNormalStr: 0.4 - - _WaveSpeed: 1 - - _WaveSteepness: 3.65 - _WorkflowMode: 1 - _ZWrite: 1 m_Colors: - - _BaseColor: {r: 0.01619794, g: 0.20062576, b: 0.26415092, a: 0.9490196} - - _Color: {r: 1, g: 1, b: 1, a: 1} - - _Direction: {r: 0.66, g: -1, b: 0, a: 0} + - _BaseColor: {r: 1, g: 1, b: 1, a: 1} + - _Color: {r: 0.9716981, g: 0.9716981, b: 0.9716981, a: 1} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} - - _HorizonColor: {r: 0.09376113, g: 0.10980392, b: 0.32156864, a: 1} - - _IntersectionColor: {r: 0.9622642, g: 0.9577252, b: 0.9577252, a: 1} - - _ShallowColor: {r: 0.105882354, g: 0.4117647, b: 0.62352943, a: 0.9372549} - - _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1} - - _WaveDirection: {r: 1, g: 1, b: 1, a: 2} - - _WaveFadeDistance: {r: 0, g: 58.3, b: 0, a: 0} + - _SpecColor: {r: 0.2, g: 0.2, b: 0.2, a: 1} m_BuildTextureStacks: [] ---- !u!114 &1635733923474585344 +--- !u!114 &2783795249461312039 MonoBehaviour: m_ObjectHideFlags: 11 m_CorrespondingSourceObject: {fileID: 0} diff --git a/Packages/zzwater/Mats/st.mat.meta b/Packages/zzwater/Mats/HorizenOcean.mat.meta similarity index 79% rename from Packages/zzwater/Mats/st.mat.meta rename to Packages/zzwater/Mats/HorizenOcean.mat.meta index de53fb4..98f1ca8 100644 --- a/Packages/zzwater/Mats/st.mat.meta +++ b/Packages/zzwater/Mats/HorizenOcean.mat.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 3bf295bb1b91a44bbbf283790912ea32 +guid: cd62425fd26814f6bbc99dec8c15104f NativeFormatImporter: externalObjects: {} mainObjectFileID: 2100000 diff --git a/Packages/zzwater/Mats/Ocean.mat b/Packages/zzwater/Mats/Ocean.mat index 3b2e376..324ae87 100644 --- a/Packages/zzwater/Mats/Ocean.mat +++ b/Packages/zzwater/Mats/Ocean.mat @@ -106,11 +106,11 @@ Material: - _Glossiness: 0 - _GlossyReflections: 0 - _HorizonDistance: 26.6 - - _IntersectionClipping: 0.917 + - _IntersectionClipping: 0.721 - _IntersectionDurationMulti1: 3.48 - _IntersectionDurationMulti2: 0.76 - - _IntersectionFalloff: 0.571 - - _IntersectionLength: 2.04 + - _IntersectionFalloff: 0.525 + - _IntersectionLength: 1.46 - _IntersectionSpeed: -0.2 - _IntersectionTiling: 1.3 - _Metallic: 0 @@ -136,10 +136,7 @@ Material: - _TranslucencyStrengthDirect: 0.421 - _WaveCount: 2 - _WaveDistance: 0.764 - - _WaveFoamClipping: 0.462 - _WaveHeight: 0.17 - - _WaveMesureMax: -2.62 - - _WaveMesureMin: -2.7 - _WaveNormalStr: 0.2 - _WaveSpeed: 1.2 - _WaveSteepness: 3.17 diff --git a/Packages/zzwater/Mats/Ocean_tess.mat b/Packages/zzwater/Mats/Ocean_tess.mat index 7a09c65..95cb11f 100644 --- a/Packages/zzwater/Mats/Ocean_tess.mat +++ b/Packages/zzwater/Mats/Ocean_tess.mat @@ -107,14 +107,14 @@ Material: - _GlossyReflections: 0 - _HorizonDistance: 26.6 - _InterscetionSpeed: 1 - - _IntersectionClipping: 0.553 + - _IntersectionClipping: 0.407 - _IntersectionDurationMulti: 1 - _IntersectionDurationMulti1: 2.53 - _IntersectionDurationMulti2: 0.83 - - _IntersectionFalloff: 1 + - _IntersectionFalloff: 0.73 - _IntersectionLength: 2.03 - _IntersectionSpeed: 0.2 - - _IntersectionTiling: 1 + - _IntersectionTiling: 0.82 - _Metallic: 0 - _OcclusionStrength: 1 - _Parallax: 0.005 @@ -123,7 +123,7 @@ Material: - _ReflectionBlur: 0.049 - _ReflectionFresnel: 4.92 - _ReflectionStrength: 1 - - _RefractionStrength: 0.213 + - _RefractionStrength: 0.278 - _Smoothness: 0.5 - _SmoothnessTextureChannel: 0 - _SpecularHighlights: 1 @@ -148,7 +148,7 @@ Material: - _WaveHeight: 0.23 - _WaveMesureMax: -2.51 - _WaveMesureMin: -2.68 - - _WaveNormalStr: 0.4 + - _WaveNormalStr: 0.2 - _WaveSpeed: 1 - _WaveSteepness: 3.65 - _WorkflowMode: 1 @@ -163,7 +163,7 @@ Material: - _ShallowColor: {r: 0.105882354, g: 0.4117647, b: 0.62352943, a: 0.9372549} - _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1} - _WaveDirection: {r: 1, g: 1, b: 1, a: 2} - - _WaveFadeDistance: {r: 0, g: 58.3, b: 0, a: 0} + - _WaveFadeDistance: {r: 100, g: 200, b: 0, a: 0} m_BuildTextureStacks: [] --- !u!114 &1635733923474585344 MonoBehaviour: diff --git a/Packages/zzwater/Mods/dis1water.zwatermesh b/Packages/zzwater/Mods/water_mesh_h.zwatermesh similarity index 100% rename from Packages/zzwater/Mods/dis1water.zwatermesh rename to Packages/zzwater/Mods/water_mesh_h.zwatermesh diff --git a/Packages/zzwater/Mods/dis1water.zwatermesh.meta b/Packages/zzwater/Mods/water_mesh_h.zwatermesh.meta similarity index 96% rename from Packages/zzwater/Mods/dis1water.zwatermesh.meta rename to Packages/zzwater/Mods/water_mesh_h.zwatermesh.meta index 9fdb84d..3cc195b 100644 --- a/Packages/zzwater/Mods/dis1water.zwatermesh.meta +++ b/Packages/zzwater/Mods/water_mesh_h.zwatermesh.meta @@ -10,7 +10,7 @@ ScriptedImporter: script: {fileID: 11500000, guid: eb1723d9808f34b8fa4c8c24d2a380d9, type: 3} waterMesh: shape: 0 - scale: 100 + scale: 180 vertexDistance: 1 UVTiling: 1 noise: 0 diff --git a/Packages/zzwater/Mods/dis2water.zwatermesh b/Packages/zzwater/Mods/water_mesh_l.zwatermesh similarity index 100% rename from Packages/zzwater/Mods/dis2water.zwatermesh rename to Packages/zzwater/Mods/water_mesh_l.zwatermesh diff --git a/Packages/zzwater/Mods/water_mesh_l.zwatermesh.meta b/Packages/zzwater/Mods/water_mesh_l.zwatermesh.meta new file mode 100644 index 0000000..d0414f9 --- /dev/null +++ b/Packages/zzwater/Mods/water_mesh_l.zwatermesh.meta @@ -0,0 +1,18 @@ +fileFormatVersion: 2 +guid: fb57683f9728c4705998b5ac260018d9 +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 11500000, guid: eb1723d9808f34b8fa4c8c24d2a380d9, type: 3} + waterMesh: + shape: 0 + scale: 90 + vertexDistance: 1 + UVTiling: 1 + noise: 0 + boundsPadding: 4 + mesh: {fileID: -8670151273213183110, guid: fb57683f9728c4705998b5ac260018d9, type: 3} diff --git a/Packages/zzwater/Mods/water_mesh_m.zwatermesh b/Packages/zzwater/Mods/water_mesh_m.zwatermesh new file mode 100644 index 0000000..e69de29 diff --git a/Packages/zzwater/Mods/dis2water.zwatermesh.meta b/Packages/zzwater/Mods/water_mesh_m.zwatermesh.meta similarity index 78% rename from Packages/zzwater/Mods/dis2water.zwatermesh.meta rename to Packages/zzwater/Mods/water_mesh_m.zwatermesh.meta index a8b78f6..2513387 100644 --- a/Packages/zzwater/Mods/dis2water.zwatermesh.meta +++ b/Packages/zzwater/Mods/water_mesh_m.zwatermesh.meta @@ -10,9 +10,9 @@ ScriptedImporter: script: {fileID: 11500000, guid: eb1723d9808f34b8fa4c8c24d2a380d9, type: 3} waterMesh: shape: 0 - scale: 100 + scale: 135 vertexDistance: 2 UVTiling: 1 noise: 0 boundsPadding: 4 - mesh: {fileID: -8670151273213183110, guid: 154c6496b574b495eaf1e56696d041c5, type: 3} + mesh: {fileID: -8670151273213183110, guid: f9a87e99a9bed4d78b46a38bbaaa03c4, type: 3} diff --git a/Packages/zzwater/shaders/horizon_ocean.shader b/Packages/zzwater/shaders/horizon_ocean.shader new file mode 100644 index 0000000..5a76f3d --- /dev/null +++ b/Packages/zzwater/shaders/horizon_ocean.shader @@ -0,0 +1,97 @@ +Shader "zzwater/horizon_ocean" +{ + + Properties + { + [HDR]_Color("Color", Color) = (0, 0.44, 0.62, 1) + _ReflectionStrength("Reflection Strength", Range(0, 2)) = 1 + _ReflectionBlur("Probe Blur Factor", Range(0, 1)) = 0 + } + + SubShader + { + Tags + { + "RenderType" = "Transparent" + "RenderPipeline" = "UniversalPipeline" + "UniversalMaterialType" = "Unlit" + "IgnoreProjector" = "True" + "Queue" = "Transparent+0" + } + + Pass + { + Name "ForwardLit" + Tags { "LightMode"="UniversalForward" } + + Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha + ZWrite On + Cull Back + ZTest LEqual + ZClip On + + HLSLPROGRAM + + #pragma vertex vert + #pragma fragment frag + + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" + + //Fog rendering (integration) + #define UnityFog + #pragma multi_compile_fog + + CBUFFER_START(UnityPerMaterial) + + float _ReflectionBlur; + float _ReflectionStrength; + half4 _Color; + + CBUFFER_END + + struct Attributes + { + float4 positionOS : POSITION; + }; + + struct Varyings + { + float4 positionCS : SV_POSITION; + float3 positionWS : TEXCOORD0; + float4 positionNDC : TEXCOORD1; + float fogFactor : TEXCOORD2; + }; + + Varyings vert(Attributes IN) + { + Varyings OUT; + + // Get the position of the vertex in different spaces + VertexPositionInputs positions = GetVertexPositionInputs(IN.positionOS); + + OUT.positionCS = positions.positionCS; + OUT.positionWS = positions.positionWS.xyz; + OUT.positionNDC = positions.positionNDC; + OUT.fogFactor = ComputeFogFactor(positions.positionCS.z); + + return OUT; + } + + half4 frag(Varyings IN) : SV_Target + { + float3 viewDir = normalize(_WorldSpaceCameraPos - IN.positionWS); + half3 reflectionVector = reflect(-viewDir, float3(0, 1, 0)); + float2 screenPos = IN.positionNDC.xy / IN.positionNDC.w; + float3 reflections = GlossyEnvironmentReflection(reflectionVector, IN.positionWS, _ReflectionBlur, 1 ,screenPos) * _ReflectionStrength; + + //Apply fog + half fogCoord = InitializeInputDataFog(float4(IN.positionWS,1), IN.fogFactor); + half3 finalColor = MixFog(_Color.rgb * reflections, fogCoord); + return half4(finalColor, _Color.a); + } + + ENDHLSL + } + } +} diff --git a/Packages/zzwater/shaders/horizon_ocean.shader.meta b/Packages/zzwater/shaders/horizon_ocean.shader.meta new file mode 100644 index 0000000..c6ea55c --- /dev/null +++ b/Packages/zzwater/shaders/horizon_ocean.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 838018c478c7946c7a600a5350e7d4e1 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/zzwater/shaders/libs/ForwardPass.hlsl b/Packages/zzwater/shaders/libs/ForwardPass.hlsl index 9d4d4aa..33f4d7d 100644 --- a/Packages/zzwater/shaders/libs/ForwardPass.hlsl +++ b/Packages/zzwater/shaders/libs/ForwardPass.hlsl @@ -5,46 +5,62 @@ struct SceneData float2 screenPos; //Normalized and no refraction float3 positionWS; float3 color; + float refractionMask; float viewDepth; float verticalDepth; }; +// Project from eye space to world space +#define WorldPosFromDepth(NDCw, depth, viewDir) -(depth.eye * (viewDir/NDCw) - _WorldSpaceCameraPos) +//Linear depth difference between scene and current (transparent) geometry pixel +#define SurfaceDepth(depth, positionCS) depth.eye - LinearEyeDepth(positionCS.z, _ZBufferParams) + + void PopulateSceneData(inout SceneData scene, in Varyings input, in WaterSurface water) { const float2 screenPos = input.positionNDC.xy / input.positionNDC.w; scene.positionNDC = input.positionNDC; scene.screenPos = screenPos; - //Default for disabled depth texture - scene.viewDepth = 1; + //Default for disabled depth texture scene.viewDepth = 1; scene.verticalDepth = 1; - float2 refractionOffset = input.normalWS.xz * 0.5 * _RefractionStrength + screenPos; + #ifdef _REFRACTION + SceneDepth depth = SampleDepth(screenPos); + float3 positionWS = WorldPosFromDepth(input.positionNDC.w, depth, water.viewDelta); + float viewDepth = SurfaceDepth(depth, input.positionCS); - //Screen depth Projection to world position - SceneDepth depth = SampleDepth(refractionOffset); - float3 projWorldPos = depth.eye * (water.viewDelta / input.positionNDC.w) - _WorldSpaceCameraPos; - scene.positionWS = -projWorldPos; + const float2 refractionOffset = input.normalWS.xz * 0.5 * _RefractionStrength; + SceneDepth depthRefract = SampleDepth(screenPos + refractionOffset); + float3 positionWSRefract = WorldPosFromDepth(input.positionNDC.w, depthRefract, water.viewDelta); + float viewDepthRefract = SurfaceDepth(depthRefract, input.positionCS); - const float sceneDepth = depth.eye; - const float clipSpaceDepth = LinearEyeDepth(input.positionCS.z, _ZBufferParams); - //Z-distance to opaque surface - scene.viewDepth = sceneDepth - clipSpaceDepth; + float mask = saturate(viewDepth) * saturate(viewDepthRefract); + scene.positionWS = lerp(positionWS, positionWSRefract, mask); + scene.viewDepth = lerp(viewDepth, viewDepthRefract, mask); //Distance to opaque geometry in normal direction scene.verticalDepth = length((water.positionWS - scene.positionWS) * water.vertexNormal); - scene.color = SampleSceneColor(refractionOffset); - + scene.color = SampleSceneColor(screenPos + refractionOffset * mask); + scene.refractionMask = mask; + #else + SceneDepth depth = SampleDepth(screenPos); + scene.positionWS = WorldPosFromDepth(input.positionNDC.w, depth, water.viewDelta); + scene.viewDepth = SurfaceDepth(depth, input.positionCS); + scene.verticalDepth = length((water.positionWS - scene.positionWS) * water.vertexNormal); + scene.color = SampleSceneColor(screenPos); + scene.refractionMask = 0; + #endif } float GetWaterDensity(SceneData scene, float heightScalar, float viewDepthScalar) { - float viewDepth = scene.viewDepth; - float verticalDepth = scene.verticalDepth; + const float viewDepth = scene.viewDepth; + const float verticalDepth = scene.verticalDepth; - float depthAttenuation = 1.0 - exp(-viewDepth * viewDepthScalar * 0.1); - float heightAttenuation = saturate(verticalDepth * heightScalar); + const float depthAttenuation = 1.0 - exp(-viewDepth * viewDepthScalar * 0.1); + const float heightAttenuation = saturate(verticalDepth * heightScalar); return max(depthAttenuation, heightAttenuation); } @@ -79,7 +95,7 @@ float4 ForwardPassFragment(Varyings input) : SV_Target PopulateSceneData(scene, input, water); //return float4(scene.verticalDepth.xxx, 1); - //return float4(scene.viewDepth.xxx, 1); + //return float4(saturate(scene.viewDepth).xxx, 1); //return float4(scene.color, 1); water.fog = GetWaterDensity(scene, _DepthHorizontal, _DepthVertical); @@ -95,43 +111,65 @@ float4 ForwardPassFragment(Varyings input) : SV_Target water.albedo = lerp(baseColor.rgb, _HorizonColor.rgb, fresnel); water.edgeFade = saturate(scene.verticalDepth / (_EdgeFade * 0.01)); -water.alpha *= water.edgeFade; + water.alpha *= water.edgeFade; // Wave Mesure - + #if _INTERSECTION || _WAVE_FOAM float waveMesure = (positionWS.y - _WaveMesureMin) / (_WaveMesureMax - _WaveMesureMin); float2 nUV = positionWS.xz * _IntersectionTiling; float foamTexNoise = SAMPLE_TEXTURE2D(_IntersectionNoise, sampler_IntersectionNoise, nUV + TIME_VERTEX/_IntersectionTiling * _IntersectionSpeed).r; + float interSecGradient = 1-saturate(exp(scene.verticalDepth) / _IntersectionLength); + #endif + + + #ifdef _WAVE_FOAM // remap wave from [_WaveFoamClipping,1] to [0,1] - float waveFoamNoise = (waveMesure - _WaveFoamClipping) / (1 - _WaveFoamClipping) * foamTexNoise; + //float waveFoamNoise = (waveMesure - _WaveFoamClipping) / (1 - _WaveFoamClipping) * foamTexNoise; + float waveFoamNoise = smoothstep(_WaveFoamClipping.x, _WaveFoamClipping.y, waveMesure * foamTexNoise); //return float4(waveFoamNoise.xxx, 1); + #else + float waveFoamNoise = 0; + #endif + /* ======== // Intercection =========== */ - - - float interSecGradient = 1-saturate(exp(scene.verticalDepth) / _IntersectionLength); + #ifdef _INTERSECTION float intersecMesure = waveMesure * interSecGradient; - float dist = saturate(interSecGradient / _IntersectionFalloff); - float intersecNoise = saturate(foamTexNoise + intersecMesure) * dist + dist; intersecNoise = smoothstep(_IntersectionClipping, 1, intersecNoise); + #else + float intersecNoise = 0; + #endif + #if _INTERSECTION || _WAVE_FOAM float noise = saturate(intersecNoise + waveFoamNoise); - //float noise = intersecNoise; - water.albedo.rgb = lerp(water.albedo.rgb, _IntersectionColor.rgb, noise); water.alpha = saturate(water.alpha + interSecGradient); + #else + float noise = 0; + #endif + /* ======== + // Causitcs + =========== */ - //water.intersection = SampleIntersection(positionWS, interSecGradient, TIME * _IntersectionSpeed) * _IntersectionColor.a; + #ifdef _CAUSTICS + float2 cuv = positionWS.xz * _CausticsTiling; + float coffset = TIME_VERTEX/_CausticsTiling * _CausticsSpeed; + float3 caustics1 = SAMPLE_TEXTURE2D(_CausticsTex, sampler_CausticsTex, cuv + coffset).rgb; + float3 caustics2 = SAMPLE_TEXTURE2D(_CausticsTex, sampler_CausticsTex, cuv*0.8 - coffset).rgb; + // Limit distance from cam and verticalDepth + float3 causticsDensity = saturate((1-length(water.viewDelta) / _CausticsClipping.x) * smoothstep(_CausticsClipping.y,1, scene.verticalDepth)); + float3 caustics = min(caustics1, caustics2) * _CausticsBrightness * causticsDensity; + #endif /* ======== // Reflection Prob @@ -161,23 +199,37 @@ water.alpha *= water.edgeFade; const float lightIntensity = lightColor.r * 0.3 + lightColor.g * 0.59 + lightColor.b * 0.11; - half attenuation = (incident) * scatteringMask * lightIntensity; + half attenuation = incident * scatteringMask * lightIntensity; - emission += lerp(emission, _ShallowColor.rgb, attenuation); + #ifdef _CAUSTICS + emission += lerp(caustics * lightIntensity, _ShallowColor.rgb, attenuation) * mainLight.distanceAttenuation; + #else + emission = lerp(0, _ShallowColor.rgb, attenuation) * mainLight.distanceAttenuation; + #endif // =====Translucency End======== + + #ifdef _LIGHT_COLOR // LightingLambert Light applied here half3 attenuatedLightColor = lightColor * (mainLight.distanceAttenuation * mainLight.shadowAttenuation); - half3 diffuseColor = LightingLambert(attenuatedLightColor, lightDir, normalWS); + half3 diffuseColor = LightingLambert(attenuatedLightColor, lightDir, normalWS) + _GlossyEnvironmentColor.rgb * _AmbientStrength; + float3 finalColor = (water.albedo * diffuseColor ) + emission + specular; + #else + float3 finalColor = (water.albedo ) + emission + specular; + #endif - float3 finalColor = (water.albedo * diffuseColor) + emission + specular; finalColor = lerp(finalColor, _IntersectionColor.rgb, noise); // =================== Make ApplyLight simple end ========================= finalColor = lerp(scene.color, finalColor, water.fog + noise); + + //Apply fog + + half fogCoord = InitializeInputDataFog(float4(positionWS,1), input.fogFactor); + finalColor = MixFog(finalColor, fogCoord); return float4(finalColor, water.alpha); } diff --git a/Packages/zzwater/shaders/libs/Input.hlsl b/Packages/zzwater/shaders/libs/Input.hlsl index 01d4f0a..5e2e1f4 100644 --- a/Packages/zzwater/shaders/libs/Input.hlsl +++ b/Packages/zzwater/shaders/libs/Input.hlsl @@ -3,10 +3,14 @@ TEXTURE2D(_IntersectionNoise); SAMPLER(sampler_IntersectionNoise); +TEXTURE2D(_CausticsTex); +SAMPLER(sampler_CausticsTex); + CBUFFER_START(UnityPerMaterial) float2 _Direction; float _Speed; + half _AmbientStrength; #ifdef TESSELLATION_ON float _TessValue; @@ -37,14 +41,19 @@ CBUFFER_START(UnityPerMaterial) float _IntersectionLength; half _IntersectionTiling; half _IntersectionSpeed; - half _IntersectionClipping; + half2 _IntersectionClipping; half _IntersectionFalloff; // Wave foam half _WaveMesureMin; half _WaveMesureMax; - half _WaveFoamClipping; + half2 _WaveFoamClipping; + // Caustics + half _CausticsBrightness; + float _CausticsTiling; + half _CausticsSpeed; + half2 _CausticsClipping; //Waves diff --git a/Packages/zzwater/shaders/libs/Tesselation.hlsl b/Packages/zzwater/shaders/libs/Tesselation.hlsl index ab4e2ba..1f58b92 100644 --- a/Packages/zzwater/shaders/libs/Tesselation.hlsl +++ b/Packages/zzwater/shaders/libs/Tesselation.hlsl @@ -27,7 +27,7 @@ struct Attributes struct Varyings { - half4 fogFactorAndVertexLight : TEXCOORD1; // x: fogFactor, yzw: vertex light + half fogFactor : TEXCOORD1; //wPos.x in w-component float4 normalWS : NORMAL; @@ -162,6 +162,7 @@ Varyings Domain(TessellationFactors factors, OutputPatch input output.normalWS = float4(waves.normal, positionWS.x); output.tangent = float4(normalInput.tangentWS, positionWS.y); output.bitangent = float4(normalInput.bitangentWS, positionWS.z); + output.fogFactor = ComputeFogFactor(positionCS.z); return output; } diff --git a/Packages/zzwater/shaders/libs/Vertex.hlsl b/Packages/zzwater/shaders/libs/Vertex.hlsl index 7a5cebb..f2aeb8c 100644 --- a/Packages/zzwater/shaders/libs/Vertex.hlsl +++ b/Packages/zzwater/shaders/libs/Vertex.hlsl @@ -10,8 +10,7 @@ struct Attributes struct Varyings { - half4 fogFactorAndVertexLight : TEXCOORD1; // x: fogFactor, yzw: vertex light - + half fogFactor : TEXCOORD1; //wPos.x in w-component float4 normalWS : NORMAL; //wPos.y in w-component @@ -59,6 +58,7 @@ Varyings LitPassVertex(Attributes input) output.normalWS = float4(waves.normal, positionWS.x); output.tangent = float4(normalInput.tangentWS, positionWS.y); output.bitangent = float4(normalInput.bitangentWS, positionWS.z); + output.fogFactor = ComputeFogFactor(positionCS.z); return output; } diff --git a/Packages/zzwater/shaders/ocean.shader b/Packages/zzwater/shaders/ocean.shader index 64bd1ec..fce09aa 100644 --- a/Packages/zzwater/shaders/ocean.shader +++ b/Packages/zzwater/shaders/ocean.shader @@ -4,8 +4,10 @@ Shader "zzwater/ocean" { _Direction("Animation direction", Vector) = (0,-1,0,0) _Speed("Animation Speed", Float) = 1 + _AmbientStrength("Ambient strength", Range(0, 1)) = 0.5 //Color + Transparency + [Toggle(_LIGHT_COLOR)] _LightColorOn("Main Light Color On", float) = 0 [HDR]_BaseColor("Deep", Color) = (0, 0.44, 0.62, 1) [HDR]_ShallowColor("Shallow", Color) = (0.1, 0.9, 0.89, 0.02) [HDR]_HorizonColor("Horizon", Color) = (0.84, 1, 1, 0.15) @@ -16,24 +18,34 @@ Shader "zzwater/ocean" _TranslucencyStrengthDirect("Translucency Strength (Direct)", Range(0 , 0.5)) = 0.05 //Reflection + Refraction + [Toggle(_REFRACTION)] _RefractionOn("Refraction On", float) = 0 _ReflectionStrength("Reflection Strength", Range(0, 1)) = 1 _ReflectionBlur("Probe Blur Factor", Range(0, 1)) = 0 _ReflectionFresnel("Reflection Curvature mask", Range(0.01, 20)) = 5 _RefractionStrength("Refraction Strength", Range(0, 1)) = 0.1 //Intersection + [Toggle(_INTERSECTION)] _IntersectionOn("Intersection On", float) = 0 [NoScaleOffset][SingleLineTexture]_IntersectionNoise("Intersection noise", 2D) = "white" {} _IntersectionTiling("Noise Tiling", float) = 0.2 _IntersectionColor("Foam Color", Color) = (1,1,1,1) _IntersectionSpeed("Intersection Speed Multi", Float) = 1 - _IntersectionClipping("Intersection Cutoff", Range(0.01, 1)) = 0.5 + _IntersectionClipping("Intersection Cutoff", Vector) = (0.5, 1, 0, 0) _IntersectionFalloff("Intersection Falloff", Range(0.01 , 1)) = 0.5 _IntersectionLength("Intersection Distance", Range(0.01 , 5)) = 2 // Wave foam + [Toggle(_WAVE_FOAM)] _WaveFoamOn("Wave Foam On", float) = 0 _WaveMesureMin("Wave Mesure Min", float) = 0 _WaveMesureMax("Wave Mesure Max", float) = 0.3 - _WaveFoamClipping("Wave Cutoff", Range(0.01,1)) = 1 + _WaveFoamClipping("Wave Cutoff", Vector) = (0.9, 1, 0, 0) + + // Caustics + [Toggle(_CAUSTICS)] _CausticsOn("Caustics On", float) = 0 + _CausticsBrightness("Brightness", Float) = 2 + _CausticsTiling("Tiling", Float) = 0.5 + _CausticsSpeed("Speed multiplier", Float) = 0.1 + _CausticsClipping("Caustics Cutoff (Y)dis (X)dep", Vector) = (6,0.3, 0, 0) //waves @@ -66,7 +78,7 @@ Shader "zzwater/ocean" Tags { "LightMode"="UniversalForward" } Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha - ZWrite Off + ZWrite On Cull Back ZTest LEqual ZClip On @@ -83,6 +95,16 @@ Shader "zzwater/ocean" #include "Packages/zzwater/shaders/libs/Input.hlsl" #include "Packages/zzwater/shaders/libs/Waves.hlsl" + #pragma shader_feature_local_fragment _LIGHT_COLOR + #pragma shader_feature_local_fragment _REFRACTION + #pragma shader_feature_local_fragment _INTERSECTION + #pragma shader_feature_local_fragment _WAVE_FOAM + #pragma shader_feature_local_fragment _CAUSTICS + + //Fog rendering (integration) + #define UnityFog + #pragma multi_compile_fog + #pragma vertex LitPassVertex #include "Packages/zzwater/shaders/libs/Vertex.hlsl" diff --git a/Packages/zzwater/shaders/ocean_tess.shader b/Packages/zzwater/shaders/ocean_tess.shader index 0a5c517..5b04f07 100644 --- a/Packages/zzwater/shaders/ocean_tess.shader +++ b/Packages/zzwater/shaders/ocean_tess.shader @@ -4,6 +4,7 @@ Shader "zzwater/ocean_tess" { _Direction("Animation direction", Vector) = (0,-1,0,0) _Speed("Animation Speed", Float) = 1 + _AmbientStrength("Ambient strength", Range(0, 1)) = 0.5 //Tessellation _TessValue("Max subdivisions", Range(1, 64)) = 16 @@ -11,6 +12,7 @@ Shader "zzwater/ocean_tess" _TessMax("End Distance", Float) = 15 //Color + Transparency + [Toggle(_LIGHT_COLOR)] _LightColorOn("Main Light Color On", float) = 0 [HDR]_BaseColor("Deep", Color) = (0, 0.44, 0.62, 1) [HDR]_ShallowColor("Shallow", Color) = (0.1, 0.9, 0.89, 0.02) [HDR]_HorizonColor("Horizon", Color) = (0.84, 1, 1, 0.15) @@ -22,24 +24,36 @@ Shader "zzwater/ocean_tess" //Reflection + Refraction - _ReflectionStrength("Reflection Strength", Range(0, 1)) = 1 + [Toggle(_REFRACTION)] _RefractionOn("Refraction On", float) = 0 + _ReflectionStrength("Reflection Strength", Range(0, 2)) = 1 _ReflectionBlur("Probe Blur Factor", Range(0, 1)) = 0 _ReflectionFresnel("Reflection Curvature mask", Range(0.01, 20)) = 5 _RefractionStrength("Refraction Strength", Range(0, 1)) = 0.1 //Intersection + [Toggle(_INTERSECTION)] _IntersectionOn("Intersection On", float) = 0 [NoScaleOffset][SingleLineTexture]_IntersectionNoise("Intersection noise", 2D) = "white" {} _IntersectionTiling("Noise Tiling", float) = 0.2 _IntersectionColor("Foam Color", Color) = (1,1,1,1) _IntersectionSpeed("Intersection Speed Multi", Float) = 1 - _IntersectionClipping("Intersection Cutoff", Range(0.01, 1)) = 0.5 + _IntersectionClipping("Intersection Cutoff", Vector) = (0.5, 1, 0, 0) _IntersectionFalloff("Intersection Falloff", Range(0.01 , 1)) = 0.5 _IntersectionLength("Intersection Distance", Range(0.01 , 5)) = 2 // Wave foam + [Toggle(_WAVE_FOAM)] _WaveFoamOn("Wave Foam On", float) = 0 _WaveMesureMin("Wave Mesure Min", float) = 0 _WaveMesureMax("Wave Mesure Max", float) = 0.3 - _WaveFoamClipping("Wave Cutoff", Range(0.01,1)) = 1 + _WaveFoamClipping("Wave Cutoff", Vector) = (0.9, 1, 0, 0) + + + // Caustics + [Toggle(_CAUSTICS)] _CausticsOn("Caustics On", float) = 0 + [NoScaleOffset][SingleLineTexture]_CausticsTex("Caustics RGB", 2D) = "black" {} + _CausticsBrightness("Brightness", Float) = 2 + _CausticsTiling("Tiling", Float) = 0.5 + _CausticsSpeed("Speed multiplier", Float) = 0.1 + _CausticsClipping("Caustics Cutoff (Y)dis (X)dep", Vector) = (6,0.3,0,0) //waves @@ -72,7 +86,7 @@ Shader "zzwater/ocean_tess" Tags { "LightMode"="UniversalForward" } Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha - ZWrite Off + ZWrite On Cull Back ZTest LEqual ZClip On @@ -95,6 +109,17 @@ Shader "zzwater/ocean_tess" #include "Packages/zzwater/shaders/libs/Input.hlsl" #include "Packages/zzwater/shaders/libs/Waves.hlsl" + + #pragma shader_feature_local_fragment _LIGHT_COLOR + #pragma shader_feature_local_fragment _REFRACTION + #pragma shader_feature_local_fragment _INTERSECTION + #pragma shader_feature_local_fragment _WAVE_FOAM + #pragma shader_feature_local_fragment _CAUSTICS + + //Fog rendering (integration) + #define UnityFog + #pragma multi_compile_fog + #pragma vertex VertexTessellation #include "Packages/zzwater/shaders/libs/Tesselation.hlsl"