PlaneGrid.shader 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. Pass
  36. {
  37. CGPROGRAM
  38. #pragma vertex vert
  39. #pragma fragment frag
  40. #include "UnityCG.cginc"
  41. struct appdata
  42. {
  43. float4 vertex : POSITION;
  44. float2 uv : TEXCOORD0;
  45. fixed4 color : COLOR;
  46. };
  47. struct v2f
  48. {
  49. float4 vertex : SV_POSITION;
  50. float2 uv : TEXCOORD0;
  51. fixed4 color : COLOR;
  52. };
  53. sampler2D _MainTex;
  54. float4 _MainTex_ST;
  55. float4 _GridColor;
  56. float3 _PlaneNormal;
  57. fixed _UvRotation;
  58. v2f vert (appdata v)
  59. {
  60. v2f o;
  61. o.vertex = UnityObjectToClipPos(v.vertex);
  62. fixed cosr = cos(_UvRotation);
  63. fixed sinr = sin(_UvRotation);
  64. fixed2x2 uvrotation = fixed2x2(cosr, -sinr, sinr, cosr);
  65. // Construct two vectors that are orthogonal to the normal.
  66. // This arbitrary choice is not co-linear with either horizontal
  67. // or vertical plane normals.
  68. const float3 arbitrary = float3(1.0, 1.0, 0.0);
  69. float3 vec_u = normalize(cross(_PlaneNormal, arbitrary));
  70. float3 vec_v = normalize(cross(_PlaneNormal, vec_u));
  71. // Project vertices in world frame onto vec_u and vec_v.
  72. float2 plane_uv = float2(dot(v.vertex.xyz, vec_u), dot(v.vertex.xyz, vec_v));
  73. float2 uv = plane_uv * _MainTex_ST.xy;
  74. o.uv = mul(uvrotation, uv);
  75. o.color = v.color;
  76. return o;
  77. }
  78. fixed4 frag (v2f i) : SV_Target
  79. {
  80. fixed4 col = tex2D(_MainTex, i.uv);
  81. return fixed4(_GridColor.rgb, col.r * i.color.a);
  82. }
  83. ENDCG
  84. }
  85. }
  86. }