浏览代码

Tweaked Caustic effect to render only on underwater pixels

/demo-work
André McGrail 5 年前
当前提交
e83a7c71
共有 6 个文件被更改,包括 40 次插入26 次删除
  1. 13
      Packages/com.verasl.water-system/Scripts/BuoyantObject.cs
  2. 12
      Packages/com.verasl.water-system/Scripts/Rendering/PlanarReflections.cs
  3. 18
      Packages/com.verasl.water-system/Scripts/Rendering/WaterCausticsFeature.cs
  4. 4
      Packages/com.verasl.water-system/Scripts/Water.cs
  5. 15
      Packages/com.verasl.water-system/Shaders/Caustics.shader
  6. 4
      Packages/com.verasl.water-system/Shaders/WaterCommon.hlsl

13
Packages/com.verasl.water-system/Scripts/BuoyantObject.cs


else if (_buoyancyType == BuoyancyType.Physical)
{
BuoyancyForce(Vector3.zero, velocity[0], heights[0].y, ref submergedAmount, ref debugInfo[0]);
UpdateDrag(submergedAmount);
//UpdateDrag(submergedAmount);
}
}
}

private void BuoyancyForce(Vector3 position, float3 velocity, float waterHeight, ref float submergedAmount, ref DebugDrawing _debug)
{
//_debug.position = position;
//_debug.waterHeight = waterHeight;
//_debug.force = Vector3.zero;
_debug.position = position;
_debug.waterHeight = waterHeight;
_debug.force = Vector3.zero;
float k = math.clamp((waterHeight - (position.y - voxelResolution)) / (voxelResolution * 2f), 0f, 1f);
float k = math.clamp(waterHeight - (position.y - voxelResolution), 0f, 1f);
velocity.y *= 2f;
//_debug.force = force; // For drawing force gizmos
_debug.force = force; // For drawing force gizmos
//Debug.Log(string.Format("Position: {0:f1} -- Force: {1:f2} -- Height: {2:f2}\nVelocty: {3:f2} -- Damp: {4:f2} -- Mass: {5:f1} -- K: {6:f2}", wp, force, waterLevel, velocity, localDampingForce, RB.mass, localArchimedesForce));
}
}

12
Packages/com.verasl.water-system/Scripts/Rendering/PlanarReflections.cs


using Unity.Mathematics;
using System;
using Unity.Mathematics;
using UnityEngine.Rendering.Universal;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Serialization;

public LayerMask m_ReflectLayers = -1;
public bool m_Shadows;
}
[SerializeField]
public PlanarReflectionSettings m_settings = new PlanarReflectionSettings();

private int planarReflectionTextureID = Shader.PropertyToID("_PlanarReflectionTexture");
private int2 m_OldReflectionTextureSize;
public static event Action<ScriptableRenderContext, Camera> beginPlanarReflections;
private void OnEnable()
{

{
if (camera.cameraType == CameraType.Reflection || camera.cameraType == CameraType.Preview)
return;
UpdateReflectionCamera(camera);
m_ReflectionCamera.cameraType = camera.cameraType;

var bias = QualitySettings.lodBias;
QualitySettings.maximumLODLevel = 1;
QualitySettings.lodBias = bias * 0.25f;
if (beginPlanarReflections != null) beginPlanarReflections(context, m_ReflectionCamera);
UniversalRenderPipeline.RenderSingleCamera(context, m_ReflectionCamera);
GL.invertCulling = false;

18
Packages/com.verasl.water-system/Scripts/Rendering/WaterCausticsFeature.cs


using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace WaterSystem
{
public class WaterCausticsFeature : ScriptableRendererFeature

public class WaterCausticSettings
{
public Material material;
public Mesh mesh;
}
public override void Create()

m_WaterCausticsPass.m_mesh = settings.mesh;
public override void AddRenderPasses(UnityEngine.Rendering.Universal.ScriptableRenderer renderer, ref UnityEngine.Rendering.Universal.RenderingData renderingData)
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
public class WaterCausticsPass : UnityEngine.Rendering.Universal.ScriptableRenderPass
public class WaterCausticsPass : ScriptableRenderPass
public Mesh m_mesh;
public override void Execute(ScriptableRenderContext context, ref UnityEngine.Rendering.Universal.RenderingData renderingData)
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
if (m_WaterCausticMaterial == null)
if (m_WaterCausticMaterial == null || m_mesh == null)
{
Debug.LogErrorFormat(
"Missing caustic material. {0} render pass will not execute. Check for missing reference in the renderer resources.",

Vector3 position = renderingData.cameraData.camera.transform.position;
position.y = 0;
Matrix4x4 matrix = Matrix4x4.TRS(position, Quaternion.identity, Vector3.one * 100f);
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_WaterCausticMaterial, 0, 0);
cmd.DrawMesh(m_mesh, matrix , m_WaterCausticMaterial, 0, 0);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

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


private void BeginCameraRendering(ScriptableRenderContext src, Camera cam)
{
if (cam.cameraType == CameraType.Game || cam.cameraType == CameraType.SceneView)
if (cam.cameraType != CameraType.Preview)
{
Vector3 fwd = cam.transform.forward;
fwd.y = 0;

{
float p = Mathf.Lerp(0.5f, 1.5f, (float)i * r);
float amp = a * p * Random.Range(0.8f, 1.2f);
float dir = d + Random.Range(-45f, 45f);
float dir = d + Random.Range(-90f, 90f);
float len = l * p * Random.Range(0.6f, 1.4f);
_waves[i] = new Wave(amp, dir, len, Vector2.zero, false);
Random.InitState(surfaceData.randomSeed + i + 1);

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


// No culling or depth
Cull Off
ZWrite Off
ZTest Always
//ZTest Always
Pass
{

struct Varyings
{
float2 uv : TEXCOORD0;
float4 screenpos : TEXCOORD1;
float4 positionOS : SV_POSITION;
};

Varyings vert (Attributes input)
{
Varyings output;
output.positionOS = float4(input.positionOS.xyz, 1.0);
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
output.positionOS = vertexInput.positionCS;
output.screenpos = ComputeScreenPos(output.positionOS);
output.uv = float2(input.uv.x, 1.0 - input.uv.y);
return output;
}

real4 frag (Varyings input) : SV_Target
{
real depth = SAMPLE_DEPTH_TEXTURE( _CameraDepthTexture, sampler_CameraDepthTexture, input.uv);
float4 screenPos = input.screenpos / input.screenpos.w;
real depth = SAMPLE_DEPTH_TEXTURE( _CameraDepthTexture, sampler_CameraDepthTexture, screenPos.xy);
float3 worldPos = ReconstructWorldPos(input.uv, depth);
float3 worldPos = ReconstructWorldPos(screenPos.xy, depth);
float waveOffset = SAMPLE_TEXTURE2D(_CausticMap, sampler_CausticMap, worldPos.xz * 0.025 + _Time.x * 0.25).w - 0.5;
float2 causticUV = CausticUVs(worldPos.xz, waveOffset);

caustics *= min(upperMask, lowerMask) * 1.5;
//return half4(waveOffset.xx, 0, 1);
//return half4(1, 0, 0, 1);
return half4(caustics + 1, 1);
}
ENDHLSL

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


half depthMulti = 1 / _MaxDepth;
half3 color = refraction;
color *= Absorption((depth.x) * depthMulti);
color += Scattering(depth.x * depthMulti) * (shadow * 0.5 + 0.5);// * saturate(1-length(reflection));// TODO - scattering from main light(maybe additional lights too depending on cost)
color += Scattering(depth.x * depthMulti) * (shadow * 0.75 + 0.15);// * saturate(1-length(reflection));// TODO - scattering from main light(maybe additional lights too depending on cost)
color *= 1 - saturate(foam);
//color *= 1-saturate(length(reflection));

float fogFactor = IN.fogFactorNoise.x;
comp = MixFog(comp, fogFactor);
return half4(comp, 1);
//return half4(spec.xxx, 1); // debug line
//return half4(color, 1); // debug line
//return half4(frac(IN.posWS.yyy), 1); // debug line
}
正在加载...
取消
保存