shader——SetGlobalTexture

本文介绍了在使用SetGlobalTexture设置全局纹理时应注意的问题,即在Shader中不能有与之同名的Properties属性,否则会使用Unity默认属性值。文中通过具体代码示例详细解释了这一限制,并提供了一个简单的Shader示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用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
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值