Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.1 KiB

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