StandardSurface shader 基础学习(一)

理解Unity标准表面着色器到顶点片段着色器的转换
本文详细记录了Unity标准表面着色器(Standard Shader)的基础结构和工作原理,通过分析其源码并转化为顶点片段着色器(vert frag shader)的过程,深入探讨了表面着色器如何处理光照和颜色。在工作实践中,作者发现将内置的surface shader转换为vfshader有助于增强对shader内部逻辑的理解,同时在遇到特定需求时,如ASE中standard shader连线到vfshader的转换,这样的知识尤为关键。

开启此篇章的原因: 现在都已经更新到 URP了,并且日常工作中也不会使用到Standard shader(工作中的shader都是自己人写的),之前了解过,也翻看过基本的standard shader 代码逻辑(尤其是PBR相关)。。。。

(1)之前在B站听大佬说:我认识的TA大佬,都把unity 内置的shader源码搂过一遍了。。。。。。 于是乎 我也想跟随大佬的脚步,认认真真的学习整理一遍,加深自己的记忆,也为以后忘记 迅速查找留下痕迹。。。

(2)最近由于工作需要,美术同学使用ASE 使用standard shader 连线做的shader… 需要翻译为普通的vf shader,。。。。。。于是乎,开启此系列:

此篇记录standardSurface shader 的基本结构
以及将standardSurface(后面简称 surface shader) 生成普通的vert frag shader (后面简称 vf shader)的代码:

在代码中有基本的结构注释,从 surface shader中声明的函数, 对应到 生成的 vf shader中的调用,体会达到的目的,更加深入的理解surface shader。

Shader "Custom/SurfaceShader01" {
   
   
	Properties {
   
   
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {
   
   }
	}
	SubShader {
   
   
		Tags {
   
    "RenderType"="Opaque" }
		LOD 200

		CGPROGRAM
		
		//
		// vertex: vert  第一步
		// vert  在顶点着色器中调用: 负责初始化一个 v2f_surf 的结构体

		// surf  第二步
		// 对传递进来的描述物体外表面材质属性信息的 SurfaceOutput结构体进行赋值 ,并且返回


		// BasicDiffuse  第三步
		//  在第二步的处理后,  用第二步的表面片元信息, 用BasicDiffuse 进行光照计算

		// final  第四步
		// 在最后的颜色输出之前, 对颜色进行自定义的修改,按照final 函数进行处理,并且输出返回	

		#pragma surface surf BasicDiffuse vertex:vert finalcolor: final noforwardadd
		#pragma target 3.0

		sampler2D _MainTex;
		float4 _Color;

		struct Input {
   
   
			float2 uv_MainTex;
		};
		void vert(inout appdata_full v,out Input o)
		{
   
   
			o.uv_MainTex = v.texcoord.xy;
		}


		//  下面两行是GPU Instance 所需要的声明 ,现在可以忽略删除
		// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
		// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
		// #pragma instancing_options assumeuniformscaling
		UNITY_INSTANCING_BUFFER_START(Props)
			// put more per-instance properties here
		// 声明改顶点是否位于视线域内,来判断这个顶点是否需要输出到frag中
		UNITY_INSTANCING_BUFFER_END(Props)


		void surf (Input IN, inout SurfaceOutput o) {
   
   
			// 只用最简单的颜色输出
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}


		//  此处的 LightingBasicDiffuse  函数的名字由两部分组成: Lighting + BasicDiffuse  
		//   BasicDiffuse  即为上面#pragma中声明的的函数
		inline float4 LightingBasicDiffuse(SurfaceOutput s,fixed3 lightDir,fixed atten)
		{
   
   
			float diffuse_term = saturate(dot(s.Normal,lightDir));
			float4 col;
			col.rgb = s.Albedo.rgb * _LightColor0.rgb * diffuse_term;
			col.a = s.Alpha;
			return col;
		}

		void final(Input IN,SurfaceOutput o,inout fixed4 color)
		{
   
   
			color = fixed4(0,1,0,1);	
		}
		ENDCG
	}
	FallBack "Diffuse"
}

在点击“show generated code” 查看vf shader 生成的具体代码:
查找上面声明函数的对应关系和具体实现的效果!!!!!
在这里插入图片描述

Shader "Custom/SurfaceShader01" {
   
   
	Properties {
   
   
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {
   
   }
	}
	SubShader {
   
   
		Tags {
   
    "RenderType"="Opaque" }
		LOD 200

		
	// ------------------------------------------------------------
	// Surface shader code generated out of a CGPROGRAM block:
	

	// ---- forward rendering base pass:
	Pass {
   
   
		Name "FORWARD"
		Tags {
   
    "LightMode" = "ForwardBase" }

CGPROGRAM
// compile directives
#pragma vertex vert_surf
#pragma fragment frag_surf
#pragma target 3.0
#pragma multi_compile_instancing
#pragma multi_compile_fog
#pragma multi_compile_fwdbase
#include "HLSLSupport.cginc"
#include "UnityShaderVariables.cginc"
#include "UnityShaderUtilities.cginc"
// -------- variant for: <when no other keywords are defined>
#if !defined(INSTANCING_ON)
// Surface shader code generated based on:
// vertex modifier: 'vert'
// writes to per-pixel normal: no
// writes to emission: no
// writes to occlusion: no
// needs world space reflection vector: no
// needs world space normal vector: no
// needs screen space position: no
// needs world space position: no
// needs view direction: no
// needs world space view direction: no
// needs world space position for lighting: no
// needs world space view direction for lighting: no
// needs world space view direction for lightmaps: no
// needs vertex color: no
// needs VFACE: no
// passes tangent-to-world matrix to pixel shader: no
// reads from normal: no
// 1 texcoords actually used
//   float2 _MainTex
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"

#define INTERNAL_DATA
#define WorldReflectionVector(data,normal) data.worldRefl
#define WorldNormalVector(data,normal) normal

// Original surface shader snippet:
#line 8 ""
#ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
#endif
/* UNITY: Original start of shader */
		
		//
		// vertex: vert  第一步
		// vert  在顶点着色器中调用: 负责初始化一个 v2f_surf 的结构体

		// surf  第二步
		// 对传递进来的描述物体外表面材质属性信息的 SurfaceOutput结构体进行赋值 ,并且返回


		// BasicDiffuse  第三步
		//  在第二步的处理后,  用第二步的表面片元信息, 用BasicDiffuse 进行光照计算

		// final  第四步
		// 在最后的颜色输出之前, 对颜色进行自定义的修改,按照final 函数进行处理,并且输出返回	

		//#pragma surface surf BasicDiffuse vertex:vert finalcolor: final noforwardadd
		//#pragma target 3.0

		sampler2D _MainTex;
		float4 _Color;

		struct Input {
   
   
			float2 uv_MainTex;
		};
		void vert(inout appdata_full v,out Input o)
		{
   
   
			o.uv_MainTex = v.texcoord.xy;
		}


		//  下面两行是GPU Instance 所需要的声明 ,现在可以忽略删除
		// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
		// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
		// //#pragma instancing_options assumeuniformscaling
		UNITY_INSTANCING_BUFFER_START(Props)
			// put more per-instance properties here
		// 声明改顶点是否位于视线域内,来判断这个顶点是否需要输出到frag中
		UNITY_INSTANCING_BUFFER_END(Props)


		void surf (Input IN, inout SurfaceOutput o) {
   
   
			// 只用最简单的颜色输出
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}


		//  此处的 LightingBasicDiffuse  函数的名字由两部分组成: Lighting + BasicDiffuse  
		//   BasicDiffuse  即为上面#pragma中声明的的函数
		inline float4 LightingBasicDiffuse(SurfaceOutput s,fixed3 lightDir,fixed atten)
		{
   
   
			float diffuse_term = saturate(dot(s.Normal,lightDir));
			float4 col;
			col.rgb = s.Albedo.rgb * _LightColor0.rgb * diffuse_term;
			col.a = s.Alpha;
			return col;
		}

		void final(Input IN,SurfaceOutput o,inout fixed4 color)
		{
   
   
			color = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值