步骤1
python 跑通 yolact 官方测试代码
步骤2
将python代码通过C编写动态链接库(dll),以遍使用 unity C# 能够调用并获取到抠像图层, 确保调用后能够回传正确的抠像结果(关于如何或取python的yolact抠像图像mask层信息请关注的其他文章)
步骤3
调试 shader graph 连线图做出shader 纹理渲染效果
步骤4
提取yolact 抠像的alphi通道也就是mask通道图层,
通道分离通道然后合并,将RGB通道全部替换成A通道
private Mat GetAChannel(Mat mat_img)
{
List<Mat> scr_channels = new List<Mat>();
Core.split(mat_img, scr_channels);//分解通道
scr_channels[2] = scr_channels[3];
scr_channels[1] = scr_channels[3];
scr_channels[0] = scr_channels[3];
Core.merge(scr_channels, mat_img);
return mat_img;
}
将通道数据传给shader暴露出来的SelectTexture纹理设置
传递方法:
gameObject.GetComponent<MeshRenderer>().material.SetTexture("Texture2D_4B548A1F", texture_mask);
值得一提的是 Texture2D_4B548A1F 纹理是从哪里来的。
我们选择在project中shader工程文件 ,右侧点击 compile and show code 显示shader脚本代码,下面是shader暴露的纹理接口直接SetTexture。
最终运行效果,调整颜色或其他参数可获得很多有趣效果