Skip to content

Commit

Permalink
Merge pull request #15167 from bghgary/obj-handedness
Browse files Browse the repository at this point in the history
Fix incorrect OBJ flip faces check
  • Loading branch information
sebavan authored Jun 5, 2024
2 parents 9ed6fd2 + 07a9bf9 commit 1be7202
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/dev/serializers/src/OBJ/objSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Tools } from "core/Misc/tools";
import type { StandardMaterial } from "core/Materials/standardMaterial";
import type { Geometry } from "core/Meshes/geometry";
import type { Mesh } from "core/Meshes/mesh";
import { Material } from "core/Materials/material";

/**
* Class for generating OBJ data from a Babylon scene.
Expand All @@ -30,29 +31,30 @@ export class OBJExport {
output.push("mtllib " + matlibname + ".mtl");
}
for (let j = 0; j < meshes.length; j++) {
const objectName = meshes[j].name || `mesh${j}}`;
const mesh = meshes[j];
const objectName = mesh.name || `mesh${j}}`;
output.push(`o ${objectName}`);

//Uses the position of the item in the scene, to the file (this back to normal in the end)
let inverseTransform: Nullable<Matrix> = null;
if (globalposition) {
const transform = meshes[j].computeWorldMatrix(true);
const transform = mesh.computeWorldMatrix(true);
inverseTransform = new Matrix();
transform.invertToRef(inverseTransform);

meshes[j].bakeTransformIntoVertices(transform);
mesh.bakeTransformIntoVertices(transform);
}

//TODO: submeshes (groups)
//TODO: smoothing groups (s 1, s off);
if (materials) {
const mat = meshes[j].material;
const mat = mesh.material;

if (mat) {
output.push("usemtl " + mat.id);
}
}
const g: Nullable<Geometry> = meshes[j].geometry;
const g: Nullable<Geometry> = mesh.geometry;

if (!g) {
Tools.Warn("No geometry is present on the mesh");
Expand Down Expand Up @@ -92,7 +94,8 @@ export class OBJExport {
}

const blanks: string[] = ["", "", ""];
const [offset1, offset2] = useRightHandedSystem ? [2, 1] : [1, 2];
const sideOrientation = mesh.overrideMaterialSideOrientation ?? mesh.material?.sideOrientation ?? mesh.getScene().defaultMaterial.sideOrientation;
const [offset1, offset2] = sideOrientation === Material.ClockWiseSideOrientation ? [2, 1] : [1, 2];

for (let i = 0; i < trunkFaces.length; i += 3) {
const indices = [String(trunkFaces[i] + v), String(trunkFaces[i + offset1] + v), String(trunkFaces[i + offset2] + v)];
Expand Down Expand Up @@ -125,7 +128,7 @@ export class OBJExport {
}
//back de previous matrix, to not change the original mesh in the scene
if (globalposition && inverseTransform) {
meshes[j].bakeTransformIntoVertices(inverseTransform);
mesh.bakeTransformIntoVertices(inverseTransform);
}
v += currentV;
textureV += currentTextureV;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions packages/tools/tests/test/visualization/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,17 @@
"replace": "//options//, roundTrip = true; useRightHandedSystem = true;",
"referenceImage": "objStanfordBunnyNormalsRoundTripRH.png"
},
{
"title": "glTF to OBJ (LH)",
"playgroundId": "#HYZWGK#3",
"referenceImage": "gltfToObjLH.png"
},
{
"title": "glTF to OBJ (RH)",
"playgroundId": "#HYZWGK#3",
"replace": "//options//, useRightHandedSystem = true;",
"referenceImage": "gltfToObjRH.png"
},
{
"title": "GLTF Extension KHR_materials_variants",
"playgroundId": "#BKGTKL#3",
Expand Down

0 comments on commit 1be7202

Please sign in to comment.