Skip to content

Commit 1660715

Browse files
committed
sculpt: fix symmetry and xray
1 parent dcb071a commit 1660715

2 files changed

Lines changed: 67 additions & 15 deletions

File tree

paint/sources/render/make_sculpt.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ node_shader_context_t *sculpt_make_sculpt_run(material_t *data, material_context
113113
.compare_mode = "always",
114114
.cull_mode = "none",
115115
.vertex_elements = any_array_create_from_raw(
116-
(void *[]){
117-
GC_ALLOC_INIT(vertex_element_t, {.name = "pos", .data = "float2"}),
118-
},
119-
1),
116+
(void *[]){
117+
GC_ALLOC_INIT(vertex_element_t, {.name = "pos", .data = "float2"}),
118+
},
119+
1),
120120
.color_attachments = any_array_create_from_raw(
121-
(void *[]){
122-
"RGBA128",
123-
"R8",
124-
},
125-
2)});
121+
(void *[]){
122+
"RGBA128",
123+
"R8",
124+
},
125+
2)});
126126
node_shader_context_t *con_paint = node_shader_context_create(data, props);
127127
con_paint->data->color_writes_red = u8_array_create_from_raw(
128128
(u8[]){
@@ -162,7 +162,7 @@ node_shader_context_t *sculpt_make_sculpt_run(material_t *data, material_context
162162
bool particle = g_context->tool == TOOL_TYPE_PARTICLE;
163163
// Decal fill layer: displacement must be confined to the decal box projection
164164
bool decal_layer = g_context->layer->fill_material != NULL && g_context->layer->uv_type == UV_TYPE_PROJECT && g_context->tool == TOOL_TYPE_FILL;
165-
bool sculpt_triplanar = !decal && !decal_layer && (g_context->tool == TOOL_TYPE_FILL || g_context->layer->fill_material != NULL);
165+
bool sculpt_triplanar = !decal && !decal_layer && g_context->tool != TOOL_TYPE_BLUR;
166166
node_shader_add_out(kong, "tex_coord: float2");
167167
node_shader_write_vert(kong, "var madd: float2 = float2(0.5, 0.5);");
168168
node_shader_write_vert(kong, "output.tex_coord = input.pos.xy * madd + madd;");
@@ -220,12 +220,28 @@ node_shader_context_t *sculpt_make_sculpt_run(material_t *data, material_context
220220
node_shader_write_frag(kong, "if (dist > 1.0) { discard; }");
221221
}
222222
else if (!decal) {
223+
if (g_context->xray) {
224+
node_shader_write_frag(kong, "var xray_ndc: float2 = float2(constants.inp.x, 1.0 - constants.inp.y) * 2.0 - 1.0;");
225+
node_shader_write_frag(kong, "var xray_near: float4 = constants.invVP * float4(xray_ndc, 0.0, 1.0);");
226+
node_shader_write_frag(kong, "var xray_far: float4 = constants.invVP * float4(xray_ndc, 1.0, 1.0);");
227+
node_shader_write_frag(kong, "var xray_axis: float3 = normalize(xray_far.xyz / xray_far.w - xray_near.xyz / xray_near.w);");
228+
}
223229
if (g_context->brush_lazy_radius > 0 && g_context->brush_lazy_step > 0) { // Sphere
224-
node_shader_write_frag(kong, "dist = distance(wposition.xyz, winp.xyz);");
230+
if (g_context->xray) {
231+
node_shader_write_frag(kong, "var pa: float3 = wposition.xyz - winp.xyz;");
232+
node_shader_write_frag(kong, "dist = length(pa - xray_axis * dot(xray_axis, pa));");
233+
}
234+
else {
235+
node_shader_write_frag(kong, "dist = distance(wposition.xyz, winp.xyz);");
236+
}
225237
}
226238
else { // Capsule
227239
node_shader_write_frag(kong, "var pa: float3 = wposition.xyz - winp.xyz;");
228240
node_shader_write_frag(kong, "var ba: float3 = winplast.xyz - winp.xyz;");
241+
if (g_context->xray) {
242+
node_shader_write_frag(kong, "pa = pa - xray_axis * dot(xray_axis, pa);");
243+
node_shader_write_frag(kong, "ba = ba - xray_axis * dot(xray_axis, ba);");
244+
}
229245
node_shader_write_frag(kong, "var h: float = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);");
230246
node_shader_write_frag(kong, "dist = length(pa - ba * h);");
231247
}
@@ -581,6 +597,16 @@ node_shader_context_t *sculpt_make_sculpt_run(material_t *data, material_context
581597
node_shader_write_frag(kong, "wn.z = 1.0 - abs(g0_undo.x) - abs(g0_undo.y);");
582598
node_shader_write_frag(kong, "if (wn.z >= 0.0) { wn.xy = g0_undo.xy; } else { wn.xy = octahedron_wrap(g0_undo.xy); }");
583599
node_shader_write_frag(kong, "var n: float3 = normalize(wn);");
600+
node_shader_add_constant(kong, "sculpt_symmetry_reflect: float4x4", "_sculpt_symmetry_reflect");
601+
node_shader_write_frag(kong, "n = normalize((constants.sculpt_symmetry_reflect * float4(n, 0.0)).xyz);");
602+
if (g_context->xray) {
603+
node_shader_write_frag(kong, "var xray_nnv: float = floor(raw_undo.a) / 255.0;");
604+
node_shader_write_frag(kong, "var xray_noct: float2 = float2(raw_undo.a - floor(raw_undo.a), xray_nnv) * 2.0 - 1.0;");
605+
node_shader_write_frag(kong, "var xray_nnz: float = 1.0 - abs(xray_noct.x) - abs(xray_noct.y);");
606+
node_shader_write_frag(kong, "var xray_fnor: float3 = float3(xray_noct.xy, xray_nnz);");
607+
node_shader_write_frag(kong, "if (xray_nnz < 0.0) { xray_fnor.xy = octahedron_wrap(xray_noct.xy); }");
608+
node_shader_write_frag(kong, "n = normalize((constants.W * float4(normalize(xray_fnor), 0.0)).xyz);");
609+
}
584610
}
585611
if (g_context->tool == TOOL_TYPE_BLUR) {
586612
// Even out the surface by relaxing each vertex toward the cursors tangent plane

paint/sources/uniforms.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ vec4_t uniforms_ext_vec3_link(object_t *object, material_data_t *mat, char *link
202202
// Discard first paint for directional brush (no prev position yet)
203203
bool allow_paint = g_context->prev_paint_vec_x > 0 && g_context->prev_paint_vec_y > 0 &&
204204
(g_context->prev_paint_vec_x != g_context->paint_vec.x || g_context->prev_paint_vec_y != g_context->paint_vec.y);
205-
f32 x = g_context->paint_vec.x;
206-
f32 y = g_context->paint_vec.y;
207-
f32 lastx = g_context->prev_paint_vec_x;
208-
f32 lasty = g_context->prev_paint_vec_y;
205+
f32 x = g_context->paint_vec.x;
206+
f32 y = g_context->paint_vec.y;
207+
f32 lastx = g_context->prev_paint_vec_x;
208+
f32 lasty = g_context->prev_paint_vec_y;
209209
if (g_context->paint2d) {
210210
x = uniforms_ext_vec2d(x);
211211
lastx = uniforms_ext_vec2d(lastx);
@@ -307,6 +307,32 @@ vec4_t uniforms_ext_vec4_link(object_t *object, material_data_t *mat, char *link
307307
}
308308

309309
mat4_t uniforms_ext_mat4_link(object_t *object, material_data_t *mat, char *link) {
310+
if (string_equals(link, "_sculpt_symmetry_reflect")) {
311+
transform_t *t = object->transform;
312+
mat4_t W = t->world;
313+
vec4_t axes[3] = {
314+
vec4_norm((vec4_t){W.m00, W.m10, W.m20, 0.0}),
315+
vec4_norm((vec4_t){W.m01, W.m11, W.m21, 0.0}),
316+
vec4_norm((vec4_t){W.m02, W.m12, W.m22, 0.0}),
317+
};
318+
f32 scale[3] = {t->scale.x, t->scale.y, t->scale.z};
319+
mat4_t F = mat4_identity();
320+
for (i32 i = 0; i < 3; ++i) {
321+
if (scale[i] < 0.0f) {
322+
vec4_t a = axes[i];
323+
F.m00 -= 2.0f * a.x * a.x;
324+
F.m01 -= 2.0f * a.x * a.y;
325+
F.m02 -= 2.0f * a.x * a.z;
326+
F.m10 -= 2.0f * a.y * a.x;
327+
F.m11 -= 2.0f * a.y * a.y;
328+
F.m12 -= 2.0f * a.y * a.z;
329+
F.m20 -= 2.0f * a.z * a.x;
330+
F.m21 -= 2.0f * a.z * a.y;
331+
F.m22 -= 2.0f * a.z * a.z;
332+
}
333+
}
334+
return F;
335+
}
310336
if (string_equals(link, "_decal_layer_matrix")) { // Decal layer
311337
mat4_t m = mat4_inv(g_context->layer->decal_mat);
312338
f32 parent_scale = object->parent != NULL ? object->parent->transform->scale.x : 1.0;

0 commit comments

Comments
 (0)