Blender V5.3
workbench_curvature.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9namespace workbench {
10
11float curvature_soft_clamp(float curvature, float control)
12{
13 if (curvature < 0.5f / control) {
14 return curvature * (1.0f - curvature * control);
15 }
16 return 0.25f / control;
17}
18
20 usampler2D object_id_tx,
21 sampler2D normal_tx,
22 float2 uv,
23 float &curvature)
24{
25 curvature = 0.0f;
26
27 const WorldData &world_data = world.world_data;
28
29 float3 offset = float3(world_data.viewport_size_inv, 0.0f) * world_data.ui_scale;
30 uint object_up = texture(object_id_tx, uv + offset.zy).r;
31 uint object_down = texture(object_id_tx, uv - offset.zy).r;
32 uint object_right = texture(object_id_tx, uv + offset.xz).r;
33 uint object_left = texture(object_id_tx, uv - offset.xz).r;
34
35 /* Remove object outlines. */
36 if ((object_up != object_down) || (object_right != object_left)) {
37 return;
38 }
39 /* Avoid shading background pixels. */
40 if ((object_up == object_right) && (object_right == 0u)) {
41 return;
42 }
43
44 float normal_up = workbench::normal_decode(texture(normal_tx, uv + offset.zy)).g;
45 float normal_down = workbench::normal_decode(texture(normal_tx, uv - offset.zy)).g;
46 float normal_right = workbench::normal_decode(texture(normal_tx, uv + offset.xz)).r;
47 float normal_left = workbench::normal_decode(texture(normal_tx, uv - offset.xz)).r;
48
49 float normal_diff = (normal_up - normal_down) + (normal_right - normal_left);
50
51 if (normal_diff < 0) {
52 curvature = -2.0f * curvature_soft_clamp(-normal_diff, world_data.curvature_valley);
53 }
54 else {
55 curvature = 2.0f * curvature_soft_clamp(normal_diff, world_data.curvature_ridge);
56 }
57}
58
59} // namespace workbench
#define resource_table
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 2 > sampler2D
SamplerBase< uint, 2 > usampler2D
float3 normal_decode(float4 enc)
float curvature_soft_clamp(float curvature, float control)
void curvature_compute(const workbench::World &world, usampler2D object_id_tx, sampler2D normal_tx, float2 uv, float &curvature)