You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

  1. Shader "Hidden/WorldNormalVector"
  2. {
  3. Properties
  4. {
  5. _A ("_WorldNormal", 2D) = "white" {}
  6. }
  7. SubShader
  8. {
  9. Pass //not connected
  10. {
  11. CGPROGRAM
  12. #include "UnityCG.cginc"
  13. #pragma vertex vert_img
  14. #pragma fragment frag
  15. float4 frag(v2f_img i) : SV_Target
  16. {
  17. float2 xy = 2 * i.uv - 1;
  18. float z = -sqrt(1-saturate(dot(xy,xy)));
  19. float3 worldNormal = normalize(float3(xy, z));
  20. return float4(worldNormal, 1);
  21. }
  22. ENDCG
  23. }
  24. Pass //connected
  25. {
  26. CGPROGRAM
  27. #include "UnityCG.cginc"
  28. #pragma vertex vert_img
  29. #pragma fragment frag
  30. sampler2D _A;
  31. float4 frag(v2f_img i) : SV_Target
  32. {
  33. float2 xy = 2 * i.uv - 1;
  34. float z = -sqrt(1-saturate(dot(xy,xy)));
  35. float3 vertexPos = float3(xy, z);
  36. float3 normal = normalize(vertexPos);
  37. float3 worldNormal = UnityObjectToWorldNormal(normal);
  38. float3 tangent = normalize(float3( -z, xy.y*0.01, xy.x ));
  39. float3 worldPos = mul(unity_ObjectToWorld, float4(vertexPos,1)).xyz;
  40. float3 worldTangent = UnityObjectToWorldDir(tangent);
  41. float tangentSign = -1;
  42. float3 worldBinormal = normalize( cross(worldNormal, worldTangent) * tangentSign);
  43. float4 tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
  44. float4 tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
  45. float4 tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
  46. float2 sphereUVs = i.uv;
  47. sphereUVs.x = (atan2(vertexPos.x, -vertexPos.z) / (UNITY_PI) + 0.5);
  48. // Needs further checking
  49. //float3 tangentNormal = tex2Dlod(_A, float4(sphereUVs,0,0)).xyz;
  50. float3 tangentNormal = tex2D(_A, sphereUVs).xyz;
  51. worldNormal = fixed3( dot( tSpace0.xyz, tangentNormal ), dot( tSpace1.xyz, tangentNormal ), dot( tSpace2.xyz, tangentNormal ) );
  52. return float4(worldNormal, 1);
  53. }
  54. ENDCG
  55. }
  56. }
  57. }