mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-09-14 04:27:36 +00:00
Add texture displacement bump and UV-check shaders
This commit is contained in:
@@ -1,14 +1,33 @@
|
||||
#version 140
|
||||
|
||||
// Fast, geometry-free preview of texture displacement: perturbs the *shading* normal from the
|
||||
// height texture's local gradient (a bump map), faded out by the per-vertex paint weight. Used
|
||||
// while the brush is actively dragging (see project plan, "Preview shaders" section); the true,
|
||||
// exact result is what "Bake" produces via libslic3r/TextureDisplacement.cpp on the CPU.
|
||||
// height texture's local gradient (a bump map), faded out by the per-vertex paint weight. The
|
||||
// true, exact result is what "Bake" produces via libslic3r/TextureDisplacement.cpp on the CPU.
|
||||
//
|
||||
// NOTE: the gradient-to-normal conversion below assumes the two planar-projection axes chosen by
|
||||
// project_uv() are reasonably aligned with the surface here; it is a cheap approximation (no
|
||||
// tangent/bitangent basis is reconstructed on the GPU), acceptable for a live preview but not
|
||||
// intended to be pixel-exact versus the CPU-side bake.
|
||||
// The bake displaces each surface point along its normal by H = +/- depth_mm * (h(uv) - midlevel),
|
||||
// with uv from the layer's projection. The perturbed normal is the analytic
|
||||
//
|
||||
// N' = normalize(N - (dH/da) * T - (dH/db) * B)
|
||||
//
|
||||
// over any orthonormal surface tangent pair (T, B), where the two slopes are real mm-per-mm
|
||||
// derivatives. Two things have to be right for the preview's apparent depth to match the bake's:
|
||||
// the tangent frame the gradient is expressed in, and the uv->mm scale that turns a texel
|
||||
// difference into a slope. Getting the scale wrong is a uniform flattening (a raw texel difference
|
||||
// is dh over one texel step, not over one mm); getting the frame wrong tilts the bump along the
|
||||
// wrong axes.
|
||||
//
|
||||
// Two projection paths:
|
||||
// * Triplanar (use_vertex_uv = 0): uv and the tangent axes are derived in-shader from the dominant
|
||||
// normal axis, mirroring libslic3r's project_planar()/apply_uv_transform(), and the slope is
|
||||
// formed analytically (there is a closed-form uv, so 1 uv unit is exactly tiling_scale mm).
|
||||
// * Precomputed uv (use_vertex_uv = 1, used for LSCM): uv comes per-vertex from the CPU (the LSCM
|
||||
// unwrap with island placement + tiling/rotation/offset already folded in), and the perturbed
|
||||
// normal is built with Mikkelsen's method -- the surface gradient taken straight from the
|
||||
// screen-space derivatives of the sampled height and position. This makes no uv->mm scale
|
||||
// assumption, which matters because an LSCM map is conformal, not isometric: the local mm-per-uv
|
||||
// varies across the chart, so a single global 1/tiling factor (what an earlier version used) got
|
||||
// the apparent depth wrong. This path is also what makes the fast preview follow the UV editor:
|
||||
// move an island and its uv -- hence its bump -- moves with it.
|
||||
|
||||
#define INTENSITY_CORRECTION 0.6
|
||||
|
||||
@@ -39,14 +58,41 @@ uniform float tiling_scale;
|
||||
uniform float rotation_rad;
|
||||
uniform vec2 uv_offset;
|
||||
uniform bool invert;
|
||||
uniform bool use_vertex_uv; // true: sample at vertex_uv with a derived tangent frame (LSCM)
|
||||
// A 2x3 affine (columns packed as lin = (m00, m01, m10, m11), tr = (m02, m12)) applied to the uv of
|
||||
// the island currently being dragged in the UV editor (active > 0.5). Identity when nothing is
|
||||
// dragged, so this whole path is a no-op then. Lets a UV island drag move the bump on the model with
|
||||
// only a uniform update -- no mesh rebuild -- exactly the way Adjust placement moves the whole texture.
|
||||
uniform vec4 island_delta_lin;
|
||||
uniform vec2 island_delta_tr;
|
||||
|
||||
in vec3 clipping_planes_dots;
|
||||
in vec4 model_pos;
|
||||
in vec4 world_pos;
|
||||
in float weight;
|
||||
in float active;
|
||||
in vec2 vertex_uv;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
// The two model-space axes the triplanar planar coordinate is read off, per dominant normal
|
||||
// component -- same choice libslic3r's project_planar() makes, so planar.x runs along t, planar.y
|
||||
// along b.
|
||||
void projection_axes(vec3 n, out vec3 t, out vec3 b)
|
||||
{
|
||||
vec3 an = abs(n);
|
||||
if (an.x >= an.y && an.x >= an.z) { // planar = p.yz
|
||||
t = vec3(0.0, 1.0, 0.0);
|
||||
b = vec3(0.0, 0.0, 1.0);
|
||||
} else if (an.y >= an.x && an.y >= an.z) { // planar = p.xz
|
||||
t = vec3(1.0, 0.0, 0.0);
|
||||
b = vec3(0.0, 0.0, 1.0);
|
||||
} else { // planar = p.xy
|
||||
t = vec3(1.0, 0.0, 0.0);
|
||||
b = vec3(0.0, 1.0, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
vec2 project_uv(vec3 p, vec3 n)
|
||||
{
|
||||
vec3 an = abs(n);
|
||||
@@ -66,15 +112,63 @@ void main()
|
||||
if (volume_mirrored)
|
||||
triangle_normal = -triangle_normal;
|
||||
|
||||
if (weight > 0.0) {
|
||||
vec2 uv = project_uv(model_pos.xyz, triangle_normal);
|
||||
if (use_vertex_uv) {
|
||||
// Precomputed-uv (LSCM) path -- Mikkelsen's surface-gradient bump ("Bump Mapping
|
||||
// Unparametrized Surfaces on the GPU"). The perturbed normal is derived straight from the
|
||||
// screen-space derivatives of the *sampled height* and the position, so it is scale-exact
|
||||
// with no uv->mm assumption at all -- which is the whole point here: an LSCM map is conformal,
|
||||
// not isometric, so the local mm-per-uv varies across the chart and the earlier "one global
|
||||
// 1/tiling factor" got the depth visibly wrong. dFdx(h) captures the true on-screen rate of
|
||||
// change however the chart is stretched or however fine the tiling is.
|
||||
//
|
||||
// use_vertex_uv is a uniform, so this whole branch is uniform control flow and the texture
|
||||
// derivatives are well defined; the paint weight gates the result by a plain multiply (k)
|
||||
// rather than a per-fragment branch, keeping it that way.
|
||||
// The dragged island's uv rides a uniform affine so its bump moves without a rebuild; every
|
||||
// other vertex (active == 0) samples its baked uv unchanged.
|
||||
vec2 uv = (active > 0.5)
|
||||
? vec2(dot(island_delta_lin.xy, vertex_uv), dot(island_delta_lin.zw, vertex_uv)) + island_delta_tr
|
||||
: vertex_uv;
|
||||
float h = texture(height_tex, uv).r;
|
||||
float k = (invert ? -1.0 : 1.0) * depth_mm * clamp(weight, 0.0, 1.0);
|
||||
vec3 sigmaS = dFdx(model_pos.xyz);
|
||||
vec3 sigmaT = dFdy(model_pos.xyz);
|
||||
vec3 R1 = cross(sigmaT, triangle_normal);
|
||||
vec3 R2 = cross(triangle_normal, sigmaS);
|
||||
float det = dot(sigmaS, R1);
|
||||
float dHdx = k * dFdx(h);
|
||||
float dHdy = k * dFdy(h);
|
||||
if (abs(det) > 1e-12)
|
||||
triangle_normal = normalize(triangle_normal - (dHdx * R1 + dHdy * R2) / det);
|
||||
} else if (weight > 0.0) {
|
||||
// Triplanar path: uv and the tangent axes are reconstructed in-shader from the dominant
|
||||
// normal component (see header). The gradient is expressed analytically because there is a
|
||||
// closed-form uv here, unlike the LSCM case.
|
||||
vec2 uv = project_uv(model_pos.xyz, triangle_normal);
|
||||
vec3 t, b;
|
||||
projection_axes(triangle_normal, t, b);
|
||||
|
||||
float hL = texture(height_tex, uv - vec2(height_tex_texel.x, 0.0)).r;
|
||||
float hR = texture(height_tex, uv + vec2(height_tex_texel.x, 0.0)).r;
|
||||
float hD = texture(height_tex, uv - vec2(0.0, height_tex_texel.y)).r;
|
||||
float hU = texture(height_tex, uv + vec2(0.0, height_tex_texel.y)).r;
|
||||
float sign_mul = invert ? -1.0 : 1.0;
|
||||
vec3 bumped_normal = normalize(triangle_normal + sign_mul * depth_mm * vec3(hL - hR, hD - hU, 0.0));
|
||||
triangle_normal = normalize(mix(triangle_normal, bumped_normal, clamp(weight, 0.0, 1.0)));
|
||||
|
||||
// Central difference, per uv unit (not per texel).
|
||||
vec2 dh_duv = vec2((hR - hL) / (2.0 * height_tex_texel.x), (hU - hD) / (2.0 * height_tex_texel.y));
|
||||
|
||||
// uv -> mm is 1/tiling_scale for the triplanar projection, so this turns the uv-space
|
||||
// gradient into a real surface slope.
|
||||
float inv_tiling = (tiling_scale > 1e-6) ? (1.0 / tiling_scale) : 1.0;
|
||||
float amplitude = (invert ? -1.0 : 1.0) * depth_mm * inv_tiling * clamp(weight, 0.0, 1.0);
|
||||
// uv was rotated by project_uv() while t/b are the unrotated model axes, so rotate the
|
||||
// gradient back into the axes' frame.
|
||||
float cs = cos(rotation_rad);
|
||||
float sn = sin(rotation_rad);
|
||||
vec2 slope = amplitude * vec2(dh_duv.x * cs + dh_duv.y * sn, -dh_duv.x * sn + dh_duv.y * cs);
|
||||
|
||||
vec3 gradient = slope.x * t + slope.y * b;
|
||||
gradient -= triangle_normal * dot(triangle_normal, gradient);
|
||||
triangle_normal = normalize(triangle_normal - gradient);
|
||||
}
|
||||
|
||||
vec3 eye_normal = normalize(view_normal_matrix * triangle_normal);
|
||||
|
||||
Reference in New Issue
Block a user