浏览代码

Fix: Imported fixed Water Caustics from demo-work branch

/demo-ssao-2020.1
andytouch 5 年前
当前提交
97982772
共有 8 个文件被更改,包括 54 次插入64 次删除
  1. 13
      Packages/com.verasl.water-system/Scripts/Rendering/WaterSystemFeature.cs
  2. 2
      Packages/com.verasl.water-system/Scripts/Rendering/WaterSystemFeature.cs.meta
  3. 58
      Packages/com.verasl.water-system/Scripts/Water.cs
  4. 2
      Packages/com.verasl.water-system/Shaders/GerstnerWaves.hlsl
  5. 29
      Packages/com.verasl.water-system/Shaders/WaterCommon.hlsl
  6. 2
      Packages/com.verasl.water-system/Shaders/WaterInput.hlsl
  7. 4
      Packages/com.verasl.water-system/Shaders/WaterLighting.hlsl
  8. 8
      Assets/scenes/_levels/level_Island/Terrain.meta

13
Packages/com.verasl.water-system/Scripts/Rendering/WaterSystemFeature.cs


class WaterFxPass : ScriptableRenderPass
{
private const string k_RenderWaterFXTag = "Render Water FX";
private ProfilingSampler m_WaterFX_Profile = new ProfilingSampler(k_RenderWaterFXTag);
private readonly ShaderTagId m_WaterFXShaderTag = new ShaderTagId("WaterFX");
private readonly Color m_ClearColor = new Color(0.0f, 0.5f, 0.5f, 0.5f); //r = foam mask, g = normal.x, b = normal.z, a = displacement
private FilteringSettings m_FilteringSettings;

public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get(k_RenderWaterFXTag);
using (new ProfilingScope(cmd, m_WaterFX_Profile)) // makes sure we have profiling ability
using (new ProfilingSample(cmd, k_RenderWaterFXTag)) // makes sure we have profiling ability
{
context.ExecuteCommandBuffer(cmd);
cmd.Clear();

class WaterCausticsPass : ScriptableRenderPass
{
private const string k_RenderWaterCausticsTag = "Render Water Caustics";
private ProfilingSampler m_WaterCaustics_Profile = new ProfilingSampler(k_RenderWaterCausticsTag);
public Material WaterCausticMaterial;
private static Mesh m_mesh;

return;
CommandBuffer cmd = CommandBufferPool.Get(k_RenderWaterCausticsTag);
using (new ProfilingScope(cmd, m_WaterCaustics_Profile))
using (new ProfilingSample(cmd, k_RenderWaterCausticsTag))
{
// Create mesh if needed
if (!m_mesh)

WaterCausticsPass m_CausticsPass;
public WaterSystemSettings settings = new WaterSystemSettings();
[SerializeField] private Shader causticShader;
[SerializeField]
private Shader causticShader;
private Material _causticMaterial;

m_CausticsPass = new WaterCausticsPass();
causticShader = causticShader ? causticShader : Shader.Find("Hidden/BoatAttack/Caustics");
if (causticShader == null) return;
_causticMaterial = _causticMaterial ? _causticMaterial : new Material(causticShader);
switch (settings.debug)

public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(m_WaterFxPass);
if(_causticMaterial == null)
renderer.EnqueuePass(m_CausticsPass);
renderer.EnqueuePass(m_CausticsPass);
}
/// <summary>

2
Packages/com.verasl.water-system/Scripts/Rendering/WaterSystemFeature.cs.meta


externalObjects: {}
serializedVersion: 2
defaultReferences:
- causticShader: {fileID: 4800000, guid: 90939d2e4b62841d29c136c866715501, type: 3}
- causticsMaterial: {fileID: 2100000, guid: 7e6ea000873112847bd146ddb78ac67d, type: 2}
executionOrder: 0
icon: {instanceID: 0}
userData:

58
Packages/com.verasl.water-system/Scripts/Water.cs


private static readonly int WaveDataBuffer = Shader.PropertyToID("_WaveDataBuffer");
private static readonly int WaveData = Shader.PropertyToID("waveData");
private static readonly int AbsorptionScatteringRamp = Shader.PropertyToID("_AbsorptionScatteringRamp");
private static readonly int DepthCamZParams = Shader.PropertyToID("_VeraslWater_DepthCamParams");
private static readonly int DepthCamZParams = Shader.PropertyToID("_depthCamZParams");
private void OnValidate() { Init(); }
private void OnEnable()
{

const float quantizeValue = 6.25f;
const float forwards = 10f;
const float yOffset = -0.25f;
gameObject.layer,
cam,
0,
null,
ShadowCastingMode.Off,
true,
gameObject.layer,
cam,
0,
null,
ShadowCastingMode.Off,
true,
null,
LightProbeUsage.Off,
null);

{
resources = Resources.Load("WaterResources") as WaterResources;
}
Invoke(nameof(CaptureDepthMap), 1.0f);
//CaptureDepthMap();
}
private void LateUpdate()

//Generate the camera
if(_depthCam == null)
{
var go =
GameObject go =
var additionalCamData = _depthCam.GetUniversalAdditionalCameraData();
if (!_depthCam.TryGetComponent<UniversalAdditionalCameraData>(out var additionalCamData))
{
additionalCamData = _depthCam.gameObject.AddComponent(typeof(UniversalAdditionalCameraData)) as UniversalAdditionalCameraData;
}
var depthExtra = 4.0f;
t.position = Vector3.up * (transform.position.y + depthExtra);//center the camera on this water plane height
t.up = Vector3.forward;//face the camera down
t.position = Vector3.up * 4f;//center the camera on this water plane
t.up = Vector3.forward;//face teh camera down
_depthCam.nearClipPlane =0.01f;
_depthCam.farClipPlane = surfaceData._waterMaxVisibility + depthExtra;
//_depthCam.depthTextureMode = DepthTextureMode.Depth;
_depthCam.nearClipPlane =0.1f;
_depthCam.farClipPlane = surfaceData._waterMaxVisibility;
_depthCam.allowHDR = false;
_depthCam.allowMSAA = false;
_depthCam.cullingMask = (1 << 10);

if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES2 || SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES3)
{
_depthTex.filterMode = FilterMode.Point;
_depthTex.wrapMode = TextureWrapMode.Clamp;
_depthTex.wrapMode = TextureWrapMode.Clamp;
_depthTex.name = "WaterDepthMap";
//do depth capture
_depthCam.targetTexture = _depthTex;

var _params = new Vector4(t.position.y, 250, 0, 0);
var n = _depthCam.nearClipPlane;
var f = _depthCam.farClipPlane;
var zParams = new Vector4(n, f, f / (f-n), f * n / (n-f));
Shader.SetGlobalVector(DepthCamZParams, _params);
Shader.SetGlobalVector(DepthCamZParams, zParams);
/* #if UNITY_EDITOR
Texture2D tex2D = new Texture2D(1024, 1024, TextureFormat.Alpha8, false);
Graphics.CopyTexture(_depthTex, tex2D);

_depthCam.enabled = false;
_depthCam.targetTexture = null;
}

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


half3 wave = 0; // wave vector
half w = 6.28318 / wavelength; // 2pi over wavelength(hardcoded)
half wSpeed = sqrt(9.8 * w); // frequency of the wave based off wavelength
half peak = 1.2; // peak value, 1 is the sharpest peaks
half peak = 1; // peak value, 1 is the sharpest peaks
half qi = peak / (amplitude * w * _WaveCount);
direction = radians(direction); // convert the incoming degrees to radians, for directional waves

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


return float2(d * additionalData.x - additionalData.y, (rawD * -_ProjectionParams.x) + (1-UNITY_REVERSED_Z));
}
float WaterTextureDepth(float3 posWS)
{
return (1 - SAMPLE_TEXTURE2D_LOD(_WaterDepthMap, sampler_WaterDepthMap_linear_clamp, posWS.xz * 0.002 + 0.5, 1).r) * (_MaxDepth + _VeraslWater_DepthCamParams.x) - _VeraslWater_DepthCamParams.x;
}
float3 WaterDepth(float3 posWS, half4 additionalData, half2 screenUVs)// x = seafloor depth, y = water depth
float3 WaterDepth(float3 posWS, half2 texcoords, half4 additionalData, half2 screenUVs)// x = seafloor depth, y = water depth
float wd = WaterTextureDepth(posWS);
outDepth.y = wd + posWS.y;
float wd = (1 - SAMPLE_TEXTURE2D_LOD(_WaterDepthMap, sampler_WaterDepthMap_linear_clamp, texcoords, 1).r) * 19.1;
outDepth.y = (wd - 3.5) + posWS.y;
return outDepth;
}

screenUV.xyz /= screenUV.w;
// shallows mask
half waterDepth = WaterTextureDepth(input.posWS);
input.posWS.y += pow(saturate((-waterDepth + 1.5) * 0.4), 2);
half waterDepth = (1 - SAMPLE_TEXTURE2D_LOD(_WaterDepthMap, sampler_WaterDepthMap_linear_clamp, (input.posWS.xz * 0.002) + 0.5, 1).r) * 19.1;
waterDepth = waterDepth - 4.1;
input.posWS.y += saturate((1-waterDepth) * 0.6 - 0.5);
SampleWaves(input.posWS, saturate((waterDepth * 0.1 + 0.05)), wave);
SampleWaves(input.posWS, saturate((waterDepth * 0.25)) + 0.05, wave);
input.normal = wave.normal.xzy;
input.posWS += wave.position;

half4 waterFX = SAMPLE_TEXTURE2D(_WaterFXMap, sampler_ScreenTextures_linear_clamp, IN.preWaveSP.xy);
// Depth
float3 depth = WaterDepth(IN.posWS, IN.additionalData, screenUV.xy);// TODO - hardcoded shore depth UVs
half depthEdge = saturate(depth.y * 20 + 1);
//return half4(0, frac(ceil(depth.y) / _MaxDepth), frac(IN.posWS.y), 1);
float3 depth = WaterDepth(IN.posWS, (IN.posWS.xz * 0.002) + 0.5, IN.additionalData, screenUV.xy);// TODO - hardcoded shore depth UVs
half depthMulti = 1 / _MaxDepth;
// Lighting

// Foam
half3 foamMap = SAMPLE_TEXTURE2D(_FoamMap, sampler_FoamMap, IN.uv.zw).rgb; //r=thick, g=medium, b=light
half waveFoam = 0;// saturate(IN.posWS.y + 0.5);
half edgeFoam = saturate(1 - depth.x * 0.5 - 0.25) * depthEdge;
half waveFoam = saturate(IN.posWS.y + 0.5);
half edgeFoam = saturate(1 - depth.x * 0.5 - 0.25);
half foamBlendMask = max(max(waveFoam, edgeFoam), waterFX.r * 2);// + IN.fogFactorNoise.y * 0.1; //max(max((foamMask + shoreMask) - IN.fogFactorNoise.y * 0.25, waterFX.r * 2), shoreWave);
half3 foamBlend = SAMPLE_TEXTURE2D(_AbsorptionScatteringRamp, sampler_AbsorptionScatteringRamp, half2(foamBlendMask, 0.66)).rgb;
half foamMask = saturate(length(foamMap * foamBlend) * 1.5 - 0.1 + saturate(1 - depth.x * 4) * 0.5);

// Fresnel
half fresnelTerm = CalculateFresnelTerm(IN.normal, IN.viewDir.xyz);
//return fresnelTerm.xxxx;
BRDFData brdfData;
InitializeBRDFData(half3(0, 0, 0), 0, half3(1, 1, 1), 0.9, 1, brdfData);

// Reflections
half3 reflection = SampleReflections(IN.normal, IN.viewDir.xyz, screenUV.xy, fresnelTerm, 0.0);
reflection = clamp(reflection + spec, 0, 1024) * depthEdge;
reflection = clamp(reflection + spec, 0, 1024);
// Refraction
half3 refraction = Refraction(distortion, depth.x, depthMulti);

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


half _MaxDepth;
half _MaxWaveHeight;
int _DebugPass;
half4 _VeraslWater_DepthCamParams;
half4 _depthCamZParams;
float4x4 _InvViewProjection;
// Screen Effects textures

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


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

half3 viewNormal = mul(normalWS, (float3x3)GetWorldToViewMatrix()).xyz;
half3 reflectVector = reflect(-viewDir, viewNormal);
half2 reflectionUV = screenUV + normalWS.zx * half2(0.02, 0.15);
reflection += SAMPLE_TEXTURE2D_LOD(_PlanarReflectionTexture, sampler_ScreenTextures_linear_clamp, reflectionUV, 6 * roughness).rgb;//planar reflection
#endif

8
Assets/scenes/_levels/level_Island/Terrain.meta


fileFormatVersion: 2
guid: f843f542f9d13484b98e281985ec0d7d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存