【问题标题】:Green Chrome Key Shader using depth使用深度的绿色 Chrome 键着色器
【发布时间】:2020-10-01 21:32:24
【问题描述】:

我编写了一个着色器,它将 RGB 相机值转换为 HSV,然后对绿色铬应用一些过滤。 当前问题

  1. 如果前景(玩家)的物体有绿色像素,它将被切掉。

  2. 我已经有了深度相机,如何使用该属性制作更好的切出镀铬键?

    frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv);

                 if (_ShowBackground)
                 {
                     fixed4 col2 = tex2D(_TexReplacer, i.uv);
                     col = col2;
                 }
                 else if (!_ShowOriginal)
                 {
                     fixed4 col2 = tex2D(_TexReplacer, i.uv);
    
                     float maskY = 0.2989 * _GreenColor.r + 0.5866 * _GreenColor.g + 0.1145 * _GreenColor.b;
                     float maskCr = 0.7132 * (_GreenColor.r - maskY);
                     float maskCb = 0.5647 * (_GreenColor.b - maskY);
    
                     float Y = 0.2989 * col.r + 0.5866 * col.g + 0.1145 * col.b;
                     float Cr = 0.7132 * (col.r - Y);
                     float Cb = 0.5647 * (col.b - Y);
    
                     float alpha = smoothstep(_Sensitivity, _Sensitivity + _Smooth, distance(float2(Cr, Cb), float2(maskCr, maskCb)));
    
                     col = (alpha * col) + ((1 - alpha) * col2);
                 }
                 return col;
             }
    

【问题讨论】:

  • 请注意,您应该避免在着色器中使用if else 语句。为此,您可以使用step(...)

标签: unity3d gpu shader fragment-shader


【解决方案1】:

Unity 的UnityObjectToClipPos(float3 pos) 让您将顶点转换为剪辑空间。这意味着01 在所有轴上,z 轴是与渲染相机的距离(我相信从近到远的剪裁平面)。

您可以使用此距离来简单地将您的键控应用于比给定阈值更远的顶点。

如果您不想使用归一化坐标,您还可以使用mul(unity_ObjectToWorld, vertex.position) 将您的顶点转换为世界空间,然后通过将世界位置与相机的世界乘以本地矩阵(您必须将其传递到你的着色器)。

要在着色器中访问相机的深度纹理,您可以使用_CameraDepthTexture (参见文档https://docs.unity3d.com/Manual/SL-CameraDepthTexture.html 部分着色器变量)。 您可以使用 tex2D(_CameraDepthTexture, i.uv);

像任何其他纹理一样对其进行采样

【讨论】:

  • 我已经有一个深度相机,你能解释一下这与你的答案有什么关系吗?我很困惑对不起
  • 谢谢,你能给我看一个伪代码吗,例如如果我可以从给我它的着色器中的函数中获取深度像素,我如何使用碎片着色器从阈值中丢弃像素距离?
  • 你简单用if(currentDepth <= threshold) return originalColor; else return modifiedColor;
猜你喜欢
  • 2012-08-02
  • 2011-11-25
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
相关资源
最近更新 更多