Article directory
The final effect of this article
I. Introduction
Hi, everyone, my name is Lin Xinfa.
A classmate privately asked me how to make 2D
the effect of water polo.
In fact, I see that someone has already written a corresponding tutorial on the Internet. Today, let's make one together~
2. Getting Started with ShaderGraph
I wrote an article before: "ShaderGraph usage tutorial and various special effects cases: Unity2020 (continuous update)"
may be used as ShaderGraph
a reference for everyone to get started.
This article directly enters the Graph
node production environment~
3. ShaderGraph makes 2D Loading water polo
Note: The version I use in this article is , the Unity
version 2021.1.9f1c1
is Universal RP
, 11.0.0
if the version you use is different from mine, it may be slightly different
1. Create Unlit Shader Graph
What we're going to do is 2D
an effect that doesn't require lighting, so we create one Unlit Shader Graph
,
LoadingBall
Okay , name it,
2. Create a circle
ShaderGraph
In the node, under the Procedural/Shape
menu, you can see the geometry node, as follows
We create a circle ( Ellipse
node),
Note:
Ellipse
It means ellipse. When theWidth
sumHeight
is equal, it is a circle, so when you want a circle, it is not a searchCircle
, but a searchEllipse
~
One by one Slider
, easy to resize,
3. The effect of filling from bottom to top
Thinking: How do we express the effect of filling a picture from bottom to top?
We decompose this question into two questions:
1. What is used to express the upper, lower, left, and right of the texture coordinates?
2. How to express the boundaries of the upper and lower parts of the filling effect?
To answer the first question first, in texture coordinates, we use the value range of UV坐标系
, U
and V
the value range is 0 ~ 1
, draw a picture,
through the UV
coordinates, we can sample any pixel of the texture, so we want to express from left to right , in fact, it is U
from 0
to 1
, if you want to express from bottom to top, it is V
from 0
to 1
.
In ShaderGraph
, there is a UV
node, through which we can access the U
sum V
,
because we want from bottom to top, so we only need V
the information of the axis, we can use Split
the UV
output to separate, after separation, U
the value corresponds Split
to R
, V
corresponding to the value Slplit
of G
, so we G
take it out and use the Preview
node to preview it,
At this point, we have obtained the effect V
from 0
to to 1
,
now to solve the second problem, how to express the boundaries of the upper and lower parts?
For this question, we must first understand what the boundary is. The boundary is a threshold. For example, above we 0
reach 1
this range, and I define 0.35
it as the threshold. A smaller value means below the boundary, and a larger value means above the boundary.
How to express it in ShaderGraph
? The node is going to be used here Step
, we create a new Step
node,
when it In
is greater than or equal Edge
to, Step
output 1
, otherwise output 0
,
we take the V
value just now as Step
, Eedge
and then use an Slider
as In
, that is to say, we take it In
as the threshold of the upper and lower boundaries, we pass In
the You can control the boundaries.
As follows, so that we show the effect of filling from bottom to top
4. Multiply the fill effect with the circle
We multiply 3
the filling effect obtained in the first 2
step with the circle in the first step to get a filled circle effect~
5. Fill border wave effect
The above border effect is flat and has no undulating effect. Now we are going to make a wavy effect for this border.
In other words, we're going Step
to In
make aperturbation, the first thing that comes to mind when doing perturbation isnoise, in ShaderGraph
, there are several kinds of noise,
You can choose according to your needs. Here I use Gradient Noise
noise.
We add the noise and the noise Slider
. You can see that the disturbing particles are too fine, causing the boundary to be torn.
We can adjust the noise Scale
. As follows,
we want to make the boundary move horizontally, we can Add a time-varying to the UV
noise , using the sum node,V
Offset
Time
Till And Offset
However, we see that the water surface fluctuates too much now,
We can make one for Remap
the noise and reduce its intensity,
At this time, the waves are much gentler. At this point
, the prototype of a water polo comes out.
6. Water polo color
We add a color to the water ball, create a Color
node, and multiply it with the water ball, as follows,
7. Output to the fragment shader
Output the water ball to the Fragment
(fragment shader) Base Color
, and also Alpha Clip Threshold
do transparency culling, as follows,
pay attention Graph Settings
to turn it on Alpha Clip
, the rule is to display the pixel when it is Alpha
larger than Alpha Clip Threshold
the pixel, otherwise discard the pixel, so as to obtain Mask
the effect,
8. Add a little more detail
Finally, we add a little more details. The final node structure is as follows
. Preview the effect.
Fourth, apply to the UI
1. Interface UI
Let's use it to UGUI
make a simple one UI
. Among them, use is used Image
to display the water polo.
Because the material ball has not been used yet, it Image
is a white square.
2. Make a material ball
We create a shader, name it LoadingBall
, and let it use the above ShaderGraph
,
as follows
Assign the shader to Image
, and the
interface effect is as follows,
3. Script control progress
We simply write a script to do progress control, the code is as follows
using UnityEngine;
using UnityEngine.UI;
public class LoadingBall : MonoBehaviour
{
[Range(0, 1)]
public float progress = 0.5f;
public Material mat;
public Text progressText;
private int propertyProgressID;
void Start()
{
propertyProgressID = Shader.PropertyToID("progress");
}
void Update()
{
mat.SetFloat(propertyProgressID, progress);
progressText.text = $"{
Mathf.Floor(progress * 100)}%";
}
}
Among them, I ShaderGraph
named the attribute name of the progress value in the progress
code, and used it in the code to Shader.PropertyToID("progress")
get the attribute ID
, and then modified the attribute value through Material
the SetFloat
method of winter,
hung the LoadingBall
script on the Image
node of the water polo, and assigned member variables, as follows,
4. Operation effect
The operation effect is as follows,
5. Project source code
I have uploaded the source code of the project in this article GitCode
. Interested students can download and study by themselves. The
project address is: https://gitcode.net/linxinfa/UnityLoadingBallSG
Note: The version I use in this article is, the Unity
version 2021.1.9f1c1
is Universal RP
, 11.0.0
if the version you use is the same as mine different, may vary slightly
Alright, I'm Lin Xinfa, https://blog.csdn.net/linxinfa , a developer who works
silently in a small company , I hope I can help more people who want to learn, and encourage each other~Unity
Unity