UnityShaderLab 实现黑白着色器效果

实现思路:取屏幕像素的RGB值,将三个通道的值相加,除以一个大于值使颜色值在0-1内,再乘上一个强度值调节黑白强度。

在URP中实现需要开启Opaque Texture

ShaderGraph实现:

ShaderLab实现:

cpp 复制代码
Shader "BlackAndWhite"
{
    Properties
    {
    }
    SubShader
    {
        Tags { "RenderType"="Transprent" "Queue"="Geometry" }

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            TEXTURE2D_X(_CameraOpaqueTexture);
            SAMPLER(sampler_CameraOpaqueTexture);

            float3 SampleSceneColor(float2 uv)
            {
                return SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, UnityStereoTransformScreenSpaceTex(uv)).rgb;
            }

            float4 ComputeScreenPos (float4 pos, float projectionSign)
            {
               float4 o = pos * 0.5f;
               o.xy = float2(o.x, o.y * projectionSign) + o.w;
               o.zw = pos.zw;
               return o;
            }

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float4 ScreenPosition : TEXCOORD0;
            };


            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
                o.ScreenPosition = ComputeScreenPos(TransformWorldToHClip(mul(unity_ObjectToWorld, v.vertex)),_ProjectionParams.x);
                return o;
            }

            float4 frag (v2f i) : SV_Target
            {
                float4 _ScreenPosition_Out_0 = float4(i.ScreenPosition.xy / i.ScreenPosition.w, 0, 0);
                float3 col = SampleSceneColor(_ScreenPosition_Out_0.xy);
                float c = (col.x + col.y + col.z) / 3;
                return float4(c,c,c,1);
            }
            ENDHLSL
        }
    }
}

实现效果:

对比将物体只变成透明:

参考链接:

Black & White Shader - Unity (URP/HDRP) Shader Graph (youtube.com)

相关推荐
UTwelve9 天前
【UE】材质与半透明 - 01.将半透明作为后期材质
ue5·材质·着色器
二狗哈19 天前
Cesium快速入门29:CMZL数据格式加载
3d·状态模式·webgl·cesium·着色器·地图可视化
二狗哈21 天前
Cesium快速入门27:GeoJson自定义样式
前端·cesium·着色器
二狗哈23 天前
Cesium快速入门24:Appearance编写着色器修改外观
3d·webgl·cesium·着色器·地图可视化
二狗哈24 天前
Cesium快速入门22:fabric自定义着色器
运维·开发语言·前端·webgl·fabric·cesium·着色器
米芝鱼1 个月前
UnityURP3D管线自定义功能shader
游戏·unity·shader·urp·着色器
联系QQ 19226381 个月前
PEM电解槽Simulink模型,得出I-V曲线图,通过调参可以分析各参数对电解电压的影响。 ...
着色器
Echo_NGC22371 个月前
【AirSim 教程指南】Part 3:相机与传感器(RGB / 深度 / 分割 / LiDAR)
人工智能·计算机视觉·游戏引擎·ar·无人机·图形渲染·着色器
gis分享者1 个月前
学习threejs,使用自定义GLSL 着色器,实现抽象艺术特效
threejs·着色器·glsl·shadermaterial·抽象艺术
熊猫悟道1 个月前
Unity shader 之,Shader内部时间离散处理
unity·游戏引擎·材质·着色器