base.chunk 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // base coordinates & color expressions for shader-graph
  2. // fraction is saturation percentage, 0 means gray
  3. vec3 Desaturation(vec3 color, float fraction)
  4. {
  5. vec3 gray = vec3(dot(color, GRAY_VECTOR));
  6. return mix(color, gray, fraction);
  7. }
  8. vec2 Rotator(vec2 uv, vec2 centerUV, float time, float speed)
  9. {
  10. uv -= centerUV;
  11. vec3 dir = vec3(uv.x, 0.0, uv.y);
  12. float dirLength = length(dir);
  13. dir /= dirLength + EPSILON;
  14. dir = RotationVecFromAxisY(dir, time * speed) * dirLength;
  15. return vec2(dir.x, dir.z) + centerUV;
  16. }
  17. // return 0.0 when out of range
  18. // hardness: 0 - 1
  19. float SphereMask(vec2 center, vec2 point, float radius, float hardness)
  20. {
  21. float length = length(center - point);
  22. float coef = pow(saturate(length / radius), 1.0 / (1.0 - saturate(sqrt(hardness)) + EPSILON));
  23. return length > radius ? 0.0 : 1.0 - coef;
  24. }
  25. float SphereMask(vec3 center, vec3 point, float radius, float hardness)
  26. {
  27. float length = length(center - point);
  28. float coef = pow(saturate(length / radius), 1.0 / (1.0 - saturate(sqrt(hardness)) + EPSILON));
  29. return length > radius ? 0.0 : 1.0 - coef;
  30. }
  31. // For Fragment Shader
  32. // tangent space -> world space
  33. // same as TransformVector
  34. vec3 TransformNormalMap(vec3 vectorFromNormalMap)
  35. {
  36. vec3 vectorTS = normalize(vectorFromNormalMap - vec3(0.5));
  37. vec3 normal = normalize(FSInput_worldNormal);
  38. vec3 tangent = normalize(FSInput_worldTangent);
  39. vec3 binormal = CalculateBinormal(normal, tangent, FSInput_mirrorNormal);
  40. vec3 vecWS = vectorTS.x * tangent + vectorTS.y * binormal + vectorTS.z * normal;
  41. return vecWS * vec3(1.0, 1.0, -1.0); // inverse Z for RH
  42. }
  43. // WorldPos -> UV
  44. vec2 GetTriplanarMappingUV()
  45. {
  46. vec3 Up = vec3(0.0, 1.0, 0.0);
  47. vec3 U = cross(Up, FSInput_worldNormal.xyz);
  48. if (length(U) < 0.01)
  49. U = vec3(1.0, 0.0, 0.0);
  50. else
  51. U = normalize(U);
  52. vec3 V = cross(U, FSInput_worldNormal.xyz);
  53. V = normalize(V);
  54. return fract(vec2(dot(FSInput_worldPos.xyz, U), dot(FSInput_worldPos.xyz, V)));
  55. }