This section of the tutorial Nanjing byte Workshop original article, reproduced, please keep this information: https://blog.csdn.net/gcj2450/article/details/104523738
Nanjing byte workshop, specializing in all kinds of interactive custom software development, 3D visualization custom application development, VR, AR class custom development of interactive applications.
Today we learn mathematics Shader achieve a Cartesian coordinate system through the Unity Shader, so that we become more familiar with the mathematical methods used in Unity Shader. XY axis by a mathematical coordinate system, XY-axis direction and the unit cells constituting the arrow. The final results as shown
The first step we use basic mathematical method to draw squares. Create a new Unlit Shader in Unity. Delete _MainTexture property and related variables required to define the grid shader property. Attribute is defined as follows
The Properties
{
// background color
_BgColor ( " BgColor " , Color) = ( . 1 , . 1 , . 1 , . 1 )
// out of line color
_LinesColor ( " LinesColor " , Color) = ( 0.7 , 0.7 , 0.7 , 1.0 )
// times level line color
_SubLinesColor ( " SubLinesColor " , color) = ( 0.95 , 0.95 , 0.95 , 1.0 )
// scale
_Scale("Scale",range(1,100)) = 10
_Subdiv("Subdiv",range(1,10)) = 10
}
Here are the properties of the background color, line color is the color of the secondary line segments scaling value and a minimum scale value.
Enter the following code in the Fragment Shader
vec2 pos = i.uv;
Pos define a variable, and scale it.
pos * = _Scale;
By taking the remainder operation, duplicate values obtained, was added _Scale = 10, will be repeated 10 times between 0-1
POS = FRAC (POS);
vec4 fragColor = _BgColor;
By step, the return value of the lower left corner, where the method step, determining if equivalent,
step(a, x) | Returns (x >= a) ? 1 : 0 |
BL = vec2 STEP ( 0.01 , POS); // bottom left
vec2 TR = STEP ( 0.01 , 1.0 -POS); // upper right
The result of the above two operations of multiplication, will get all> 0, the position of
a float Alpha = bl.x tr.x * * * bl.y tr.y;
The method then mix by mixing basic color and line color.
Mix = fragColor (fragColor, _LinesColor, . 1 -alpha);
the pos Subdiv times enlarged, the above operation is repeated, to give decile scale squares
vec2 BL2 = STEP ( 0.01 , FRAC (pos * _Subdiv)); // bottom left
vec2 tr2 = STEP ( 0.01 , 1.0 - FRAC (POS _Subdiv *)); // the upper right
a float alpha2 = bl2.x tr2.x * * * bl2.y tr2.y;
fragColor = Mix (fragColor, _SubLinesColor, . 1 -alpha2);
returns the final color.
return fragColor;
After completion of the above effects obtained finally, to obtain the final results are as follows: the FIG _Scale = 2, _Subdive = 5
The complete code is as follows:
Shader " Unlit / StepGrid "
{
the Properties
{
// background color
_BgColor ( " BgColor " , Color) = ( . 1 , . 1 , . 1 , . 1 )
// out of line color
_LinesColor ( " LinesColor " , Color) = ( 0.7 , 0.7 , 0.7 , 1.0 )
// secondary line color
_SubLinesColor ( " SubLinesColor " , color) = ( 0.95 , 0.95 , 0.95,1.0)
//缩放
_Scale("Scale",range(1,100)) = 10
_Subdiv("Subdiv",range(1,10)) = 10
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define vec2 half2
#define vec3 half3
#define vec4 half4
#define mix lerp
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float _Scale;
float _Subdiv;
fixed4 _BgColor;
fixed4 _LinesColor;
fixed4 _SubLinesColor;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
vec2 pos = i.uv;
pos *= _Scale;
pos=frac(pos);
vec4 fragColor = _BgColor;
vec2 bl = step(0.01,pos); // 左下
vec2 tr = step(0.01,1.0-pos); // 右上
float alpha =bl.x * bl.y * tr.x * tr.y;
fragColor = mix(fragColor, _LinesColor, 1-alpha);
vec2 bl2 = step(0.01,frac(pos*_Subdiv)); // 左下
vec2 tr2 = step(0.01,1.0-frac(pos*_Subdiv)); // upper right
a float alpha2 = bl2.x tr2.x * * * bl2.y tr2.y;
fragColor = Mix (fragColor, _SubLinesColor, . 1 -alpha2);
return fragColor;
}
ENDCG
}
}
}
simple Shader above to achieve Shader with a very basic scale of the grid. The next section we will continue to optimize.