| 1 | // Custom shader to draw our toy cubes and balls with an outline around them. |
| 2 | Shader "Custom/ToyCubeOutline" |
| 3 | { |
| 4 | Properties |
| 5 | { |
| 6 | _Color("Color", Color) = (1,1,1,1) |
| 7 | _MainTex("Albedo", 2D) = "white" {} |
| 8 | [PerRendererData] _OutlineColor ("Outline Color", Color) = (0,0,0,1) |
| 9 | _OutlineWidth ("Outline width", Range (.002, 0.03)) = .005 |
| 10 | |
| 11 | [HideInInspector] _Mode ("__mode", Float) = 0.0 |
| 12 | [HideInInspector] _SrcBlend ("__src", Float) = 1.0 |
| 13 | [HideInInspector] _DstBlend ("__dst", Float) = 0.0 |
| 14 | [HideInInspector] _ZWrite ("__zw", Float) = 1.0 |
| 15 | } |
| 16 | |
| 17 | |
| 18 | CGINCLUDE |
| 19 | #include "UnityCG.cginc" |
| 20 | |
| 21 | struct appdata |
| 22 | { |
| 23 | float4 vertex : POSITION; |
| 24 | float3 normal : NORMAL; |
| 25 | }; |
| 26 | |
| 27 | struct v2f |
| 28 | { |
| 29 | float4 pos : SV_POSITION; |
| 30 | fixed4 color : COLOR; |
| 31 | }; |
| 32 | |
| 33 | uniform float _OutlineWidth; |
| 34 | uniform float4 _OutlineColor; |
| 35 | uniform float4x4 _ObjectToWorldFixed; |
| 36 | |
| 37 | // Pushes the verts out a little from the object center. |
| 38 | // Lets us give an outline to objects that all have normals facing away from the center. |
| 39 | // If we can't assume that, we need to tweak the math of this shader. |
| 40 | v2f vert(appdata v) |
| 41 | { |
| 42 | v2f o; |
| 43 | |
| 44 | // MTF TODO |
| 45 | // 1. Fix batching so that it actually occurs. |
| 46 | // 2. See if batching causes problems, |
| 47 | // if it does fix this line by adding that component that sets it. |
| 48 | //float4 objectCenterWorld = mul(_ObjectToWorldFixed, float4(0.0, 0.0, 0.0, 1.0)); |
| 49 | float4 objectCenterWorld = mul(unity_ObjectToWorld, float4(0.0, 0.0, 0.0, 1.0)); |
| 50 | float4 vertWorld = mul(unity_ObjectToWorld, v.vertex); |
| 51 | |
| 52 | float3 offsetDir = vertWorld.xyz - objectCenterWorld.xyz; |
| 53 | offsetDir = normalize(offsetDir) * _OutlineWidth; |
| 54 | |
| 55 | o.pos = UnityWorldToClipPos(vertWorld+offsetDir); |
| 56 | |
| 57 | o.color = _OutlineColor; |
| 58 | return o; |
| 59 | } |
| 60 | ENDCG |
| 61 | |
| 62 | SubShader |
| 63 | { |
| 64 | Tags { "Queue" = "Transparent" } |
| 65 | Pass |
| 66 | { |
| 67 | Name "OUTLINE" |
| 68 | // To allow the cube to render entirely on top of the outline. |
| 69 | ZWrite Off |
| 70 | Blend SrcAlpha OneMinusSrcAlpha |
| 71 | |
| 72 | CGPROGRAM |
| 73 | #pragma vertex vert |
| 74 | #pragma fragment frag |
| 75 | fixed4 frag(v2f i) : SV_Target |
| 76 | { |
| 77 | // Just draw the _OutlineColor from the vert pass above. |
| 78 | return i.color; |
| 79 | } |
| 80 | ENDCG |
| 81 | } |
| 82 | // Standard forward render. |
| 83 | UsePass "Standard/FORWARD" |
| 84 | } |
| 85 | |
| 86 | Fallback Off |
| 87 | } |