variable_layer_height.fs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #version 140
  2. #define M_PI 3.1415926535897932384626433832795
  3. // 2D texture (1D texture split by the rows) of color along the object Z axis.
  4. uniform sampler2D z_texture;
  5. // Scaling from the Z texture rows coordinate to the normalized texture row coordinate.
  6. uniform float z_to_texture_row;
  7. uniform float z_texture_row_to_normalized;
  8. uniform float z_cursor;
  9. uniform float z_cursor_band_width;
  10. // x = tainted, y = specular;
  11. in vec2 intensity;
  12. in float object_z;
  13. out vec4 out_color;
  14. void main()
  15. {
  16. float object_z_row = z_to_texture_row * object_z;
  17. // Index of the row in the texture.
  18. float z_texture_row = floor(object_z_row);
  19. // Normalized coordinate from 0. to 1.
  20. float z_texture_col = object_z_row - z_texture_row;
  21. float z_blend = 0.25 * cos(min(M_PI, abs(M_PI * (object_z - z_cursor) * 1.8 / z_cursor_band_width))) + 0.25;
  22. // Calculate level of detail from the object Z coordinate.
  23. // This makes the slowly sloping surfaces to be shown with high detail (with stripes),
  24. // and the vertical surfaces to be shown with low detail (no stripes)
  25. float z_in_cells = object_z_row * 190.;
  26. // Gradient of Z projected on the screen.
  27. float dx_vtc = dFdx(z_in_cells);
  28. float dy_vtc = dFdy(z_in_cells);
  29. float lod = clamp(0.5 * log2(max(dx_vtc * dx_vtc, dy_vtc * dy_vtc)), 0., 1.);
  30. // Sample the Z texture. Texture coordinates are normalized to <0, 1>.
  31. vec4 color = vec4(0.25, 0.25, 0.25, 1.0);
  32. if (z_texture_row >= 0.0)
  33. color = mix(texture(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row + 0.5 )), -10000.),
  34. texture(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row * 2. + 1.)), 10000.), lod);
  35. // Mix the final color.
  36. out_color = vec4(vec3(intensity.y), 1.0) + intensity.x * mix(color, vec4(1.0, 1.0, 0.0, 1.0), z_blend);
  37. }