ITeye博客

unity3d实现描边shader

wgcode · 2015-05-14 09:47 · Unity · 移动开发 · Markdown

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. Shader "Custom/OutLine" {Properties {
  2. _Color ("Main Color", Color) = (.5,.5,.5,1)
  3. _OutlineColor ("Outline Color", Color) = (0,0,0,1)
  4. _Outline ("Outline width", Range (0.0, 0.03)) = .005
  5. _MainTex ("Base (RGB)", 2D) = "white" { }
  6. }
  7. CGINCLUDE
  8. #include "UnityCG.cginc"
  9. struct appdata {
  10. float4 vertex : POSITION;
  11. float3 normal : NORMAL;
  12. };
  13. struct v2f {
  14. float4 pos : POSITION;
  15. float4 color : COLOR;
  16. };
  17. uniform float _Outline;
  18. uniform float4 _OutlineColor;
  19. v2f vert(appdata v) {
  20. v2f o;
  21. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  22. float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
  23. float2 offset = TransformViewToProjection(norm.xy);
  24. o.pos.xy += offset * o.pos.z * _Outline;
  25. o.color = _OutlineColor;
  26. return o;
  27. }
  28. ENDCG
  29. SubShader {
  30. Tags { "Queue" = "Transparent" }
  31. Pass {
  32. Tags { "LightMode" = "Always" }
  33. Cull Off
  34. ZWrite Off
  35. //ZTest Always//始终通过深度测试,即可以渲染
  36. //ColorMask RGB // alpha not used
  37. Blend SrcAlpha OneMinusSrcAlpha // Normal
  38. CGPROGRAM
  39. #pragma vertex vert
  40. #pragma fragment frag
  41. half4 frag(v2f i) :COLOR {
  42. return i.color;
  43. }
  44. ENDCG
  45. }
  46. Pass {
  47. Name "BASE"
  48. ZWrite On
  49. ZTest LEqual
  50. Blend SrcAlpha OneMinusSrcAlpha
  51. Material {
  52. Diffuse [_Color]
  53. Ambient [_Color]
  54. }
  55. Lighting On
  56. SetTexture [_MainTex] {
  57. ConstantColor [_Color]
  58. Combine texture * constant
  59. }
  60. SetTexture [_MainTex] {
  61. Combine previous * primary DOUBLE
  62. }
  63. }
  64. }
  65. Fallback "Diffuse"
  66. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。