shader——SetGlobalTexture
在使用SetGlobalTexture设置全局变量的时候,其shader中,不能有Property同名属性。
如下:
private int m_sourceID = Shader.PropertyToID("sourceTex");
private void CopyToCameraTarget(RenderTargetIdentifier source, RenderTargetIdentifier dest)
{
m_commandBuffer.SetGlobalTexture(m_sourceID, source);
m_commandBuffer.SetRenderTarget(dest, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
m_commandBuffer.DrawProcedural(Matrix4x4.identity, m_copyMaterial, 0, MeshTopology.Triangles, 3);
m_context.ExecuteCommandBuffer(m_commandBuffer);
m_commandBuffer.Clear();
}
这样就不能在shader中的Properties中加同名的属性了,否则设置全局变量使用的是unity属性中的默认值。
这个有点类似unity中C#的public变量,优先使用inspector面板中变量的值。
Shader "Unlit/CopyScreen"
{
/* Properties
{
sourceTex("Texture", 2D) = "white" {}
}*/
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 positionCS: SV_POSITION;
float2 uv: TEXCOORD0;
};
v2f vert (uint vertexID:SV_VertexID)
{
v2f output;
output.positionCS = float4(
vertexID <= 1 ? -1.0 : 3.0,
vertexID == 1 ? 3.0 : -1.0,
0.0, 1.0
);
output.uv = float2(
vertexID <= 1 ? 0.0 : 2.0,
vertexID == 1 ? 2.0 : 0.0
);
return output;
}
sampler2D sourceTex;
fixed4 frag (v2f i) : SV_Target
{
return tex2D(sourceTex, i.uv);
}
ENDCG
}
}
}