1、Pass中添加
//添加GPU Instance预处理指令
#pragma multi_compile_instancing
2、VertexInput中添加
//为顶点实例化一个ID
UNITY_VERTEX_INPUT_INSTANCE_ID
3、VertexOutput中添加
//为顶点实例化一个ID
UNITY_VERTEX_INPUT_INSTANCE_ID
//在顶点着色器声明立体目标眼睛字段输出结构
//与普通的GPU Instancing不一样的地方,XR的Single-Pass-Instanced需要添加这个宏
UNITY_VERTEX_OUTPUT_STEREO
4、Vert中添加
//(1)
//初始化顶点信息
//这个宏必须在Vertex Shader的最开始调用
//如果你需要在Fragment Shader里访问Instanced属性,则需要在Fragment Shader的开始也用一下
//这个宏的目的在于让Instance ID在Shader函数里也能够被访问到。
UNITY_SETUP_INSTANCE_ID(v);
//(2)
//在顶点程序中,将实例ID从输入结构复制到输出结构
//在Vertex Shader中把Instance ID从输入结构拷贝至输出结构中
//只有当你需要在Fragment Shader中访问每个Instance独有的属性时才需要写这个宏。
UNITY_TRANSFER_INSTANCE_ID(v,o);
//分配立体目标的眼睛
//与普通的GPU Instancing不一样的地方,XR的Single-Pass-Instanced需要添加这个宏
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
5、frag中添加
//初始化顶点信息
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预处理指令
#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;
//为顶点实例化一个ID
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
//为顶点实例化一个ID
UNITY_VERTEX_INPUT_INSTANCE_ID
//在顶点着色器声明立体目标眼睛字段输出结构
//与普通的GPU Instancing不一样的地方,XR的Single-Pass-Instanced需要添加这个宏
UNITY_VERTEX_OUTPUT_STEREO
};
VertexOutput vert (VertexInput v) {
//初始化顶点信息
//这个宏必须在Vertex Shader的最开始调用
//如果你需要在Fragment Shader里访问Instanced属性,则需要在Fragment Shader的开始也用一下
//这个宏的目的在于让Instance ID在Shader函数里也能够被访问到。
UNITY_SETUP_INSTANCE_ID(v);
VertexOutput o = (VertexOutput)0;
//在顶点程序中,将实例ID从输入结构复制到输出结构
//在Vertex Shader中把Instance ID从输入结构拷贝至输出结构中
//只有当你需要在Fragment Shader中访问每个Instance独有的属性时才需要写这个宏。
UNITY_TRANSFER_INSTANCE_ID(v,o);
//分配立体目标的眼睛
//与普通的GPU Instancing不一样的地方,XR的Single-Pass-Instanced需要添加这个宏
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.uv0 = v.texcoord0;
o.pos = UnityObjectToClipPos( v.vertex );
return o;
}
float4 frag(VertexOutput i) : COLOR {
//初始化顶点信息
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"
}