[UnityShader2]顶点片段着色器实例(二)

参考链接:http://liweizhaolili.blog.163.com/blog/static/162307442015228112425158/


1.条纹向上运动

Shader "Custom/New" {
	Properties
	{
		_MainTex("Base (RGB)", 2D) = "white" {}
		_Speed("Speed", float) = 1
		_Width("Width", float) = 1
		_Space("Space", float) = 0.1
	}
	SubShader
	{
		//Blend One One
		//AlphaTest Greater 0.1
		Cull off
		Pass
		{
			CGPROGRAM
			#pragma vertex vert   
			#pragma fragment frag    
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float _Speed;
			float _Width;
			float _Space;

			struct vertexOutput 
			{
				float4 pos : SV_POSITION;
				float2 tex : TEXCOORD0;
				float4 srcPos : TEXCOORD1;
			};
			vertexOutput vert(appdata_full input)
			{
				vertexOutput output;
				output.pos = mul(UNITY_MATRIX_MVP, input.vertex);  
				output.tex = input.texcoord;
				output.srcPos = input.vertex;
				
				return output;
			} 
			float4 frag(vertexOutput input) : COLOR
			{
				/*if (fmod(abs(input.srcPos.y), _Space) < _Width)
				{
					float x = input.tex.x + _Time * _Speed;
					float2 newUV = float2(x, input.tex.y);
					float4 c = tex2D(_MainTex, newUV);
					return c;
				}*/
				float a = input.srcPos.y - _Time * _Speed;
				if (fmod(abs(a), _Space) < _Width)
				{
					float4 c = tex2D(_MainTex, input.tex);
					return c;
				}
				else
				{
					float4 c = float4(0, 0, 0, 0);
					clip(c.a - 0.5);
					return c;
				}			
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}


2.反色

Shader "Custom/New" {
	Properties
	{
		_MainTex("Base (RGB)", 2D) = "white" {}
		_Color("MainColor(RGB)", Color) = (1, 1, 1, 1)
	}
	SubShader
	{
		Pass
		{
			CGPROGRAM
			#pragma vertex vert   
			#pragma fragment frag    
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _Color;

			struct vertexOutput
			{
				float4 pos : SV_POSITION;
				float2 tex : TEXCOORD0;
				float4 srcPos : TEXCOORD1;
			};
			vertexOutput vert(appdata_full input)
			{
				vertexOutput output;
				output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
				output.tex = input.texcoord;
				output.srcPos = input.vertex;

				return output;
			}
			float4 frag(vertexOutput input) : COLOR
			{
				float4 c = tex2D(_MainTex, input.tex);
				float4 newCol = _Color - c;
				return newCol;	
			}
		ENDCG
	}
	}
	FallBack "Diffuse"
}


3.边缘高光

Shader "Custom/New" {
	Properties
	{
		_MainTex("Base (RGB)", 2D) = "white" {}
		//_Color("MainColor(RGB)", Color) = (1, 1, 1, 1)
		_RimColor("RimColor(RGB)", Color) = (1, 1, 1, 1)
		_RimPower("RimPower", float) = 1
	}
	SubShader
	{
		Pass
		{
			CGPROGRAM
			#pragma vertex vert   
			#pragma fragment frag    
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			//float4 _Color;
			float4 _RimColor;
			float _RimPower;

			struct vertexOutput
			{
				float4 pos : SV_POSITION;
				float2 tex : TEXCOORD0;
				float4 srcPos : TEXCOORD1;
				float3 col : COLOR;
			};
			vertexOutput vert(appdata_full input)
			{
				vertexOutput output;
				output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
				output.tex = input.texcoord;
				output.srcPos = input.vertex;

				float3 viewDir = normalize(ObjSpaceViewDir(input.vertex));
				float dotProduct = 1 - saturate(dot(input.normal, viewDir));
				//output.col = _RimColor.rgb * pow(dotProduct, _RimPower);
				float rimWidth = 0.7;
				output.col.rgb = smoothstep(1 - rimWidth, 1.0, dotProduct);
				output.col *= _RimColor;
				return output;
			}
			float4 frag(vertexOutput input) : COLOR
			{
				float4 a = float4(input.col, 1);
				float4 b = tex2D(_MainTex, input.tex);
				float4 c = a + b;
				return c;
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}


4.遮挡高光

Shader "Custom/New" {
	Properties
	{
		_MainTex("Base (RGB)", 2D) = "white" {}
		_RimColor("RimColor(RGB)", Color) = (1, 1, 1, 1)
		_RimPower("RimPower", float) = 1
	}
	SubShader
	{
		Pass
		{
			//Blend SrcAlpha OneMinusSrcAlpha
			Blend One One
			ZTest Greater

			CGPROGRAM
			#pragma vertex vert   
			#pragma fragment frag    
			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _RimColor;
			float _RimPower;

			struct vertexOutput
			{
				float4 pos : SV_POSITION;
				float2 tex : TEXCOORD0;
				float4 srcPos : TEXCOORD1;
				float3 col : COLOR;
			};
			vertexOutput vert(appdata_full input)
			{
				vertexOutput output;
				output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
				output.tex = input.texcoord;
				output.srcPos = input.vertex;

				float3 viewDir = normalize(ObjSpaceViewDir(input.vertex));
				float dotProduct = 1 - saturate(dot(input.normal, viewDir));
				//output.col = _RimColor.rgb * pow(dotProduct, _RimPower);
				float rimWidth = 0.7;
				output.col.rgb = smoothstep(1 - rimWidth, 1.0, dotProduct);
				output.col *= _RimColor;
				return output;
			}
			float4 frag(vertexOutput input) : COLOR
			{
				float4 a = float4(input.col, 1);
				return a * 2;
			}
			ENDCG
		}
		Pass
		{
			ZTest Less
			SetTexture[_MainTex]{}
		}
	}
	FallBack "Diffuse"
}

### GPU实例化与着色器优化 在Unity中实现高效的GPU实例化和着色器优化能够显著提升性能,尤其是在处理大量对象时。通过利用硬件加速功能,可以减少CPU负载并提高渲染效率。 #### 使用GPU实例化的原理 GPU实例化允许一次性绘制多个相同网格的对象,而不是逐个发送绘制调用给图形API。这减少了状态切换次数以及批处理开销。为了启用此特性,在编写自定义Shader时需加入`Instancing On`指令[^1]: ```hlsl Shader "Custom/InstancedShader" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_instancing struct appdata_t { float4 vertex : POSITION; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct v2f { float4 pos : SV_POSITION; UNITY_VERTEX_OUTPUT_STEREO }; sampler2D _MainTex; v2f vert (appdata_t v) { v2f o; UNITY_SETUP_INSTANCE_ID(v); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); o.pos = UnityObjectToClipPos(v.vertex); return o; } fixed4 frag (v2f i) : SV_Target { return tex2D(_MainTex, i.uv); } ENDCG } } } ``` 上述代码展示了如何创建支持实例化的简单顶点片段着色器,并启用了多编译选项来适应不同平台需求。 #### 实现细节 当使用带有实例化特性的材质时,可通过脚本批量设置变换矩阵和其他属性数据到MaterialPropertyBlock中传递给MeshRenderer组件。这样可以在一次DrawCall内完成大批量物体的更新操作[^2]: ```csharp using UnityEngine; public class InstancerExample : MonoBehaviour { public GameObject prefab; // 被实例化的预制件 private MaterialPropertyBlock mpb; void Start() { int count = 1000; Transform[] transforms = new Transform[count]; for(int i=0;i<count;++i){ var go = Instantiate(prefab); go.transform.SetParent(transform,false); transforms[i]=go.transform; } mpb=new MaterialPropertyBlock(); UpdateInstancesData(transforms); } void LateUpdate(){ UpdateInstancesData(GetComponentsInChildren<Transform>()); } void UpdateInstancesData(Transform[] ts){ Matrix4x4[] matrices = new Matrix4x4[ts.Length]; for(int i=0;i<matrices.Length && i<ts.Length;++i){ matrices[i]=ts[i].localToWorldMatrix; } Graphics.DrawMeshInstanced( ((SkinnedMeshRenderer)prefab.GetComponent(typeof(SkinnedMeshRenderer))).sharedMesh, 0,//submeshIndex ((Renderer)prefab.GetComponent(typeof(Renderer))).material, matrices, matrices.Length, mpb//optional material properties block. ); } } ``` 这段C#代码实现了动态管理场景中的实例化对象集合的功能,包括初始化阶段创建指定数量的副本及其后期位置姿态同步刷新逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值