common.glsl.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. export const common = `
  2. // TODO find a better place for these convenience defines
  3. #if defined(dRenderVariant_colorBlended) || defined(dRenderVariant_colorWboit)
  4. #define dRenderVariant_color
  5. #endif
  6. #if defined(dRenderVariant_pickObject) || defined(dRenderVariant_pickInstance) || defined(dRenderVariant_pickGroup)
  7. #define dRenderVariant_pick
  8. #endif
  9. #if defined(dRenderVariant_markingDepth) || defined(dRenderVariant_markingMask)
  10. #define dRenderVariant_marking
  11. #endif
  12. #if defined(dColorType_instance) || defined(dColorType_group) || defined(dColorType_groupInstance) || defined(dColorType_vertex) || defined(dColorType_vertexInstance)
  13. #define dColorType_texture
  14. #endif
  15. #if defined(dColorType_volume) || defined(dColorType_volumeInstance)
  16. #define dColorType_grid
  17. #endif
  18. #if defined(dColorType_attribute) || defined(dColorType_texture) || defined(dColorType_grid)
  19. #define dColorType_varying
  20. #endif
  21. //
  22. #define PI 3.14159265
  23. #define RECIPROCAL_PI 0.31830988618
  24. #define EPSILON 1e-6
  25. #define saturate(a) clamp(a, 0.0, 1.0)
  26. float intDiv(const in float a, const in float b) { return float(int(a) / int(b)); }
  27. vec2 ivec2Div(const in vec2 a, const in vec2 b) { return vec2(ivec2(a) / ivec2(b)); }
  28. float intMod(const in float a, const in float b) { return a - b * float(int(a) / int(b)); }
  29. int imod(const in int a, const in int b) { return a - b * (a / b); }
  30. float pow2(const in float x) { return x * x; }
  31. const float maxFloat = 10000.0; // NOTE constant also set in TypeScript
  32. const float floatLogFactor = 9.210440366976517; // log(maxFloat + 1.0);
  33. float encodeFloatLog(const in float value) { return log(value + 1.0) / floatLogFactor; }
  34. float decodeFloatLog(const in float value) { return exp(value * floatLogFactor) - 1.0; }
  35. vec3 encodeFloatRGB(in float value) {
  36. value = clamp(value, 0.0, 16777216.0 - 1.0) + 1.0;
  37. vec3 c = vec3(0.0);
  38. c.b = mod(value, 256.0);
  39. value = floor(value / 256.0);
  40. c.g = mod(value, 256.0);
  41. value = floor(value / 256.0);
  42. c.r = mod(value, 256.0);
  43. return c / 255.0;
  44. }
  45. float decodeFloatRGB(const in vec3 rgb) {
  46. return (rgb.r * 256.0 * 256.0 * 255.0 + rgb.g * 256.0 * 255.0 + rgb.b * 255.0) - 1.0;
  47. }
  48. vec2 packUnitIntervalToRG(const in float v) {
  49. vec2 enc;
  50. enc.xy = vec2(fract(v * 256.0), v);
  51. enc.y -= enc.x * (1.0 / 256.0);
  52. enc.xy *= 256.0 / 255.0;
  53. return enc;
  54. }
  55. float unpackRGToUnitInterval(const in vec2 enc) {
  56. return dot(enc, vec2(255.0 / (256.0 * 256.0), 255.0 / 256.0));
  57. }
  58. vec3 screenSpaceToViewSpace(const in vec3 ssPos, const in mat4 invProjection) {
  59. vec4 p = vec4(ssPos * 2.0 - 1.0, 1.0);
  60. p = invProjection * p;
  61. return p.xyz / p.w;
  62. }
  63. const float PackUpscale = 256.0 / 255.0; // fraction -> 0..1 (including 1)
  64. const float UnpackDownscale = 255.0 / 256.0; // 0..1 -> fraction (excluding 1)
  65. const vec3 PackFactors = vec3(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0);
  66. const vec4 UnpackFactors = UnpackDownscale / vec4(PackFactors, 1.0);
  67. const float ShiftRight8 = 1.0 / 256.0;
  68. vec4 packDepthToRGBA(const in float v) {
  69. vec4 r = vec4(fract(v * PackFactors), v);
  70. r.yzw -= r.xyz * ShiftRight8; // tidy overflow
  71. return r * PackUpscale;
  72. }
  73. float unpackRGBAToDepth(const in vec4 v) {
  74. return dot(v, UnpackFactors);
  75. }
  76. vec4 sRGBToLinear(const in vec4 c) {
  77. return vec4(mix(pow(c.rgb * 0.9478672986 + vec3(0.0521327014), vec3(2.4)), c.rgb * 0.0773993808, vec3(lessThanEqual(c.rgb, vec3(0.04045)))), c.a);
  78. }
  79. vec4 linearTosRGB(const in vec4 c) {
  80. return vec4(mix(pow(c.rgb, vec3(0.41666)) * 1.055 - vec3(0.055), c.rgb * 12.92, vec3(lessThanEqual(c.rgb, vec3(0.0031308)))), c.a);
  81. }
  82. float linearizeDepth(const in float depth, const in float near, const in float far) {
  83. return (2.0 * near) / (far + near - depth * (far - near));
  84. }
  85. float perspectiveDepthToViewZ(const in float invClipZ, const in float near, const in float far) {
  86. return (near * far) / ((far - near) * invClipZ - far);
  87. }
  88. float orthographicDepthToViewZ(const in float linearClipZ, const in float near, const in float far) {
  89. return linearClipZ * (near - far) - near;
  90. }
  91. float depthToViewZ(const in float isOrtho, const in float linearClipZ, const in float near, const in float far) {
  92. return isOrtho == 1.0 ? orthographicDepthToViewZ(linearClipZ, near, far) : perspectiveDepthToViewZ(linearClipZ, near, far);
  93. }
  94. #if __VERSION__ == 100
  95. // transpose
  96. float transpose(const in float m) {
  97. return m;
  98. }
  99. mat2 transpose2(const in mat2 m) {
  100. return mat2(
  101. m[0][0], m[1][0],
  102. m[0][1], m[1][1]
  103. );
  104. }
  105. mat3 transpose3(const in mat3 m) {
  106. return mat3(
  107. m[0][0], m[1][0], m[2][0],
  108. m[0][1], m[1][1], m[2][1],
  109. m[0][2], m[1][2], m[2][2]
  110. );
  111. }
  112. mat4 transpose4(const in mat4 m) {
  113. return mat4(
  114. m[0][0], m[1][0], m[2][0], m[3][0],
  115. m[0][1], m[1][1], m[2][1], m[3][1],
  116. m[0][2], m[1][2], m[2][2], m[3][2],
  117. m[0][3], m[1][3], m[2][3], m[3][3]
  118. );
  119. }
  120. // inverse
  121. float inverse(const in float m) {
  122. return 1.0 / m;
  123. }
  124. mat2 inverse2(const in mat2 m) {
  125. return mat2(m[1][1],-m[0][1],
  126. -m[1][0], m[0][0]) / (m[0][0]*m[1][1] - m[0][1]*m[1][0]);
  127. }
  128. mat3 inverse3(const in mat3 m) {
  129. float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
  130. float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
  131. float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
  132. float b01 = a22 * a11 - a12 * a21;
  133. float b11 = -a22 * a10 + a12 * a20;
  134. float b21 = a21 * a10 - a11 * a20;
  135. float det = a00 * b01 + a01 * b11 + a02 * b21;
  136. return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
  137. b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
  138. b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
  139. }
  140. mat4 inverse4(const in mat4 m) {
  141. float
  142. a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],
  143. a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],
  144. a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3],
  145. a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3],
  146. b00 = a00 * a11 - a01 * a10,
  147. b01 = a00 * a12 - a02 * a10,
  148. b02 = a00 * a13 - a03 * a10,
  149. b03 = a01 * a12 - a02 * a11,
  150. b04 = a01 * a13 - a03 * a11,
  151. b05 = a02 * a13 - a03 * a12,
  152. b06 = a20 * a31 - a21 * a30,
  153. b07 = a20 * a32 - a22 * a30,
  154. b08 = a20 * a33 - a23 * a30,
  155. b09 = a21 * a32 - a22 * a31,
  156. b10 = a21 * a33 - a23 * a31,
  157. b11 = a22 * a33 - a23 * a32,
  158. det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  159. return mat4(
  160. a11 * b11 - a12 * b10 + a13 * b09,
  161. a02 * b10 - a01 * b11 - a03 * b09,
  162. a31 * b05 - a32 * b04 + a33 * b03,
  163. a22 * b04 - a21 * b05 - a23 * b03,
  164. a12 * b08 - a10 * b11 - a13 * b07,
  165. a00 * b11 - a02 * b08 + a03 * b07,
  166. a32 * b02 - a30 * b05 - a33 * b01,
  167. a20 * b05 - a22 * b02 + a23 * b01,
  168. a10 * b10 - a11 * b08 + a13 * b06,
  169. a01 * b08 - a00 * b10 - a03 * b06,
  170. a30 * b04 - a31 * b02 + a33 * b00,
  171. a21 * b02 - a20 * b04 - a23 * b00,
  172. a11 * b07 - a10 * b09 - a12 * b06,
  173. a00 * b09 - a01 * b07 + a02 * b06,
  174. a31 * b01 - a30 * b03 - a32 * b00,
  175. a20 * b03 - a21 * b01 + a22 * b00) / det;
  176. }
  177. #else
  178. #define transpose2(m) transpose(m)
  179. #define transpose3(m) transpose(m)
  180. #define transpose4(m) transpose(m)
  181. #define inverse2(m) inverse(m)
  182. #define inverse3(m) inverse(m)
  183. #define inverse4(m) inverse(m)
  184. #endif
  185. `;