Skip to content

Commit

Permalink
Plaster: Add mesh support
Browse files Browse the repository at this point in the history
  • Loading branch information
JannisX11 committed Apr 25, 2024
1 parent 3a81984 commit ecaa8c0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
3 changes: 2 additions & 1 deletion plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"author": "JannisX11",
"description": "Fixes texture bleeding (small white or colored lines around the edges of your model) by slightly shrinking UV maps",
"icon": "healing",
"min_version": "3.0.0",
"min_version": "3.0.5",
"version": "1.1.0",
"variant": "both"
},
"shape_generator": {
Expand Down
59 changes: 38 additions & 21 deletions plugins/plaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Plugin.register('plaster', {
icon: 'healing',
author: 'JannisX11',
description: 'Fixes texture bleeding (small white or colored lines around the edges of your model) by slightly shrinking UV maps',
version: '1.0.4',
version: '1.1.0',
min_version: '3.0.5',
variant: 'both',
onload() {
Expand All @@ -18,12 +18,13 @@ Plugin.register('plaster', {
name: 'Plaster',
icon: 'healing',
category: 'edit',
condition: () => !Project.box_uv,
click: function(ev) {
condition: () => !(Project.box_uv && !Format.optional_box_uv),
click(ev) {
if (selected.length === 0) {
Blockbench.showMessage('No cubes selected', 'center')
return;
}
let elements = Outliner.selected.filter(element => element.faces);

new Dialog({
id: 'plaster',
Expand All @@ -39,7 +40,7 @@ Plugin.register('plaster', {
}, default: 'm'},
custom: {label: 'Custom Margin (%)', value: 5, min: 0, max: 50, condition: result => result.margin == 'custom'}
},
onConfirm: function(formData) {
onConfirm(formData) {
this.hide()
//Margin
var margin;
Expand All @@ -60,12 +61,12 @@ Plugin.register('plaster', {
margin = formData.custom/100
break;
}
var fixNumber = function(number, isSecond) {
let fixNumber = function(number, isSecond) {
//Vars
var adapted_margin = margin
var x1 = number
var edge = x1%1
var floor = Math.floor(x1)
let adapted_margin = margin
let x1 = number
let edge = x1%1
let floor = Math.floor(x1)

//Switches
if (edge > 0.9 && !isSecond) {
Expand All @@ -78,22 +79,38 @@ Plugin.register('plaster', {
//Return
return floor+edge
}
Undo.initEdit({elements: Cube.selected, uv_only: true})
Undo.initEdit({elements, uv_only: true})
//Processing
Cube.selected.forEach(function(obj) {
for (var face in obj.faces) {
if (obj.faces.hasOwnProperty(face) && obj.faces[face].texture !== null) {
//Vars
var faceTag = obj.faces[face];
elements.forEach(element => {
for (let fkey in element.faces) {
let face = element.faces[fkey];
if (face.texture !== null) {

//Calculating
faceTag.uv.forEach(function(u, i) {
var is_mirrored = faceTag.uv[ (i>1?i-2:i) ] > faceTag.uv[i+ (i>1?0:2) ]
faceTag.uv[i] = fixNumber(faceTag.uv[i], i>1 !== is_mirrored)
})
if (face instanceof MeshFace) {
let uv_center = [0, 0];
let vertices = face.getSortedVertices();
for (let vkey of vertices) {
console.log(uv_center.slice(), face.uv[vkey].slice())
uv_center.V2_add(face.uv[vkey]);
}
uv_center.V2_divide(vertices.length);
for (let vkey of vertices) {
let diff_to_center = uv_center.slice().V2_subtract(face.uv[vkey]);
let distance = Math.sqrt(Math.pow(diff_to_center[0], 2) + Math.pow(diff_to_center[1], 2));
console.log({uv_center, uv: face.uv[vkey].slice(), diff_to_center, distance, margin, vkey, face})
face.uv[vkey][0] += (diff_to_center[0] / distance) * margin;
face.uv[vkey][1] += (diff_to_center[1] / distance) * margin;
}

} else if (face.uv instanceof Array) {
face.uv.forEach(function(u, i) {
let is_mirrored = face.uv[ (i>1?i-2:i) ] > face.uv[i+ (i>1?0:2) ]
face.uv[i] = fixNumber(face.uv[i], i>1 !== is_mirrored)
})
}
}
}
Canvas.updateUV(obj)
element.preview_controller.updateUV(element);
})
UVEditor.loadData()
Undo.finishEdit('plaster')
Expand Down

0 comments on commit ecaa8c0

Please sign in to comment.