Skip to content

Commit

Permalink
Auto Set Cullfaces tool
Browse files Browse the repository at this point in the history
Scale all elements if none are selected, closes #970
  • Loading branch information
JannisX11 committed Nov 2, 2022
1 parent fbdbc91 commit 3100a48
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 15 deletions.
1 change: 1 addition & 0 deletions css/panels.css
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,7 @@
gap: 2px;
text-align: center;
color: var(--color-subtle_text);
white-space: nowrap;
}
.uv_face_properties_labels > .flexible {
flex-grow: 1;
Expand Down
1 change: 1 addition & 0 deletions js/interface/menu_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ const MenuBar = {
'_',
'predicate_overrides',
'convert_to_mesh',
'auto_set_cullfaces',
'remove_blank_faces',
])
MenuBar.menus.filter = MenuBar.menus.tools;
Expand Down
15 changes: 3 additions & 12 deletions js/io/formats/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,11 @@ var codec = new Codec('obj', {

output.push(`o ${element.name||'cube'}`)

function addVertex(x, y, z) {
vertex.set(x - element.origin[0], y - element.origin[1], z - element.origin[2]);
vertex.applyMatrix4( mesh.matrixWorld ).divideScalar(16);
element.getGlobalVertexPositions().forEach((coords) => {
vertex.set(...coords.V3_subtract(8, 8, 8)).divideScalar(16);
output.push('v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z);
nbVertex++;
}
addVertex(element.to[0] + element.inflate, element.to[1] + element.inflate, element.to[2] + element.inflate);
addVertex(element.to[0] + element.inflate, element.to[1] + element.inflate, element.from[2] - element.inflate);
addVertex(element.to[0] + element.inflate, element.from[1] - element.inflate, element.to[2] + element.inflate);
addVertex(element.to[0] + element.inflate, element.from[1] - element.inflate, element.from[2] - element.inflate);
addVertex(element.from[0] - element.inflate, element.to[1] + element.inflate, element.from[2] - element.inflate);
addVertex(element.from[0] - element.inflate, element.to[1] + element.inflate, element.to[2] + element.inflate);
addVertex(element.from[0] - element.inflate, element.from[1] - element.inflate, element.from[2] - element.inflate);
addVertex(element.from[0] - element.inflate, element.from[1] - element.inflate, element.to[2] + element.inflate);
})

for (let key in element.faces) {
if (element.faces[key].texture !== null) {
Expand Down
41 changes: 39 additions & 2 deletions js/modeling/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1553,12 +1553,17 @@ BARS.defineActions(function() {
new Action('scale', {
icon: 'settings_overscan',
category: 'transform',
condition: () => (Modes.edit && selected.length),
condition: () => (Modes.edit && Outliner.elements.length),
click() {
$('#model_scale_range, #model_scale_label').val(1)
$('#scaling_clipping_warning').text('')

Undo.initEdit({elements: Outliner.selected, outliner: Format.bone_rig})
if (Outliner.selected.length == 0) {
Prop.active_panel = 'preview';
selectAll();
}

Undo.initEdit({elements: Outliner.selected, outliner: Format.bone_rig});

Outliner.selected.forEach(function(obj) {
obj.before = {
Expand Down Expand Up @@ -1919,4 +1924,36 @@ BARS.defineActions(function() {
}
}
})
new Action('auto_set_cullfaces', {
icon: 'smart_button',
condition: () => Modes.edit && Format.java_face_properties,
click() {
if (!Cube.selected.length) {
selectAll();
}
Undo.initEdit({elements: Cube.selected});

Cube.selected.forEach(cube => {
let vertices = cube.getGlobalVertexPositions();
['east', 'up', 'south', 'west', 'down', 'north'].forEach((fkey, index) => {
let axis = index % 3;
let face = cube.faces[fkey];
if (face.texture === null) return;
let face_corners = face.getVertexIndices().map(vindex => vertices[vindex]);
let offset = index < 3 ? [15.98, 32] : [-16, 0.02];

let culled = face_corners.allAre(corner => {
let off_axes = [0, 1, 2].filter(v => axis !== v);
return (corner[axis] > offset[0] && corner[axis] < offset[1]
&& corner[off_axes[0]] >= 0 && corner[off_axes[0]] <= 16
&& corner[off_axes[1]] >= 0 && corner[off_axes[1]] <= 16
);
})
face.cullface = culled ? fkey : '';
});
})

Undo.finishEdit('Automatically set cullfaces')
}
})
})
30 changes: 30 additions & 0 deletions js/outliner/cube.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class CubeFace extends Face {
getBoundingRect() {
return getRectangle(...this.uv);
}
getVertexIndices() {
switch (this.direction) {
case 'north': return [1, 4, 6, 3];
case 'east': return [0, 1, 3, 2];
case 'south': return [5, 0, 2, 7];
case 'west': return [4, 5, 7, 6];
case 'up': return [4, 1, 0, 5];
case 'down': return [7, 2, 3, 6];
}
}
}
new Property(CubeFace, 'number', 'rotation', {default: 0});
new Property(CubeFace, 'number', 'tint', {default: -1});
Expand Down Expand Up @@ -482,6 +492,26 @@ class Cube extends OutlinerElement {
}
return pos;
}
getGlobalVertexPositions() {
let vertices = [
[this.to[0] + this.inflate, this.to[1] + this.inflate, this.to[2] + this.inflate],
[this.to[0] + this.inflate, this.to[1] + this.inflate, this.from[2] - this.inflate],
[this.to[0] + this.inflate, this.from[1] - this.inflate, this.to[2] + this.inflate],
[this.to[0] + this.inflate, this.from[1] - this.inflate, this.from[2] - this.inflate],
[this.from[0] - this.inflate, this.to[1] + this.inflate, this.from[2] - this.inflate],
[this.from[0] - this.inflate, this.to[1] + this.inflate, this.to[2] + this.inflate],
[this.from[0] - this.inflate, this.from[1] - this.inflate, this.from[2] - this.inflate],
[this.from[0] - this.inflate, this.from[1] - this.inflate, this.to[2] + this.inflate],
];
let vec = new THREE.Vector3();
return vertices.map(coords => {
vec.set(...coords.V3_subtract(this.origin));
vec.applyMatrix4( this.mesh.matrixWorld );
let arr = vec.toArray();
arr.V3_add(8, 8, 8);
return arr;
})
}
setUVMode(box_uv) {
if (this.box_uv == !!box_uv) return this;
this.box_uv = !!box_uv;
Expand Down
4 changes: 3 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@
"action.uv_reset.desc": "Resets the current face",
"action.cullface": "Cullface",
"action.cullface.desc": "Disables rendering for this face if the selected side of the model is covered",
"action.auto_cullface": "Auto Cullface",
"action.auto_cullface": "Enable Cullface",
"action.auto_cullface.desc": "Sets the cullface for this face to itself",
"action.face_tint": "Tint",
"action.face_tint.desc": "Enables the tint option for the current face",
Expand All @@ -1432,6 +1432,8 @@
"action.paint_mode_uv_overlay.desc": "Display the UV map as an overlay in paint mode",
"action.remove_blank_faces": "Remove Blank Faces",
"action.remove_blank_faces.desc": "Deletes all untextured faces of the selection",
"action.auto_set_cullfaces": "Set Cullfaces Automatically",
"action.auto_set_cullfaces.desc": "Automatically generate cullfaces to hide faces when they are covered by the neighboring block.",
"action.move_texture_with_uv": "Move Texture with UV",
"action.move_texture_with_uv.desc": "Move the texture of the face along when dragging UV faces",

Expand Down

0 comments on commit 3100a48

Please sign in to comment.