1
- using System ;
2
1
using Unity . Collections ;
3
2
using Unity . Mathematics ;
4
3
using UnityEngine . Experimental . Rendering ;
5
- using static Unity . Mathematics . math ;
6
4
7
5
namespace UnityEngine . Rendering . HighDefinition
8
6
{
@@ -30,11 +28,11 @@ public partial class HDRenderPipeline
30
28
// The pass used in the WaterFoam.shader
31
29
int m_ShoreWaveFoamGenerationPass ;
32
30
int m_OtherFoamGenerationPass ;
33
- int m_ReprojectionPass ;
31
+ int m_AttenuationPass ;
34
32
35
33
ComputeShader m_WaterFoamCS ;
36
34
int m_ReprojectFoamKernel ;
37
- int m_PostProcessFoamKernel ;
35
+ int m_AttenuateFoamKernel ;
38
36
39
37
// Keeps track of maximum possible foam intensity in to estimate when there is no more foam
40
38
float m_MaxInjectedFoamIntensity = 0.0f ;
@@ -54,11 +52,11 @@ void InitializeWaterFoam()
54
52
m_FoamTextureAtlas = new PowerOfTwoTextureAtlas ( ( int ) m_Asset . currentPlatformRenderPipelineSettings . foamAtlasSize , 0 , GraphicsFormat . R16G16_UNorm , name : "Water Foam Atlas" , useMipMap : false ) ;
55
53
m_WaterFoamCS = runtimeShaders . waterFoamCS ;
56
54
m_ReprojectFoamKernel = m_WaterFoamCS . FindKernel ( "ReprojectFoam" ) ;
57
- m_PostProcessFoamKernel = m_WaterFoamCS . FindKernel ( "PostProcessFoam " ) ;
55
+ m_AttenuateFoamKernel = m_WaterFoamCS . FindKernel ( "AttenuateFoam " ) ;
58
56
59
57
m_ShoreWaveFoamGenerationPass = m_FoamMaterial . FindPass ( "ShoreWaveFoamGeneration" ) ;
60
58
m_OtherFoamGenerationPass = m_FoamMaterial . FindPass ( "OtherFoamGeneration" ) ;
61
- m_ReprojectionPass = m_FoamMaterial . FindPass ( "Reprojection " ) ;
59
+ m_AttenuationPass = m_FoamMaterial . FindPass ( "Attenuation " ) ;
62
60
}
63
61
64
62
void ReleaseWaterFoam ( )
@@ -216,38 +214,46 @@ void UpdateWaterFoamSimulation(CommandBuffer cmd, WaterSurface currentWater)
216
214
else
217
215
m_MaxInjectedFoamIntensity = 1.0f ;
218
216
219
- // Grab the foam buffers
220
- RTHandle currentFoamBuffer = currentWater . foamBuffers [ 0 ] ;
221
- RTHandle tmpFoamBuffer = currentWater . foamBuffers [ 1 ] ;
222
-
223
217
using ( new ProfilingScope ( cmd , ProfilingSampler . Get ( HDProfileId . WaterSurfaceFoam ) ) )
224
218
{
219
+ RTHandle currentFoamBuffer = currentWater . FoamBuffer ( ) ;
225
220
BindPerSurfaceConstantBuffer ( cmd , m_WaterFoamCS , m_ShaderVariablesWaterPerSurface [ currentWater . surfaceIndex ] ) ;
226
221
227
- // Reproject the previous frame's foam buffer
228
- int tileC = HDUtils . DivRoundUp ( ( int ) currentWater . foamResolution , 8 ) ;
229
- cmd . SetComputeVectorParam ( m_WaterFoamCS , HDShaderIDs . _PreviousFoamRegionScaleOffset , currentWater . previousFoamRegionScaleOffset ) ;
230
- cmd . SetComputeTextureParam ( m_WaterFoamCS , m_ReprojectFoamKernel , HDShaderIDs . _WaterFoamBuffer , currentFoamBuffer ) ;
231
- cmd . SetComputeTextureParam ( m_WaterFoamCS , m_ReprojectFoamKernel , HDShaderIDs . _WaterFoamBufferRW , tmpFoamBuffer ) ;
232
- cmd . DispatchCompute ( m_WaterFoamCS , m_ReprojectFoamKernel , tileC , tileC , 1 ) ;
233
-
234
- // Apply an attenuation on the existing foam
235
- CoreUtils . SetRenderTarget ( cmd , tmpFoamBuffer ) ;
236
- cmd . DrawProcedural ( Matrix4x4 . identity , m_FoamMaterial , m_ReprojectionPass , MeshTopology . Triangles , 3 , 1 , currentWater . mpb ) ;
222
+ // Check if we need to reproj
223
+ if ( currentWater . previousFoamRegionScaleOffset . x != cb . _FoamRegionScale . x ||
224
+ currentWater . previousFoamRegionScaleOffset . y != cb . _FoamRegionScale . y ||
225
+ currentWater . previousFoamRegionScaleOffset . z != cb . _FoamRegionOffset . x ||
226
+ currentWater . previousFoamRegionScaleOffset . w != cb . _FoamRegionOffset . y )
227
+ {
228
+ RTHandle tmpFoamBuffer = currentWater . TmpFoamBuffer ( ) ;
229
+
230
+ // Reproject the previous frame's foam buffer
231
+ int tileC = HDUtils . DivRoundUp ( ( int ) currentWater . foamResolution , 8 ) ;
232
+ cmd . SetComputeVectorParam ( m_WaterFoamCS , HDShaderIDs . _PreviousFoamRegionScaleOffset , currentWater . previousFoamRegionScaleOffset ) ;
233
+ cmd . SetComputeTextureParam ( m_WaterFoamCS , m_ReprojectFoamKernel , HDShaderIDs . _WaterFoamBuffer , currentFoamBuffer ) ;
234
+ cmd . SetComputeTextureParam ( m_WaterFoamCS , m_ReprojectFoamKernel , HDShaderIDs . _WaterFoamBufferRW , tmpFoamBuffer ) ;
235
+ cmd . DispatchCompute ( m_WaterFoamCS , m_ReprojectFoamKernel , tileC , tileC , 1 ) ;
236
+
237
+ // Attenuate the foam buffer
238
+ cmd . SetComputeTextureParam ( m_WaterFoamCS , m_AttenuateFoamKernel , HDShaderIDs . _WaterFoamBuffer , tmpFoamBuffer ) ;
239
+ cmd . SetComputeTextureParam ( m_WaterFoamCS , m_AttenuateFoamKernel , HDShaderIDs . _WaterFoamBufferRW , currentFoamBuffer ) ;
240
+ cmd . DispatchCompute ( m_WaterFoamCS , m_AttenuateFoamKernel , tileC , tileC , 1 ) ;
241
+
242
+ // Update the foam data for the next frame
243
+ currentWater . previousFoamRegionScaleOffset = new float4 ( cb . _FoamRegionScale , cb . _FoamRegionOffset ) ;
244
+ }
245
+ else
246
+ {
247
+ // Attenuate the foam buffer
248
+ CoreUtils . SetRenderTarget ( cmd , currentFoamBuffer ) ;
249
+ cmd . DrawProcedural ( Matrix4x4 . identity , m_FoamMaterial , m_AttenuationPass , MeshTopology . Triangles , 3 , 1 , currentWater . mpb ) ;
250
+ }
237
251
238
252
// Then we render the deformers and the generators
239
253
if ( waterDeformers )
240
254
cmd . DrawProcedural ( Matrix4x4 . identity , m_FoamMaterial , m_ShoreWaveFoamGenerationPass , MeshTopology . Triangles , 6 , m_ActiveWaterDeformers , currentWater . mpb ) ;
241
255
if ( foamGenerators )
242
256
cmd . DrawProcedural ( Matrix4x4 . identity , m_FoamMaterial , m_OtherFoamGenerationPass , MeshTopology . Triangles , 6 , m_ActiveWaterFoamGenerators , currentWater . mpb ) ;
243
-
244
- // To avoid the swap in swap out of the textures, we do this.
245
- cmd . SetComputeTextureParam ( m_WaterFoamCS , m_PostProcessFoamKernel , HDShaderIDs . _WaterFoamBuffer , tmpFoamBuffer ) ;
246
- cmd . SetComputeTextureParam ( m_WaterFoamCS , m_PostProcessFoamKernel , HDShaderIDs . _WaterFoamBufferRW , currentFoamBuffer ) ;
247
- cmd . DispatchCompute ( m_WaterFoamCS , m_PostProcessFoamKernel , tileC , tileC , 1 ) ;
248
-
249
- // Update the foam data for the next frame
250
- currentWater . previousFoamRegionScaleOffset = new float4 ( cb . _FoamRegionScale , cb . _FoamRegionOffset ) ;
251
257
}
252
258
}
253
259
}
0 commit comments