(Ii) Unity vertex / fragment shader base

1 Introduction

This article explains some of the basics of the Unity shader to the absence of light shader as an example, subsequent detailed analysis and explanation. This can be directly copied to Shader using Unity (unity2018 / 2018 available pro-test), using the following analysis recommends watching side. Shader as follows:

Shader "LL/Unlit/UnlitShader_basicDismiss"
{
	Properties
	{
		_MainTex("Main Texture",2D)="white"{} _DissolveTex("Dissolve Texture",2D)="white"{} _DissolveCutoff("Dissolve Cutoff",Range(0,1))=1 _Dismiss("Model Dismiss",Range(-1,1))=0 } SubShader { pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" float _DissolveCutoff; float _Dismiss; sampler2D _MainTex; sampler2D _DissolveTex; struct a2v { float4 vertex:POSITION; float2 uv:TEXCOORD0; float3 normal:NORMAL; }; struct v2f { float2 uv:TEXCOORD0; float4 vertex:SV_POSITION; }; v2f vert(a2v v) { v2f f; f.uv=v.uv; v.vertex.xyz=v.vertex.xyz+v.normal*_Dismiss; f.vertex=mul(UNITY_MATRIX_MVP,v.vertex); //f.vertex=UnityObjectToClipPos(v.vertex); return f; } float4 frag(v2f f):SV_TARGET { float4 mainTexColor=tex2D(_MainTex,f.uv); float4 dissolveTexColor=tex2D(_DissolveTex,f.uv); clip(dissolveTexColor.rgb-_DissolveCutoff); return mainTexColor; } ENDCG } } } 

2.Shader name

Are named as follows, this shader name UnlitShader_basicDismiss, via the path LL / Unlit / to find.

Shader "LL/Unlit/UnlitShader_basicDismiss"
{
}

3. Properties

Property exposed in the Inspector window parameters, can be artificially changed, the basic format is: attribute name ( "Display Name" variable type) = default, as shown below.
Properties Basic format _MainTex("Main Texture",2D)="white"{}example explained: _MainTex variable name, display content on the first value within double quotation marks in parenthesis "Main Texture" the panel, a second value represented by 2D content for this variable, namely 2D texture . "White" {} or the default value is white texture, written {} double-quote, but not for the other variables, such as _DissolveTex type float, the value 1, after each variable without a semicolon.

	Properties
	{
		_MainTex("Main Texture",2D)="white"{} _DissolveTex("Dissolve Texture",2D)="white"{} _DissolveCutoff("Dissolve Cutoff",Range(0,1))=1 _Dismiss("Model Dismiss",Range(-1,1))=0 } 

And variable types using the type shown in the following table:

Property type example Explanation
Int _Int(“Int”,Int)=2 Int type, the default value is 2
Float _Float(“Float”,Float)=2.0 Float type, the default value is 2
Range _Range(“Range”,Range(0,1))=0.5 Float type, the default value is 0.5, and the lock range
Color _Color(“Color”,Color)=(1,1,1,1) Color type, the default value is white
Vector _Vector(“Vector”,Vector)=(1,1,1,1) Vector type (four-dimensional vector), the default value is white (1,1,1,1)
2D _MainTex(“Main Texture”,2D)=“white”{} 2D textures, the default is white texture
Cube _Cube(“SkyBox”,Cube)=“white”{} Cube textures, default is white texture
3D _MainTex(“Main Texture”,3D)=“white”{} 3D texture, the default value is white Texture

Where: Range type is a slider style, modify and manual sliding support direct input, but no slider style after 2018; Vector name type to see though is a vector, is in fact on behalf of the four-dimensional data, it can also be used as a position input; Cube said this texture is hexahedral texture.

4.SubShader

It may comprise a plurality of Unity Shader SubShader code blocks, a minimum, matching the selected target Unity Platform SubShader first loading operation, if not supported, Fallback semantics specified Shader is used. A SubShader can contain multiple pass. pass the statement defined between CGPROGRAM ENDCG, expressed using the CG / HLSL written, of course, it can also be used to write other languages.

	SubShader
	{
		pass
		{
		    CGPROGRAM
		    。。。
		    ENDCG
		}
	}

5. Define vertex / fragment shader

The method defined vertex shader Vert (This is the method name, can freely modify) by #pragma vertex, added simultaneously achieved. Similarly #pragma fragment defined by the fragment shader frag, change and implement methods. Where #include "UnityCG.cginc" indicates introduction method defined in this file, and the variable semantic chunk. This file will get chapters explain in detail.

			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc" float _DissolveCutoff; float _Dismiss; sampler2D _MainTex; sampler2D _DissolveTex; struct a2v { float4 vertex:POSITION; float2 uv:TEXCOORD0; float3 normal:NORMAL; }; struct v2f { float2 uv:TEXCOORD0; float4 vertex:SV_POSITION; }; v2f vert(a2v v) { v2f f; f.uv=v.uv; v.vertex.xyz=v.vertex.xyz+v.normal*_Dismiss; f.vertex=mul(UNITY_MATRIX_MVP,v.vertex); //f.vertex=UnityObjectToClipPos(v.vertex); return f; } float4 frag(v2f f):SV_TARGET { float4 mainTexColor=tex2D(_MainTex,f.uv); float4 dissolveTexColor=tex2D(_DissolveTex,f.uv); clip(dissolveTexColor.rgb-_DissolveCutoff); return mainTexColor; } 

5.1 variable references

In the above code, and defines two float sampler2D format variables, these two variables with the same name as the variable properties of the module, so that the value becomes available to the variable properties panel.

5.2 Custom Variables

Struct defines two types of v2f a2v in the above code. Wherein a struct defined variable format: Name Variable Type Variable + + semantics variables, as follows:

float4 vertex:POSITION;

float4 is a variable representing a vertex or an array of four-dimensional vectors. However, the meaning of which is represented by the variable semantic POSITION defined, this variable represents the vertex coordinates. So it was normal variable texture coordinates for this here at the vertex normals, uv. Similarly v2f in SV_POSITION was the clip space coordinates. Return value vert v2f type process, this time the clip-space vertex coordinates of vertex representation; in frag as inlet parameters, coordinates indicating the position of the vertex of the tile, by analogy, is also a sheet-membered uv uv coordinate. Specific reference on a vertex / fragment shader portion.

Variable type as shown in the following table

Types of DEFINITIONS
float The highest median data variables
half Intermediate data variable number of bits
fixed Lowest variable precision data bits (since the color value of 0-1, so the color type available fixed4)

+ Number variable type and n represents an n-dimensional variable, the value of each dimension of the corresponding type, such as fixed4, 4 denotes a variable dimension, each dimension type is fixed.

5.3 vertex shader vert

The method vert primary task is to convert the clip space vertex coordinates (i.e. GPU vert calls this method for each vertex processing), so that the inlet comprises at least one semantic parameter containing semantic vertex coordinates, the output value is SV_POSITION POSITION variable vert basic method is described below, other variables can be passed as required, such as the present embodiment also pass the uv and normals. Example UnityObjectToClipPos by the following method UnityCG.cginc the vertex coordinates transformed into the clip space. May be realized by mul (UNITY_MATRIX_MVP, v.vertex) method, i.e., directly into the matrix MVP coordinate variables. In fact UnityObjectToClipPos is a direct calculation matrix for mvp.

			float4 vert(float4 vertex:POSITION):SV_POSITION { return UnityObjectToClipPos(vertex); } 

In the present embodiment prior to computing the vertex coordinates do clip space vertex coordinate processing, i.e., moving distance _Dismiss normal direction thereof, if its value is 0, is not moved.

5.4 yuan shader frag

Fragment shader most fundamental is used to produce a color pixel, the inlet parameters can not directly return a color value, as follows:

            float4 frag():SV_TARGET
			{ return fixed4(0.5,1,0,1); } 

This embodiment is relatively complex, i.e., in accordance with uv value, the main sample textures by tex2D (_MainTex, f.uv) method, to obtain the corresponding color values ​​returned, according _DissolveTex texture, do clip discarded pixel ( dissolveTexColor.rgb-_DissolveCutoff); i.e., when the value is less than 0 discard clip off value, i.e. in this position does not return frag color value, corresponding to the position at this time will not show any color because the color component values ​​range rgb 0-1, the corresponding value is also _DissolveCutoff 0-1.

6. Conclusion

By a basic shader Shader The basic syntax explained clearly, follow-up will continue to expand.

Guess you like

Origin www.cnblogs.com/llstart-new0201/p/11953025.html