Unity 图片画线Shader
上周分享了一个水墨风格的Shader,这周分享一个图片画点连线的Shader,话不多说,先看效果
我做的是一个百度人脸识别划线功能,图中所有蓝色的点都是通过百度人脸识别获取到的,要做的也只是把Vector4的数组传给Shader去处理
public Material material;public List<Vector4> vector4s;public BaiduTest baidu;// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Z)){vector4s = baidu.allPoint;//material是图片所用材质球material.SetVectorArray("_Vectors", vector4s);material.SetInt("_VectorCount", vector4s.Count);}}
操作界面的基本属性
具体的Shader逻辑代码如下
Shader "Unlit/NewUnlitShader"
{Properties{_MainTex ("Texture", 2D) = "white" {}_Distortion ("_Distortion", Range(0.0, 1.0)) = 0.3_ScreenResolution ("_ScreenResolution", Vector) = (0.,0.,0.,0.)_LP1("linePoint1",vector) = (300,100,0,0) _LP2("linePoint2",vector) = (600,400,0,0) _LineWidth("LineWidth",range(0,20)) = 10.0_Point("LineWidth",range(0,100)) = 50.0}SubShader{Tags { "RenderType"="Opaque" }LOD 100Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex;struct appdata { float4 vertex : POSITION; }; struct appdata_t{float4 vertex : POSITION;float4 color : COLOR;float2 texcoord : TEXCOORD0;};struct v2f {float2 texcoord : TEXCOORD0;float4 vertex : SV_POSITION;float4 color : COLOR;};float4 _LP1; float4 _LP2; float _LineWidth; float _Point; half4 _MainTex_ST;v2f vert(appdata_t IN){v2f OUT;OUT.vertex = UnityObjectToClipPos(IN.vertex);OUT.texcoord = IN.texcoord;OUT.color = IN.color;return OUT;}int _VectorCount = 0;float4 _Vectors[1000];fixed4 frag (v2f i) : SV_Target {//画点操作for (int j=0;j<_VectorCount;j++){if( pow((i.vertex.x- _Vectors[j].x ),2) + pow((i.vertex.y- _Vectors[j].y ),2) <_Point) { return fixed4(0,0,1,1); } }int color=0;//划线操作for (int k=0;k<_VectorCount-1;k++){_LP1 = _Vectors[k];_LP2 = _Vectors[k+1];//划线分段处理if(_Vectors[k].x==0&&_Vectors[k].y==0&&_Vectors[k].z==0&&_Vectors[k].w==0){continue;}if(_Vectors[k+1].x==0&&_Vectors[k+1].y==0&&_Vectors[k+1].z==0&&_Vectors[k+1].w==0){continue;}//计算点到直线的距离 float d = abs((_LP2.y-_LP1.y)*i.vertex.x + (_LP1.x - _LP2.x)*i.vertex.y +_LP2.x*_LP1.y -_LP2.y*_LP1.x )/sqrt(pow(_LP2.y-_LP1.y,2) + pow(_LP1.x-_LP2.x,2)); //小于或者等于线宽的一半时,属于直线范围 if(d<=_LineWidth/2) {float width = _LineWidth/2;float xp=0;if(_LP1.x < _LP2.x){xp = clamp( i.vertex.x,_LP1.x-width , _LP2.x+width);}else{xp = clamp( i.vertex.x,_LP2.x- width, _LP1.x+width);}float yp=0;if(_LP1.y < _LP2.y){yp = clamp( i.vertex.y,_LP1.y- width, _LP2.y+width);}else{yp = clamp( i.vertex.y,_LP2.y- width , _LP1.y+width);}if(xp==i.vertex.x){if(yp==i.vertex.y){color=1;break;}}}//默认颜色color=0;continue;}if(color>0){return fixed4(1,0,0,1); }else{fixed4 col= tex2D(_MainTex,i.texcoord);return col;}}ENDCG}}
}
Shader代码我大体写了注释,主要也就是关于点集合的操作,唯一需要注意的是,因为我传的是一个点的集合,所以线必然是重头连到尾的,所以每条线段的点集合我都加了一个Vector4.Zero,所以下面的操作是为了跳过连线(PS:因为fixed4类型是不能直接拿出做比较的,所以只能把每个数字依次拿出来做比较)
就这么多,用的自取,或者私信我要代码。码字不易,用的上给点个赞,多谢
Unity 图片画线Shader相关推荐
- python中怎样在图片上画线段_python 实现PIL模块在图片画线写字
图片上画线条 import sys from PIL import Image,ImageDraw im = Image.open("th.png") draw = ImageDr ...
- python怎么在图片上写字的软件_python 实现PIL模块在图片画线写字
图片上画线条 import sys from PIL import Image,ImageDraw im = Image.open("th.png") draw = ImageDr ...
- python写字_python 实现PIL模块在图片画线写字
图片上画线条 import sys from PIL import Image,ImageDraw im = Image.open("th.png") draw = ImageDr ...
- python画波浪线_python 实现PIL模块在图片画线写字
图片上画线条 import sys from PIL import Image,ImageDraw im = Image.open("th.png") draw = ImageDr ...
- Android在图片画线(放大,旋转,平移后可继续正确画线,限制画线区域)
最近因为公司业务需求,需要给学校的老师们做一个在手机和电脑上同步进行图片上画线批注的效果.刚开始在github上看了几天, 但跟我们要求吻合的开源项目几乎没有.最后只能自己来实现一个相应的功能,经过一 ...
- PIL篇---python 实现PIL模块在图片画线写字
python 实现PIL模块在图片画线写字 from PIL import Image, ImageDraw, ImageFont# get an image base = Image.open('2 ...
- Unity GL画线的坑
Unity中使用GL画线时注意一个问题,材质设置pass的代码应该放在GL.Color后面.类似下面这样: void RenderLine(DTGizmos gizmos) {if (!gizmos) ...
- Picture Control 控件 加载图片 画线
编程中遇到问题,如何在已有图片上添加 虚线,图片使用Picture控件显示 参考网页https://bbs.csdn.net/topics/10044979 其中参考程序段如下: 这是我画线的程序: ...
- MATLAB 图片画线(简简单单一根线)
1.hua.m 主画线函数,rm是传过来的图片(灰度图像),(a,b),(c,d)为线的端点. 由于默认生成灰度图像全黑,所以这里直接将像素值改为1,画白线. 单独处理竖线和横线的情况. 为防止单独沿 ...
最新文章
- android基础ui控件,Android基础——基础UI控件
- 汇编语言---冒泡排序
- “诺奖风向标”沃尔夫奖出炉:数学颁给表示论,物理颁给激光
- uva 11732 - strcmp() Anyone? 不错的Trie题
- halcon算子盘点:Chapter 15灰度分割
- ProxySQL Cluster 概述
- Maven系列2--pom.xml 配置详解
- 使用Python 3.5/3.6监听本机任意窗口中的按键操作
- 客户端与服务站《Socket》
- u3d目标与摄像机之间的遮挡物变为透明
- 驱动开发入门 - 之二:Win7-x64 + VMWare (Win7-x64) + WinDbg 双机调试环境搭建
- 扫雷php,扫雷php
- 南京市城乡地籍信息一体化管理的几点思考
- 传说中的Q_PROPERTY怎么使用
- 计算机控制台什么意思,电脑上,控制面板是什么意思
- ubuntu 编译SDK报appt 问题,32
- [电影]《指环王》新老三部曲完全赏析(意外之旅)
- 小样本关系分类:STAD: Self-Training with Ambiguous Data for Low-Resource Relation Extraction Extraction
- [概述] 网络的性能指标
- linux下的c开发工具,4.3.2 Linux下的C开发工具