Preview_BlendNormalsNode.shader 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Shader "Hidden/BlendNormalsNode"
  2. {
  3. Properties
  4. {
  5. _A ("_A", 2D) = "white" {}
  6. _B ("_B", 2D) = "white" {}
  7. _C ("_C", 2D) = "white" {}
  8. }
  9. SubShader
  10. {
  11. CGINCLUDE
  12. #pragma vertex vert_img
  13. #pragma fragment frag
  14. #pragma target 3.0
  15. #include "UnityCG.cginc"
  16. #include "Preview.cginc"
  17. #include "UnityStandardUtils.cginc"
  18. float3 BlendNormalWorldspaceRNM(float3 n1, float3 n2, float3 vtxNormal)
  19. {
  20. float4 q = float4(cross(vtxNormal, n2), dot(vtxNormal, n2) + 1.0) / sqrt(2.0 * (dot(vtxNormal, n2) + 1));
  21. return n1 * (q.w * q.w - dot(q.xyz, q.xyz)) + 2 * q.xyz * dot(q.xyz, n1) + 2 * q.w * cross(q.xyz, n1);
  22. }
  23. float3 BlendNormalRNM(float3 n1, float3 n2)
  24. {
  25. float3 t = n1.xyz + float3(0.0, 0.0, 1.0);
  26. float3 u = n2.xyz * float3(-1.0, -1.0, 1.0);
  27. float3 r = (t / t.z) * dot(t, u) - u;
  28. return r;
  29. }
  30. ENDCG
  31. Pass
  32. {
  33. CGPROGRAM
  34. sampler2D _A;
  35. sampler2D _B;
  36. float4 frag(v2f_img i) : SV_Target
  37. {
  38. float3 a = tex2D( _A, i.uv ).rgb;
  39. float3 b = tex2D( _B, i.uv ).rgb;
  40. return float4(BlendNormals(a, b), 0);
  41. }
  42. ENDCG
  43. }
  44. Pass
  45. {
  46. CGPROGRAM
  47. sampler2D _A;
  48. sampler2D _B;
  49. float4 frag(v2f_img i) : SV_Target
  50. {
  51. float3 a = tex2D(_A, i.uv).rgb;
  52. float3 b = tex2D(_B, i.uv).rgb;
  53. return float4(BlendNormalRNM(a, b), 0);
  54. }
  55. ENDCG
  56. }
  57. Pass
  58. {
  59. CGPROGRAM
  60. sampler2D _A;
  61. sampler2D _B;
  62. sampler2D _C;
  63. float4 frag(v2f_img i) : SV_Target
  64. {
  65. float3 a = tex2D(_A, i.uv).rgb;
  66. float3 b = tex2D(_B, i.uv).rgb;
  67. float3 c = tex2D(_C, i.uv).rgb;
  68. return float4(BlendNormalWorldspaceRNM(a,b,c), 0);
  69. }
  70. ENDCG
  71. }
  72. }
  73. }