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

相关推荐
IT_张三3 分钟前
Docker+Kubernetes_第一章_Docker入门
java·docker·kubernetes
知初~10 分钟前
Scala基础学习
开发语言·学习·scala
郑州吴彦祖77220 分钟前
数据结构——二叉树经典习题讲解
java·数据结构·算法·leetcode
ふり24 分钟前
图书管理系统
java·开发语言·intellij-idea
楠枬27 分钟前
网页五子棋——匹配模块
java·spring boot·websocket
qq_124987075339 分钟前
Java+SpringBoot+Vue+数据可视化的综合健身管理平台(程序+论文+讲解+安装+调试+售后)
java·开发语言·spring boot·毕业设计
煤炭里de黑猫39 分钟前
Lua C API:深入理解 lua_pushnumber 函数 — 将数字压入 Lua 栈中
开发语言·lua
哥坐11路44 分钟前
网络IP跳动问题解决详
开发语言·php
奔跑吧邓邓子1 小时前
【Python爬虫(27)】探索数据可视化的魔法世界
开发语言·爬虫·python·数据可视化
qq_12498707531 小时前
Java+SpringBoot+Vue+数据可视化的美食餐饮连锁店管理系统
java·spring boot·毕业设计·美食