Unity透明视频/去除视频黑色shader

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity透明视频/去除视频黑色shader相关的知识,希望对你有一定的参考价值。

参考技术A // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "ChromaKeyKit/ChromaKey/ChromaKey_Alpha_General"

Properties

_MainTex("MainTex", 2D) = "white"

_KeyColor("KeyColor", Color) = (1,1,1,1)

_DChroma("D Chroma", range(0.0, 1.0)) = 0.5

_DChromaT("D ChromaT", range(0.0, 1.0)) = 0.05

_DLuma("D Luma", range(0.0, 1.0)) = 0.5

_DLumaT("D LumaT", range(0.0, 1.0)) = 0.05



CGINCLUDE

#include "UnityCG.cginc"

struct VS_OUT

float4 position:POSITION;

half2 texcoord0:TEXCOORD0;

;

sampler2D _MainTex;

half4 _MainTex_ST;

half4 _KeyColor;

half _DChroma;

half _DLuma;

half _DLumaT;

half _DChromaT;

VS_OUT vert(appdata_base input)

VS_OUT o;

o.position = UnityObjectToClipPos(input.vertex);

o.texcoord0 = TRANSFORM_TEX(input.texcoord, _MainTex);

return o;



half3 RGB_To_YCbCr(half3 rgb)

half Y = 0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b;

half Cb = 0.564 * (rgb.b - Y);

half Cr = 0.713 * (rgb.r - Y);

return half3(Cb, Cr, Y);



half4 frag(VS_OUT input) : SV_Target

half4 c = tex2D(_MainTex, input.texcoord0);

half3 src_YCbCr = RGB_To_YCbCr(c.rgb);

half3 key_YCbCr = RGB_To_YCbCr(_KeyColor);

half dChroma = distance(src_YCbCr.xy, key_YCbCr.xy);

half dLuma = distance(src_YCbCr.z, key_YCbCr.z);

if (dLuma < _DLuma && dChroma < _DChroma)

half a = 0;

if (dChroma > _DChroma - _DChromaT)

a = (dChroma -_DChroma + _DChromaT) / _DChromaT;



if (dLuma > _DLuma - _DLumaT)

a = max(a, (dLuma - _DLuma + _DLumaT) / _DLumaT);



c.a = a;



return c;



ENDCG

SubShader

//Cull back

Tags "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True"

Lighting Off

ZWrite Off

AlphaTest Off

Blend SrcAlpha OneMinusSrcAlpha

Pass

CGPROGRAM

  #pragma vertex vert

  #pragma fragment frag

ENDCG





Fallback Off

如何去除视频中的黑色空间?斯威夫特

【中文标题】如何去除视频中的黑色空间?斯威夫特【英文标题】:How to remove black space in a video? SWIFTUI 【发布时间】:2021-06-17 15:42:08 【问题描述】:

我不知道如何去除视频中的黑色空间,.ignoresSafeArea(.all) 没有帮助。

image

这就是我的代码

    struct PlayerView: UIViewControllerRepresentable 
    var videoURL : String
    func makeUIViewController(context: UIViewControllerRepresentableContext<PlayerView>) -> AVPlayerViewController 
    
    let controller = AVPlayerViewController()
    let player = AVPlayer(url: URL(string: videoURL)!)
    player.play()
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main)  _ in
                           player.seek(to: CMTime.zero)
                           player.play()
       
        
    controller.player = player
    controller.showsPlaybackControls = false
    return controller

    

func updateUIViewController(_ uiViewController: AVPlayerViewController, context: UIViewControllerRepresentableContext<PlayerView>) 
    
    

【问题讨论】:

这是你的答案吗? ***.com/a/65795050/14733292 【参考方案1】:

这可能会解决问题:

    首先用视频的 aspectRatio 声明一个属性。例如:

    let aspectRatio: CGFloat = 478 / 849
    

    在查看代码的第二步,将视频播放器包装在 Geometry Reader 中,如下所示:

    GeometryReader  geo in  
        VideoPlayer(player: AVPlayer(url: URL(string: videoURL)!))  
            .frame(height: geo.size.width * aspectRatio)
    
    

【讨论】:

以上是关于Unity透明视频/去除视频黑色shader的主要内容,如果未能解决你的问题,请参考以下文章

如何去除视频中的黑色空间?斯威夫特

Unity中播放带有alpha通道格式为Mp4的视频

Unity 播放透明视频新思路

Android Videoview - 去除两侧的黑色空间

去除 youtube 缩略图上的黑色边框 4:3

UNITY记录——导入透明视频