Unity 后处理优化

复制代码
  public void Draw(CommandBuffer cmd, RenderTargetIdentifier source, int destination, int pass = -1)
    {
        cmd.SetGlobalTexture(mSourceTextureId, source);
        CoreUtils.SetRenderTarget(cmd, destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
        if (pass == -1 || bloomMaterial == null)
        {

            cmd.DrawProcedural(Matrix4x4.identity, copyMaterial, 0, MeshTopology.Triangles, 3);
        }
        else
        {
            cmd.DrawProcedural(Matrix4x4.identity, bloomMaterial, pass, MeshTopology.Triangles, 3);
            
        }
    }

cs部分

复制代码
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"

TEXTURE2D(_SourceTexture);
SAMPLER(sampler_SourceTexture);

TEXTURE2D(_CameraDepthTexture);
SAMPLER(sampler_CameraDepthTexture);

struct Varyings {
    float4 positionCS : SV_POSITION;
    float2 uv : TEXCOORD0;
    UNITY_VERTEX_OUTPUT_STEREO
};

half4 GetSource(float2 uv) {
    return SAMPLE_TEXTURE2D(_SourceTexture, sampler_SourceTexture, uv);
}

half4 GetSource(Varyings input) {
    return GetSource(input.uv);
}

float SampleDepth(float2 uv) {
    #if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
    return SAMPLE_TEXTURE2D_ARRAY(_CameraDepthTexture, sampler_CameraDepthTexture, uv, unity_StereoEyeIndex).r;
    #else
    return SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv);
    #endif
}

float SampleDepth(Varyings input) {
    return SampleDepth(input.uv);
}
struct ScreenSpaceData {
    float4 positionCS;
    float2 uv;
};

ScreenSpaceData GetScreenSpaceData(uint vertexID : SV_VertexID) {
    ScreenSpaceData output;
    // 根据id判断三角形顶点的坐标
    // 坐标顺序为(-1, -1) (-1, 3) (3, -1)
    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);
    // 不同API可能会产生颠倒的情况 进行判断
    if (_ProjectionParams.x < 0.0) {
        output.uv.y = 1.0 - output.uv.y;
    }
    return output;
}

Varyings Vert(uint vertexID : SV_VertexID) {
    Varyings output;
    ScreenSpaceData ssData = GetScreenSpaceData(vertexID);
    output.positionCS = ssData.positionCS;
    output.uv = ssData.uv;
    return output;
}

shader

复制代码
Shader "Hidden/PostProcess/PostProcessCopy" {
    SubShader {
        Tags {
            "RenderType"="Opaque"
            "RenderPipeline" = "UniversalPipeline"
        }
        LOD 200
        ZWrite Off
        Cull Off

        HLSLINCLUDE
        #include "PostProcessing.hlsl"
        ENDHLSL

        Pass {
            Name "CopyPass"

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment frag;

            half4 frag(Varyings input) : SV_Target {
                half4 color = GetSource(input);
                return half4(color.rgb, 1.0);
            }
            ENDHLSL
        }
    }
}

CopyShader

相关推荐
皮皮林5515 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
卡尔特斯9 小时前
Android Kotlin 项目代理配置【详细步骤(可选)】
android·java·kotlin
白鲸开源9 小时前
Ubuntu 22 下 DolphinScheduler 3.x 伪集群部署实录
java·ubuntu·开源
ytadpole9 小时前
Java 25 新特性 更简洁、更高效、更现代
java·后端
纪莫9 小时前
A公司一面:类加载的过程是怎么样的? 双亲委派的优点和缺点? 产生fullGC的情况有哪些? spring的动态代理有哪些?区别是什么? 如何排查CPU使用率过高?
java·java面试⑧股
JavaGuide10 小时前
JDK 25(长期支持版) 发布,新特性解读!
java·后端
用户37215742613510 小时前
Java 轻松批量替换 Word 文档文字内容
java
白鲸开源10 小时前
教你数分钟内创建并运行一个 DolphinScheduler Workflow!
java
Java中文社群11 小时前
有点意思!Java8后最有用新特性排行榜!
java·后端·面试
代码匠心11 小时前
从零开始学Flink:数据源
java·大数据·后端·flink