Liquid On Screen Effect

I really wasn’t sure what to call this Unity3D post processing effect, but it’s a pretty nice one that creates a splash of liquid across the screen. It could be used in racing game when driving through a puddle, a boating game at any time, change the color to red and you’ve got a nice blood or lava splash, or easily modify it to have liquid dripping down the screen for rainy scenes, etc. etc. Just to have an idea of what I’m talking about, you can check out a WebGL example here – just click on the Unity scene to trigger the effect. If it seems useful, I’ll try to describe it below.

The effect starts with a particle system, so we’ll create that first. Start with a circular texture (say, 128×128) with a red/green normal map. Feel free to use the one here for simplicity’s sake. I know it looks pretty weird, but we’ll use those colors later to create a faky refraction effect you typically find in water drops.

Create a new material using an alpha blended particle shader and set that texture as the material’s particle texture. In your Unity scene create a particle system using that new material. I set mine to a max of 100 particles and a burst of 100 particles in Emission. Turn off looping and play on awake, set an initial start speed to pretty high (10 to 30 or so), set the particle life to a range of 2 to 5, a start size range of 1 to 3, and a small amount of gravity (say a range of .05 to .2). Turn on the Limit Velocity over Lifetime module and dampen the speed to a .1. Use a circular shape emitter with a radius of 2 and, in the renderer, set the mode to vertical billboard. For Color over Lifetime, I alpha’d the particle out towards the end, and in Size over Lifetime, I used a downwards circular slope starting at 1 and ending around .1 or thereabouts. When viewing the particle effect in the scene window, you should wind up with something similar to what’s below.

Next we’ll want to draw these particles into a texture in order to access them in a shader so create a 2D RenderTexture named EffectTexture. For a size, I went with 960×600, but that’s only because that’s the default size of Unity’s WebGL output. You can choose what makes sense to you. In a game supporting multiple resolutions, it may make more sense to create the texture in code at runtime, but for this example creating one in the editor is fine. For mine, I went with ARGB32 format, no depth buffer and no mipmaps.

Create a second camera in your scene, set to clear to a solid color and set the color to black. Set the projection to orthographic with a size of 10 or so. The clipping plane of this camera can be small. I set the near to 1 and far to 10. Set this camera’s target texture to the EffectTexture you just created and position the camera so that it captures the particle system as best as possible. Now, we don’t want these particles to be seen by our main camera, so create a new layer in Unity named “HiddenEffects” or whatever makes sense to you. Set the layer of your particle system to this HiddenEffects layer. For the orthographic camera you just created, set the Culling Mask to the HiddenEffects layer as well. In your Main Camera, set the Culling Mask to be everything except the HiddenEffects layer. Just a quick recap, we now have a particle effect that is drawn into a texture by an effect camera but not drawn into the game by the main camera. Just to add a bit of quick visual interest without a lot of work, I threw in a photo of Copenhagen as a 3D sprite, but if you have a 3D scene already set up you can use that. In any case, that is the scene set up complete.

The next thing we’ll want is a shader that will do all the actual work for us. In the Unity project create a new Image Effect Shader. In the shader add an _EffectTex property to make use of our render texture. I also know the first thing we’ll want to do is blur our effect texture, so we’ll add a blur function to our shader. So we don’t have to reinvent the wheel, we’ll just borrow the blur functions from the shader I talked about back in this post. For a blur amount, I went with a hardcoded, .0035, because that looked right. Feel free to play around with that or even throw in a Range property to adjust the blur amount at runtime. Our initial shader, then, looks like this:

Splash Effect Shader

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

/**

* Copyright (c) 2019 Devon O. Wolfgang

*

* Permission is hereby granted, free of charge, to any person obtaining a copy

* of this software and associated documentation files (the "Software"), to deal

* in the Software without restriction, including without limitation the rights

Before we go on, let’s create a new material that uses that SplashEffect shader we just created. Also, so we can start to see things, let’s create a post processor script which will apply that shader to to the scene and also trigger our particle effect. This script will want to expose two properties to the editor: the material using our shader and the particle system. Here, then is the entire post processor script:

PostProcessor.cs

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

/**

* Copyright (c) 2019 Devon O. Wolfgang

*

* Permission is hereby granted, free of charge, to any person obtaining a copy

* of this software and associated documentation files (the "Software"), to deal

* in the Software without restriction, including without limitation the rights

Attach that script to the Main Camera in the Unity scene. From the project panel, drag in the material you just created to the effect material slot, and from the scene panel, drag in the particle system we created to the particles slot. Finally, we have something we can see. If you run the game at this point and click on the scene, you’ll see something like below.

This is exactly as expected – which is to say, this is our particle system drawn into the render texture then blurred.

The next step we want to take is run a bit of threshold over that blurred image. This will have the effect of setting the alpha of the blurred image to either 1 or 0 which will have a result of ‘glooping’ it up a bit. Add a float _Threshold property to the shader which ranges from 0-1 and default it to .85 (obviously you can change that later, but that’s a decent setting). We’ll calculate the threshold by stepping between the _Threshold value and the alpha value of our blurred effect sample and multiply our effect sample by that value. So update the fragment shader to look like this:

Updated fragment shader

C#

1

2

3

4

5

6

7

fixed4 frag(v2fi):SV_Target

{

fixed4 fxSample=blur(_EffectTex,i.uv,.0035);

floatthresh=step(_Threshold,fxSample.w);

fxSample*=thresh;

returnfxSample;

}

If you run the game and click now, you’ll see something like below. Already the effect is really coming together.

So, what we’ve created at this point is the normal map for our liquid effect. Remember when I said before how we’d use that weird red/green particle texture to create the liquid refraction? Well, now’s the time to do that. In a nutshell what we want to do is sample our _MainTex texture using the normal map green channel for the u position and the green channel for the v position (Remember, when blitting in a post process shader, the _MainTex is actually our scene). Sounds confusing, but really, all we’re doing updating the fragment shader to the below:

Updated Fragment shader using Normal Map

C#

1

2

3

4

5

6

7

8

9

10

fixed4 frag(v2fi):SV_Target

{

fixed4 fxSample=blur(_EffectTex,i.uv,.0035);

floatthresh=step(_Threshold,fxSample.w);

fxSample*=thresh;

fixed4 effectColor=tex2D(_MainTex,fxSample.yx)*thresh;

returneffectColor;

}

And that will give you something that looks like this:

And that is really the whole effect! The only thing left to do is blend the effect with the main texture, add a color property to change the liquid color, and maybe add an alpha property to lower the strength of the effect. I won’t go into all of that, but the final shader I used looks like this:

Final SplashEffect.shader

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

/**

* Copyright (c) 2019 Devon O. Wolfgang

*

* Permission is hereby granted, free of charge, to any person obtaining a copy

* of this software and associated documentation files (the "Software"), to deal

* in the Software without restriction, including without limitation the rights

For my quick demo here, I used a threshold of .85, an alpha of .75, and light blue color for tint.

Hopefully this may help out. There’s plenty of room for improvement, like adding trails or subemitters to the particle effect for trailing drops of water, but this should be a good starting point for anyone looking to add some splash to their games.