浏览代码

More cleanup work, setting up water shader functions

/feature-infinite-sea
André McGrail 5 年前
当前提交
6d11c41d
共有 8 个文件被更改,包括 211 次插入70 次删除
  1. 124
      Assets/scenes/Testing/InfiniteWater.unity
  2. 16
      Packages/com.verasl.water-system/Materials/InfiniteWater.mat
  3. 2
      Packages/com.verasl.water-system/Shaders/GerstnerWaves.hlsl
  4. 13
      Packages/com.verasl.water-system/Shaders/InfiniteWater.shader
  5. 3
      Packages/com.verasl.water-system/Shaders/InfiniteWater.shader.meta
  6. 92
      Packages/com.verasl.water-system/Shaders/WaterCommon.hlsl
  7. 25
      Packages/com.verasl.water-system/Shaders/WaterInput.hlsl
  8. 6
      Packages/com.verasl.water-system/Shaders/WaterLighting.hlsl

124
Assets/scenes/Testing/InfiniteWater.unity


m_AmbientIntensity: 1
m_AmbientMode: 0
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
m_SkyboxMaterial: {fileID: 2100000, guid: e14f29569c2db49fdb0a5fd5b3e95b86, type: 2}
m_SkyboxMaterial: {fileID: 0}
m_HaloStrength: 0.5
m_FlareStrength: 1
m_FlareFadeSpeed: 3

m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 8900000, guid: 6e8b646e70b4e410daa65a80eedd3182, type: 3}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.18028545, g: 0.22571565, b: 0.30692434, a: 1}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:

- target: {fileID: 1015499191777110, guid: 2cac2e60f3f6f2447ad9b550450f60ed, type: 3}
propertyPath: m_Name
value: SeaVisual
objectReference: {fileID: 0}
- target: {fileID: 1015499191777110, guid: 2cac2e60f3f6f2447ad9b550450f60ed, type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1153691288952736, guid: 2cac2e60f3f6f2447ad9b550450f60ed, type: 3}
propertyPath: m_IsActive

type: 3}
propertyPath: _depthTex
value:
objectReference: {fileID: 1938023269}
objectReference: {fileID: 0}
- target: {fileID: 114943683913522278, guid: 2cac2e60f3f6f2447ad9b550450f60ed,
type: 3}
propertyPath: _waves.Array.data[0].direction

- component: {fileID: 322090441}
- component: {fileID: 322090440}
- component: {fileID: 322090439}
m_Layer: 0
m_Layer: 10
m_Name: Plane
m_TagString: Untagged
m_Icon: {fileID: 0}

m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 322090437}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: -2, z: 0}
m_LocalPosition: {x: 0, y: -8, z: 0}
m_LocalScale: {x: 20, y: 20, z: 20}
m_Children: []
m_Father: {fileID: 537372328}

m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 611616626}
m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
--- !u!84 &734942345
RenderTexture:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: WaterDepthMap
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
serializedVersion: 3
m_Width: 1024
m_Height: 1024
m_AntiAliasing: 1
m_MipCount: -1
m_DepthFormat: 2
m_ColorFormat: 142
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_Aniso: 1
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1
--- !u!1 &867751506
GameObject:
m_ObjectHideFlags: 0

serializedVersion: 10
m_Type: 1
m_Shape: 0
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
m_Intensity: 4
m_Color: {r: 1, g: 0.7621352, b: 0.4575472, a: 1}
m_Intensity: 0.5
m_Range: 10
m_SpotAngle: 30
m_InnerSpotAngle: 21.802082

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1326083684}
m_LocalRotation: {x: -0.020138003, y: -0.9389292, z: 0.33896112, w: -0.055782672}
m_LocalRotation: {x: -0.00941555, y: -0.98557925, z: 0.15848172, w: -0.0585542}
m_LocalEulerAnglesHint: {x: 39.7, y: -186.8, z: 0}
m_LocalEulerAnglesHint: {x: 18.27, y: -186.8, z: 0}
--- !u!1001 &1617765646
PrefabInstance:
m_ObjectHideFlags: 0

m_RequiresDepthTexture: 0
m_RequiresColorTexture: 0
m_Version: 2
--- !u!84 &1938023269
RenderTexture:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: WaterDepthMap
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
serializedVersion: 3
m_Width: 1024
m_Height: 1024
m_AntiAliasing: 1
m_MipCount: -1
m_DepthFormat: 2
m_ColorFormat: 142
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_Aniso: 1
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1
--- !u!1001 &2012797122
PrefabInstance:
m_ObjectHideFlags: 0

m_Modifications:
- target: {fileID: 1008232119289452, guid: 780fd72a347584392bf8fd00139cf652, type: 3}
propertyPath: m_Layer
value: 10
objectReference: {fileID: 0}
- target: {fileID: 4173175211220914, guid: 780fd72a347584392bf8fd00139cf652, type: 3}
propertyPath: m_LocalPosition.x
value: -26.7

propertyPath: m_RootOrder
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2208899242920299761, guid: 780fd72a347584392bf8fd00139cf652,
type: 3}
propertyPath: m_Layer
value: 10
objectReference: {fileID: 0}
- target: {fileID: 3118962134926908003, guid: 780fd72a347584392bf8fd00139cf652,
type: 3}
propertyPath: m_Layer
value: 10
objectReference: {fileID: 0}
- target: {fileID: 3810107875068976558, guid: 780fd72a347584392bf8fd00139cf652,
type: 3}
propertyPath: m_Layer
value: 10
objectReference: {fileID: 0}
- target: {fileID: 4899698395614972821, guid: 780fd72a347584392bf8fd00139cf652,
type: 3}
propertyPath: m_Layer
value: 10
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 780fd72a347584392bf8fd00139cf652, type: 3}
--- !u!4 &2012797123 stripped

- target: {fileID: 100000, guid: 0694fda6617df4cd6a3d240f5eeb580b, type: 3}
propertyPath: m_Layer
value: 4
objectReference: {fileID: 0}
- target: {fileID: 100000, guid: 0694fda6617df4cd6a3d240f5eeb580b, type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
- target: {fileID: 400000, guid: 0694fda6617df4cd6a3d240f5eeb580b, type: 3}
propertyPath: m_LocalPosition.x

propertyPath: m_Materials.Array.data[0]
value:
objectReference: {fileID: 2100000, guid: e9927401bf96341d5bba91ea4c815e14, type: 2}
- target: {fileID: 3300000, guid: 0694fda6617df4cd6a3d240f5eeb580b, type: 3}
propertyPath: m_Mesh
value:
objectReference: {fileID: 4300000, guid: 0694fda6617df4cd6a3d240f5eeb580b, type: 3}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 0694fda6617df4cd6a3d240f5eeb580b, type: 3}

16
Packages/com.verasl.water-system/Materials/InfiniteWater.mat


serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: InfiniteWater
m_Shader: {fileID: 4800000, guid: 5f51d4f8f480441339ead24ab495af7d, type: 3}
m_ShaderKeywords:

m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DitherPattern:
m_Texture: {fileID: 2800000, guid: 7247454f970266b4c8d99e1f01e15296, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Texture: {fileID: 2800000, guid: 4b5fce50854cd46f1b97bcd5ba7f019b, type: 3}
m_Texture: {fileID: 2800000, guid: f395b7d64f44848c6a3cba1f1173fe90, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:

m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _BumpScale: 0.3
- _Debug: 0
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1

- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _Size: 1.2
- _Size: 4
- _Static: 0
- _UVSec: 0
- _WorkflowMode: 1
- _ZWrite: 1

- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []

2
Packages/com.verasl.water-system/Shaders/GerstnerWaves.hlsl


WaveStruct GerstnerWave(half2 pos, float waveCountMulti, half amplitude, half direction, half wavelength, half omni, half2 omniPos)
{
WaveStruct waveOut;
#if defined(_STATIC_WATER)
#if defined(_STATIC_SHADER)
float time = 0;
#else
float time = _Time.y;

13
Packages/com.verasl.water-system/Shaders/InfiniteWater.shader


Properties
{
_Size ("size", float) = 3.0
_DitherPattern ("Dithering Pattern", 2D) = "bump" {}
[Toggle(_STATIC_SHADER)] _Static ("Static", Float) = 0
_BumpScale("Detail Wave Amount", Range(0, 2)) = 0.2//fine detail multiplier
[KeywordEnum(Off, SSS, Refraction, Reflection, Normal, Fresnel, WaterEffects, Foam, WaterDepth)] _Debug ("Debug mode", Float) = 0

InfinitePlane plane = WorldPlane(i.screenPosition, i.viewDir);
float3 normal = half3(0.0, 1.0, 0.0);
half3 viewDirectionWS = normalize(GetCameraPositionWS() - plane.positionWS);
float4 additionalData = float4(1, length(viewDirectionWS), waterFX.w, 1);
float3 depth = WaterDepth(plane.positionWS, 0.0, screenUV.xy);
float3 depth = WaterDepth(plane.positionWS, additionalData, screenUV.xy);
// Detail waves
DetailNormals(normal, DetailUVs(plane.positionWS * (1 / _Size), 1.0), waterFX, depth);

// SSS
half3 directLighting = dot(mainLight.direction, half3(0, 1, 0)) * mainLight.color;
directLighting += saturate(pow(dot(i.viewDir.xyz, -mainLight.direction) * 1, 3)) * 5 * mainLight.color;
directLighting += saturate(pow(dot(viewDirectionWS, -mainLight.direction) * 1, 3)) * 5 * mainLight.color;
half3 sss = directLighting * shadow + GI;
half4 col = SAMPLE_TEXTURE2D(_SurfaceMap, sampler_SurfaceMap, plane.positionWS.xz);

// Fresnel
half fresnelTerm = CalculateFresnelTerm(normal, i.viewDir.xyz);
half fresnelTerm = CalculateFresnelTerm(normal, viewDirectionWS);
half3 spec = DirectBDRF(brdfData, normal, mainLight.direction, i.viewDir.xyz) * shadow * mainLight.color;
half3 spec = DirectBDRF(brdfData, normal, mainLight.direction, viewDirectionWS) * shadow * mainLight.color;
half3 reflection = SampleReflections(normal, i.viewDir.xyz, screenUV.xy, 0.0);
half3 reflection = SampleReflections(normal, viewDirectionWS, screenUV.xy, 0.0);
// Refraction
half3 refraction = Refraction(screenUV, depth.x);

3
Packages/com.verasl.water-system/Shaders/InfiniteWater.shader.meta


guid: 5f51d4f8f480441339ead24ab495af7d
ShaderImporter:
externalObjects: {}
defaultTextures: []
defaultTextures:
- _DitherPattern: {fileID: 2800000, guid: 7247454f970266b4c8d99e1f01e15296, type: 3}
nonModifiableTextures: []
userData:
assetBundleName:

92
Packages/com.verasl.water-system/Shaders/WaterCommon.hlsl


#define SHADOWS_SCREEN 0
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#if defined(_STATIC_WATER)
#if defined(_STATIC_SHADER)
#define WATER_TIME 0.0
#else
#define WATER_TIME _Time.y

half4 data = half4(0.0, 0.0, 0.0, 0.0);
float3 viewPos = TransformWorldToView(postionWS);
data.x = length(viewPos / viewPos.z);// distance to surface
data.y = length(GetCameraPositionWS().xyz - postionWS); // local position in camera space
data.y = length(GetCameraPositionWS().xyz - postionWS); // local position in camera space(view direction WS)
data.z = wave.position.y / _MaxWaveHeight * 0.5 + 0.5; // encode the normalized wave height into additional data
data.w = wave.position.x + wave.position.z;
return data;

return input;
}
void InitializeInputData(Varyings input, out WaterInputData inputData)
{
inputData.positionWS = input.posWS;
inputData.normalWS = input.normal;
inputData.viewDirectionWS = input.viewDir;
inputData.reflectionUV = 0;
inputData.shadowCoord = TransformWorldToShadowCoord(inputData.normalWS);
inputData.fogCoord = input.fogFactorNoise.x;
inputData.depth = 1;
inputData.refractionUV = DistortionUVs(inputData.depth, inputData.normalWS);
inputData.GI = 0;
}
void InitializeSurfaceData(WaterInputData input, out WaterSurfaceData surfaceData)
{
surfaceData.absorption = 0;
surfaceData.scattering = 0;
surfaceData.normal = 0;
/*
// Foam
half3 foamMap = SAMPLE_TEXTURE2D(_FoamMap, sampler_FoamMap, IN.uv.zw).rgb; //r=thick, g=medium, b=light
half depthEdge = saturate(depth.x * 20);
half waveFoam = saturate(IN.additionalData.z - 0.75 * 0.5); // wave tips
half depthAdd = saturate(1 - depth.x * 4) * 0.5;
half edgeFoam = saturate((1 - min(depth.x, depth.y) * 0.5 - 0.25) + depthAdd) * depthEdge;
half foamBlendMask = max(max(waveFoam, edgeFoam), waterFX.r * 2);
half3 foamBlend = SAMPLE_TEXTURE2D(_AbsorptionScatteringRamp, sampler_AbsorptionScatteringRamp, half2(foamBlendMask, 0.66)).rgb;
*/
surfaceData.foamMask = 0;// saturate(length(foamMap * foamBlend) * 1.5 - 0.1);
surfaceData.foam = 0;
}
float3 WaterShading(WaterInputData input, WaterSurfaceData surfaceData, float2 screenUV)
{
// Lighting
Light mainLight = GetMainLight(TransformWorldToShadowCoord(input.positionWS));
half shadow = SoftShadows(screenUV, input.positionWS);
half3 GI = SampleSH(input.normalWS);
BRDFData brdfData;
InitializeBRDFData(half3(0, 0, 0), 0, half3(1, 1, 1), 0.95, 1, brdfData);
half3 spec = DirectBDRF(brdfData, input.normalWS, mainLight.direction, input.viewDirectionWS);// * shadow * mainLight.color;
// Fresnel
half fresnelTerm = CalculateFresnelTerm(input.normalWS, input.viewDirectionWS);
half3 sss = 0;
sss *= Scattering(input.depth);
// Reflections
half3 reflection = SampleReflections(input.normalWS, input.viewDirectionWS, screenUV, 0.0);
// Refraction
half3 refraction = Refraction(input.refractionUV, input.depth);
// Do compositing
half3 output = lerp(lerp(refraction, reflection, fresnelTerm) + sss + spec, surfaceData.foam, surfaceData.foamMask);
return MixFog(output, input.fogCoord);
}
///////////////////////////////////////////////////////////////////////////////
// Vertex and Fragment functions //
///////////////////////////////////////////////////////////////////////////////

UNITY_SETUP_INSTANCE_ID(IN);
half3 screenUV = IN.screenPosition.xyz / IN.screenPosition.w; // screen UVs
WaterInputData inputData;
InitializeInputData(IN, inputData);
WaterSurfaceData surfaceData;
InitializeSurfaceData(inputData, surfaceData);
half4 color;
color.a = 1;
color.rgb = WaterShading(inputData, surfaceData, screenUV.xy);
//return color;
half4 waterFX = SAMPLE_TEXTURE2D(_WaterFXMap, sampler_ScreenTextures_linear_clamp, IN.preWaveSP.xy);
// Depth

// Lighting
Light mainLight = GetMainLight(TransformWorldToShadowCoord(IN.posWS));
half shadow = SoftShadows(screenUV, IN.posWS);
half shadow = SoftShadows(screenUV.xy, IN.posWS);
half3 GI = SampleSH(IN.normal);
// SSS

half2 distortion = DistortionUVs(depth.x, IN.normal);
distortion = screenUV.xy + distortion;// * clamp(depth.x, 0, 5);
float d = depth.x;
depth.xz = AdjustedDepth(distortion, IN.additionalData);
depth.xz = AdjustedDepth(distortion, IN.additionalData); // only x y
distortion = depth.x < 0 ? screenUV.xy : distortion;
depth.x = depth.x < 0 ? d : depth.x;

#elif defined(_DEBUG_WATEREFFECTS)
return half4(waterFX);
#elif defined(_DEBUG_WATERDEPTH)
return half4(frac(depth), 1);
return half4(frac(depth.z).xxx, 1);
#else
return half4(comp, alpha);
#endif

25
Packages/com.verasl.water-system/Shaders/WaterInput.hlsl


TEXTURE2D(_FoamMap); SAMPLER(sampler_FoamMap);
TEXTURE2D(_DitherPattern); SAMPLER(sampler_DitherPattern);
// Must match Lightweigth ShaderGraph master node
struct SurfaceData
struct WaterSurfaceData
{
half3 absorption;
half3 scattering;
half3 normal;
half foam;
half foamMask;
};
struct WaterInputData
half3 absorption;
half3 scattering;
half3 normal;
half foam;
float3 positionWS;
half3 normalWS;
half3 viewDirectionWS;
float2 reflectionUV;
float2 refractionUV;
float4 shadowCoord;
half fogCoord;
float depth;
half3 GI;
};
#endif // WATER_INPUT_INCLUDED

6
Packages/com.verasl.water-system/Shaders/WaterLighting.hlsl


}
//Soft Shadows
half SoftShadows(float3 screenUV, float3 positionWS)
half SoftShadows(float2 screenUV, float3 positionWS)
half2 jitterUV = screenUV.xy * _ScreenParams.xy * _DitherPattern_TexelSize.xy;
half2 jitterUV = screenUV * _ScreenParams.xy * _DitherPattern_TexelSize.xy;
uint loop = 4;
float loopDiv = 1.0 / loop;
for (uint i = 0u; i < loop; ++i)

正在加载...
取消
保存