PlaneGrid.shader 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. Shader "NRSDK/PlaneGrid"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _GridColor("Grid Color", Color) = (1.0, 1.0, 0.0, 1.0)
  7. _PlaneNormal("Plane Normal", Vector) = (1.0, -1.0, 0.0)
  8. _PlaneIn("Plane In Vector", Vector) = (0.0, -0.0, 0.0)
  9. _UvRotation("UV Rotation", float) = 30
  10. }
  11. SubShader
  12. {
  13. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
  14. Blend SrcAlpha OneMinusSrcAlpha
  15. ZTest on
  16. ZWrite off
  17. Cull off
  18. Pass
  19. {
  20. CGPROGRAM
  21. #pragma vertex vert
  22. #pragma fragment frag
  23. #include "UnityCG.cginc"
  24. struct appdata
  25. {
  26. float4 vertex : POSITION;
  27. float2 uv : TEXCOORD0;
  28. fixed4 color : COLOR;
  29. };
  30. struct v2f
  31. {
  32. float4 vertex : SV_POSITION;
  33. float2 uv : TEXCOORD0;
  34. fixed4 color : COLOR;
  35. };
  36. sampler2D _MainTex;
  37. float4 _MainTex_ST;
  38. float4 _GridColor;
  39. float3 _PlaneNormal;
  40. float3 _PlaneIn;
  41. fixed _UvRotation;
  42. v2f vert(appdata v)
  43. {
  44. v2f o;
  45. o.vertex = UnityObjectToClipPos(v.vertex);
  46. fixed cosr = cos(_UvRotation);
  47. fixed sinr = sin(_UvRotation);
  48. fixed2x2 uvrotation = fixed2x2(cosr, -sinr, sinr, cosr);
  49. // Construct two vectors that are orthogonal to the normal.
  50. // This arbitrary choice is not co-linear with either horizontal
  51. // or vertical plane normals.
  52. //const float3 arbitrary = float3(1.0, 1.0, 0.0);
  53. float3 vec_u = normalize(cross(_PlaneNormal, _PlaneIn));
  54. float3 vec_v = normalize(cross(_PlaneNormal, vec_u));
  55. // Project vertices in world frame onto vec_u and vec_v.
  56. float2 plane_uv = float2(dot(v.vertex.xyz, vec_u), dot(v.vertex.xyz, vec_v));
  57. float2 uv = plane_uv * _MainTex_ST.xy;
  58. o.uv = mul(uvrotation, uv);
  59. o.color = v.color;
  60. return o;
  61. }
  62. fixed4 frag(v2f i) : SV_Target
  63. {
  64. fixed4 col = tex2D(_MainTex, i.uv);
  65. return fixed4(_GridColor.rgb, col.r * i.color.a);
  66. }
  67. ENDCG
  68. }
  69. }
  70. }