PlaneGrid.shader 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //-----------------------------------------------------------------------
  2. // <copyright file="PlaneGrid.shader" company="Google LLC">
  3. //
  4. // Copyright 2019 Google LLC. All Rights Reserved.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. // </copyright>
  19. //-----------------------------------------------------------------------
  20. Shader "ARCore/PlaneGrid"
  21. {
  22. Properties
  23. {
  24. _MainTex ("Texture", 2D) = "white" {}
  25. _GridColor ("Grid Color", Color) = (1.0, 1.0, 0.0, 1.0)
  26. _PlaneNormal ("Plane Normal", Vector) = (0.0, 0.0, 0.0)
  27. _UvRotation ("UV Rotation", float) = 30
  28. }
  29. SubShader
  30. {
  31. Tags { "Queue"="Transparent" "RenderType"="Transparent" }
  32. Blend SrcAlpha OneMinusSrcAlpha
  33. ZTest on
  34. ZWrite off
  35. cull off
  36. Pass
  37. {
  38. CGPROGRAM
  39. #pragma vertex vert
  40. #pragma fragment frag
  41. #include "UnityCG.cginc"
  42. struct appdata
  43. {
  44. float4 vertex : POSITION;
  45. float2 uv : TEXCOORD0;
  46. fixed4 color : COLOR;
  47. };
  48. struct v2f
  49. {
  50. float4 vertex : SV_POSITION;
  51. float2 uv : TEXCOORD0;
  52. fixed4 color : COLOR;
  53. };
  54. sampler2D _MainTex;
  55. float4 _MainTex_ST;
  56. float4 _GridColor;
  57. float3 _PlaneNormal;
  58. fixed _UvRotation;
  59. v2f vert (appdata v)
  60. {
  61. v2f o;
  62. o.vertex = UnityObjectToClipPos(v.vertex);
  63. fixed cosr = cos(_UvRotation);
  64. fixed sinr = sin(_UvRotation);
  65. fixed2x2 uvrotation = fixed2x2(cosr, -sinr, sinr, cosr);
  66. // Construct two vectors that are orthogonal to the normal.
  67. // This arbitrary choice is not co-linear with either horizontal
  68. // or vertical plane normals.
  69. const float3 arbitrary = float3(1.0, 1.0, 0.0);
  70. float3 vec_u = normalize(cross(_PlaneNormal, arbitrary));
  71. float3 vec_v = normalize(cross(_PlaneNormal, vec_u));
  72. // Project vertices in world frame onto vec_u and vec_v.
  73. float2 plane_uv = float2(dot(v.vertex.xyz, vec_u), dot(v.vertex.xyz, vec_v));
  74. float2 uv = plane_uv * _MainTex_ST.xy;
  75. o.uv = mul(uvrotation, uv);
  76. o.color = v.color;
  77. return o;
  78. }
  79. fixed4 frag (v2f i) : SV_Target
  80. {
  81. fixed4 col = tex2D(_MainTex, i.uv);
  82. return fixed4(_GridColor.rgb, col.r * i.color.a);
  83. }
  84. ENDCG
  85. }
  86. }
  87. }