Browse Source

Fix: make default handedness for OBJ serializer to righthanded

Thibault Durand 5 years ago
parent
commit
abd8217184
2 changed files with 10 additions and 2 deletions
  1. 1 0
      dist/preview release/what's new.md
  2. 9 2
      serializers/src/OBJ/objSerializer.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -203,6 +203,7 @@
 - Fixed `Sound` not accepting a `TransformNode` as a source for spatial sound ([Poolminer](https://github.com/Poolminer))
 - Fixed an issue with transformation set after physics body was created using cannon.js ([#7928](https://github.com/BabylonJS/Babylon.js/issues/7928)) ([RaananW](https://github.com/RaananW))
 - Fix bug when using `ShadowOnlyMaterial` with Cascaded Shadow Map and `autoCalcDepthBounds` is `true` ([Popov72](https://github.com/Popov72))
+- Fix OBJ serializer default scene scene handedness causing [OBJ Mirror export](https://forum.babylonjs.com/t/obj-export-mirrored/10835/10)
 
 ## Breaking changes
 

+ 9 - 2
serializers/src/OBJ/objSerializer.ts

@@ -16,9 +16,10 @@ export class OBJExport {
      * @param materials defines if materials should be exported
      * @param matlibname defines the name of the associated mtl file
      * @param globalposition defines if the exported positions are globals or local to the exported mesh
+     * @param useRightHandedSystem defines if the babylon scene is right handed, in this case the exported obj vertices will be inverted 
      * @returns the OBJ content
      */
-    public static OBJ(mesh: Mesh[], materials?: boolean, matlibname?: string, globalposition?: boolean): string {
+    public static OBJ(mesh: Mesh[], materials?: boolean, matlibname?: string, globalposition?: boolean, useRightHandedSystem?: boolean): string {
         const output: string[] = [];
         let v = 1;
         if (materials) {
@@ -67,7 +68,13 @@ export class OBJExport {
             }
 
             for (var i = 0; i < trunkVerts.length; i += 3) {
-                output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
+                // Babylon default is left handed, while OBJ default is right handed
+                // By default we serialize in OBJ inverting the X vertices
+                if(useRightHandedSystem) {
+                    output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
+                } else {
+                    output.push("v " + -trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
+                }
                 curV++;
             }