ShowNormals.shader 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. Shader "Hidden/ShowNormals"
  2. {
  3. SubShader
  4. {
  5. Tags { "RenderType"="Opaque" }
  6. LOD 100
  7. Pass
  8. {
  9. CGPROGRAM
  10. #pragma vertex vert
  11. #pragma fragment frag
  12. #include "UnityCG.cginc"
  13. struct appdata
  14. {
  15. float4 vertex : POSITION;
  16. float3 normal : NORMAL;
  17. float4 tangent : TANGENT;
  18. float2 uv : TEXCOORD0;
  19. };
  20. struct v2f
  21. {
  22. float4 vertex : SV_POSITION;
  23. float2 uv : TEXCOORD0;
  24. float3x3 tbn : TEXCOORD1;
  25. };
  26. v2f vert (appdata v)
  27. {
  28. v2f o;
  29. o.vertex = UnityObjectToClipPos(v.vertex);
  30. o.uv = v.uv;
  31. float3 tangent = normalize(v.tangent.xyz - v.normal * dot(v.normal, v.tangent.xyz));
  32. float3 bitangent = cross(v.normal, tangent) * v.tangent.w;
  33. o.tbn = float3x3(tangent, bitangent, v.normal);
  34. return o;
  35. }
  36. sampler2D _BumpMap;
  37. float4 frag(v2f i) : SV_Target
  38. {
  39. float3 normal = tex2D(_BumpMap, i.uv).xyz;
  40. if (length(normal) == 0.0 || any(abs(normal) > 1.0))
  41. {
  42. normal = float3(0.0, 0.0, 1.0);
  43. }
  44. normal = (2.0 * normal) - 1.0;
  45. normal = normalize(mul(normal, i.tbn));
  46. normal = mul(normal, unity_ObjectToWorld).xyz;
  47. normal = (normal + 1.0) / 2.0;
  48. return float4(normal, 1.0);
  49. }
  50. ENDCG
  51. }
  52. }
  53. }