variable_layer_height.fs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #version 110
  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. varying vec2 intensity;
  12. varying float object_z;
  13. void main()
  14. {
  15. float object_z_row = z_to_texture_row * object_z;
  16. // Index of the row in the texture.
  17. float z_texture_row = floor(object_z_row);
  18. // Normalized coordinate from 0. to 1.
  19. float z_texture_col = object_z_row - z_texture_row;
  20. float z_blend = 0.25 * cos(min(M_PI, abs(M_PI * (object_z - z_cursor) * 1.8 / z_cursor_band_width))) + 0.25;
  21. // Calculate level of detail from the object Z coordinate.
  22. // This makes the slowly sloping surfaces to be show with high detail (with stripes),
  23. // and the vertical surfaces to be shown with low detail (no stripes)
  24. float z_in_cells = object_z_row * 190.;
  25. // Gradient of Z projected on the screen.
  26. float dx_vtc = dFdx(z_in_cells);
  27. float dy_vtc = dFdy(z_in_cells);
  28. float lod = clamp(0.5 * log2(max(dx_vtc * dx_vtc, dy_vtc * dy_vtc)), 0., 1.);
  29. // Sample the Z texture. Texture coordinates are normalized to <0, 1>.
  30. vec4 color = mix(texture2D(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row + 0.5 )), -10000.),
  31. texture2D(z_texture, vec2(z_texture_col, z_texture_row_to_normalized * (z_texture_row * 2. + 1.)), 10000.), lod);
  32. // Mix the final color.
  33. gl_FragColor = vec4(intensity.y, intensity.y, intensity.y, 1.0) + intensity.x * mix(color, vec4(1.0, 1.0, 0.0, 1.0), z_blend);
  34. }