浏览代码

Fixed some lighting issues with the water and added subtle SSS on the peaks, fixed truncation warning on caustics shader

/main
Andre McGrail 4 年前
当前提交
f57f2586
共有 5 个文件被更改,包括 23 次插入23 次删除
  1. 2
      Packages/com.verasl.water-system/Shaders/Caustics.shader
  2. 3
      Packages/com.verasl.water-system/Shaders/GerstnerWaves.hlsl
  3. 4
      Packages/com.verasl.water-system/Shaders/Water.shader
  4. 33
      Packages/com.verasl.water-system/Shaders/WaterCommon.hlsl
  5. 4
      Packages/com.verasl.water-system/Shaders/WaterLighting.hlsl

2
Packages/com.verasl.water-system/Shaders/Caustics.shader


float4 screenPos = input.screenpos / input.screenpos.w;
real depth = SampleSceneDepth(screenPos.xy);
float3 WorldPos = ReconstructWorldPos(screenPos.xy, depth);
float4 WorldPos = ReconstructWorldPos(screenPos.xy, depth).xyzz;
Light mainLight = GetMainLight();
float3 lightPos = mul(WorldPos, _MainLightDir).xyz;

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


////////////////////////////////assign to output///////////////////////////////
waveOut.position = wave * saturate(amplitude * 10000);
waveOut.normal = (n * waveCountMulti);
waveOut.normal = (n.xzy * waveCountMulti);
return waveOut;
}

waveOut.normal += wave.normal; // add the normal
}
waveOut.position *= opacityMask;
waveOut.normal *= half3(opacity, 1, opacity);
}
#endif // GERSTNER_WAVES_INCLUDED

4
Packages/com.verasl.water-system/Shaders/Water.shader


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

#pragma shader_feature _REFLECTION_CUBEMAP _REFLECTION_PROBES _REFLECTION_PLANARREFLECTION
#pragma multi_compile _ USE_STRUCTURED_BUFFER
#pragma shader_feature _ _STATIC_SHADER
#pragma shader_feature _DEBUG_OFF _DEBUG_SSS _DEBUG_REFLECTION _DEBUG_NORMAL _DEBUG_FRESNEL _DEBUG_WATEREFFECTS _DEBUG_WATERDEPTH
#pragma shader_feature _DEBUG_OFF _DEBUG_SSS _DEBUG_REFRACTION _DEBUG_REFLECTION _DEBUG_NORMAL _DEBUG_FRESNEL _DEBUG_WATEREFFECTS _DEBUG_WATERDEPTH
// -------------------------------------
// Lightweight Pipeline keywords

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


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.z = wave.position.y / _MaxWaveHeight; // encode the normalized wave height into additional data
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;
}

//Gerstner here
WaveStruct wave;
SampleWaves(input.posWS, saturate((waterDepth * 0.1 + 0.05)), wave);
input.normal = wave.normal.xzy;
input.normal = wave.normal;
input.posWS += wave.position;
#ifdef SHADER_API_PS4

//return half4(0, frac(ceil(depth.y) / _MaxDepth), frac(IN.posWS.y), 1);
half depthMulti = 1 / _MaxDepth;
// Detail waves
half2 detailBump1 = SAMPLE_TEXTURE2D(_SurfaceMap, sampler_SurfaceMap, IN.uv.zw).xy * 2 - 1;
half2 detailBump2 = SAMPLE_TEXTURE2D(_SurfaceMap, sampler_SurfaceMap, IN.uv.xy).xy * 2 - 1;
half2 detailBump = (detailBump1 + detailBump2 * 0.5) * saturate(depth.x * 0.25 + 0.25);
IN.normal += half3(detailBump.x, 0, detailBump.y) * _BumpScale;
IN.normal += half3(1-waterFX.y, 0.5h, 1-waterFX.z) - 0.5;
IN.normal = normalize(IN.normal);
// Lighting
Light mainLight = GetMainLight(TransformWorldToShadowCoord(IN.posWS));
half shadow = SoftShadows(screenUV, IN.posWS);

half3 sss = 1 * (shadow * mainLight.color + GI);
half3 directLighting = dot(mainLight.direction, half3(0, 1, 0)) * mainLight.color;
directLighting += saturate(pow(dot(IN.viewDir, -mainLight.direction) * IN.additionalData.z, 3)) * 5 * mainLight.color;
half3 sss = directLighting * shadow + GI;
// Foam
half3 foamMap = SAMPLE_TEXTURE2D(_FoamMap, sampler_FoamMap, IN.uv.zw).rgb; //r=thick, g=medium, b=light

// Foam lighting
half3 foam = foamMask.xxx * (mainLight.shadowAttenuation * mainLight.color + GI);
// Detail waves
half2 detailBump1 = SAMPLE_TEXTURE2D(_SurfaceMap, sampler_SurfaceMap, IN.uv.zw).xy * 2 - 1;
half2 detailBump2 = SAMPLE_TEXTURE2D(_SurfaceMap, sampler_SurfaceMap, IN.uv.xy).xy * 2 - 1;
half2 detailBump = (detailBump1 + detailBump2 * 0.5) * saturate(depth.x * 0.25 + 0.25);
IN.normal += half3(detailBump.x, 0, detailBump.y) * _BumpScale;
IN.normal += half3(1-waterFX.y, 0.5h, 1-waterFX.z) - 0.5;
IN.normal = normalize(IN.normal);
// Distortion
half2 distortion = DistortionUVs(depth.x, IN.normal);
distortion = screenUV.xy + distortion;// * clamp(depth.x, 0, 5);

//comp = DebugWaterFX(comp, waterFX, screenUV.x);
#if defined(_DEBUG_SSS)
return half4(sss, 1);
#elif defined(_DEBUG_REFRACTION)
return half4(refraction, 1);
#elif defined(_DEBUG_REFLECTION)
return half4(reflection, 1);
#elif defined(_DEBUG_NORMAL)

#else
return half4(comp, 1);
#endif
//return SAMPLE_TEXTURE2D(_PlanarReflectionTexture, sampler_ScreenTextures_linear_clamp, screenUV);
//return half4(spec, 1); // debug line
//return half4(diffuse, 1); // debug line
//return half4( (1 - foamMask).xxx, 1); // debug line
//eturn half4(pow(dot(IN.normal,float3(0, 1, 0)), 10).xxx, 1); // debug line
}
#endif // WATER_COMMON_INCLUDED

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


half CalculateFresnelTerm(half3 normalWS, half3 viewDirectionWS)
{
return pow(1.0 - (saturate(dot(normalWS, viewDirectionWS)) - 0.05), 5);//fresnel TODO - find a better place
return saturate(pow(1.0 - dot(normalWS, viewDirectionWS), 5));//fresnel TODO - find a better place
}
///////////////////////////////////////////////////////////////////////////////

float3 lightJitter = positionWS + jitterTexture.xzy * 2;
shadowAttenuation += SAMPLE_TEXTURE2D_SHADOW(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture, TransformWorldToShadowCoord(lightJitter));
}
return shadowAttenuation * loopDiv;
return BEYOND_SHADOW_FAR(TransformWorldToShadowCoord(positionWS * 1.1)) ? 1.0 : shadowAttenuation * loopDiv;
}
///////////////////////////////////////////////////////////////////////////////

正在加载...
取消
保存