Skip to content

Commit

Permalink
Add OBJ export face mode option
Browse files Browse the repository at this point in the history
Closes #1257
  • Loading branch information
JannisX11 committed Jan 22, 2022
1 parent a201903 commit 4ff4c3f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
5 changes: 5 additions & 0 deletions js/interface/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ const Settings = {
new Setting('minify_bbmodel', {category: 'export', value: true});
new Setting('export_empty_groups', {category: 'export', value: true});
new Setting('export_groups', {category: 'export', value: true});
new Setting('obj_face_export_mode', {category: 'export', value: 'both', type: 'select', options: {
both: 'settings.obj_face_export_mode.both',
tris: 'settings.obj_face_export_mode.tris',
quads: 'settings.obj_face_export_mode.quads',
}});
new Setting('animation_sample_rate',{category: 'export', value: 24, type: 'number'});
new Setting('sketchfab_token', {category: 'export', value: '', type: 'password'});
new Setting('credit', {category: 'export', value: 'Made with Blockbench', type: 'text'});
Expand Down
76 changes: 58 additions & 18 deletions js/io/formats/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var codec = new Codec('obj', {
const normal = new THREE.Vector3();
const uv = new THREE.Vector2();
const face = [];
let face_export_mode = Settings.get('obj_face_export_mode');

output.push('mtllib ' + (options.mtl_name||'materials.mtl') +'\n');

Expand Down Expand Up @@ -133,12 +134,25 @@ var codec = new Codec('obj', {
case 'up': vertices = [5, 2, 1, 6]; break;
case 'down': vertices = [8, 3, 4, 7]; break;
}
output.push('f '+[
`${vertices[3] + indexVertex}/${i*4 + 4 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[2] + indexVertex}/${i*4 + 3 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[1] + indexVertex}/${i*4 + 2 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[0] + indexVertex}/${i*4 + 1 + indexVertexUvs}/${i+1+indexNormals}`,
].join(' '));
if (face_export_mode == 'tris') {
output.push('f '+[
`${vertices[2] + indexVertex}/${i*4 + 3 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[1] + indexVertex}/${i*4 + 2 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[0] + indexVertex}/${i*4 + 1 + indexVertexUvs}/${i+1+indexNormals}`,
].join(' '));
output.push('f '+[
`${vertices[3] + indexVertex}/${i*4 + 4 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[2] + indexVertex}/${i*4 + 3 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[0] + indexVertex}/${i*4 + 1 + indexVertexUvs}/${i+1+indexNormals}`,
].join(' '));
} else {
output.push('f '+[
`${vertices[3] + indexVertex}/${i*4 + 4 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[2] + indexVertex}/${i*4 + 3 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[1] + indexVertex}/${i*4 + 2 + indexVertexUvs}/${i+1+indexNormals}`,
`${vertices[0] + indexVertex}/${i*4 + 1 + indexVertexUvs}/${i+1+indexNormals}`,
].join(' '));
}
i++;
}
}
Expand Down Expand Up @@ -167,10 +181,9 @@ var codec = new Codec('obj', {
for (let key in element.faces) {
if (element.faces[key].texture !== null && element.faces[key].vertices.length >= 3) {
let face = element.faces[key];
let vertices = face.getSortedVertices();
let vertices = face.getSortedVertices().slice();
let tex = element.faces[key].getTexture();


vertices.forEach(vkey => {
output.push(`vt ${face.uv[vkey][0] / Project.texture_width} ${1 - face.uv[vkey][1] / Project.texture_height}`);
nbVertexUvs += 1;
Expand All @@ -192,16 +205,43 @@ var codec = new Codec('obj', {
faces.push('usemtl '+mtl);
}

let triplets = [];
vertices.forEach((vkey, vi) => {
let triplet = [
vertex_keys.indexOf(vkey) + 1 + indexVertex,
nbVertexUvs - vertices.length + vi + 1 + indexVertexUvs,
i+1+indexNormals,
]
triplets.push(triplet.join('/'));
})
faces.push('f ' + triplets.join(' '));
if (face_export_mode == 'quads' && vertices.length == 3) vertices.push(vertices[0]);

if (face_export_mode == 'tris' && vertices.length == 4) {
let triplets_a = [];
vertices.slice(0, 3).forEach((vkey, vi) => {
let triplet = [
vertex_keys.indexOf(vkey) + 1 + indexVertex,
nbVertexUvs - vertices.length + vi + 1 + indexVertexUvs,
i+1+indexNormals,
]
triplets_a.push(triplet.join('/'));
})
faces.push('f ' + triplets_a.join(' '));

let triplets_b = [];
[vertices[0], vertices[2], vertices[3]].forEach((vkey, vi) => {
let triplet = [
vertex_keys.indexOf(vkey) + 1 + indexVertex,
nbVertexUvs - vertices.length + (vi ? 1 : 0) + vi + 1 + indexVertexUvs,
i+1+indexNormals,
]
triplets_b.push(triplet.join('/'));
})
faces.push('f ' + triplets_b.join(' '));

} else {
let triplets = [];
vertices.forEach(vkey => {
let triplet = [
vertex_keys.indexOf(vkey) + 1 + indexVertex,
nbVertexUvs - vertices.length + vertices.indexOf(vkey) + 1 + indexVertexUvs,
i+1+indexNormals,
]
triplets.push(triplet.join('/'));
})
faces.push('f ' + triplets.join(' '));
}
i++;
}
}
Expand Down
5 changes: 5 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@
"settings.export_empty_groups.desc": "Include groups without content in exported files",
"settings.export_groups": "Export Groups in Java block/item models",
"settings.export_groups.desc": "Save groups in java block or item model JSON files",
"settings.obj_face_export_mode": "OBJ Face Export Mode",
"settings.obj_face_export_mode.desc": "Choose to let the OBJ exporter convert all faces into tris or quads",
"settings.obj_face_export_mode.both": "Default",
"settings.obj_face_export_mode.tris": "Convert to Tris",
"settings.obj_face_export_mode.quads": "Convert to Quads",
"settings.animation_sample_rate": "Animation Sample Rate",
"settings.animation_sample_rate.desc": "Sample rate in samples per second for curves that the respective format does not support natively",
"settings.credit": "Credit Comment",
Expand Down

0 comments on commit 4ff4c3f

Please sign in to comment.