diff --git a/plugins.json b/plugins.json index 65e07fc0..07b15ae5 100644 --- a/plugins.json +++ b/plugins.json @@ -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": { diff --git a/plugins/plaster.js b/plugins/plaster.js index 2f1b5bd0..667e85e1 100644 --- a/plugins/plaster.js +++ b/plugins/plaster.js @@ -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() { @@ -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', @@ -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; @@ -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) { @@ -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')