当前位置:   article > 正文

Unity3D开发之在表面着色器shader中使用顶点着色器制造流光效果_unity模型按照顶点色上贴图

unity模型按照顶点色上贴图

先上流光效果。

变动的是世界坐标x值。当顶点世界坐标靠近变动值,则在附近取流光贴图颜色值附加到物体上。

首先,我们需要在使用shader模型位置处加上vertex:Myvert 后面名字是顶点着色器函数名字

#pragma surface surf Lambert alpha:fade keepalpha noshadow vertex:Myvert

然后编写顶点着色器片段。这里在结构体处增加了世界坐标。

  1. struct Input
  2. {
  3. float2 uv_texcoord;
  4. float3 worldSpacePos;
  5. };
  6. void Myvert(inout appdata_full v, out Input i)
  7. {
  8. UNITY_INITIALIZE_OUTPUT(Input, i);
  9. i.worldSpacePos = mul(unity_ObjectToWorld, v.vertex);
  10. }

然后就可以在表面着色器片段中编写自己的需求了。

整体代码如下:

  1. Shader "Map-base"
  2. {
  3. Properties
  4. {
  5. _Color("_Color", Color) = (0,0,0,1)
  6. _Normal("Normal", 2D) = "white" {}
  7. [HDR]_linecolor("line-color", Color) = (0,0,0,0)
  8. _linemap("line-map", 2D) = "white" {}
  9. [HideInInspector] _texcoord( "", 2D ) = "white" {}
  10. [HideInInspector] __dirty( "", Int ) = 1
  11. _FlowX("_FlowX",float)=0
  12. _FlowTex("_FlowTex",2D)="white" {}
  13. _FlowWidth("_FlowWidth",Range(0.01,1))=1
  14. }
  15. SubShader
  16. {
  17. Tags{ "RenderType" = "Transparent" "Queue" = "Transparent+100" "IgnoreProjector" = "True" "IsEmissive" = "true" }
  18. Cull Back
  19. CGPROGRAM
  20. #include "UnityStandardUtils.cginc"
  21. #pragma target 3.0
  22. #pragma surface surf Lambert alpha:fade keepalpha noshadow vertex:Myvert
  23. struct Input
  24. {
  25. float2 uv_texcoord;
  26. float3 worldSpacePos;
  27. };
  28. uniform sampler2D _Normal;
  29. uniform float4 _Normal_ST;
  30. uniform float4 _Color;
  31. uniform float4 _linecolor;
  32. uniform sampler2D _linemap;
  33. uniform float4 _linemap_ST;
  34. uniform float _FlowX;
  35. uniform sampler2D _FlowTex;
  36. uniform float _FlowWidth;
  37. void Myvert(inout appdata_full v, out Input i)
  38. {
  39. UNITY_INITIALIZE_OUTPUT(Input, i);
  40. i.worldSpacePos = mul(unity_ObjectToWorld, v.vertex);
  41. }
  42. void surf( Input i , inout SurfaceOutput o )
  43. {
  44. float2 uv_Normal = i.uv_texcoord * _Normal_ST.xy + _Normal_ST.zw;
  45. o.Normal = UnpackScaleNormal( tex2D( _Normal, uv_Normal ), 2.0 );
  46. float2 uv_linemap = i.uv_texcoord * _linemap_ST.xy + _linemap_ST.zw;
  47. o.Emission = ( _linecolor * tex2D( _linemap, uv_linemap ).a ).rgb;
  48. float d1=i.worldSpacePos.x-_FlowX;
  49. fixed4 c=fixed4(0,0,0,1);
  50. if(abs(d1)>_FlowWidth/2)
  51. c=fixed4(0,0,0,1);
  52. else c=tex2D(_FlowTex, fixed2(0.5+d1/_FlowWidth,1));
  53. o.Albedo = _Color.rgb+c.rgb;
  54. o.Alpha = _Color.a*c.a;
  55. }
  56. ENDCG
  57. }
  58. CustomEditor "ASEMaterialInspector"
  59. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/109494?site
推荐阅读
相关标签
  

闽ICP备14008679号