1、Pass中添加
//添加GPU Instance預(yù)處理指令
#pragma multi_compile_instancing
2氧敢、VertexInput中添加
//為頂點(diǎn)實(shí)例化一個(gè)ID
UNITY_VERTEX_INPUT_INSTANCE_ID
3侯嘀、VertexOutput中添加
//為頂點(diǎn)實(shí)例化一個(gè)ID
UNITY_VERTEX_INPUT_INSTANCE_ID
//在頂點(diǎn)著色器聲明立體目標(biāo)眼睛字段輸出結(jié)構(gòu)
//與普通的GPU Instancing不一樣的地方另凌,XR的Single-Pass-Instanced需要添加這個(gè)宏
UNITY_VERTEX_OUTPUT_STEREO
4、Vert中添加
//(1)
//初始化頂點(diǎn)信息
//這個(gè)宏必須在Vertex Shader的最開(kāi)始調(diào)用
//如果你需要在Fragment Shader里訪問(wèn)Instanced屬性戒幔,則需要在Fragment Shader的開(kāi)始也用一下
//這個(gè)宏的目的在于讓Instance ID在Shader函數(shù)里也能夠被訪問(wèn)到吠谢。
UNITY_SETUP_INSTANCE_ID(v);
//(2)
//在頂點(diǎn)程序中,將實(shí)例ID從輸入結(jié)構(gòu)復(fù)制到輸出結(jié)構(gòu)
//在Vertex Shader中把Instance ID從輸入結(jié)構(gòu)拷貝至輸出結(jié)構(gòu)中
//只有當(dāng)你需要在Fragment Shader中訪問(wèn)每個(gè)Instance獨(dú)有的屬性時(shí)才需要寫(xiě)這個(gè)宏溪食。
UNITY_TRANSFER_INSTANCE_ID(v,o);
//分配立體目標(biāo)的眼睛
//與普通的GPU Instancing不一樣的地方囊卜,XR的Single-Pass-Instanced需要添加這個(gè)宏
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
5、frag中添加
//初始化頂點(diǎn)信息
UNITY_SETUP_INSTANCE_ID(i);
6错沃、示例代碼
Shader "CCsShader/PipeFlow" {
Properties {
_Color ("Color", Color) = (0.07843138,0.3921569,0.7843137,1)
_CutValue ("CutValue", Range(0, 1)) = 0
[MaterialToggle] _Invert ("Invert", Float ) = 0
_FlowMap ("FlowMap", 2D) = "white" {}
_Speed ("Speed", Float ) = 1
_ColorFlow ("ColorFlow", Color) = (0.5,0.5,0.5,1)
_MainTex ("MainTex", 2D) = "white" {}
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
//添加GPU Instance預(yù)處理指令
#pragma multi_compile_instancing
#pragma multi_compile_fwdbase
#pragma only_renderers d3d9 d3d11 glcore gles gles3 metal d3d11_9x xboxone ps4 psp2 n3ds wiiu switch
#pragma target 3.0
uniform float4 _Color;
uniform float _CutValue;
uniform fixed _Invert;
uniform sampler2D _FlowMap; uniform float4 _FlowMap_ST;
uniform float _Speed;
uniform float4 _ColorFlow;
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
//為頂點(diǎn)實(shí)例化一個(gè)ID
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
//為頂點(diǎn)實(shí)例化一個(gè)ID
UNITY_VERTEX_INPUT_INSTANCE_ID
//在頂點(diǎn)著色器聲明立體目標(biāo)眼睛字段輸出結(jié)構(gòu)
//與普通的GPU Instancing不一樣的地方栅组,XR的Single-Pass-Instanced需要添加這個(gè)宏
UNITY_VERTEX_OUTPUT_STEREO
};
VertexOutput vert (VertexInput v) {
//初始化頂點(diǎn)信息
//這個(gè)宏必須在Vertex Shader的最開(kāi)始調(diào)用
//如果你需要在Fragment Shader里訪問(wèn)Instanced屬性,則需要在Fragment Shader的開(kāi)始也用一下
//這個(gè)宏的目的在于讓Instance ID在Shader函數(shù)里也能夠被訪問(wèn)到枢析。
UNITY_SETUP_INSTANCE_ID(v);
VertexOutput o = (VertexOutput)0;
//在頂點(diǎn)程序中玉掸,將實(shí)例ID從輸入結(jié)構(gòu)復(fù)制到輸出結(jié)構(gòu)
//在Vertex Shader中把Instance ID從輸入結(jié)構(gòu)拷貝至輸出結(jié)構(gòu)中
//只有當(dāng)你需要在Fragment Shader中訪問(wèn)每個(gè)Instance獨(dú)有的屬性時(shí)才需要寫(xiě)這個(gè)宏。
UNITY_TRANSFER_INSTANCE_ID(v,o);
//分配立體目標(biāo)的眼睛
//與普通的GPU Instancing不一樣的地方醒叁,XR的Single-Pass-Instanced需要添加這個(gè)宏
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.uv0 = v.texcoord0;
o.pos = UnityObjectToClipPos( v.vertex );
return o;
}
float4 frag(VertexOutput i) : COLOR {
//初始化頂點(diǎn)信息
UNITY_SETUP_INSTANCE_ID(i);
////// Lighting:
////// Emissive:
float node_4467_if_leA = step(_Invert,1.0);
float node_4467_if_leB = step(1.0,_Invert);
float4 node_6752 = _Time;
float node_6378 = (node_6752.r*_Speed);
float2 node_1422 = (i.uv0+node_6378*float2(0,1));
float2 node_4467 = lerp((node_4467_if_leA*(i.uv0+node_6378*float2(0,-1)))+(node_4467_if_leB*node_1422),node_1422,node_4467_if_leA*node_4467_if_leB);
float4 _FlowMap_var = tex2D(_FlowMap,TRANSFORM_TEX(node_4467, _FlowMap));
float3 emissive = (_ColorFlow.rgb*_FlowMap_var.r);
float3 finalColor = emissive + _Color.rgb;
float node_7797 = 1.0;
float node_7284_if_leA = step(_Invert,node_7797);
float node_7284_if_leB = step(node_7797,_Invert);
float node_5687_if_leA = step(i.uv0.g,_CutValue);
float node_5687_if_leB = step(_CutValue,i.uv0.g);
float node_2463 = 0.0;
float node_8445_if_leA = step(i.uv0.g,(1.0 - _CutValue));
float node_8445_if_leB = step((1.0 - _CutValue),i.uv0.g);
float node_8445 = lerp((node_8445_if_leA*node_2463)+(node_8445_if_leB*node_7797),node_2463,node_8445_if_leA*node_8445_if_leB);
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
return fixed4(finalColor,(lerp((node_7284_if_leA*lerp((node_5687_if_leA*node_7797)+(node_5687_if_leB*node_2463),node_2463,node_5687_if_leA*node_5687_if_leB))+(node_7284_if_leB*node_8445),node_8445,node_7284_if_leA*node_7284_if_leB)*_MainTex_var.r));
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}