common.glsl.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. export default /* glsl */`
  2. #define PI 3.14159265359
  3. #define PI2 6.28318530718
  4. #define PI_HALF 1.5707963267949
  5. #define RECIPROCAL_PI 0.31830988618
  6. #define RECIPROCAL_PI2 0.15915494
  7. #define LOG2 1.442695
  8. #define EPSILON 1e-6
  9. #define saturate(a) clamp( a, 0.0, 1.0 )
  10. #define whiteComplement(a) ( 1.0 - saturate( a ) )
  11. float pow2( const in float x ) { return x*x; }
  12. float pow3( const in float x ) { return x*x*x; }
  13. float pow4( const in float x ) { float x2 = x*x; return x2*x2; }
  14. float average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }
  15. // expects values in the range of [0,1]x[0,1], returns values in the [0,1] range.
  16. // do not collapse into a single function per: http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/
  17. highp float rand( const in vec2 uv ) {
  18. const highp float a = 12.9898, b = 78.233, c = 43758.5453;
  19. highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );
  20. return fract(sin(sn) * c);
  21. }
  22. #ifdef HIGH_PRECISION
  23. float precisionSafeLength( vec3 v ) { return length( v ); }
  24. #else
  25. float max3( vec3 v ) { return max( max( v.x, v.y ), v.z ); }
  26. float precisionSafeLength( vec3 v ) {
  27. float maxComponent = max3( abs( v ) );
  28. return length( v / maxComponent ) * maxComponent;
  29. }
  30. #endif
  31. struct IncidentLight {
  32. vec3 color;
  33. vec3 direction;
  34. bool visible;
  35. };
  36. struct ReflectedLight {
  37. vec3 directDiffuse;
  38. vec3 directSpecular;
  39. vec3 indirectDiffuse;
  40. vec3 indirectSpecular;
  41. };
  42. struct GeometricContext {
  43. vec3 position;
  44. vec3 normal;
  45. vec3 viewDir;
  46. #ifdef CLEARCOAT
  47. vec3 clearcoatNormal;
  48. #endif
  49. };
  50. vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
  51. return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
  52. }
  53. // http://en.wikibooks.org/wiki/GLSL_Programming/Applying_Matrix_Transformations
  54. vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
  55. return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
  56. }
  57. vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
  58. float distance = dot( planeNormal, point - pointOnPlane );
  59. return - distance * planeNormal + point;
  60. }
  61. float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
  62. return sign( dot( point - pointOnPlane, planeNormal ) );
  63. }
  64. vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
  65. return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;
  66. }
  67. mat3 transposeMat3( const in mat3 m ) {
  68. mat3 tmp;
  69. tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );
  70. tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );
  71. tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );
  72. return tmp;
  73. }
  74. // https://en.wikipedia.org/wiki/Relative_luminance
  75. float linearToRelativeLuminance( const in vec3 color ) {
  76. vec3 weights = vec3( 0.2126, 0.7152, 0.0722 );
  77. return dot( weights, color.rgb );
  78. }
  79. `;