Browse Source

Fix glTF serializer, Add color space conversion for mesh vertex colors with StandardMaterial applied. (#9021)

* Add support for converting sRGB color to Linear at export, based on the material type

* Add fix for linear space calculation, should writeback to temporary vector

* what's new

* Remove material diffuse color scaling

* Remove additional vector allocations for vertex reads

* Update test image that round tripped standard material to glTF

* Remove TmpColors import

* update metallicRoughness conversion unit test

* Revert changes to specGloss->metalRough conversion and unit tests
Nicholas Barlow 4 năm trước cách đây
mục cha
commit
f893981e40

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

@@ -322,6 +322,7 @@
 - Fix gltf2 Morph Target export code style, add additional test case for non-animation group created morph targets ([drigax](https://github.com/drigax))
 - Fix "Uncaught ReferenceError: name is not defined" ([outermeasure](https://github.com/outermeasure))
 - Fix wrong winding when applying a transform matrix on VertexData ([Popov72](https://github.com/Popov72))
+- Fix exporting vertex color of mesh with `StandardMaterial` when exporting to glTF ([Drigax](https://github.com/drigax))
 
 ## Breaking changes
 - `FollowCamera.target` was renamed to `FollowCamera.meshTarget` to not be in conflict with `TargetCamera.target` ([Deltakosh](https://github.com/deltakosh))

+ 16 - 2
serializers/src/glTF/2.0/glTFExporter.ts

@@ -2,7 +2,7 @@ import { AccessorType, IBufferView, IAccessor, INode, IScene, IMesh, IMaterial,
 
 import { FloatArray, Nullable, IndicesArray } from "babylonjs/types";
 import { Vector2, Vector3, Vector4, Quaternion, Matrix } from "babylonjs/Maths/math.vector";
-import { Color3 } from "babylonjs/Maths/math.color";
+import { Color3, Color4 } from "babylonjs/Maths/math.color";
 import { Tools } from "babylonjs/Misc/tools";
 import { VertexBuffer } from "babylonjs/Meshes/buffer";
 import { Node } from "babylonjs/node";
@@ -18,6 +18,7 @@ import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
 import { Texture } from "babylonjs/Materials/Textures/texture";
 import { Material } from "babylonjs/Materials/material";
 import { MultiMaterial } from "babylonjs/Materials/multiMaterial";
+import { StandardMaterial } from 'babylonjs/Materials/standardMaterial';
 import { Engine } from "babylonjs/Engines/engine";
 import { Scene } from "babylonjs/scene";
 
@@ -728,9 +729,22 @@ export class _Exporter {
                 break;
             }
             case VertexBuffer.ColorKind: {
+                const meshMaterial = (babylonTransformNode as Mesh).material;
+                const convertToLinear = meshMaterial ? (meshMaterial instanceof StandardMaterial) : true;
+                const vertexData : Color3 | Color4 = stride === 3 ? new Color3() : new Color4();
                 for (let k = 0, length = meshAttributeArray.length / stride; k < length; ++k) {
                     index = k * stride;
-                    const vertexData = stride === 3 ? Vector3.FromArray(meshAttributeArray, index) : Vector4.FromArray(meshAttributeArray, index);
+                    if (stride === 3) {
+                        Color3.FromArrayToRef(meshAttributeArray, index, (vertexData as Color3));
+                        if (convertToLinear) {
+                            (vertexData as Color3).toLinearSpaceToRef((vertexData as Color3));
+                        }
+                    } else {
+                        Color4.FromArrayToRef(meshAttributeArray, index, (vertexData as Color4));
+                        if (convertToLinear) {
+                            (vertexData as Color4).toLinearSpaceToRef((vertexData as Color4));
+                        }
+                    }
                     vertexAttributes.push(vertexData.asArray());
                 }
                 break;