浏览代码

Shader abstraction work

/feature-infinite-sea
André McGrail 4 年前
当前提交
8a080024
共有 5 个文件被更改,包括 102 次插入63 次删除
  1. 87
      Assets/scenes/Testing/InfiniteWater.unity
  2. 2
      Packages/com.verasl.water-system/Scripts/Data/WaterSettingsData.asset
  3. 25
      Packages/com.verasl.water-system/Shaders/InfiniteWater.shader
  4. 50
      Packages/com.verasl.water-system/Shaders/WaterCommon.hlsl
  5. 1
      Packages/com.verasl.water-system/Shaders/WaterInput.hlsl

87
Assets/scenes/Testing/InfiniteWater.unity


m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1

m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1

m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1

m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1

m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!84 &915593709
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
m_IsAlphaChannelOptional: 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!28 &952346665
Texture2D:
m_ObjectHideFlags: 0

m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1326083684}
m_LocalRotation: {x: 0.06510497, y: 0.89899063, z: -0.1431909, w: 0.4087463}
m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalRotation: {x: -0.013000863, y: -0.9098737, z: 0.028593907, w: -0.41369456}
m_LocalPosition: {x: 0, y: 10.23, z: 0}
m_LocalEulerAnglesHint: {x: -341.9, y: -228.9, z: 0}
--- !u!84 &1360944041
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
m_IsAlphaChannelOptional: 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
m_LocalEulerAnglesHint: {x: 3.6, y: -228.9, z: 0}
--- !u!1001 &1617765646
PrefabInstance:
m_ObjectHideFlags: 0

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1704475455}
m_LocalRotation: {x: 0.036915828, y: -0.3823778, z: 0.015291036, w: 0.9231417}
m_LocalRotation: {x: 0.06903013, y: -0.3816137, z: 0.028593214, w: 0.921297}
m_LocalEulerAnglesHint: {x: 4.58, y: -45.000004, z: 0}
m_LocalEulerAnglesHint: {x: 8.57, y: -45.000004, z: 0}
--- !u!114 &1704475460
MonoBehaviour:
m_ObjectHideFlags: 0

2
Packages/com.verasl.water-system/Scripts/Data/WaterSettingsData.asset


m_ClipPlaneOffset: 0.24
m_ReflectLayers:
serializedVersion: 2
m_Bits: 4294967295
m_Bits: 4294966767
m_Shadows: 0
cubemapRefType: {fileID: 8900000, guid: a90d3c9b1c3ed4d36bffc02f6b335464, type: 3}
isInfinite: 0

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


void InfiniteWaterFragment(Varyings i, out half4 outColor:SV_Target, out float outDepth : SV_Depth) //: SV_Target
{
half2 screenUV = i.screenPosition.xy / i.screenPosition.w; // screen UVs
half4 screenUV = 0.0;
screenUV.xy = i.screenPosition.xy / i.screenPosition.w; // screen UVs
screenUV.zw = screenUV.xy; // screen UVs
//half2 screenUV = i.screenPosition.xy / i.screenPosition.w; // screen UVs
half4 waterFX = SAMPLE_TEXTURE2D(_WaterFXMap, sampler_ScreenTextures_linear_clamp, screenUV.xy);

float4 additionalData = float4(1, length(viewDirectionWS), waterFX.w, 1);
i.positionWS = plane.positionWS;
i.normalWS = half3(0.0, 1.0, 0.0);
i.viewDirectionWS = normalize(GetCameraPositionWS() - plane.positionWS).xyzz;
i.additionalData = additionalData;
//i.uv =
WaterInputData inputData;
InitializeInputData(i, inputData, screenUV.xy);
WaterSurfaceData surfaceData;
InitializeSurfaceData(inputData, surfaceData);
half4 color;
color.a = 1;
color.rgb = WaterShading(inputData, surfaceData, additionalData, screenUV.xy);
outColor = color;
// Depth
float3 depth = WaterDepth(plane.positionWS, additionalData, screenUV.xy);

//outColor = half4(reflection * fresnelTerm + spec, 1);
outColor = half4(comp, 1);
//outColor = half4(comp, 1);
outDepth = 1-plane.depth;
}
ENDHLSL

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


void InitializeInputData(Varyings input, out WaterInputData inputData, float2 screenUV)
{
float3 depth = WaterDepth(input.positionWS, input.additionalData, screenUV);// TODO - hardcoded shore depth UVs
// Sample water FX texture
inputData.waterFX = WaterFX(input.preWaveSP.xy);
// Detail waves
DetailNormals(inputData.normalWS, input.uv, inputData.waterFX, depth);
inputData.reflectionUV = 0;
half2 distortion = DistortionUVs(depth.x, inputData.normalWS);
distortion = screenUV.xy + distortion;// * clamp(depth.x, 0, 5);
float d = depth.x;
depth.xz = AdjustedDepth(distortion, input.additionalData); // only x y
distortion = depth.x < 0 ? screenUV.xy : distortion;
inputData.refractionUV = distortion;
depth.x = depth.x < 0 ? d : depth.x;
// Sample water FX texture
inputData.waterFX = WaterFX(input.preWaveSP.xy);
inputData.depth = 0;
inputData.refractionUV = DistortionUVs(inputData.depth, inputData.normalWS);
inputData.depth = depth.x;
inputData.reflectionUV = 0;
void InitializeSurfaceData(WaterInputData input, out WaterSurfaceData surfaceData)
void InitializeSurfaceData(inout WaterInputData input, out WaterSurfaceData surfaceData)
// Detail waves
DetailNormals(input.normalWS, input.detailUV, input.waterFX, input.depth);
surfaceData.normalWS = 0;
/*
// Foam
half3 foamMap = SAMPLE_TEXTURE2D(_FoamMap, sampler_FoamMap, IN.uv.zw).rgb; //r=thick, g=medium, b=light

surfaceData.foam = 0;
}
float3 WaterShading(WaterInputData input, WaterSurfaceData surfaceData, float2 screenUV)
float3 WaterShading(WaterInputData input, WaterSurfaceData surfaceData, float4 additionalData, float2 screenUV)
{
// Lighting
Light mainLight = GetMainLight(TransformWorldToShadowCoord(input.positionWS));

// SSS
half3 directLighting = dot(mainLight.direction, half3(0, 1, 0)) * mainLight.color;
//directLighting += saturate(pow(dot(input.viewDirectionWS.xyz, -mainLight.direction) * IN.additionalData.z, 3)) * 5 * mainLight.color;
//half3 sss = directLighting * shadow + GI;
directLighting += saturate(pow(dot(input.viewDirectionWS.xyz, -mainLight.direction) * additionalData.z, 3)) * 5 * mainLight.color;
half3 spec = DirectBDRF(brdfData, input.normalWS, mainLight.direction, input.viewDirectionWS);// * shadow * mainLight.color;
half3 spec = DirectBDRF(brdfData, input.normalWS, mainLight.direction, input.viewDirectionWS) * mainLight.color * shadow;
half3 sss = 0;
half3 sss = directLighting * shadow + GI;
sss *= Scattering(input.depth);
// Reflections

// Do compositing
half3 output = lerp(lerp(refraction, reflection, fresnelTerm) + sss + spec, surfaceData.foam, surfaceData.foamMask);
//return refraction;
return MixFog(output, input.fogCoord);
}

WaterSurfaceData surfaceData;
InitializeSurfaceData(inputData, surfaceData);
half4 color;
color.a = 1;
color.rgb = WaterShading(inputData, surfaceData, screenUV.xy);
half4 current;
current.a = 1;
current.rgb = WaterShading(inputData, surfaceData, IN.additionalData, screenUV.xy);
//return color;

float3 camPos = GetCameraPositionWS();
camPos.y = 0;
float alpha = 1 - saturate((distance(IN.positionWS, camPos) - 50) * 1);
//return half4(IN.additionalData.www, alpha);
half3 old = comp;
half fiftyfifty = round(screenUV.x);
return half4(lerp(old, current.rgb, fiftyfifty), alpha);
#if defined(_DEBUG_FOAM)
return half4(foamMask.xxx, 1);

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


{
half3 absorption;
half3 scattering;
half3 normalWS;
half foam;
half foamMask;
};

正在加载...
取消
保存