Shader "Hidden/MOITComposite" { Properties { [Toggle] _CATCHBIASERRORS ("Catch Bias Errors", Float) = 0 // set this on if you see bloom fireflies and are not able to fix it with the other controls / are scared to see it again in build } HLSLINCLUDE #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; v2f vert(uint vertexID: SV_VertexID) { v2f o; float2 uv = float2((vertexID << 1) & 2, vertexID & 2); float4 pos = float4(uv * 2.0 - 1.0, UNITY_NEAR_CLIP_VALUE, 1.0); #ifdef UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION pos = ApplyPretransformRotation(pos); #endif #if UNITY_UV_STARTS_AT_TOP uv = float2((vertexID << 1) & 2, 1.0 - (vertexID & 2)); #endif o.vertex = pos; o.uv = uv; return o; } #ifdef _MOMENT_SINGLE_PRECISION TEXTURE2D_FLOAT (_B0); #else TEXTURE2D_HALF(_B0); #endif // TEXTURE2D(_MOIT_Texture); SAMPLER(sampler_LinearClamp); TEXTURE2D_HALF(_MOIT); SAMPLER(sampler_MOIT); half4 MOITComposite(v2f input) : SV_Target { half4 moit = SAMPLE_TEXTURE2D(_MOIT, sampler_MOIT, input.uv); #ifdef _MOMENT_SINGLE_PRECISION float b0 = SAMPLE_TEXTURE2D(_B0, singleSampler, input.uv).r; #else half b0 = SAMPLE_TEXTURE2D(_B0, sampler_LinearClamp, input.uv).r; #endif moit.rgb /= moit.a; #if _CATCHBIASERRORS_ON // catch negative color values when alpha is very close to 1 (>0.99) // TODO: explore why the non shader graph tmpro shader writes negatives values in moit texture (in game view only) return half4(max(moit.rgb, 0.0), exp(-b0)); #else return half4(max(0,moit.rgb), exp(-b0)); #endif } ENDHLSL SubShader { Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" } ZWrite Off Cull Off Pass { Name "Composite" ZTest Always Blend OneMinusSrcAlpha SrcAlpha HLSLPROGRAM #pragma vertex vert #pragma fragment MOITComposite #pragma enable_d3d11_debug_symbols #pragma multi_compile_fragment _MOMENT_HALF_PRECISION __MOMENT_SINGLE_PRECISION #pragma shader_feature _CATCHBIASERRORS_ON ENDHLSL } // Pass // { // Name "CompositeFrameBuffer" // // ZTest Always // Blend OneMinusSrcAlpha SrcAlpha // // HLSLPROGRAM // // #pragma vertex Vert // #pragma fragment MOITCompositeFB // // #pragma shader_feature _CATCHBIASERRORS_ON // // FRAMEBUFFER_INPUT_X_FLOAT(0); // // half4 MOITCompositeFB(Varyings input) : SV_Target // { // UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); // // //float4 moit = SAMPLE_TEXTURE2D(_BlitTexture, singleSampler, input.texcoord); // //float4 moit = LOAD_FRAMEBUFFER_X_INPUT(0, input.positionCS.xy); // half4 moit = LOAD_FRAMEBUFFER_X_INPUT(0, input.positionCS.xy); // #ifdef _MOMENT_SINGLE_PRECISION // float b0 = SAMPLE_TEXTURE2D(_B0, singleSampler, input.texcoord).r; // #else // half b0 = SAMPLE_TEXTURE2D(_B0, singleSampler, input.texcoord).r; // #endif // moit.rgb /= moit.a; // // #if _CATCHBIASERRORS_ON // // catch negative color values when alpha is very close to 1 (>0.99) // // TODO: explore why the non shader graph tmpro shader writes negatives values in moit texture (in game view only) // return half4(max(moit.rgb, 0.0), exp(-b0)); // #else // return half4(moit.rgb, exp(-b0)); // #endif // } // // ENDHLSL // } } }