"Unity3D ShaderLab Development combat explain" in Chapter 2 Unity Shader (shaders) form

Shader architecture

Shader "Tut/NewBie/FirstShader"{
	Properties{
		//[可选的特性]用来控制在编辑器上如何显示,可写可不写。
		//2D纹理,默认值为""、"white"、"back"、"gray"、"bump"、"red"。
		[可选的特性] _MyTex2D("Tex2D Value", 2D) = "white"{}
		
		_MyTex3D("Tex3D Value", 3D) = "white"{}//3D纹理
		_MyCube("Cube Value", Cube) = "white"{}//Cube纹理
		_MyColor("Color Value", Color) = (1,1,1,1)
		_MyVertor("Vector Value", vector) = (1,1,1,1)
		_MyInt("Int Value", int) = 1
		_MyFloat("Float Value", float) = 1
		_MyRange("Range Value", range(0,1) = 1
	}
	SubShader{
		Tags{"Queue"="Background" "RenderType"="Opaque"}
		
		Pass{
			//定义名字(必须大写),可以在其它Pass中用UsePass "路径/Pass名字"引用。
			Name "MYPASS"
			
			[Pass Tags]
			[RenderSetup]
			
			//使用之前要先声明一下。
			sampler2D _MyTex2D;
			sampler3D _MyTex3D;
			samplerCUBE _MyCube;
			
			float4 _MyColor;
			float4 _MyVector;
			
			float _MyFloat;
			float _MyRange;
		}
	}
	SubShader{
		...
	}
	FallBack "Diffuse"
}
  • [可选的特性]:How to control the display on the editor, write from time to write.
    • [HideInInspector]: Do not show editor.
    • [NoScaleOffset]: Display tiling / offset entry edit the texture is not editor.
    • [Normal]: This is a normals texture texture.
    • [HDR]: this is a texture (HDR) texture.
    • [Gamma]: sRGB values ​​designated in the editor interface float / vector attribute.
    • [PerRendererData]: texture attributes will form MaterialPropertyBlock incoming shader.
  • [RenderSetup]:
    • Cull Back | Front | Off
    • ZTest (Less | Greater | LEqual | GEqual | Equal | NotEqual | Always)
    • ZWrite On | Off
    • Offset factor unit (offset = m * factor + r * units)
      • m: slope polygon maximum depth (z changes / x and y change the smallest variation).
      • r: it is a constant (specifically dependent on the particular implementation).
    • Blend :
      • Blend Off:Do not mix.
      • Blend src dst:(Source color * src) + (target color * dst), a value of zero, one, SrcColor, SrcAlpha, DstColor, DstAlpha, OneMinusSrcColor, OneMinusSrcAlpha, OneMinusDstColor, OneMinusSrcAlpha.
      • BlendOp colorOp:colorOp为Add、Min、Max、Sub。
      • BlendOp colorOp, alphaOp
      • AlphaToMask On | Off
    • ColorMask RGB | A | 0 | any combination of R, G, B, A of the
  • SubShader Tags:SubShader in the Tags (Tags not Pass in).
    • Queue: rendering order, the value Background, Geometry, AlphaTest, Transparent, Overlay, specific figures.
    • RenderType: render type, is Opaque, Transparent, custom string, for rendering frequently replaced.
    • DisableBatching: whether to disable batch. True, False, LODFading,
    • ForceNoShadowCasting:
    • IgnoreProjector: Ignore the projected value of True, False.
    • CanUseSpriteAtlas:
    • PreviewType: How to display the Material Editor, the value of Sphere, Plane, Skybox.
  • Pass Tags:Pass in the Tags.
    • LightMode: light mode.
      • Always:
      • ForwardBase: prior to rendering, for ambient light, the main parallel light, spherical harmonic light, light texture.
      • ForwardAdd: Forward rendering, additional lighting each pixel is performed once.
      • Deferred: deferred rendering, render to GBuffer in.
      • ShadowCaster: rendering objects to the depth or depth shadow map.
      • MotionVectors: used to calculate the motion vector of each object.
      • PrepassBase: for the old deferred rendering, render the normal and specular exponent.
      • PrepassFinal: Old delay for rendering, rendering the final color, and the light emission.
      • Vertex: vertex lighting, if the object is not lightmaps light vertex being enabled.
      • VertexLMRGBM (deprecated): vertex lighting can be obtained unity_Lightmap process Lightmaps (for PC).
      • VertexLM: vertex lighting can be obtained unity_Lightmap process Lightmaps (for mobile devices).
    • PassFlags:
    • RequireOptions:
  • FallBack "Diffuse":If you do not find a suitable SubShader, to use "Diffuse" default Shader realized.
  • Shader语言:
    • GLSL:Between GLSLPROGRAM and ENDGLSL write GLSL code.
    • CG:CGPROGRAM between CG and ENDCG write code.

ShaderLab

  • ShaderLab:If you want to write a support lighting, shadows and other features of Shader, you can write directly Surface Shader, it has been for us a good package of all complexity.
Shader "Tul/NewBie/SurfShader"{
	SubShader{
		Tags{"RenderType"="Opaque"}
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert
		sampler2D _MainText;
		struct Input{
			float2 uv_MainTex;
		}
		vodi surf(Input IN, inout SurfaceOutput o){
			half4 c = tex2D(_MainTex, IN.uv_MainTex);
			o.Albedo= c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}
struct SurfaceOutput{
	half3 Albedo;
	half3 Normal;
	half3 Emission;
	half Specular;
	half Gloss;
	half Alpha;
}

Material to transfer data in C #

  • 向Shader的Properties中传递数据:
    • Material.SetTexture("_MyTexture", MyTexture);
    • Material.SetTexture("_MyCube", MyCube);
    • Material.SetColor("_MyColor", MyColor);
    • Material.SetVector("_MyVector", MyVector);
    • Material.SetFloat("_MyFloat", MyFloat);
    • Material.SetFloat("_MyRange", MyRange);
  • 矩阵的传递:
    • Properties can not be defined in the matrix.
    • Shader defined in the uniform float4x4 m;
    • Assignment Material.SetMatrix in C # ( "m", mat);

To be continued ...

Published 41 original articles · won praise 4 · Views 3897

Guess you like

Origin blog.csdn.net/weixin_42487874/article/details/103225223