common.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. export default
  2. /* glsl */
  3. `
  4. #define PI 3.141592653589793
  5. #define PI2 6.283185307179586
  6. #define PI_HALF 1.5707963267948966
  7. #define RECIPROCAL_PI 0.3183098861837907
  8. #define RECIPROCAL_PI2 0.15915494309189535
  9. #define EPSILON 1e-6
  10. #define EPSILON_ATAN 1e-2
  11. #define WX_MANUAL_SRGB true
  12. #define WX_SRGB_FAST_APPROXIMATION true
  13. #define saturate( a ) clamp( a, 0.0, 1.0 )
  14. float pow2( const in float x ) { return x*x; }
  15. float pow3( const in float x ) { return x*x*x; }
  16. float pow4( const in float x ) { float x2 = x*x; return x2*x2; }
  17. float max3( const in vec3 v ) { return max( max( v.x, v.y ), v.z ); }
  18. float average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }
  19. struct GeometricContext {
  20. vec3 position;
  21. vec3 normal;
  22. vec3 viewDir;
  23. vec3 clearcoatNormal;
  24. };
  25. struct IncidentLight {
  26. vec3 color;
  27. vec3 direction;
  28. bool visible;
  29. };
  30. struct ReflectedLight {
  31. vec3 directDiffuse;
  32. vec3 directSpecular;
  33. vec3 indirectDiffuse;
  34. vec3 indirectSpecular;
  35. };
  36. uniform vec4 u_ambientLightColorIns;
  37. uniform vec3 u_mainLightDir;
  38. uniform vec4 u_mainLightColorIns;
  39. uniform vec3 u_addLightsPos[4];
  40. uniform vec3 u_addLightsDir[4];
  41. uniform vec4 u_addLightsColorIns[4];
  42. uniform vec4 u_addLightsInfo[4];
  43. vec4 SRGBtoLINEAR(vec4 srgbIn)
  44. {
  45. #ifdef WX_MANUAL_SRGB
  46. #ifdef WX_SRGB_FAST_APPROXIMATION
  47. vec3 linOut = pow(srgbIn.xyz,vec3(2.2));
  48. #else //SRGB_FAST_APPROXIMATION
  49. vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
  50. vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
  51. #endif //SRGB_FAST_APPROXIMATION
  52. return vec4(linOut, srgbIn.w);;
  53. #else //MANUAL_SRGB
  54. return srgbIn;
  55. #endif //MANUAL_SRGB
  56. }
  57. vec3 gammaCorrection(vec3 color) {
  58. return pow(color, vec3(1.0 / 2.2));
  59. }
  60. vec3 removeGammaCorrection(vec3 color) {
  61. return pow(color, vec3(2.2));
  62. }
  63. // https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Vendor/EXT_lights_image_based/README.md#rgbd
  64. vec3 decodeRGBD(in vec4 color){
  65. return color.rgb / color.a;
  66. }
  67. float getFace( vec3 direction ) {
  68. vec3 absDirection = abs( direction );
  69. float face = - 1.0;
  70. if ( absDirection.x > absDirection.z ) {
  71. if ( absDirection.x > absDirection.y )
  72. face = direction.x > 0.0 ? 0.0 : 3.0;
  73. else
  74. face = direction.y > 0.0 ? 1.0 : 4.0;
  75. } else {
  76. if ( absDirection.z > absDirection.y )
  77. face = direction.z > 0.0 ? 2.0 : 5.0;
  78. else
  79. face = direction.y > 0.0 ? 1.0 : 4.0;
  80. }
  81. return face;
  82. }
  83. vec2 getUV( vec3 direction, float face ) {
  84. vec2 uv;
  85. if ( face == 0.0 ) {
  86. uv = vec2( direction.z, direction.y ) / abs( direction.x ); // pos x
  87. } else if ( face == 1.0 ) {
  88. uv = vec2( - direction.x, - direction.z ) / abs( direction.y ); // pos y
  89. } else if ( face == 2.0 ) {
  90. uv = vec2( - direction.x, direction.y ) / abs( direction.z ); // pos z
  91. } else if ( face == 3.0 ) {
  92. uv = vec2( - direction.z, direction.y ) / abs( direction.x ); // neg x
  93. } else if ( face == 4.0 ) {
  94. uv = vec2( - direction.x, direction.z ) / abs( direction.y ); // neg y
  95. } else {
  96. uv = vec2( direction.x, direction.y ) / abs( direction.z ); // neg z
  97. }
  98. return 0.5 * ( uv + 1.0 );
  99. }
  100. vec4 textureEnvMap(sampler2D texture, vec3 position){
  101. return texture2D(texture, vec2(atan(position.x, position.z) * RECIPROCAL_PI * 0.5+0.5, acos(position.y) * RECIPROCAL_PI));
  102. }
  103. vec4 textureEnvMapIncludeMipmapsLod(sampler2D texture, vec3 position, float lod){
  104. lod = floor(lod);
  105. if (lod > 7.0) {
  106. lod = 7.0;
  107. }
  108. float posZ = abs(position.z) < EPSILON_ATAN ? EPSILON_ATAN : position.z;
  109. vec2 uv = vec2(atan(position.x, posZ) * RECIPROCAL_PI * 0.5 + 0.5, acos(position.y) * RECIPROCAL_PI);
  110. float scale = pow(2.0, lod);
  111. return texture2D(texture, vec2(uv.x / scale, (uv.y / scale / 2.0) + 1.0 - 1.0/pow(2.0, lod)));
  112. return vec4( position, 0.0);
  113. }
  114. vec4 textureBilinearEnvMap(sampler2D texture, vec3 direction, float roughness){
  115. float uvX = (atan(direction.x, direction.z) ) * RECIPROCAL_PI * 0.5 + 0.5;
  116. float uvY = acos(direction.y) * RECIPROCAL_PI;
  117. vec2 uv = vec2(uvX, uvY);
  118. vec4 envmap = texture2D(texture, uv);
  119. return envmap;
  120. }
  121. vec3 computeDiffuseSHLight(vec3 normal, in vec3 sh[9]) {
  122. return sh[0] +
  123. sh[1] * (normal.y) +
  124. sh[2] * (normal.z) +
  125. sh[3] * (normal.x) +
  126. sh[4] * (normal.y * normal.x) +
  127. sh[5] * (normal.y * normal.z) +
  128. sh[6] * ((3.0 * normal.z * normal.z) - 1.0) +
  129. sh[7] * (normal.z * normal.x) +
  130. sh[8] * (normal.x * normal.x - (normal.y * normal.y));
  131. }
  132. vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
  133. // dir can be either a direction vector or a normal vector
  134. // upper-left 3x3 of matrix is assumed to be orthogonal
  135. return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
  136. }
  137. `;